林荫的ACM生涯——Class和Struct的一部分模板
------------恢复内容开始------------
有关Class类
不要用private,否则没办法以X.XX的形式引用
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; class T_counter { public: T_counter(int x) { element=x; } void Changeadd(int x) { element+=x; } void Changereduce(int x) { element-=x; } void Changemultiply(int x) { element=element*x; } void Changedivide(int x) { element=element/x; } void Changeleftbitmove(int x) { while(x) { x--; element=element<<1; } } void Changerightbitmove(int x) { while(x) { x--; element=element>>1; } } int add(int x) { return x+element; } int reduce(int x) { return element-x; } int multiply(int x) { return element*x; } int divide(int x) { return element/x; } int leftbitmove(int x) { int ssd=element; while(x) { x--; ssd=ssd<<1; } return ssd; } int rightbitmove(int x) { int ssd=element; while(x) { x--; ssd=ssd>>1; } return ssd; } int element; }; class T_vector { public: T_vector(int l,int basic[]) { length=l; for(int i=1;i<=l;i++) { element[i]=basic[i]; } } T_vector ADD(T_vector A) { int ssds[10001]; for(int i=1;i<=A.length;i++) ssds[i]=A.element[i]+element[i]; T_vector C(A.length,ssds); return C; } T_vector REDUCE(T_vector A) { int ssds[10001]; for(int i=1;i<=A.length;i++) ssds[i]=element[i]-A.element[i]; T_vector C(A.length,ssds); return C; } double MOD(T_vector A) { double sum=0; for(int i=1;i<A.length;i++) { sum+=A.element[i]*A.element[i]; } return sqrt(sum); } int length; int element[10001]; }; class T_Matrix { public: T_Matrix(int a1,int a2,int mp[101][101]) { length=a1; width=a2; for(int i=1;i<=length;i++) { for(int j=1;j<=width;j++) { element[i][j]=mp[i][j]; } } } T_Matrix ADD(T_Matrix A) { int ssds[101][101]; for(int i=1;i<=A.length;i++) { for(int j=1;j<=A.width;j++) { ssds[i][j]=A.element[i][j]+element[i][j]; } } T_Matrix C(A.length,A.width,ssds); return C; } T_Matrix REDUCE(T_Matrix A) { int ssds[101][101]; for(int i=1;i<=A.length;i++) { for(int j=1;j<=A.width;j++) { ssds[i][j]=element[i][j]-A.element[i][j]; } } T_Matrix C(A.length,A.width,ssds); return C; } T_Matrix INTMULTIPLY(int x) { int ssds[101][101]; for(int i=1;i<=length;i++) { for(int j=1;j<=width;j++) { ssds[i][j]=element[i][j]*x; } } T_Matrix A(length,width,ssds); return A; } int length,width,element[101][101]; }; int main() { //TEST T_counter t1(10); cout<<t1.add(1)<<' '<<t1.reduce(1)<<' '<<t1.multiply(5)<<' '<<t1.divide(2)<<' '<<t1.leftbitmove(2)<<' '<<t1.rightbitmove(2)<<endl; int sds1[4]={0,1,2,3}; int sds2[4]={0,2,2,2}; T_vector t2(3,sds1); T_vector t3(3,sds2); T_vector t4=t2.ADD(t3); T_vector t5=t2.REDUCE(t3); for(int i=1;i<=t4.length;i++) { cout<<t4.element[i]<<' '; } cout<<endl; for(int i=1;i<=t5.length;i++) { cout<<t5.element[i]<<' '; } cout<<endl; int sds3[101][101]; int sds4[101][101]; for(int i=1;i<=3;i++) { for(int j=1;j<=3;j++) { sds3[i][j]=2; sds4[i][j]=1; } } T_Matrix t6(3,3,sds3); T_Matrix t7(3,3,sds4); T_Matrix t8=t6.ADD(t7); T_Matrix t9=t6.REDUCE(t7); for(int i=1;i<=t8.length;i++) { for(int j=1;j<=t8.width;j++) { cout<<t8.element[i][j]<<' '; } cout<<endl; } for(int i=1;i<=t8.length;i++) { for(int j=1;j<=t9.width;j++) { cout<<t9.element[i][j]<<' '; } cout<<endl; } return 0; }
有关struct重载运算符
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; struct Vector { int a[100001]; int Length; friend inline Vector operator + (Vector x,Vector y) { Vector ss; for(int i=1;i<=x.Length;i++) { ss.a[i]=x.a[i]+y.a[i]; } ss.Length=x.Length; return ss; } friend inline Vector operator - (Vector x,Vector y) { Vector ss; for(int i=1;i<=x.Length;i++) { ss.a[i]=x.a[i]-y.a[i]; } ss.Length=x.Length; return ss; } friend inline int operator * (Vector x,Vector y) { int sum=0; for(int i=1;i<=x.Length;i++) { sum+=x.a[i]*y.a[i]; } return sum; } }; Vector S1,S2,S3; char s; int n; int main() { while(cin>>s) { cin>>n; S1.Length=n; S2.Length=n; for(int i=1;i<=n;i++) { cin>>S1.a[i]; } for(int i=1;i<=n;i++) { cin>>S2.a[i]; } if(s=='+') { S3=S1+S2; for(int i=1;i<n;i++) { cout<<S3.a[i]<<' '; } cout<<S3.a[n]; cout<<endl; } if(s=='-') { S3=S1-S2; for(int i=1;i<n;i++) { cout<<S3.a[i]<<' '; } cout<<S3.a[n]; cout<<endl; } if(s=='*') cout<<S1*S2<<endl; } return 0; }
结构体里面包函数
实际上还可以结构体套结构体
#include<iostream> #include<cstdio> #include<cstring> using namespace std; struct Worker { int basic=2000; string name; }; struct Teacher { Worker x; int time,level; int pay[4]={0,50,30,20}; void Print() { cout<<x.name<<' '<<x.basic+time*pay[level]<<endl; } }; struct Manager { Worker x; int paid; void Print() { cout<<x.name<<' '<<x.basic+paid<<endl; } }; struct Laber { Worker x; int day; void Print() { cout<<x.name<<' '<<x.basic+day*20<<endl; } }; Teacher a; Manager b; Laber c; int main() { cin>>a.x.name>>a.level>>a.time; cin>>b.x.name>>b.paid; cin>>c.x.name>>c.day; a.Print(); b.Print(); c.Print(); return 0; }
------------恢复内容结束------------