|
#include <iostream>
#include <fstream>
#include <iomanip>
#include<string>
using namespace std;
class student {
private:
long num;
string name;
float chin;
float math;
float Eng;
public:
student() {}
student(long a, string b, float c, float d, float e) {
num = a;
name = b;
chin = c;
math = d;
Eng = e;
}
~student() {
}
void setstudent() {//设置学生信息
cout << "请输入学生考号:";
cin >> num;
cout<< "请输入学生姓名:";
cin >> name;
cout << "请输入学生语文成绩:";
cin >> chin;
cout<< "请输入学生数学成绩:";
cin >> math;
cout << "请输入学生英语成绩:";
cin >> Eng;
}
float grade();
friend void wenjian1(student stu[], int n);
friend void wenjian2(student stu[], int n);
};
float student::grade(){return chin+math+Eng;}
void wenjian1(student stu[], int n) {
ofstream myfile("f:\\student1.txt");
if (!myfile) {
cout << "Unable to open myfile!";
exit(1);
}
if (!myfile.eof()) {
myfile << setw(12) << setiosflags(ios::left) << "考号" << setw(8) << "姓名"
<< setw(8) << "语文" << setw(8) << "数学" << setw(8) << "英语"
<< endl;
for (int i = 0; i < n; i++) {
myfile << setw(12) << setiosflags(ios::left) << stu[i].num << setw(
8) << stu[i].name << setw(8) << stu[i].chin << setw(8)
<< stu[i].math << setw(8) << stu[i].Eng << endl;
}
}
myfile.close();
}
void wenjian2(student stu[], int n) {
ofstream myfile("f:\\student2.txt");
if (!myfile) {
cout << "Unable to open myfile!";
exit(1);
}
if (!myfile.eof()) {
myfile << setw(12) << setiosflags(ios::left) << "考号" << setw(8) << "姓名"
<< setw(8) << "语文" << setw(8) << "数学" << setw(8) << "英语"
<< endl;
for (int i = 0; i < n; i++) {
myfile << setw(12) << setiosflags(ios::left) << stu[i].num << setw(
8) << stu[i].name << setw(8) << stu[i].chin << setw(8)
<< stu[i].math << setw(8) << stu[i].Eng << endl;
}
}
myfile.close();
}
int main() {
student stu1[25];
for(int i=0;i<25;i++){
stu1[i].setstudent();
}
wenjian1(stu1,25);
student stu2[30];
for(int i=0;i<30;i++){
stu2[i].setstudent();
}
wenjian2(stu2,30);
怎样读取txt中的数据,及求3科总分和排序
return 0;
}
怎样读取文件,还有对3科总分计算及排序。 |
|