#include<cstring>
#include<string>
#include<malloc.h>
#include<cstdlib>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<fstream>
using namespace std;
typedef struct student
{ int sno;
char sname[8];
int score;
}student;
typedef struct Node
{
student studentinfo;
struct Node *next;
}LinkList;
LinkList *createHeadList(LinkList *L)
{
LinkList *s;
int x;
char y[8];
int z;
cout<<"请输入学号,-1结束"<<endl;
cin>>x;
while(x != -1)
{
s = ( LinkList *)malloc(sizeof (LinkList));
s->studentinfo.sno = x;
cout<<"请输入姓名"<<endl;
cin>>y;
strcpy(s->studentinfo.sname,y);
cout<<"请输入成绩"<<endl;
cin>>z;
s->studentinfo.score = z;
s->next = L->next;
L->next = s;
cout<<"请输入学号"<<endl;
cin>>x;
}
return L;
}
LinkList *createTailList(LinkList *L)
{
LinkList *s,*r;
r = L;
while(r->next)
{
r = r->next;
}
int x;
char y[8];
int z;
cout<<"请输入学号,-1结束"<<endl;
cin>>x;
while(x != -1)
{
s = ( LinkList *)malloc(sizeof (LinkList));
s->studentinfo.sno = x;
cout<<"请输入姓名"<<endl;
cin>>y;
strcpy(s->studentinfo.sname,y);
cout<<"请输入成绩"<<endl;
cin>>z;
s->studentinfo.score = z;
r->next = s;
r = s;
cout<<"请输入学号"<<endl;
cin>>x;
}
r->next = NULL;
return L;
}
void print(LinkList *L)
{
LinkList *s;
s = L->next;
while(s)
{
std::cout<<"学号是: "<<s->studentinfo.sno
<<"姓名是: "<<s->studentinfo.sname
<<"成绩是: "<<s->studentinfo.score<<std::endl;
s = s->next;
}
}
void insertElem(LinkList *L,int i)
{
LinkList *s,*p = L;
int j = 0;
cout<<"请输入学号"<<endl;
int x;
cin>>x;
cout<<"请输入学号"<<endl;
char y[8];
cin>>y;
cout<<"请输入学号"<<endl;
int z;
cin>>z;
while(p && j < i - 1 && x != -1)
{
p = p->next; j ++;
}
if(p && p->next &&x != -1)
{
s = ( LinkList *)malloc(sizeof (LinkList));
s->studentinfo.sno = x;
strcpy(s->studentinfo.sname,y);
s->studentinfo.score = z;
s->next = p->next;
p->next = s;
}
else cout<<"error"<<endl;
cout<<"插入成功" <<endl;
}
void deleteElem(LinkList *L,int i)
{
LinkList *p,*q;int j;
p = L;
j = 0;
while(p && j<i -1)
{
p = p->next;j ++;
}
if(p && p->next)
{
q = p->next;
p->next = q->next;
free(q);
}
else cout<<"error"<<endl;
cout<<"删除成功" <<endl;
}
void grade(LinkList *L,int score)
{
LinkList *p,*q;
q = L;
p = L->next;
while(p && p->next)
{
if(p->studentinfo.score != score)
{
p = p->next;
q = q->next;
}
else
{
q->next = p->next;
free(p);
p = q->next;
}
}
cout<<"删除成功" <<endl;
}
void save_students_from_file(LinkList *L)
{
LinkList *p;
p = L;
ofstream ofs;
ofs.open("Student.txt");
while(p && p->next){
ofs <<p->next->studentinfo.sno<<" ";
ofs <<p->next->studentinfo.sname<<" ";
ofs <<p->next->studentinfo.score<<endl;
p = p->next;
}
ofs.close();
cout<<"保存成功" <<endl;
}
void read_students_from_file(LinkList *L) {
ifstream ifs("Student.txt");
if (!ifs.is_open()) {
cout << "无法打开文件" << endl;
return;
}
LinkList *p = (LinkList*)malloc(sizeof(LinkList));
while (p && !ifs.eof() ){
ifs >> p->studentinfo.sno >> p->studentinfo.sname >> p->studentinfo.score;
if (!L->next) {
p->next = NULL;
}
else {
p->next = L->next;
}
L->next = p;
p = (LinkList*)malloc(sizeof(LinkList));
}
free(p);
ifs.close();
}
int main(){
LinkList *L;
int i;
L=( LinkList *)malloc(sizeof (LinkList));
L->next = NULL;
printf("------------------------------------------------------\n");
printf("| 这里是xxx的作业! |\n");
printf("| 欢迎使用学生信息管理系统 |\n");
printf("| (1)头插法输入学生 (2)尾插法输入学生 |\n");
printf("| (3)遍历全部学生 (4)输入指定位置插入节点 |\n");
printf("| (5)删除指定位置的节点 (6)删除指定成绩的节点 |\n");
printf("| (7)把链表写入文件 (8)把文件读入链表 |\n");
printf("| (9)退出系统 |\n");
printf("------------------------------------------------------\n");
int op;
scanf("%d",&op);
while(op != 0){
switch(op)
{
case 1:createHeadList(L); break;
case 2:createTailList(L); break;
case 3:print(L); break;
case 4:
cout<<"请输入想要插入的节点"<<endl;
cin>>i;
insertElem(L,i); break;
case 5:
cout<<"请输入想要删除的节点"<<endl;
cin>>i;
deleteElem(L,i); break;
case 6:
cout<<"请输入想要删除的成绩"<<endl;
cin>>i;
grade(L,i);
case 7: save_students_from_file(L); break;
case 8: read_students_from_file(L); break;
case 9: exit(1);
}
scanf("%d",&op);
}
}
我感觉我的错误是在read_students_from_file函数中,我试着读取一下文件中的数据,总是多出一条多余的、不该有的数据。帮帮我谢谢