首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

那位大哥帮小弟我看上,有点长,浪费你的时间了

2012-12-29 
那位大哥帮我看下,有点长,浪费你的时间了#includestdio.h#includeiostream.h#includestdlib.h#inclu

那位大哥帮我看下,有点长,浪费你的时间了
#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#include<string>
#include<windows.h>
#include<fstream.h>
#define LEN sizeof(struct student)
typedef struct student
{
char name[20]; //学生姓名
char sex; //学生性别
long ID; //学生学号
int Chinese; //学生语文成绩
int Math; //学生数学成绩
int English; //学生英语成绩
int sum;  //学生总成绩
int aver; //学生平均成绩
struct student *next;
}seqlist;

void menu();
void select(); //界面选择函数
void creatlist(seqlist*p1); //录入学生信息函数
void printlist(seqlist*head); //输出学生信息
seqlist *searchlist(seqlist*L);//查找学生信息
void insert(seqlist *L,int i);
void savelist(seqlist);
void select()
{
int n;
menu();
seqlist *p;
while(true)
{
cout<<"请输入你的操作:"<<endl;
cin>>n;
switch(n)
{
case 1:
p=(seqlist*)malloc(LEN);
creatlist(p);
break;
case 2:
p=(seqlist*)malloc(LEN);
printlist(p);
break;
case 3:
p=searchlist(p);
break;
}
}
}
void menu()
{
cout<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<"                              湖南工业大学欢迎您!          "<<endl;
cout<<"                    ______________________________________ "<<endl;
cout<<"                   |          一(1):输入学生信息          |"<<endl;
cout<<"                   |          二(2):输出学生信息          |"<<endl;
cout<<"                   |          三(3):查找学生信息          |"<<endl;
cout<<"                   |          四(4):插入学生信息          |"<<endl;
    cout<<"                   |          五(5):删除学生信息          |"<<endl;
cout<<"                   |______________________________________|"<<endl;
}

void creatlist(seqlist*p1)//录入学生信息
{
char ch;
seqlist *p2;
do
{

p2=(seqlist*)malloc(LEN);
        cout<<"请输入学生的名字:";
cin>>p2->name;
cout<<"请输入学生的性别(如果是男性请输入M,女性请输入W):";
    cin>>p2->sex;
while(p2->sex!='M'&&p2->sex!='W')


{
cout<<"你的输入有误,请重新输入"<<endl;
cin>>p2->sex;
}
    cout<<"请输入学生的学号:";
cin>>p2->ID;
while(p2->ID<10000000000&&p2->ID>99999999999)
{
             cout<<"你的输入有误,请重新输入(学好的)范围10000000000<=ID<=999999999999"<<endl;
 cin>>p2->ID;
}
        cout<<"请输入学生的语文成绩:";
cin>>p2->Chinese;
while(p2->Chinese<0||p2->Chinese>100)
{
cout<<"你所输入的成绩有误,请重新输入,成绩范围在<0-100>"<<endl;
cin>>p2->Chinese;
}
        cout<<"请输入学生的数学成绩:";
cin>>p2->Math;
     while(p2->Math<0||p2->Math>100)
{
cout<<"你所输入的成绩有误,请重新输入,成绩范围在<0-100>"<<endl;
cin>>p2->Math;
}
cout<<"请输入学生的英语成绩:";
cin>>p2->English;
    while(p2->English<0||p2->English>100)
{
cout<<"你所输入的成绩有误,请重新输入,成绩范围在<0-100>"<<endl;
cin>>p2->English;
}
     cout<<"是否继续添加<y/Y--n/N>:"<<endl;
    cin>>ch;

}
while(ch=='y'||ch=='Y');
select();
}
void printlist(seqlist *head)//输出学生信息
{
seqlist *p;
cout<<"姓名\t性别\t学号\t语文\t数学\t英语"<<endl;
    p=head;
p=p->next;
    do
{
cout<<p->name<<p->sex<<p->ID<<p->Chinese<<p->Math<<p->English<<endl;
        p=p->next;
}
while(p!=NULL);
    
}
seqlist *searchlist (seqlist *L)//查找学生信息
{
    seqlist *p=L->next;
    int classnum,m;
char names[12];
cout<<"学号查找请输入1,姓名查找请输入2"<<endl;
cout<<" ___________________________ "<<endl;
cout<<"|       一(1):学号查找      |"<<endl;
cout<<"|       二(2):姓名查找      |"<<endl;
cout<<"|___________________________|"<<endl;
    cin>>m;
if(m==1)
{
cout<<"请输入学号:"<<endl;
cin>>classnum;
    while(p!=NULL&&(p->ID==classnum))
{
p=p->next;
}
if(p==NULL)
{
cout<<"没有学生信息"<<endl;
return (NULL);
}
else
return (p);

}
else if(m==2)
{
cout<<"请输入姓名:"<<endl;
cin>>names;
while(p!=NULL&&strcmp(p->name,names)!=0)
{
p=p->next;
}
if(p==NULL)
{
            cout<<"没有学生信息"<<endl;
return (NULL);
}
else
return(p);
}


}
void insert(seqlist *L,int i)
{
seqlist *p=L,*s;
int j=1;
    while(p&&j<=i-1)
{
p=p->next; 
++j;
}
    s=(seqlist *)malloc(LEN);
cout<<"请输入所添加学生的姓名:"<<endl;
cin>>p->name;
    cout<<"请输入所添加学生的性别(如果是男性请输入M,女性请输入W):"<<endl;
cin>>p->sex;


    while(p->sex!='M'&&p->sex!='W')
{
cout<<"你的输入有误,请重新输入"<<endl;
cin>>p->sex;
}
cout<<"请输入所添加学生的学号:"<<endl;
cin>>p->ID;
while(p->ID<10000000000&&p->ID>99999999999)
{
        cout<<"你的输入有误,请重新输入(学好的)范围10000000000<=ID<=999999999999"<<endl;
    cin>>p->ID;
}
cout<<"请输入所添加学生的语文成绩:"<<endl;
cin>>p->Chinese;
   while(p->Chinese<0||p->Chinese>100)
{
     cout<<"你所输入的成绩有误,请重新输入,成绩范围在<0-100>"<<endl;
cin>>p->Chinese;
}
cout<<"请输入所添加学生的数学成绩:"<<endl;
cin>>p->Math;
while(p->Math<0||p->Math>100)
{
cout<<"你所输入的成绩有误,请重新输入,成绩范围在<0-100>"<<endl;
cin>>p->Math;
}
cout<<"请输入所添加学生的英语成绩:"<<endl;
    cin>>p->English;
    while(p->English<0||p->English>100)
{
cout<<"你所输入的成绩有误,请重新输入,成绩范围在<0-100>"<<endl;
cin>>p->English;
}
s->next=p->next;
    p->next=s;

}
void savelist(seqlist *L,int i)  //将学生成绩保存入文本
{
seqlist *p=L;
ofstream ofile("D:\\data.txt");
ofile<<"学号  姓名  语文  数学  英语  计算机"<<endl;
for( i=0;i<10;i++)
{
ofile<<p->name<<endl;
}
ofile.close();
cout<<"学生信息已存入文件!"<<endl;
}

int main()
{
    system("color 4E");
    select();
return 0;
}
运行后,输入信息可以,然后在
执行别的时出问题了,谢谢。
[解决办法]
1,代码中只有malloc却没有free
2,select这个函数中,执行2输出学生信息时,新申请了一个seqlist*,这个指针既没初始化,也没有赋值,完全就一野指针,然后在printlist函数中直接使用,这样是错误的用法

一眼就看到这两个问题,其他的不想继续看了
[解决办法]

引用:
#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#include<string>
#include<windows.h>
#include<fstream.h>
#define LEN sizeof(struct student)
typedef struct student
{
cha……


想让别人帮你,先把代码格式弄成csdn的代码格式
[解决办法]
单步调试和设断点调试是程序员必须掌握的技能之一。

热点排行