还是那道链表题,我简化了下,请高手帮忙看下,谢谢啊。。。
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);
enum MAIN_CMD{REG,SEL=1};
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 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[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;
}