herrDeng網內搜尋

自訂搜尋

Ads

2016年10月28日 星期五

Ex8 C++檔案讀寫與operator overloading

定義student class






運算子多載處理輸出



字串分割、字串轉換與運算子多載處理輸入





主程式

16 則留言:

b10233127 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
student() {};
student(string x, int y, int z, int w){
name = x; math = y; java = z; OS = w;
AVGS();
}
double AVGS(){
return (math + java + OS) / 3.0;
}
};

ostream&operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << "," << X.AVGS() << endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream&operator>>(istream& ff, student&X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int AVG = (math + java + OS) / 3.0;
X = student(name, math, java, OS);
return ff;
}
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;

}

B10133186 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
double av;
student(){};
student(string x, int y, int z, int w){
name = x; math = y; java = z; OS = w; av = (y + z + w) / 3.0;
}
};
ostream& operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << "," << X.av << endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream& operator >>(istream& ff, student& X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
X = student(name, math, java, OS);
return ff;
}
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;
}

B10233082 唐偉強 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
student() {};
student(string x, int y, int z, int w){
name = x; math = y; java = z; OS = w;
AVGS();
}
double AVGS(){
return (math + java + OS) / 3.0;
}
};

ostream&operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << "," << X.AVGS() << endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream&operator>>(istream& ff, student&X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int AVG = (math + java + OS) / 3.0;
X = student(name, math, java, OS);
return ff;
}
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;

}

b10233137 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
student() {};
student(string x, int y, int z, int w){
name = x; math = y; java = z; OS = w;
AVGS();
}
double AVGS(){
return (math + java + OS) / 3.0;
}
};

ostream&operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << "," << X.AVGS() << endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream&operator>>(istream& ff, student&X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int AVG = (math + java + OS) / 3.0;
X = student(name, math, java, OS);
return ff;
}
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;

}

b10233169 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
student() {};
student(string x, int y, int z, int w){
name = x; math = y; java = z; OS = w;
AVGS();
}
double AVGS(){
return (math + java + OS) / 3.0;
}
};

ostream&operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << "," << X.AVGS() << endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream&operator>>(istream& ff, student&X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int AVG = (math + java + OS) / 3.0;
X = student(name, math, java, OS);
return ff;
}
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;

}

b10233139 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
student() {};
student(string x, int y, int z, int w){
name = x; math = y; java = z; OS = w;
AVGS();
}
double AVGS(){
return (math + java + OS) / 3.0;
}
};

ostream&operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << "," << X.AVGS() << endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream&operator>>(istream& ff, student&X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int AVG = (math + java + OS) / 3.0;
X = student(name, math, java, OS);
return ff;
}
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;

}

A10433015 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

class student
{
public:
string name;
int math;
int java;
int OS;
float san;

student(){};
student(string x, int y, int z, int w,float c){
name = x; math = y; java = z; OS = w;
san = c;
}

};

ostream&operator<<(ostream&ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS <<"," <<X.san<<endl;
return ff;

}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1)
{
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream& operator>>(istream&ff, student&X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
float san = (math + java + OS) / 3.0;
X = student(name, math, java, OS,san);

return ff;

}
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
float san;
do
{
student X;
f_in >> X;
f_out << X;
cout << X;

} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;
}

B10433092 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include<string>
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
int XO;
student(){};
student(string x, int y, int z, int w){

name = x; math = y; java = z; OS = w;
XO = (math + java + OS) / 3.0;
}

};
ostream& operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << "," << X.XO << endl;
return ff;
};
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
};
istream& operator >> (istream& ff, student& X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];

int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
X = student(name, math, java, OS);
return ff;
};

int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;
}

B10433080 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
int OX;


student(){};
student(string x, int y, int z, int w){

name = x; math = y; java = z; OS = w;
OX = (math + java + OS) / 3.0;
}






};
ostream& operator<<(ostream&ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << ","<<X.OX<<endl;
return ff;
};
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;

}
a[i] = tmp.substr(start, tmp.length() - start);
};
istream& operator >>(istream& ff, student&X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
X = student(name, math, java, OS);
return ff;
};
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;

} while (!f_in.eof());
f_in.close();

system("Pause");
return 0;


}

B10333016 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

class student{
public:
string name;
int math;
int java;
int OS;
int avg;
student(){};
student(string x, int y, int z, int w ,int t){
name = x; math = y; java = z; OS = w; avg = t;
}
};

ostream& operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS <<","<<X.avg<<endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream& operator >>(istream& ff, student& X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int avg = (math + java + OS) / 3.0;
X = student(name, math, java, OS,avg);
return ff;
}

int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
do{
student X;
f_in >> X;
f_out << X;
cout <<X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;
}

B10333049 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

class student{
public:
string name;
int math;
int java;
int OS;
int avg;
student(){};
student(string x, int y, int z, int w ,int t){
name = x; math = y; java = z; OS = w; avg = t;
}
};

ostream& operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS <<","<<X.avg<<endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream& operator >>(istream& ff, student& X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int avg = (math + java + OS) / 3.0;
X = student(name, math, java, OS,avg);
return ff;
}

int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
do{
student X;
f_in >> X;
f_out << X;
cout <<X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;
}

B10333054 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

class student{
public:
string name;
int math;
int java;
int OS;
int avg;
student(){};
student(string x, int y, int z, int w ,int t){
name = x; math = y; java = z; OS = w; avg = t;
}
};

ostream& operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS <<","<<X.avg<<endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream& operator >>(istream& ff, student& X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int avg = (math + java + OS) / 3.0;
X = student(name, math, java, OS,avg);
return ff;
}

int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
do{
student X;
f_in >> X;
f_out << X;
cout <<X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;
}

B10233108 提到...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
student() {};
student(string x, int y, int z, int w){
name = x; math = y; java = z; OS = w;
AVGS();
}
double AVGS(){
return (math + java + OS) / 3.0;
}
};

ostream&operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << "," << X.AVGS() << endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream&operator>>(istream& ff, student&X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int AVG = (math + java + OS) / 3.0;
X = student(name, math, java, OS);
return ff;
}
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;
}

B10233082 唐偉強 提到...

#include
#include
#include
#include
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
student() {};
student(string x, int y, int z, int w){
name = x; math = y; java = z; OS = w;
AVGS();
}
double AVGS(){
return (math + java + OS) / 3.0;
}
};

ostream&operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << "," << X.AVGS() << endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream&operator>>(istream& ff, student&X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int AVG = (math + java + OS) / 3.0;
X = student(name, math, java, OS);
return ff;
}
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;

}

b10233074 提到...

#include
#include
#include
#include
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
student() {};
student(string x, int y, int z, int w){
name = x; math = y; java = z; OS = w;
AVGS();
}
double AVGS(){
return (math + java + OS) / 3.0;
}
};

ostream&operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << "," << X.AVGS() << endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream&operator>>(istream& ff, student&X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int AVG = (math + java + OS) / 3.0;
X = student(name, math, java, OS);
return ff;
}
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;

}

B10233077 提到...

#include
#include
#include
#include
using namespace std;
class student {
public:
string name;
int math;
int java;
int OS;
student() {};
student(string x, int y, int z, int w){
name = x; math = y; java = z; OS = w;
AVGS();
}
double AVGS(){
return (math + java + OS) / 3.0;
}
};

ostream&operator<<(ostream& ff, student X){
ff << X.name << ",";
ff << X.math << "," << X.java << "," << X.OS << "," << X.AVGS() << endl;
return ff;
}
void stringSplitter(string tmp, string a[4]){
int n;
int i = 0, start = 0;
while ((n = tmp.find(",", start)) != -1){
a[i] = tmp.substr(start, n - start);
start = n + 1;
i++;
}
a[i] = tmp.substr(start, tmp.length() - start);
}
istream&operator>>(istream& ff, student&X){
string tmp;
getline(ff, tmp);
string a[4];
stringSplitter(tmp, a);
string name = a[0];
int math = stoi(a[1]);
int java = stoi(a[2]);
int OS = stoi(a[3]);
int AVG = (math + java + OS) / 3.0;
X = student(name, math, java, OS);
return ff;
}
int main(){
ifstream f_in;
ofstream f_out;
f_out.open("out.csv", ios::out);
f_in.open("ez.csv", ios::in);
string tmp;
string name;
int math, java, OS;
do{
student X;
f_in >> X;
f_out << X;
cout << X;
} while (!f_in.eof());
f_in.close();
system("Pause");
return 0;

}

Related Posts Plugin for WordPress, Blogger...

熱門文章