【面向对象】代码存一存

日期类

#include
using namespace std;

const int D[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

bool isR(int x){
	return x%4 == 0 && x%100!=0 || x%400 == 0;
}

class Date{
	private:
		int y,m,d;
	public:
		void isr(){
			bool f = (y%4 == 0 && y%100!=0 || y%400 == 0);
			if (f) printf("%d is leap year.\n",y);
			else printf("%d is not leap year.\n",y);
		}
		void Input(){
			cin>>y>>m>>d;
		}
		int Sum(Date &b){
			int sum = 0;
			int y1 = y , y2 = b.y;
			int m1 = m , m2 = b.m;
			int d1 = d , d2 = b.d;
			if (y1 > y2) swap(y1,y2) , swap(m1,m2) , swap(d1,d2);
			int now = D[m1];
			if (m1 == 2 && isR(y1)) now++;
			sum+=now-d1;
			for (int i = m1+1; i <= 12; i++){
				sum+=D[i];
				if (i == 2 && isR(y1)) sum++;
			}
			for (int i = y1+1; i < y2; i++){
				sum+=365;
				if (isR(i)) sum++;
			}
			for (int i = 1; i < m2; i++){
				sum+=D[i];
				if (i == 2 && isR(y2)) sum++;
			}
			sum+=d2;
			return sum;
		}
};

int main(){
	Date a,b;
	a.Input();
	b.Input();
	a.isr(); b.isr();
	printf("The skip of two date is %d.",a.Sum(b));
	return 0; 
}
/*
2012 10 20
2017 11 07
*/

三角形类

#include
using namespace std;



class Beeline{
	private:
		int x1,y1,x2,y2;
	public:
		void In(int x,int xx,int y,int yy){
			x1 = x , y1 = xx , x2 = y , y2 = yy;
 			return;
 		}
		double Length(){
			return sqrt((x1-x2)*(x1-x2)*1.0+(y1-y2)*(y1-y2)*1.0);
		}
		void Show(){
 			printf("(%d,%d),(%d,%d)\n",x1,y1,x2,y2);
		}
};



class Tr{
	private:
		Beeline l1,l2,l3;
	public:
		void In(int x,int y,int xx,int yy,int xxx,int yyy){
			l1.In(x,y,xx,yy); l2.In(xx,yy,xxx,yyy); l3.In(xxx,yyy,x,y); 
			return ;
		}
 		void show(){
			l1.Show(); l2.Show(); l3.Show();
		}
		double calc(){
			double a = l1.Length() , b = l2.Length() , c = l3.Length();
			double h = 0.5*(a+b+c);
			return sqrt(h*(h-a)*(h-b)*(h-c));
		}

};



int main(){
	int x,y,xx,yy,xxx,yyy;
	cin>>x>>y>>xx>>yy>>xxx>>yyy;
	Tr sjx;
	printf("Three edges' points are listed as follows:\n");
	sjx.In(x,y,xx,yy,xxx,yyy);
	sjx.show();
	printf("The area of this triangle is:%.2lf",sjx.calc());
	return 0;
}



点类

#include
using namespace std;



class point{
	private:
		double x,y;
	public:
		void In(){
			cin>>x>>y;
		}
		void Print(){
			cout<<"("<<x<<','<<y<<')'<<endl;
		}
		double js(point a){
			double xx = a.x , yy = a.y;
			double ans = sqrt((x-xx)*(x-xx)+(y-yy)*(y-yy));
			return ans;
 	    }
};



int main(){
	point a,b;
	a.In(); b.In();
	a.Print(); b.Print();
	cout<<a.js(b);
	return 0;
}

学生成绩类

#include
using namespace std;



class Stu{
	private:
		int yw,sx,yy;
		static int sum;
		static int cnt;
	public:
		void In(){
			cin>>yw>>sx>>yy;
			sum+=yw+sx+yy;
			cnt++;
		} 
		void getsum(){
			cout<<yw+sx+yy<<endl;
		}
		void gets(){
			cout<<yw<<' '<<sx<<' '<<yy<<endl;
		}
		static double Av(){ 
			double av = sum*1.0/cnt;
			return av;
		}
};

int Stu::sum = 0;
int Stu::cnt = 0;
Stu a[1010100];
int n = 0,now = 0;

int main(){
	cin>>n;
	while (n--){
		int op; cin>>op;
		if (op == 1){
			int x,y,z;
			a[++now].In();
		}
		if (op == 2){
			int x;
			cin>>x;
			a[x].getsum();
		}
		if (op == 3){
			int x; cin>>x;
			a[x].gets();
		}
		if (op == 4) printf("%.2lf\n",a[now].Av());
	}
	return 0;
}


实验七1

#include
using namespace std;

class Per{
	private:
		string namee;
		string sex;
		string ag;
	public:
		Per(string Name,string Ag,string Sex){
			namee = Name , sex = Sex , ag = Ag;
		}
		void Show1(int x){
			if (x)printf("== per1.Display() => name,age,sex\n");
			cout<<namee<<' '<<ag<<' '<<sex;
			if (x) cout<<endl; else cout<<' ';
		}
};

class Stu:public Per{
	private:
		string xh,xb;
	public:
		Stu(string N,string A,string S,string X,string XX):Per(N,A,S){
			xh = X , xb = XX;
		}
		void Show2(int x){
			if (x) printf("== stu1.Display() => name,age,sex,Reg_Number,department\n");
			Show1(0);
			cout<<xh<<' '<<xb;
			if (x) cout<<endl; else cout<<' ';
		}
};
//
class Tea:public Per{
	private:
		string zc,cl;
	public:
		void Show3(){
			printf("== teach1.Display() => name,age,sex,post,course\n");
			Show1(0);
			cout<<zc<<' '<<cl<<endl;
		}
		Tea(string N,string A,string S,string X,string XX):Per(N,A,S){
			zc = X , cl = XX;
		}
};
//
class gra:public Stu{
	private:
		string te;
	public:
		gra(string N,string A,string S,string X,string XX,string T):Stu(N,A,S,X,XX){
			te = T;
		}
		void Show4(){
			printf("== gStu1.Display() => name,age,sex,Reg_Number,department,advisor\n");
			Show2(0);
			cout<<te<<endl;
		}
};

int main(){
	string s;
	string ss[100]; int cnt = 0;
	getline(cin,s);
	s+=' '; string now = "";
	for (int i = 0; i < s.size(); i++)
	  if (s[i] == ' ') ss[++cnt] = now , now = "";
	  else now+=s[i];
	Per a(ss[1],ss[2],ss[3]); a.Show1(1);
	
	now = "" , cnt = 0;
	getline(cin,s);
	s+=' ';
	for (int i = 0; i < s.size(); i++)
	  if (s[i] == ' ') ss[++cnt] = now , now = "";
	  else now+=s[i];
	Stu aa(ss[1],ss[2],ss[3],ss[4],ss[5]); aa.Show2(1);
	
	now = "" , cnt = 0;
	getline(cin,s);
	s+=' ';
	for (int i = 0; i < s.size(); i++)
	  if (s[i] == ' ') ss[++cnt] = now , now = "";
	  else now+=s[i];
	gra aaa(ss[1],ss[2],ss[3],ss[4],ss[5],ss[6]); aaa.Show4();
	
	now = "" , cnt = 0;
	getline(cin,s);
	s+=' ';
	for (int i = 0; i < s.size(); i++)
	  if (s[i] == ' ') ss[++cnt] = now , now = "";
	  else now+=s[i];
	Tea aaaa(ss[1],ss[2],ss[3],ss[4],ss[5]); aaaa.Show3();
}
/*
sun 42 m
guo 22 f 1001 com
wu 25 m 1021 com wei
fang 38 m eng prof
*/

1818类

#include
using namespace std;

class HH{
	private:
		string Na,x,y;
		int num,v;
		static double tot;
	public:
		double calcC(){
			double sum = num*v;
			if (num > 6) sum*=0.8;
			else if (num > 4) sum*=0.6;
			return sum;
		}
		double calcT(){
			double sum = num*v*0.8;
			if (num > 6) sum*=0.8;
			else if (num > 4) sum*=0.6;
			return sum;
		}
		void In(){
			string s; string ss[10];
			getline(cin,s);
			if (s.size() == 0) getline(cin,s);
			s+=' ';
			int cnt = 0 , now = 0;
			string noww = "";
			for (int i = 0; i < s.size(); i++)
			  if (s[i] == ' ') ss[++cnt] = noww , noww = "";
			  else noww+=s[i];
			Na = ss[1];
			for (int i = 0; i < ss[2].size(); i++)
			  now = now*10+ss[2][i]-48;
			num = now; now = 0;
			for (int i = 0; i < ss[3].size(); i++)
			  now = now*10+ss[3][i]-48;
			v = now;
			x = ss[4]; y = ss[5];
			if (Na == "C") tot+=calcC();
			else tot+=calcT();
		}
		void show(){
			cout<<Na<<' '<<num<<' '<<v<<' '<<x<<' '<<y<<' ';
			if (Na == "C") cout<<calcC()<<endl;
			else cout<<calcT()<<endl;
		}
		static double Tot(){
			return tot;
		}
};
double HH::tot = 0;
int main(){
	int n;
	cin>>n;
	HH A;
	for (int i = 1; i <= n; i++) A.In(),A.show();
	cout<<"总价:"; cout<<A.Tot();
	return 0; 
}

年份时间类

#include
using namespace std;

const int D[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

class HTD{
	private:
		int y,m,d;
		int sh,fe,mi;
	public:
		bool isr(){
			return (y%4 == 0 && y%100!=0 || y%400 == 0);
		}
		void In(){
			cin>>y>>m>>d;
			cin>>sh>>fe>>mi;
		}
		void Pl(){
			mi+=30;
			if (mi < 60) return;
			fe+=1; mi-=60;
			if (fe < 60) return;
			sh+=1; fe-=60;
			if (sh < 24) return;
			d+=1; sh-=24;
			int nowd = D[m];
			if (isr() && m == 2) nowd++;
			if (d <= nowd) return;
			d = 1; m++;
			if (m <= 12) return;
			m = 1 , y++;
		}
		void Show(){
			printf("%d-%d-%d %d:%d:%d\n",y,m,d,sh,fe,mi);
		}
};

int main(){
	int n; cin>>n;
	HTD a;
	for (int i = 1; i <= n; i++)
	  a.In() , a.Show() , a.Pl() , a.Show();
	 return 0; 
}

水果店类

#include
using namespace std;

class Fruit{
//	private:
//		double w,p;
	public:
		virtual double Add()=0;
//		Fruit(double pp,double ww){
//			w = ww , p = pp;
//		}
};

class Apple:public Fruit{
	private:
		double w,p;
	public:
		Apple(double pp,double ww){
			p = pp , w = ww;
		}
	    double Add(){
//	    	cout<<"A"<
	    	return w*p;
		}
};

class banana:public Fruit{
	private:
		double w,p;
	public:
		banana(double pp,double ww){
			w = ww , p = pp;
		}
		double Add(){
			return w*p*0.5;
		}
};

class orange:public Fruit{
	private:
		double w,p;
	public:
		orange(double pp,double ww){
			w = ww , p = pp;
		}
		double Add(){
//	    	cout<<"O"<
			if (w > 10) return w*p*0.5;
			if (w > 5) return w*p*0.8;
			return w*p;
		}
};

char ch;

bool Cin(){
	cin>>ch;
	while (ch!='a' && ch!='b' && ch!='o' && ch!='q') cin>>ch;
	if (ch == 'q') return 0;
	return 1;
}

int main(){
	double Ans = 0;
	while (Cin()){
		double w,p; cin>>w>>p;
//		cout<<"w = "<
		Fruit *x;
		Apple A(p,w); banana B(p,w); orange O(p,w);
		if (ch == 'a') x = &A;
		if (ch == 'b') x = &B;
		if (ch == 'o') x = &O;
		Ans+=x->Add();
//		cout<<"Ans = "<
	}
	cout<<Ans;
	return  0;
}
/*
a 1 1
b 10 1
c 10 1
q
*/

你可能感兴趣的:(算法,数据结构)