有没有高手帮我看下这个链表题啊,有些长,我做出来了,可总调不出正确结果
我是新手,没什么分啊,有高手耐心的帮忙看下,小弟感激不尽!!!#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>
//using namespace std;
// 循环链表结构 head->[,->next]...[data0,->next][data1,->next]...[datan,->head]
struct SCORE
{
int courseId;
int score;
SCORE *pnext;
};
struct STU_SCORE
{
char name[16];
int stuId;
SCORE *scoreHead;//学生课程成绩列表
struct STU_SCORE *pnext;
};
int mainMenu();
int stuReg(STU_SCORE *pHead);
int selCourse(STU_SCORE *pHead);
int inputData(STU_SCORE *pHead);
int scoreSort(STU_SCORE *pHead);
int scoreQuery(STU_SCORE *pHead);
int modify(STU_SCORE *pHead);
enum MAIN_CMD{REG,SEL=1,INPUT,QUERY,MODI,EXIT};
int main(int argc, char* argv[])
{
STU_SCORE *head=( STU_SCORE *)malloc(sizeof(STU_SCORE));
head->stuId=-1;
head->pnext=head;
while(1)
{
int cmd=mainMenu();
switch(cmd)
{
case REG:
stuReg(head);
break;
case SEL:
selCourse(head);
break;
case INPUT:
inputData(head);
break;
case QUERY:
scoreQuery(head);
break;
case MODI:
modify(head);
break;
case EXIT:
return 0;
}
}
return 0;
}
int mainMenu()
{
system("cls");
cout<<"\t\t《学生成绩管理系统》\n";
cout<<" \t\t[0]新生注册\n";
cout<<" \t\t[1]选课登记\n";
cout<<" \t\t[2]学生成绩输入\n";
cout<<" \t\t[3]成绩查询\n";
cout<<" \t\t[4]修改成绩\n";
cout<<" \t\t[5]退出\n";
cout<<"命令? ";
int cmd;
cin>>cmd;
return cmd;
}
//新生注册:建立学生信息链表,从键盘输入学生姓名学号,插入到链表中。
int stuReg(STU_SCORE *pHead)
{
cout<<"注意:新生注册学号不能重复!\n";
start:
{ struct STU_SCORE *ptail,*pnew;
cout<<"学号 姓名"<<"\n";
pnew=(STU_SCORE *)malloc(sizeof(STU_SCORE));
cin>>pnew->stuId>>pnew->name;
if(pHead->pnext==pHead)
{ pHead->pnext=pnew;
ptail=pnew;
}
else
{ ptail->pnext=pnew;
ptail=pnew;
}
}
cout<<"继续新的注册?";
char c;
cin>>c;
if(c=='y'||c=='Y')goto start;
return 0;
}
//学生选课
int selCourse(STU_SCORE *pHead)
{
start:
int cid;
cout<<"课程号:";
cin>>cid;
cout<<"输入选择该课程的学生学号\n";
int stuId;
cin>>stuId;
while(stuId>0)
{
STU_SCORE *pStu=pHead->pnext;
while(pStu!=pHead)//链表已有记录
{
if(pStu->stuId==stuId)//有该学号
{
bool be=false;
SCORE *pcourse=pStu->scoreHead->pnext;
while(pcourse!=pStu->scoreHead)//已有选课
{
if(pcourse->courseId==cid)
{
be=true;////已有该选课
break;
}
pcourse=pcourse->pnext;
}
if(!be)
{ //还没有选此课程
SCORE *pNewcourse=(SCORE *)malloc(sizeof(SCORE));
pNewcourse->courseId=cid;
pcourse=pNewcourse;
pcourse=pNewcourse->pnext;
break;
// 。。。。。。插入到选课链的头节点后
}
}
pStu=pStu->pnext;
}
cin>>stuId;
}
int count=0;
STU_SCORE *pStu=pHead->pnext;
while(pStu!=pHead)//链表已有记录
{
SCORE *pcourse=pStu->scoreHead->pnext;
while(pcourse!=pStu->scoreHead)
{
if(pcourse->courseId==cid)
{
count++;
}
pcourse=pcourse->pnext;
}
pStu=pStu->pnext;
}
cout<<"共有"<<count<<"个学生选此课程\n";
cout<<"继续新的选课输入?";
char c;
cin>>c;
if(c=='y'||c=='Y')goto start;
return 0;
}
//学生成绩输入
int inputData(STU_SCORE *pStuHead)
{
int count;
start:
count=0;
cout<<"课程号:";
int cid;
cin>>cid;
STU_SCORE *pStu=pStuHead->pnext;
while(pStu!=pStuHead)//if链表已有学生记录
{
SCORE *pcourse=pStu->scoreHead->pnext;
while(pcourse!=pStu->scoreHead)
{if(pcourse->courseId==cid)
{
cout<<"学号:"<<pStu->stuId<<" 姓名:"<<pStu->name;
cout<<" 成绩:";
if(pcourse->score>=0)
cout<<"已输入";
else
{
cin>>pcourse->score;
count++;
}
break;
}
pcourse=pcourse->pnext;
}
pStu=pStu->pnext;
}
if(count==0)
cout<<"该课程无学生选!\n";
else
cout<<cid<<"课程成绩已输入完成!"<<"\n";
cout<<"继续其他课程成绩输入?";
char c;
cin>>c;
if(c=='y'||c=='Y')goto start;
return 0;
}
//课程成绩查询
int scoreQuery(STU_SCORE *pStuHead)
{int count,stuId,cid;
start:
cout<<"请输入要查询的学号和课程号";
cin>>stuId>>cid;
count=0;
STU_SCORE *pStu=pStuHead->pnext;
while(pStu!=pStuHead)//if链表已有学生记录
{if (pStu->stuId==stuId)
{SCORE *pcourse=pStu->scoreHead->pnext;
while(pcourse!=pStu->scoreHead)
{
if(pcourse->courseId==cid)
{cout<<"要查询的分数为"<<pcourse->score;
count++;
break;}
pcourse=pcourse->pnext;}
}//。。。。。。
pStu=pStu->pnext;}
if(count==0)
cout<<"无此记录!";
else
cout<<cid<<"课程成绩已查询完成!"<<"\n";
cout<<"继续其他课程成绩查询?";
char c;
cin>>c;
if(c=='y'||c=='Y')goto start;
return 0;
}
//课程成绩修改
int modify(STU_SCORE *pStuHead)
{
int count,stuId,cid;
start:
cout<<"请输入要修改的学号和课程号";
cin>>stuId>>cid;
count=0;
STU_SCORE *pStu=pStuHead->pnext;
while(pStu!=pStuHead)//if链表已有学生记录
{if (pStu->stuId==stuId)
{SCORE *pcourse=pStu->scoreHead->pnext;
while(pcourse!=pStu->scoreHead)
{
if(pcourse->courseId==cid)
{cout<<"要修改的分数为";
cin>>pcourse->score;
count++;
break;}
pcourse=pcourse->pnext;}
}//。。。。。。
pStu=pStu->pnext;}
if(count==0)
cout<<"无此记录!";
else
cout<<cid<<"课程成绩已修改完成!"<<"\n";
cout<<"继续其他课程成绩修改?";
char c;
cin>>c;
if(c=='y'||c=='Y')goto start;
return 0;
}