完整代码如下:
#include<iostream>
#include<iomanip>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
#define MAXSIZE 56
struct datatype
{
long no;
char name[10];
float score;
};
class student
{
private:
datatype data[MAXSIZE];
int last;
public:
void input();
void output();
int insert_sq(datatype x);
int delete_sq(int i);
void printf_sq();
void sort_sq(); /* 按成绩排序 */
};
void menu();
int main()
{student sq;
int n,m=1;
sq.input();
while(m)
{menu();
cin>>n;
switch(n)
{
case 1:
sq.printf_sq();break;
case 2:
{int i;
datatype x;
cout<<"请输入需要添加的学生个数:";
cin>>i;
while(i--)
{cout<<"请输入学生(学号,姓名,成绩):"<<endl;
cin>>x.no>>x.name>>x.score;
sq.insert_sq(x);}
cout<<"插入后信息:"<<endl;
sq.printf_sq();
sq.output();
break;}
case 3:
{int i;
cout<<"请输入删除位置:";
cin>>i;
sq.delete_sq(i);
cout<<"删除后信息:"<<endl;
sq.printf_sq();
sq.output();
break;}
case 4:
{sq.sort_sq();
sq.printf_sq();
sq.output();
break;}
case 0:
m=0;
}
}
return 0;
}
void menu()
{
cout<<endl<<"1:查看"<<endl;
cout<<"2:添加"<<endl;
cout<<"3:删除"<<endl;
cout<<"4:排序"<<endl;
cout<<"0:退出"<<endl;
cout<<endl<<"请选择:";
}
int student::insert_sq(datatype x)
{
if(last==MAXSIZE)
{cout<<"已满!"<<endl;
return (-1);}
else {data[++last]=x;
return(1);}
}
int student::delete_sq(int i)
{int j;
if(i<1||i>last)
{cout<<"位置不存在"<<endl;
return (0);}
for(j=i;j<=last;j++)
data[j]=data[j+1];
last--;
return (1);
}
void student::sort_sq()
{int n1,n2;
datatype temp;
for(n1=1;n1<last;n1++)
for(n2=n1+1;n2<(last+1);n2++)
{if(data[n1].score<data[n2].score)
{temp=data[n1];
data[n1]=data[n2];
data[n2]=temp;}
}
}
void student::printf_sq()
{int i;
cout<<"学生人数:"<<last<<endl;
for(i=1;i<(last+1);i++)
cout<<"第"<<i<<"名"<<setw(12)<<data[i].no<<setw(11)<<data[i].name<<setw(4)<<data[i].score<<endl;
cout<<endl;
cout<<"按任意键继续:";
getch();
cout<<endl;
}
void student::input()
{
int i;
ifstream inFile("score.txt",ios::in);
for( i=0;inFile;++i)
{
inFile>>data[i].no;
inFile>>data[i].name;
inFile>>data[i].score;
}
last=i;
inFile.close();
}
void student::output()
{
ofstream writeFile("score.txt",ios::out);
for(int i=0;i<last;++i)
{
writeFile<<data[i].no;
writeFile<<data[i].name;
writeFile<<data[i].score<<"\n";
}
}
#include<iostream>
#include<iomanip>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
#define MAXSIZE 56
struct datatype
{
long no;
char name[10];
float score;
};
class student
{
private:
datatype data[MAXSIZE];
int last;
public:
void input();
void output();
int insert_sq(datatype x);
int delete_sq(int i);
void printf_sq();
void sort_sq(); /* 按成绩排序 */
};
void menu();
int main()
{student sq;
int n,m=1;
sq.input();
while(m)
{menu();
cin>>n;
switch(n)
{
case 1:
sq.printf_sq();break;
case 2:
{int i;
datatype x;
cout<<"请输入需要添加的学生个数:";
cin>>i;
while(i--)
{cout<<"请输入学生(学号,姓名,成绩):"<<endl;
cin>>x.no>>x.name>>x.score;
sq.insert_sq(x);}
cout<<"插入后信息:"<<endl;
sq.printf_sq();
sq.output();
break;}
case 3:
{int i;
cout<<"请输入删除位置:";
cin>>i;
sq.delete_sq(i);
cout<<"删除后信息:"<<endl;
sq.printf_sq();
sq.output();
break;}
case 4:
{sq.sort_sq();
sq.printf_sq();
sq.output();
break;}
case 0:
m=0;
}
}
return 0;
}
void menu()
{
cout<<endl<<"1:查看"<<endl;
cout<<"2:添加"<<endl;
cout<<"3:删除"<<endl;
cout<<"4:排序"<<endl;
cout<<"0:退出"<<endl;
cout<<endl<<"请选择:";
}
int student::insert_sq(datatype x)
{
if(last==MAXSIZE)
{cout<<"已满!"<<endl;
return (-1);}
else {data[++last]=x;
return(1);}
}
int student::delete_sq(int i)
{int j;
if(i<1||i>last)
{cout<<"位置不存在"<<endl;
return (0);}
for(j=i;j<=last;j++)
data[j]=data[j+1];
last--;
return (1);
}
void student::sort_sq()
{int n1,n2;
datatype temp;
for(n1=1;n1<last;n1++)
for(n2=n1+1;n2<(last+1);n2++)
{if(data[n1].score<data[n2].score)
{temp=data[n1];
data[n1]=data[n2];
data[n2]=temp;}
}
}
void student::printf_sq()
{int i;
cout<<"学生人数:"<<last<<endl;
for(i=1;i<(last+1);i++)
cout<<"第"<<i<<"名"<<setw(12)<<data[i].no<<setw(11)<<data[i].name<<setw(4)<<data[i].score<<endl;
cout<<endl;
cout<<"按任意键继续:";
getch();
cout<<endl;
}
void student::input()
{
int i;
ifstream inFile("score.txt",ios::in);
for( i=0;inFile;++i)
{
inFile>>data[i].no;
inFile>>data[i].name;
inFile>>data[i].score;
}
last=i;
inFile.close();
}
void student::output()
{
ofstream writeFile("score.txt",ios::out);
for(int i=0;i<last;++i)
{
writeFile<<data[i].no;
writeFile<<data[i].name;
writeFile<<data[i].score<<"\n";
}
}