总结:这次实验算是自闭场吧,发现了有好多不会的知识点,大概是上次作业搞得没听讲的缘故吧。
Problem A: 数组类(II)
main 函数:
int main() { int cases, len; cin >> cases; for(int ca = 1; ca <= cases; ca++) { cin >> len; if(ca % 3 == 0) { Array<char> chr_arr(len); for(int i = 0; i < chr_arr.size(); i++) cin >> chr_arr[i]; chr_arr.put(10); } if(ca % 3 == 1) { Array<int> int_arr(len); for(int i = 0; i < int_arr.size(); i++) cin >> int_arr[i]; int_arr.put(10); } if(ca % 3 == 2) { Array<double> dbl_arr(len); for(int i = 0; i < dbl_arr.size(); i++) cin >> dbl_arr[i]; dbl_arr.put(10); } } }
遗漏知识点:sort从大到小排,加上第三个参数great<type>();
AC代码:
#include <bits/stdc++.h> using namespace std; template <class T> class Array { T *p; int len; public: Array(int l = 0){p = new T[l+1];len = l;} int size(){return len; } void put(int n) { if(n==0) return ; sort(p,p+len,greater<int>()); int en = len<n?len:n; cout << p[0]; for(int i = 1;i<en;i++) cout << " " << p[i] ; cout << endl; } T& operator[](int n)//因为要更改输出中的值,所以要传引用。 { return p[n]; } ~Array()//原则上来说,要判断p是否为空指针,但,根据main函数编程嘛,嘿嘿 { len = 0; delete []p; } }; int main() { int cases, len; cin >> cases; for(int ca = 1; ca <= cases; ca++) { cin >> len; if(ca % 3 == 0) { Array<char> chr_arr(len); for(int i = 0; i < chr_arr.size(); i++) cin >> chr_arr[i]; chr_arr.put(10); } if(ca % 3 == 1) { Array<int> int_arr(len); for(int i = 0; i < int_arr.size(); i++) cin >> int_arr[i]; int_arr.put(10); } if(ca % 3 == 2) { Array<double> dbl_arr(len); for(int i = 0; i < dbl_arr.size(); i++) cin >> dbl_arr[i]; dbl_arr.put(10); } } }
Problem B: 数量的类模板
main 函数:
int main() { Data<int> iData[1001]; Data<double> dData[1001]; int cases, num; char ch; int u; double v; Data<int> a(10), b(20); Data<double> c(3.14), d(-4.1); cout<<"a + b = "<<(a + b)<<endl; cout<<"max(a, b) = "<<(a > b ? a : b)<<endl; cout<<"min(a, b) = "<<(a < b ? a : b)<<endl; cout<<"c + d = "<<(c + d)<<endl; cout<<"max(c, d) = "<<(c > d ? c : d)<<endl; cout<<"min(c, d) = "<<(c < d ? c : d)<<endl; cin>>cases; for (int i = 0; i < cases; i++) { cin>>ch; cin>>num; for (int j = 0; j < num; j++) { if (ch == 'i') { cin>>u; iData[j].setValue(u); } else if (ch == 'd') { cin>>v; dData[j].setValue(v); } } if (ch == 'i') { cout<<GetResult<int>::getMax(iData, num); cout<<" "<<GetResult<int>::getMin(iData, num); cout<<" "<<GetResult<int>::getSum(iData, num)<<endl; } else if (ch == 'd') { cout<<GetResult<double>::getMax(dData, num); cout<<" "<<GetResult<double>::getMin(dData, num); cout<<" "<<GetResult<double>::getSum(dData, num)<<endl; } } return 0; }
AC代码:
#include <bits/stdc++.h> using namespace std; template <class T> class Data { //template <class T1> //friend class GetResult; T value; public: Data(int l ):value(l) {}; Data(double l):value(l) {}; Data():value(0) {}; bool operator>(Data<T> &b) { return value>b.value; } bool operator<(Data<T> &b) { return value < b.value; } Data<T> operator +(Data<T> &b) { return Data<T>(value+b.get()); } template <class T2>//着重点,如何在类模板中用友元重载输出运算符 friend ostream&operator<<(ostream &os, const Data<T2> &b); void setValue(T a) { value =a; } T get() { return value; } }; template <class T> ostream&operator<<(ostream &os, const Data<T> &b) { os << fixed << setprecision(2) << b.value; return os; } template <class T> class GetResult { //friend class Data; public: static Data<T> getSum(Data<T> *arr, int num) { Data<T> b; for(int i = 0; i<num; i++) b.setValue(b.get()+arr[i].get()); return b; } static Data<T> getMax(Data<T> *arr, int num) { Data<T> b(arr[0].get()); for(int i = 1; i<num; i++) b.setValue(arr[i].get()>b.get()?arr[i].get():b.get()); return b; } static Data<T> getMin(Data<T> *arr, int num) { Data<T> b(arr[0].get()); for(int i = 1; i<num; i++) b.setValue(arr[i].get()<b.get()?arr[i].get():b.get()); return b; } }; int main() { Data<int> iData[1001]; Data<double> dData[1001]; int cases, num; char ch; int u; double v; Data<int> a(10), b(20); Data<double> c(3.14), d(-4.1); cout<<"a + b = "<<(a + b)<<endl; cout<<"max(a, b) = "<<(a > b ? a : b)<<endl; cout<<"min(a, b) = "<<(a < b ? a : b)<<endl; cout<<"c + d = "<<(c + d)<<endl; cout<<"max(c, d) = "<<(c > d ? c : d)<<endl; cout<<"min(c, d) = "<<(c < d ? c : d)<<endl; cin>>cases; for (int i = 0; i < cases; i++) { cin>>ch; cin>>num; for (int j = 0; j < num; j++) { if (ch == 'i') { cin>>u; iData[j].setValue(u); } else if (ch == 'd') { cin>>v; dData[j].setValue(v); } } if (ch == 'i') { cout<<GetResult<int>::getMax(iData, num); cout<<" "<<GetResult<int>::getMin(iData, num); cout<<" "<<GetResult<int>::getSum(iData, num)<<endl; } else if (ch == 'd') { cout<<GetResult<double>::getMax(dData, num); cout<<" "<<GetResult<double>::getMin(dData, num); cout<<" "<<GetResult<double>::getSum(dData, num)<<endl; } } return 0; }
模板类中友元的使用(模板类之间并非无法实现友元):
https://blog.csdn.net/hqz1989/article/details/21942867
Problem C: 模板是个好东西
main 函数:
int main() { string n; int x, y, d; char c; cin>>n; cin>>x>>y>>d; cin>>c; Point p(x, y); Data<char> aChar(c); Data<int> anInt(d); Data<Point> aPoint(p); Data<string> aString(n); aChar.show(); anInt.show(); aString.show(); aPoint.show(); return 0; }
水题,怎么说怎么写就行:
#include <bits/stdc++.h> using namespace std; class Point { int x,y; public: Point():x(0),y(0){}; Point(int _x,int _y):x(_x),y(_y){}; friend ostream&operator<< (ostream &os,const Point&b) { os << b.x << " " << b.y ; return os; } }; template <class T> class Data { T value; public: Data(T b):value(b){}; void show() { cout << value << endl; } }; int main() { string n; int x, y, d; char c; cin>>n; cin>>x>>y>>d; cin>>c; Point p(x, y); Data<char> aChar(c); Data<int> anInt(d); Data<Point> aPoint(p); Data<string> aString(n); aChar.show(); anInt.show(); aString.show(); aPoint.show(); return 0; }