文件输入流怎么用啊!
[size=24px][/size]
#include<iostream>
#include<iomanip>
#include<string.h>
using namespace std;
#define forfor int i,j,k; student s;for(i=0;i<n-1;i++) { k=i; for(j=i+1;j<n;j++)
const int n=3;
const int m=10;
class student{
private:
int ID,english,math,computer,total;
char name[m];
char sex[4];
int chinese;
public:
student(){cout<<"构造函数在执行\n";}
~student(){cout<<"析构函数在执行\n";}
student(student &p);
void Getinfo();
void Print(student st[])const;
int FindID(student st[])const;
char FindName(student st[])const;
void SortName(student st[]);
void SortID(student st[],int (*fun)(int a,int b));
void SortTotal(student st[],int (*fun)(int a,int b));
void SortChinese(student st[],int (*fun)(int a,int b));
void SortMath(student st[],int (*fun)(int a,int b));
void SortEnglish(student st[],int (*fun)(int a,int b));
void SortComputer(student st[],int (*fun)(int a,int b));
void swap(student &,student &);
};
student::student(student &p)
{
ID=p.ID;chinese=p.chinese;english=p.english;math=p.math;
computer=p.computer;total=p.total;
strcpy_s(name,p.name);strcpy_s(sex,p.sex );
}
void student::Getinfo()
{
cout<<"请输入学生的姓名\n";cin>>name;
cout<<"请输入学生的学号\n";cin>>ID;
cout<<"请输入学生的性别\n";cin>>sex;
cout<<"请输入语文成绩\n";cin>>chinese;
cout<<"请输入英语成绩\n";cin>>english;
cout<<"请输入数学成绩\n";cin>>math;
cout<<"请输入计算机成绩\n";cin>>computer;
total=chinese+english+math+computer;
}
void student::Print(student st[n]) const
{
for(int i=0;i<n;i++)
{
cout<<"姓名 "<<setw(6)<<st[i].name<<" 学号"<<setw(4)<<st[i].ID <<" 性别"<<setw(4)<<st[i].sex<<" 语文"<<setw(4)<<st[i].chinese;
cout<<" 数学"<<setw(4)<<st[i].math<<" 英语"<<setw(4)<<st[i].english <<" 计算机"<<setw(4)<<st[i].computer<<" 总分"<<setw(4)<<st[i].total<<endl;
}
}
void student::SortTotal(student st[n],int (*fun)(int a,int b))
{
forfor //替换两个for语句
if(fun(st[i].total,st[j].total)) {k=j;}
if(k!=i){s.swap(st[i],st[k]);}
}
s.Print(st);
}
void student::SortID(student st[n],int (*fun)(int a,int b))
{
forfor //替换两个for语句
if(fun(st[i].ID,st[j].ID)) {k=j;}
if(k!=i) {s.swap(st[i],st[k]); }
}
s. Print(st);
}
void student::SortName(student st[n])
{
forfor //替换两个for语句
{if(strcmp(st[i].name,st[j].name)>0) {k=j;}
if(k!=i){s.swap(st[i],st[k]); }
}
}
s. Print(st);
}
void student::SortChinese(student st[n],int (*fun)(int a,int b))
{
forfor //替换两个for语句
{if(fun(st[i].chinese,st[j].chinese)) {k=j;}
if(k!=i){s.swap(st[i],st[k]); }
}
}
s.Print(st);
}
void student::SortMath(student st[n],int (*fun)(int a,int b))
{
forfor //替换两个for语句
{if(fun(st[i].math,st[j].math)) {k=j;}
if(k!=i){s.swap(st[i],st[k]); }
}
}
s. Print(st);
}
void student::SortComputer(student st[n],int (*fun)(int a,int b))
{
forfor //替换两个for语句
{ if(fun(st[i].computer,st[j].computer)) {k=j;}
if(k!=i){s.swap(st[i],st[k]); }
}
}
s. Print(st);
}
void student::SortEnglish(student st[n],int (*fun)(int a,int b))
{
forfor //替换两个for语句
{if(fun(st[i].english,st[j].english)) {k=j;}
if(k!=i){s.swap(st[i],st[k]);}
}
}
s.Print(st);
}
int student::FindID(student st[n])const
{
int i,id;
ff:cout<<"请输入您要查找的学号\n";
cin>>id;
for(i=0;i<n;i++)
{
if(st[i].ID ==id)
{
cout<<"姓名"<<setw(4)<<st[i].name<<" 学号"<<setw(2)<<st[i].ID <<" 性别"<<setw(2)<<st[i].sex <<" 语文"<<setw(2)<<st[i].chinese ;
cout<<" 数学"<<setw(2)<<st[i].math<<" 英语"<<setw(2)<<st[i].english <<" 计算机"<<setw(2)<<st[i].computer<<" 总分"<<setw(3)<<st[i].total<<endl;
return 0;
}
}
cout<<"您查找的学号不存在\n";goto ff;
}
char student::FindName(student st[])const
{
char name1[m];
int i,j=0;
nn:cout<<"请输入要查找学生姓名\n";
cin>>name1;
for(i=0;i<n;i++)
if(strcmp(name1,st[i].name )==0)
{
cout<<"姓名"<<setw(4)<<st[i].name<<" 学号"<<setw(2)<<st[i].ID <<" 性别"<<setw(2)<<st[i].sex<<" 语文"<<setw(2)<<st[i].chinese ;
cout<<" 数学"<<setw(2)<<st[i].math<<" 英语"<<setw(2)<<st[i].english <<" 计算机"<<setw(2)<<st[i].computer<<" 总分"<<setw(3)<<st[i].total<<endl;
return 0;
}
cout<<"此姓名不存在,请重新输入\n";goto nn;
}
void student::swap(student &a,student &b)
{
student s(a);
a=b;b=s;
}
void print()
{
cout<<"欢迎使用荣强学生信息管理系统,请根据提示选择操作\n";
cout<<"温馨提示\n";
cout<<"输入1,进入输入数据界面\n";
cout<<"输入2,进入输出数据界面\n";
cout<<"输入3,进入数据按学号排序界面\n";
cout<<"输入4,进入数据按学号查找界面\n";
cout<<"输入5,进入数据按姓名排序界面\n";
cout<<"输入6,进入数据按姓名查找界面\n";
cout<<"输入7,进入数据按总分排序界面\n";
cout<<"输入8,进入数据按语文成绩排序界面\n";
cout<<"输入9,进入数据按数学成绩排序界面\n";
cout<<"输入10,进入数据按英语成绩排序界面\n";
cout<<"输入11,进入数据按计算机成绩排序界面\n";
cout<<"输入0,退出该系统\n";
}
int ascending(int a,int b){if(a>b) return 1;else return 0;}
int descending(int a,int b){if(a<b) return 1;else return 0;}
int main()
{
print();
student ST[n];
int i,choice=1;
while(choice!=0)
{
kk: cout<<"请输入你的选择\n";cin>>choice;
int scend;
switch(choice)
{
case 0:cout<<"退出系统,欢迎下次使用\n";break;
case 1:cout<<"进入输入数据界面\n";
for(i=0;i<n;i++) ST[i].Getinfo(); print();break;
case 2:cout<<"进入输出数据界面\n";ST[0].Print(ST);print();
break;
case 3:cout<<"进入数据按学号排序界面\n";cout<<"升序请按1,否则降序\n";cin>>scend;
if(scend==1) {ST[0].SortID(ST,ascending);}
else {ST[0].SortID(ST,descending);}
print(); break;
case 4:cout<<"进入数据按学号查找界面\n";ST[0].FindID(ST);print();break;
case 5:cout<<"进入数据按姓名排序界面\n";ST[0].SortName(ST);print(); break;
case 6:cout<<"进入数据按姓名查找界面\n";ST[0].FindName(ST); print();break;
case 7:cout<<"进入数据按总分排序界面\n";cout<<"升序请按1,否则降序\n";cin>>scend;
if(scend==1) {ST[0].SortTotal(ST,ascending);}
else {ST[0].SortTotal(ST,descending);}
print(); break;
case 8:cout<<"进入数据按语文排序界面\n";cout<<"升序请按1,否则降序\n";cin>>scend;
if(scend==1) {ST[0].SortChinese(ST,ascending);}
else {ST[0].SortChinese(ST,descending);}
print(); break;
case 9:cout<<"进入数据按数学排序界面\n";cout<<"升序请按1,否则降序\n";cin>>scend;
if(scend==1) {ST[0].SortMath(ST,ascending);}
else {ST[0].SortMath(ST,descending);}
print(); break;
case 10:cout<<"进入数据按英语排序界面\n";cout<<"升序请按1,否则降序\n";cin>>scend;
if(scend==1) {ST[0].SortEnglish(ST,ascending);}
else {ST[0].SortEnglish(ST,descending);}
print(); break;
case 11:cout<<"进入数据按计算机排序界面\n";cout<<"升序请按1,否则降序\n";cin>>scend;
if(scend==1) {ST[0].SortComputer(ST,ascending);}
else {ST[0].SortComputer(ST,descending);}
print(); break;
default:cout<<"输入有误,请重新输入\n";goto kk;break;
}
}
return 0;
}
晒晒自己的代码!同时求助。。。文件输入流怎么用啊!比如把我的Getinfo()函数改为由文本文件data.txt 中输入,即我的信息放在data.txt中!求助!
[解决办法]
把你的数据保存在data.txt已你定的格式,然后你要用这里面的数据时,用定的格式把数据读出来就可以了吧。学习,了解fstream