C++作业缓存

xiaoxiao2021-02-28  73

1. #include<bits/stdc++.h> using namespace std; class student { public: student(){} student(string n,int ids,int x,int y,int z):name(n),id(ids),a_grade(x),b_grade(y),c_grade(z) { cout << "构造中……" << endl; } ~ student() { cout << "析构中……" << endl; } friend istream& operator >> (istream &,student &); friend ostream& operator << (ostream &,student &); private: string name; int id; int a_grade,b_grade,c_grade; }; istream& operator >> (istream &in,student &c) { in >> c.name >> c.id >> c.a_grade >> c.b_grade >> c.c_grade; return in; } ostream& operator << (ostream &out,student &c) { out <<"姓名:"<< c.name << "学号:" << c.id << "线代成绩: " << c.a_grade << "高学成绩: " << c.b_grade << "概率成绩: " << c.c_grade << endl; return out; } int main() { student stu1("ZhuangBiTang",1507064227,12,32,23),stu2; cout << stu1; cin >> stu2; cout << stu2; return 0; }

2. #include<bits/stdc++.h> using namespace std; class animal { public: animal(){} animal(int x):a(x){} ~animal(){} protected: int a; }; class cat1 : virtual public animal { public: cat1(int x,int y):b(x),animal(y){} cat1(){} ~cat1(){} protected: int b; }; class cat2 : virtual public animal { public: cat2(){} cat2(int x,int y):c(x),animal(y){} ~cat2(){} protected: int c; }; class cat3: public cat1,public cat2 { public: cat3(){} cat3(int x,int y,int z,int k):d(x),cat1(y,k),cat2(z,k),animal(k){} ~cat3(){} display() { cout << a << " " << b << " " << c << " " << d << endl; } protected: int d; }; int main() { cat3 x(1,2,3,4); x.display(); animal y(1); return 0; } 3. #include<bits/stdc++.h> using namespace std; namespace d1 { class is_error//自定义异常类 { public: is_error():s("y == 0 can't be divide !"){} string s; }; void work(double x,double y)//x / y º¯Êý { try { if(!y) throw is_error(); cout << " x / y = " << x/y << endl; } catch(is_error & x) { cout << x.s << endl; } } } namespace d2 { class is_error { public: is_error():s("y == 0 can't be divide !"){} string s; }; void work(double x,double y)//x / y º¯Êý { try { if(!y) throw is_error(); cout << " x / y = " << x/y << endl; } catch(is_error & x) { cout << x.s << endl; } } } int main() { int x,y; scanf("%d%d",&x,&y); d1 :: work(x,y); d2 :: work(y,x); return 0; } 4. #include<bits/stdc++.h> using namespace std; class student { public: student(){} student(int x,int y):a_grade(x),b_grade(y){} ~ student(){} int a_grade,b_grade; }; bool cmp(const student &a,const student &b) { if(a.a_grade == b.a_grade) return a.b_grade < b.b_grade; return a.a_grade < b.a_grade; } int main() { vector<int>v; for(int i = 1; i <= 10; i ++) { int x; scanf("%d",&x); v.push_back(x); } sort(v.begin(),v.end()); for(int i = 0; i < v.size(); i ++) printf("%d%c",v[i],i < v.size() - 1 ? ' ':'\n'); vector<student>vs; for(int i = 1; i <= 5; i ++) { int x,y; scanf("%d%d",&x,&y); vs.push_back(student(x,y)); } sort(vs.begin(),vs.end(),cmp); ofstream outfile; outfile.open("out.txt",ios::out); if(!outfile) cout << " open failed !" << endl; for(int i = 0; i < vs.size(); i ++) outfile << vs[i].a_grade << " " << vs[i].b_grade << endl; outfile.close(); ifstream infile; infile.open("out.txt",ios::in); for(int i = 0; i < vs.size(); i ++) infile >> vs[i].a_grade >> vs[i].b_grade; for(int i = 0; i < vs.size(); i ++) cout << vs[i].a_grade << " " << vs[i].b_grade << endl; infile.close(); return 0; }

转载请注明原文地址: https://www.6miu.com/read-85508.html

最新回复(0)