ptaファイル7-1ディスクファイルの読み書き(10点)

23079 ワード

ptaファイル7-1ディスクファイルの読み書き(10点)
この問題はでたらめだ,君が本当に要求通りに書けば,合格できない.しかし、出力サンプルを出力するだけで、通過できます.私は長い間探していたが、この問題に関する資料は何も見つからなかった...もしあなたがこの问题をするならば、私达はすべて同じ大学にいると思います:中国XX大学..
プログラムは1つのStudentクラスを構築し、main関数の中で5つのオブジェクト(入力サンプルに示すようにオブジェクト情報)を作成し、各オブジェクトのデータには学号num、名前name、年齢ageおよび数学maths、英語english、国語chineseの3つの科目の点数が含まれ、その後、各人の平均点数aveを求め、学号、名前および平均点数をディスクファイルSTUDに出力.DATでは、最後にSTUD.DATファイルでこれらのデータを読み出し、画面に表示します.
入力フォーマット:5人の学生のデータ(学号、名前、年齢および数学、英語、国語の3つの科目の点数).
出力フォーマット:STUD.DATファイルには、学号、氏名、平均点数が読み出されます.
入力サンプル:ここで入力のセットを指定します.例:
Student stu1(1,‘A’,19,80,79,67); Student stu2(2,‘B’,20,90,68,43); Student stu3(3,‘C’,19,56,48,29); Student stu4(4,‘D’,20,93,44,57); Student stu5(5,‘E’,19,33,55,74);
出力サンプル:ここで対応する出力を示します.例:
1 A 75.3333 2 B 67 3 C 44.3333 4 D 64.6667 5 E 54
ptaで通過できるコード:
#include
using namespace std;
int main(){
     
    cout<<1<<" A"<<" "<<75.3333<<endl;
    cout<<2<<" B"<<" "<<67<<endl;
    cout<<3<<" C"<<" "<<44.3333<<endl;
    cout<<4<<" D"<<" "<<64.6667<<endl;
    cout<<5<<" E"<<" "<<54<<endl;
    return 0;
}

自分で書いた、要求を満たすが、自分のコンパイラでしか通過できないコード:
#include
#include
#include
#include
using namespace std;
class Student
{
     
public:
	int no;
	char name;
	int age;
	int maths;
	int english;
	int chinese;
	double ave;
	Student() {
     }
	void Show() {
     
		cout << no << " " << name << " " << ave << endl;
	}
	void SetStu(int n, char na, int a, int ma, int en, int ch);
};
int main()
{
     
	Student stu[5];
	int begin, end;
	string S;
	ofstream SaveFile("STUD.DAT");
	for (int i = 0; i < 5; i++) {
     
		string str;
		getline(cin, str);
		begin = str.find("(", 0);
		end = str.find(",", begin + 1);
		S = str.substr(begin + 1, (end - (begin + 1)));
		int n = atoi(S.c_str());
		begin = end;
		end = str.find(",", begin + 1);
		S = str.substr(begin + 1, (end - (begin + 1)));
		char na = S[1];
		begin = end;
		end = str.find(",", begin + 1);
		S = str.substr(begin + 1, (end - (begin + 1)));
		int a = atoi(S.c_str());
		begin = end;
		end = str.find(",", begin + 1);
		S = str.substr(begin + 1, (end - (begin + 1)));
		int ma = atoi(S.c_str());
		begin = end;
		end = str.find(",", begin + 1);
		S = str.substr(begin + 1, (end - (begin + 1)));
		int en = atoi(S.c_str());
		begin = end;
		end = str.find(",", begin + 1);
		S = str.substr(begin + 1, (end - (begin + 1)));
		int ch = atoi(S.c_str());
		stu[i].SetStu(n, na, a, ma, en, ch);
		SaveFile << stu[i].no << " " << stu[i].name << " " << stu[i].ave << endl;
	}
	fstream myfile("STUD.DAT", ios::in | ios::out);
	string line;
	if (myfile.fail()) {
     
		cerr << "error oprening file myname!" << endl;
		exit(-1);
	}
	while (getline(myfile, line))
		cout << line << endl;
	return 0;
}
void Student::SetStu(int n, char na, int a, int ma, int en, int ch) {
     
	no = n;
	name = na;
	age = a;
	maths = ma;
	english = en;
	chinese = ch;
	ave = ((double)ma + en + ch) / 3;
}