这段程序谁给看一下啊,我怎么搞了半天搞不定啊,纠结了好久!

zhaolj20080808 2009-10-26 03:56:09
#include<iostream>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<iomanip>
#define LEN sizeof(struct Student)
using namespace std;

struct Student
{
char Sno[10]; //学号
char Sname[20]; //姓名
char Ssex[10]; //性别
char Sclass[10]; //班级
Student *next;
};


int menu()
{
char c;
for(;c<'0'||c>'9';)
{
system("cls");
cout<<"************** 学生成绩管理系统 ************** "<<endl;
cout<<setw(15)<<" "<<"1.输入学生信息"<<endl;
cout<<setw(15)<<" "<<"2.输出学生信息"<<endl;
cout<<setw(15)<<" "<<"3.按学号排序排序"<<endl;
cout<<setw(15)<<" "<<"4.按学号查找"<<endl;
cout<<setw(15)<<" "<<"5.按姓名查"<<endl;
cout<<setw(15)<<" "<<"6.插入一条记录"<<endl;
cout<<setw(15)<<" "<<"7.删除一条记录"<<endl;
cout<<setw(15)<<" "<<"8.修改一条记录"<<endl;
cout<<setw(15)<<" "<<".将所有记录保存在文件中"<<endl;
cout<<setfill('*')<<setw(48)<<"*"<<endl;
//cout<<setw(15)<<" ";
cout<<"请选择(1-9):";
c=getchar();
}
return c;
}


Student *create(Student *head)//输入数据,创建链表;
{
int n;
Student *p1,*p2;//p1为当前节点;
int i;
cout<<"请输入学生的个数:"<<endl;
cin>>n;
cout<<" 请输入学号 姓名 性别 班级"<<endl;
//p2=head;
for(i=0;i<n;i++)
{
p1=(Student *)malloc(LEN);
//p2=(Student *)malloc(LEN);
cin>>p1->Sno;
cin>>p1->Sname;
cin>>p1->Ssex;
cin>>p1->Sclass;

p2->next=p1;
p2=p1;
p2->next=NULL;
/*if(i==0)
{
head=p2;
p1=head;
}
else
{
p2->next=p1;
}
p2->next=NULL;
p1=p2;*/
}
cout<<"输入结束!"<<endl;
return head;
}
/*****************输出函数**********************/
/********函数功能:将所有数据打印在屏幕上********/
Student *output(Student *head)
{
Student *p1,*p3;
cout<<"输出结果为:"<<endl;
p1=head;
p3=p1->next;
if(p3==NULL)
{
cout<<"表格为空,无法输出"<<endl;
}
while(p3!=NULL)
{
cout<<" "<<p1->Sno<<" "<<p1->Sname<<" "<<p1->Ssex<<" "<<p1->Sclass<<endl;
p1=p1->next;
}

return 0;
}

/*****************排序函数*********************/
/******函数功能:将所有数据按学号从小到大进行排序******/
Student *rank(Student *head)
{
Student *p1,*p2;
p1=head;
while(p1->next!=NULL)
{
if(strcmp(p1->Sno,p1->next->Sno)<0)
{
p1=p1->next;
}
else
{
p2=p1->next;
p1->next=p2->next;
}
}
return head;
}


Student *find_Sno(Student *head)//查找函数:按学号查询;
{
char num[10];
Student *p1;
cout<<"请输入要查找学生的学号:"<<endl;
cin>>num;
p1=head;
while(p1!=NULL)
{
if(strcmp(p1->Sno,num)==0)
{
cout<<setw(10)<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"班级"<<endl;
cout<<" "<<p1->Sno<<" "<<p1->Sname<<" "<<p1->Ssex<<" "<<p1->Sclass<<endl;
return head;
}
else
{
p1=p1->next;
}
}
if(p1==NULL)
{
cout<<"要查询的数据不存在,请输入正确数值从新查找!"<<endl;
}
return 0;
}



Student *find_Sname(Student *head)//查找函数:按姓名查询;
{
char name[20];
Student *p1;
cout<<"请输入要查找学生的姓名:"<<endl;
cin>>name;
p1=head;
while(p1!=NULL)
{
if(strcmp(p1->Sname,name)==0)
{
cout<<"学号"<<" "<<"姓名"<<" "<<"性别"<<" "<<"班级"<<endl;
cout<<" "<<p1->Sno<<" "<<p1->Sname<<" "<<p1->Ssex<<" "<<p1->Sclass<<endl;
return head;
}
else
{
p1=p1->next;
}
}
if(p1==NULL)
{
cout<<"要查询的数据不存在,请输入正确数值从新查找!"<<endl;
}
return 0;
}


/*********************插入函数***************************/
/******************* 函数功能:插入一条新的数据*************/
Student *insert(Student *head)
{
Student *p1,*p2,*p3;
p3=(Student *)malloc(LEN);
if(head==NULL)
{
head=p3;
head->next=NULL;
return head;
}
p1=head;
while(p1->next!=NULL)
{
if(p1->Sno<=p3->Sno)
{
p2=p1;
p1=p1->next;
}
else
{
p2->next=p3;
p3->next=p1;
}
}
if(p1->next==NULL)
{
if(strcmp(p1->Sno,p3->Sno)>0)
{
p3->next=p2->next;
p2->next=p3;
}
else
{
p1->next=p3;
p3->next=NULL;
}
}
return head;
}


/*********************删除函数***************************/
/******************* 函数功能:查找并删除一条数据*************/
Student *delet(Student *head)
{
Student *p1,*p2;
char num[10];
cout<<"请输入要删除的学生学号:"<<endl;
cin>>num;
if(head==NULL)
{
cout<<"数据表已空,无法删除!"<<endl;
}
p1=head;
while((p1->next!=NULL)&&(strcmp(p1->Sno,num)!=0))
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->Sno,num)==0)
{
if(strcmp(head->Sno,num)==0)
{
head=p1->next;
}
else
{
p2->next=p1->next;
free(p1);
cout<<"数据以删除"<<endl;
}
}
else
{
cout<<"找不到数据,无法删除!"<<endl;
}
return head;
}


/***********************修改函数********************/
/******************函数功能:修改一条数据******************/
Student *alter(Student *head)
{ char num[10];
cout<<"请输入要删除的学生学号:"<<endl;
cin>>num;
delet(head,num);
cout<<"请输入修改后的数据"<<endl;
insert(head);
return 0;
};


/***********************保存函数********************/
/***********函数功能:将所有数据出入一个文件中**********/
Student *savefile(Student *head)
{
FILE *fp;
Student *p1;
p1=head;
char file[30];//存放文件名及路径;
cout<<"请输入文件名及路径:"<<endl;
if((fp=fopen(file,"w+"))==NULL)
{
cout<<"不能打开文件:"<<endl;
return 0;
}
fprintf(fp,"学号 姓名 姓名 班级");
while(p1!=NULL)
{
fprintf(fp,"%s\t%s\t%s\t%s\n",p1->Sno,p1->Sname,p1->Ssex,p1->Sclass);
p1=p1->next;
}
fclose(fp);
cout<<"保存完毕"<<endl;
return 0;
}

/************主函数****************/
void main()
{
//cout<<"xuesheng"<<endl;
Student *head=0;

//create(head);
//output(head);
for(; ;)
{
switch(menu())
{
case 1:
create();
break;
case 2:
output(head);
break;
case 3:
rank(head);
break;
case 4:
find_Sno(head);
break;
case 5:
find_Sname(head);
break;
case 6:
insert(head);
break;
case 7:
delet(head);
break;
case 8:
alter(head);
break;
case 9:
savefile(head);
break;
}
}

}
...全文
204 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaolj20080808 2009-10-27
  • 打赏
  • 举报
回复
多谢大家了!!!!!!
主要代码里的指针太乱了
我又从新写了一遍,问题解决了
在此谢过各位大师了!!!!!!
lzh9955 2009-10-26
  • 打赏
  • 举报
回复
好久没看帖了,高手越来越多啊!
smile_everyday 2009-10-26
  • 打赏
  • 举报
回复
1、for(;c <'0'||c>'9';) 在之前c就没有初始化值,会引起for语句的不可预测
2、int menu() 返回值为字符值,并非'0' '9' 等。也就是case 需要修改为case '1':等
3、楼主贴的代码诸多地方的<< 中间夹了空格,简单编译问题一堆。
4、函数的调用,需要参数的地方没有赋予参数
5、每个变量的使用请初始化,函数入口请检查 参数的有效性,特别是指针。
6、一个C的程序,还是用C写的好。C++ 的好处是面向对象。
  • 打赏
  • 举报
回复
仔细排查内存操作。
kouwenlong 2009-10-26
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wanjingwei 的回复:]
改成这样吧
int menu()
{
char c;

do
{
system("cls");
cout < <"**************  学生成绩管理系统  ************** " < <endl;
cout < <setw(15) < <" " < <"1.输入学生信息" < <endl;
cout < <setw(15) < <" " < <"2.输出学生信息" < <endl;
cout < <setw(15) < <" " < <"3.按学号排序排序" < <endl;
cout < <setw(15) < <" " < <"4.按学号查找" < <endl;
cout < <setw(15) < <" " < <"5.按姓名查" < <endl;
cout < <setw(15) < <" " < <"6.插入一条记录" < <endl;
cout < <setw(15) < <" " < <"7.删除一条记录" < <endl;
cout < <setw(15) < <" " < <"8.修改一条记录" < <endl;
cout < <setw(15) < <" " < <".将所有记录保存在文件中" < <endl;
cout < <setfill('*') < <setw(48) < <"*" < <endl;
//cout < <setw(15) < <" ";
cout < <"请选择(1-9):";
c=getchar();
} while(c <'0'||c>'9') ;
[/Quote]
他的这个程序,该一点不行,有好多东西都得改,我改了这里,那里又出现错误,到头来,还得从新改。
wanjingwei 2009-10-26
  • 打赏
  • 举报
回复
改成这样吧
int menu()
{
char c;

do
{
system("cls");
cout <<"************** 学生成绩管理系统 ************** " <<endl;
cout <<setw(15) <<" " <<"1.输入学生信息" <<endl;
cout <<setw(15) <<" " <<"2.输出学生信息" <<endl;
cout <<setw(15) <<" " <<"3.按学号排序排序" <<endl;
cout <<setw(15) <<" " <<"4.按学号查找" <<endl;
cout <<setw(15) <<" " <<"5.按姓名查" <<endl;
cout <<setw(15) <<" " <<"6.插入一条记录" <<endl;
cout <<setw(15) <<" " <<"7.删除一条记录" <<endl;
cout <<setw(15) <<" " <<"8.修改一条记录" <<endl;
cout <<setw(15) <<" " <<".将所有记录保存在文件中" <<endl;
cout <<setfill('*') <<setw(48) <<"*" <<endl;
//cout <<setw(15) <<" ";
cout <<"请选择(1-9):";
c=getchar();
} while(c<'0'||c>'9') ;
kouwenlong 2009-10-26
  • 打赏
  • 举报
回复
int menu()
{
char c;
for(;c <'0'||c>'9';)
{
system("cls");
cout <<"************** ѧÉú³É¼¨¹ÜÀíϵͳ ************** " <<endl;
cout <<setw(15) <<" " <<"1.ÊäÈëѧÉúÐÅÏ¢" <<endl;
cout <<setw(15) <<" " <<"2.Êä³öѧÉúÐÅÏ¢" <<endl;
cout <<setw(15) <<" " <<"3.°´Ñ§ºÅÅÅÐòÅÅÐò" <<endl;
cout <<setw(15) <<" " <<"4.°´Ñ§ºÅ²éÕÒ" <<endl;
cout <<setw(15) <<" " <<"5.°´ÐÕÃû²é" <<endl;
cout <<setw(15) <<" " <<"6.²åÈëÒ»Ìõ¼Ç¼" <<endl;
cout <<setw(15) <<" " <<"7.ɾ³ýÒ»Ìõ¼Ç¼" <<endl;
cout <<setw(15) <<" " <<"8.ÐÞ¸ÄÒ»Ìõ¼Ç¼" <<endl;
cout <<setw(15) <<" " <<".½«ËùÓмǼ±£´æÔÚÎļþÖÐ" <<endl;
cout <<setfill('*') <<setw(48) <<"*" <<endl;
//cout < <setw(15) <<" ";
cout <<"ÇëÑ¡Ôñ(1-9):";
c=getchar();
}
return c;
}
这里你c都没有初始化,就开始c <'0'||c>'9'比较了。
kouwenlong 2009-10-26
  • 打赏
  • 举报
回复
switch(menu())
你的这里是个死循环。
wanjingwei 2009-10-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhaolj20080808 的回复:]
谢谢3楼:
把这些错误改掉后,编译没错误了
但是运行结果出不来,每次按提示输入相应操作的对应数字后
无法调用相应的函数啊
[/Quote]
你断点跟踪下看哪里出的错
zhaolj20080808 2009-10-26
  • 打赏
  • 举报
回复
回复5楼:
就是让它一直循环的,不断的进行相关操作,
zhaolj20080808 2009-10-26
  • 打赏
  • 举报
回复
谢谢3楼:
把这些错误改掉后,编译没错误了
但是运行结果出不来,每次按提示输入相应操作的对应数字后
无法调用相应的函数啊
delphiwcdj 2009-10-26
  • 打赏
  • 举报
回复

switch(menu())
{
case 1:
create(); //这里需要带一个参数
break;
case 2:
output(head);
//...
delphiwcdj 2009-10-26
  • 打赏
  • 举报
回复
delet(head,num); //只能有一个参数
zhengjiankang 2009-10-26
  • 打赏
  • 举报
回复
够长的啊,也不说哪里出了问题。
不过这主函数的for循环看起来没有退出的条件。
zyxk5211 2009-10-26
  • 打赏
  • 举报
回复
我也看的头晕啊!编译下,看提示什么错误!
wanjingwei 2009-10-26
  • 打赏
  • 举报
回复
case 1:
create(); 这个没带参数

Student *alter(Student *head)
{ char num[10];
cout < <"请输入要删除的学生学号:" < <endl;
cin>>num;
delet(head,num); //这个多了一个参数
ToBeTough 2009-10-26
  • 打赏
  • 举报
回复
建议把错误贴出来 !
zhaolj20080808 2009-10-26
  • 打赏
  • 举报
回复
有人吗?
帮忙给看一下嘛,多谢

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧