Questions and codes:
/* Copyright (c) 2015, Yantai University School of Computer * All rights reserved. * File name: Project4.cpp * Author: Linan * Completion Date: June 14, 2015 * Version number: v1.0 * * Problem Description: (1) Define student class, which includes number, name, C + + class, high number and English score and total data member, member function according to need to determine
。 (2) Read the student's score and find out the total score, and store it with an array of objects.
The ASCII file Score.dat holds 100 students ' number, name and C + + classes, high scores, and English grades.
(3) Save all the data to a binary file Binary_score.dat, and finally input your information through the keyboard, and write to the file (I am not modest, three subjects full 100 points, the end of the good luck).
(4) To verify that the output file is correct, the records in the Binary_score.dat are read out to the student object and the output is viewed. (5) Viewing binary files with binaryviewer command * input Description: Slightly * program output: Slightly */#include <iostream> #include <fstream> #include <cstring&
Gt
#include <cstdlib> using namespace std;
Class Student {Private:int num;
String name;
Double CPP;
Double math;
Double 中文版;
Double total;
public:student (int n=0,string na= "0", double c=0,double m=0,double e=0);
int Getnum () {return num;
} string GetName () {return name;
} double Getcpp () {return CPP;
} double Getmath () {return math;
} double Getenglish () {return 中文版;
} double Gettotal () {return total;
} void SetValue (int n,string na, double C, double m, double e);
Friend ostream& operator<< (ostream&, student&);
};
student::student (int n,string na,double c,double m,double e) {num=n;
Name=na;
Cpp=c;
Math=m;
English=e;
Total=m+c+e;
} void Student::setvalue (int n,string nam, double C, double m, double e) {num=n;
Name=nam;
Cpp=c;
Math=m;
English=e;
Total=c+m+e; } ostream& operator<< (ostream& out, student& s) {out<<s.num<< "" <<s.name<<
<<s.cpp<< "<<s.math<<" <<s.english<< "<<s.total<<endl;
return out;
} int main () {Student stu[50];
int i;
int num;
String lname;
Double Lcpp, Lmath, lenglish;
Ifstream infile ("Score.txt", ios::in); if (!infile)//test successfully opened {cerr<< "open error!"<<endl;
Exit (1);
} for (i=0;i<45;i++) {infile>>num>>lname>>lcpp>>lmath>>lenglish;
Stu[i].setvalue (Num,lname,lcpp,lmath,lenglish);
} infile.close (); Ofstream outfile ("Binary_score.dat", ios::out|ios::binary);//binary file if (!outfile) {cerr<< "Open error!"
<<endl;
Exit (1); } for (i=0;i<45;i++) {outfile.write ((char*) &stu[i], sizeof (stu[i]));//Note write function} cout<
< "Enter your information:";
cin>>num>>lname>>lcpp>>lmath>>lenglish;
Student my (num,lname,lcpp,lmath,lenglish);
Outfile.write ((char*) &my, sizeof (my));//Note the Write function outfile.close ();
Student Stud;
Ifstream infile1 ("Binary_score.dat", ios::in|ios::binary);//Read the records in Binary_score.dat to the student object and output the view. if (!infile1) {cerr<< "Open error!"
<<endl;
Exit (1); } while (1) {Infile1.read ((char*) &stud, sizeof (stud));
if (infile1.eof ()) break;
cout<<stud;
} infile1.close ();
return 0;
}
Operation Result:
Summary of Knowledge points:
At first did not pay attention to the length of the study number with an int, and then changed a bit before the output is correct.
Because the member function is relatively simple to write, the main function is much more, and needs to be improved.