#include<iostream>
using namespace std;
#include<string>
#include<fstream>
#include <stdio.h>
class Information
{
private:
int num; //考號
int sum; //總分
string name; //姓名
int chinese; //語文
int math; //數學
int english; //英語
public:
void setdown(); //錄入第一批學生成績
void setdown2(); //錄入第二批學生成績
void score_rank(); //總分排序
void english_rank(); //英語分數超100分排序
}; Information person[100]; //實體100個物件陣列
void Information::setdown()
{
int n;
cout << "請輸入錄入學生人數:" << endl;
cin >> n;
ofstream outf("information.txt", ios::app); //打開檔案,沒有時創造
if (!outf) //判斷檔案是否打開
{
cout << "Cannot open the file\n" << endl;
return;
}
outf << '\t' << '\t' << '\t' << '\t' << '\t' << "第一批成績匯總表" << endl; //保存資訊
outf << "考號" << '\t' << "姓名" << '\t' << "語文" << '\t' << "數學" << '\t' << "英語"<<'\t'<<"總分" << endl;
outf << "(由八位數字組成)" <<'\t'<<"中文"<< endl;
outf.close(); //關閉檔案
for (int i = 0; i < n; i++)
{
cout << "請輸入學生的考號、姓名、語文、數學、英語各科成績:" << endl;
cin >> num >> name >> chinese >> math >> english;
sum = chinese + math + english;
ofstream outf("information.txt", ios::app); //打開檔案,沒有時創造
if (!outf) //判斷檔案是否打開
{
cout << "Cannot open the file\n" << endl;
return;
}
outf << num << '\t' << name << '\t' << chinese << '\t' << math << '\t' << english <<'\t'<<sum<< endl; //保存資訊
outf.close(); //關閉檔案
}
}
void Information::setdown2()
{
int n;
cout << "請輸入錄入學生人數:" << endl;
cin >> n;
ofstream outf("information2.txt", ios::app); //打開檔案,沒有時創造
if (!outf) //判斷檔案是否打開
{
cout << "Cannot open the file\n" << endl;
return;
}
outf << '\t' << '\t' << '\t' << '\t' << '\t' << "第二批成績匯總表" << endl; //保存資訊
outf << "考號" << '\t' << "姓名" << '\t' << "語文" << '\t' << "數學" << '\t' << "英語" << '\t' << "總分" << endl;
outf << "(由八位數字組成)" << '\t' << "中文" << endl;
outf.close(); //關閉檔案
for (int i = 0; i < n; i++)
{
cout << "請輸入學生的考號、姓名、語文、數學、英語各科成績:" << endl;
cin >> num >> name >> chinese >> math >> english;
sum = chinese + math + english;
ofstream outf("information2.txt", ios::app); //打開檔案,沒有時創造
if (!outf) //判斷檔案是否打開
{
cout << "Cannot open the file\n" << endl;
return;
}
outf << num << '\t' << name << '\t' << chinese << '\t' << math << '\t' << english << '\t' << sum << endl; //保存資訊
outf.close(); //關閉檔案
}
}
void Information::score_rank()
{
int line = 0;
int line3 = 0;
int line2;
int line_score = 0;
ofstream myfile("information.txt", ios::app); //打開檔案,沒有時創造
if (!myfile) //判斷檔案是否打開
{
cout << "Cannot open the file\n" << endl;
return;
}
for (int i = 0; i < 100; i++)
{
line++;
if (myfile.eof())
{
break;
}
else if (line <= 3)
{
continue;
}
else
{
myfile >> person[i].num >> person[i].name >> person[i].chinese >> person[i].math >> person[i].english >> person[i].sum;
line_score++;
}
}
myfile.close(); //關閉檔案
ofstream myfile6("information2.txt", ios::app); //打開檔案,沒有時創造
if (!myfile6) //判斷檔案是否打開
{
cout << "Cannot open the file\n" << endl;
return;
}
for (int i = (line_score + 1); i < 100; i++)
{
line3++;
if (myfile6.eof())
{
break;
}
else if (line <= 3)
{
continue;
}
else
{
myfile6 >> person[i].num >> person[i].name >> person[i].chinese >> person[i].math >> person[i].english >> person[i].sum;
}
}
myfile6.close(); //關閉檔案
line2 = (line_score - 1);
int temp;
for (int o = 0; o < line2; o++)
for (int i = 0; i >line2-i; i++)
{
if (person[i].sum < person[i + 1].sum)
{
temp = person[i].sum;
person[i].sum = person[i + 1].sum;
person[i + 1].sum = temp;
}
}
ofstream myfile2("student.txt", ios::app); //打開檔案,沒有時創造
if (!myfile2) //判斷檔案是否打開
{
cout << "Cannot open the file\n" << endl;
return;
}
for (int i = 0; i < line2; i++)
{
myfile2 << person[i].num << '\t' << person[i].name << '\t' << person[i].chinese << '\t' << person[i].math << '\t' << person[i].english
<<'\t'<< person[i].sum << endl;
}
}
void Information::english_rank()
{
int n=0;
ofstream myfile3("student.txt", ios::app); //打開檔案,沒有時創造
if (!myfile3) //判斷檔案是否打開
{
cout << "Cannot open the file\n" << endl;
return;
}
while ( myfile3 >> num >> name >> chinese >> math >> english >> sum )
{
n++;
}
myfile3.close;
ofstream myfile4("student.txt", ios::app); //打開檔案,沒有時創造
if (!myfile4) //判斷檔案是否打開
{
cout << "Cannot open the file\n" << endl;
return;
}
for (int i = 0; i < n; i++)
{
myfile4 >> person[i].num >> person[i].name >> person[i].chinese >> person[i].math >> person[i].english >> person[i].sum;
if ((person[i].english > 100) && (person[i].sum > 300))
{
ofstream myfile5("English.txt", ios::app); //打開檔案,沒有時創造
if (!myfile5) //判斷檔案是否打開
{
cout << "Cannot open the file\n" << endl;
return;
}
myfile5 << person[i].num << '\t' << person[i].name << '\t' << person[i].chinese << '\t' << person[i].math << '\t' << person[i].english << '\t' << person[i].sum << endl;
myfile5.close();
}
}
myfile4.close();
}
uj5u.com熱心網友回復:
自己頂一個
uj5u.com熱心網友回復:
最好把錯誤貼出來,方便別人幫你查錯轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/118585.html
標籤:基礎類
下一篇:從無到有,電腦小白學python
