C++

输出大写英文字母

 

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     string str, input;
 8     getline(cin, input);
 9 
10     for(unsigned i(0); i < input.length(); i++)
11     {
12         if(input[i] <= 'Z' && input[i] >= 'A')
13         {
14             bool flag(true);
15 
16             for(unsigned j(0); j < str.length(); j++)
17             {
18                 if(input[i] == str[j])
19                 {
20                     flag = false;
21                     break;
22                 }
23             }
24 
25             if(flag)
26                 str.append(&input[i], 1);
27         }
28     }
29 
30     if(str == "") cout << "Not Found";
31     else cout << str;
32 
33     return 0;
34 }

 

2017Final 圆周率山

 

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 #define Pi "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196"
 5 
 6 int main()
 7 {
 8     string str(Pi);
 9     int heigh(0), postion(0), width(1);
10     cin >> heigh;
11     string *p = new string[heigh];
12 
13     for(int i(0); i < heigh; i++)
14     {
15         (*(p + i)).append(str.substr(postion, width));
16         postion += +width, width += 2;
17 
18         for(int j(heigh - 1 - i); j > 0; j--) cout << " ";
19 
20         cout << *(p + i) << endl;
21     }
22 
23     return 0;
24 }

 

宿舍谁最高?

 

 1 #include<iostream>
 2 #include<string>
 3 #include<iomanip>
 4 using namespace std;
 5 class Student
 6 {
 7 public:
 8     int id;
 9     string name;
10     int h = 0;
11     int w;
12 };
13 
14 Student s[1000000];
15 int main()
16 {
17     int n, p = 0;
18     cin >> n;
19     for (int i = 0; i < 1000000; i++)
20         s[i].id = i;
21 
22     Student stu;
23     bool isendl = false;
24     for (int i = 0; i < n; i++)
25     {
26         cin >> stu.id >> stu.name >> stu.h >> stu.w;
27         if (stu.h > s[stu.id].h)
28         {
29             s[stu.id].name = stu.name;
30             s[stu.id].h = stu.h;
31             s[stu.id].w = stu.w;
32             p++;
33         }
34     }
35     for (int i = 0; i < 1000000; i++)
36     {
37         if (s[i].h == 0)
38             continue;
39         if (isendl) {
40             cout << endl;
41         }
42         isendl = true;
43        cout<<fixed<<setw(6)<<setfill('0')<<s[i].id;
44         cout << ' ' << s[i].name << ' ' << s[i].h << ' ' << s[i].w;
45     }
46 }

 

立方体类的实现

 

 1 #include<iostream>
 2 using namespace std;
 3 class Box
 4 {
 5 public:
 6     void seta(float ab)
 7     {a=ab;}
 8     void getvolume()
 9     {volume=a*a*a;}
10     void getarea()
11     {area=a*a*6;}
12     void disp()
13     {cout<<volume<<" "<<area<<endl;}
14 private:
15     float a,volume,area;};
16 int  main( ){
17     float ab;
18     cin>>ab;
19     Box  obj;
20     obj.seta( ab );
21     obj.getvolume( );
22     obj.getarea( );
23     obj.disp( );
24     return 0;
25 }

 

鸿鸿哥的苹果树

 

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int height[10],h,number(0);
 6     for(int i(0);i<10;i++)
 7     {
 8         cin>>height[i];
 9     }
10     cin>>h;
11     for(int i(0);i<10;i++)
12     {
13         if(height[i]<=h+30)
14           number++;
15     }
16     cout<<number;
17     return 0;
18 }

 

学生CPP成绩计算

 

 1 #include<iostream>
 2 #include<string>
 3 #include<iomanip>
 4 using namespace std;
 5 class Person
 6 {
 7 protected:
 8     string name;
 9     int age;
10 public:
11     Person(){};
12     Person(string p_name,int p_age):name(p_name),age(p_age){};
13     void disply(){cout<<name<<":"<<age<<endl;};
14 };
15 class student:protected Person
16 {
17     int ID;
18     float cpp_score,cpp_count,cpp_grade;
19 public:
20     student(){};
21     student(string,int,int,float,float);
22     void print();
23 };
24 student::student(string n,int id,int a,float s,float c):Person(n,a),ID(id),cpp_score(s),cpp_count(c),cpp_grade(s*0.9+c*2){}
25 void student::print()
26 {    
27     cout<<ID<<' '<<name<<' '<<fixed<<setprecision(1)<<cpp_grade<<endl;
28 }
29 int main()
30 {
31     string name;
32     int id,age;
33     float score,count;
34     while(cin>>name)
35     {
36         if(name=="0") break;
37         cin>>id>>age>>score>>count;
38         student stu(name,id,age,score,count);
39         stu.print();
40     }
41     return 0;
42 }

 

点到原点的距离(继承)

 

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 class Point_1D
 5 {
 6 protected:
 7     float x;
 8 public:
 9     Point_1D(float p=0.0):x(p){};
10     float distance(){return abs(x);};
11     void getdistance(){
12         cout<<"Distance from Point "<<x<<" to original point is "<<distance()<<endl;
13         }
14 };
15 class Point_2D:protected Point_1D
16 {
17 protected:
18     float y;
19 public:
20     Point_2D(float xx,float yy):Point_1D(xx),y(yy){};
21     float distance(){return sqrt(x*x+y*y);};
22     void getdistance(){
23         cout<<"Distance from Point("<<x<<","<<y<<") to original point is "<<distance()<<endl;
24         }
25 };
26 class Point_3D:protected Point_2D
27 {
28 protected:
29     float z;
30 public:
31     Point_3D(float xx,float yy,float zz):Point_2D(xx,yy),z(zz){};
32     float distance(){return sqrt(x*x+y*y+z*z);};
33     void getdistance(){
34         cout<<"Distance from Point("<<x<<","<<y<<","<<z<<") to original point is "<<distance()<<endl;
35         }
36 };
37 int main()
38 {    
39     int flag;
40     while(cin>>flag)
41     {
42         if(flag==0) break;
43         float a,b,c;
44         switch(flag)
45         {    
46         case 1:
47             {
48                 cin>>a;
49                 Point_1D P(a);
50                 P.getdistance();
51                 break;
52             }
53         case 2:
54             {
55                 cin>>a>>b;
56                 Point_2D P(a,b);
57                 P.getdistance();
58                 break;}
59         case 3:
60             {    
61                 cin>>a>>b>>c;
62                 Point_3D P(a,b,c);
63                 P.getdistance();
64                 break;
65             }
66         }
67     }
68     return 0;
69 }

 

车辆选择(继承)

 

 1 #include<iostream>
 2 #include<string>
 3 #include<iomanip>
 4 using namespace std;
 5 enum type{Vehicle,Car,Truck};
 6 class vehicle
 7 {
 8 protected:
 9     int wheels;
10     double weight;
11 public:
12     vehicle(){};
13     vehicle(int wh,double we):wheels(wh),weight(we){};
14     int get_wheels(){return wheels;};
15     double get_weight(){return weight;};
16     double wheel_load(){return weight/wheels;}
17     void print(){};
18 };
19 class car:protected vehicle
20 {    
21     int passenger_load;
22 public:
23     car(){};
24     car(int wh,double we,int pa):vehicle(wh,we),passenger_load(pa){};
25     int get_passengers(){return passenger_load;};
26     int get_wheels(){return vehicle::get_wheels();};
27     double get_weight(){return vehicle::get_weight();};
28     void print(){};
29 };
30 class truck:protected vehicle
31 {    
32     int passenger_load;
33     double payload;
34 public:
35     truck(){};
36     truck(int wh,double we,int pass,double pay):vehicle(wh,we),passenger_load(pass),payload(pay){};
37     int get_passengers(){return passenger_load;};
38     double efficiency(){return payload/(payload+weight);};
39     int get_wheels(){return vehicle::get_wheels();};
40     double get_weight(){return vehicle::get_weight();};
41     void print(){};
42 };
43 int main()
44 {
45     int n(1);
46     string ty;
47     while(cin>>ty)
48     {
49         if(ty=="-1") break;
50         enum type k(Vehicle);
51         string r;
52         if(ty=="vehicle") k=Vehicle;
53         if(ty=="car") k=Car;
54         if(ty=="truck") k=Truck;
55         if(n==1) r="st";
56         if(n==2) r="nd";
57         if(n==3) r="rd";
58         if(n>3) r="th";
59         switch(k)
60         {
61         case Vehicle:{
62                         int id,wheels;double weight;
63                         cin>>id>>wheels>>weight;
64                         vehicle V(wheels,weight);
65                         cout<<"The "<<n<<r<<" object is Vehicle No. "<<id<<": weight "<<V.get_weight()<<" Kg and wheels "<<V.get_wheels()<<endl;
66                         n++;
67                         break;
68                      }
69         case Car:{
70                     int id,wheels,passenger;double weight;
71                     cin>>id>>wheels>>weight>>passenger;
72                     car C(wheels,weight,passenger);
73                     cout<<"The "<<n<<r<<" object is Car No. "<<id<<": passenger_load "<<C.get_passengers()<<" weight "<<C.get_weight()<<" Kg and wheels "<<C.get_wheels()<<endl;
74                     n++;
75                     break;
76                  }
77         case Truck:{
78                     int id,wheels,passenger;double weight,load;
79                    cin>>id>>wheels>>weight>>passenger>>load;
80                    truck T(wheels,weight,passenger,load);
81                    cout<<"The "<<n<<r<<" object is Truck No. "<<id<<": passenger_load "<<T.get_passengers()<<" weight "<<T.get_weight()<<" Kg wheels "<<T.get_wheels()<<" and efficiency "<<fixed<<setprecision(2)<<T.efficiency()<<setprecision(0)<<endl;
82                    n++;
83                    break;
84                    }
85         }
86     }
87     return 0;
88 }

 

时间换算

 

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 
 5 class Time
 6 {
 7 private:
 8     int hour,minute,second;
 9 public:
10     friend Time operator + (Time &T1,Time &T2);
11     friend Time operator - (Time &T1,Time &T2);
12     friend istream& operator >> (istream &input,Time &T);
13     friend ostream& operator << (ostream &output,Time &T);
14     Time(){};
15     Time(int h,int m,int s):hour(h),minute(m),second(s){};
16     bool getEnd(void);
17 };
18 
19 Time operator + (Time &T1,Time &T2)
20 {
21     Time T(0,0,0);
22     if(T2.hour<=0&&T2.minute<=0&&T2.second<=0) 
23     {
24         Time T2_tmp(0,0,0);
25         T2_tmp.hour=abs(T2.hour),T2_tmp.minute=abs(T2.minute),T2_tmp.second=abs(T2.second);
26         T=T1-T2_tmp;
27         return T;
28     }
29     T.second=(T1.second+T2.second<60)?(T1.second+T2.second):(T.minute++,T1.second+T2.second-60);
30     T.minute=(T1.minute+T2.minute+T.minute<60)?(T.minute+T1.minute+T2.minute):(T.hour++,T.minute+T1.minute+T2.minute-60);
31     T.hour=(T1.hour+T2.hour+T.hour<24)?(T.hour+T1.hour+T2.hour):(T.hour-=24,T.hour+T1.hour+T2.hour);
32     return T;
33 }
34 
35 Time operator - (Time &T1,Time &T2)
36 {
37     Time T(0,0,0);
38     T.second=(T1.second>=T2.second)?(T1.second-T2.second):(T.minute--,T1.second-T2.second+60);
39     T.minute=(T1.minute+T.minute>=T2.minute)?(T1.minute+T.minute-T2.minute):(T.hour--,T1.minute+T.minute-T2.minute+60);
40     T.hour=(T1.hour+T.hour>=T2.hour)?(T1.hour+T.hour-T2.hour):(T.hour+=24,T1.hour+T.hour-T2.hour);
41     return T;
42 }
43 
44 istream& operator >> (istream &input,Time &T)
45 {
46     input>>T.hour>>T.minute>>T.second;
47     return input;
48 }
49 
50 ostream& operator << (ostream &output,Time &T)
51 {
52     output<<"time:"<<T.hour<<":"<<T.minute<<":"<<T.second;
53     return output;
54 }
55 
56 bool Time::getEnd(void)
57 {
58     if(!hour&&!minute&&!second) return false;
59     else return true;
60 }
61 
62 int main()
63 {
64     Time T;
65     int S;
66     cin>>T>>S;
67     while(T.getEnd()||S)
68     {
69         int hh,mm,ss;
70         ss=S%60,mm=((S-ss)/60)%60,hh=((((S-ss)/60)-mm)/60)%24;
71         Time T_tmp(hh,mm,ss);
72         T=T+T_tmp;
73         cout<<T<<endl;
74         cin>>T>>S;
75     }
76     return 0;
77 }

 

2017final复数的比较

 

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Complex
 5 {
 6 private:
 7     int i,j;
 8 public:
 9     friend bool operator > (Complex &C1,Complex &C2);
10     friend istream& operator >> (istream &input, Complex &C);
11     friend ostream& operator << (ostream &output, Complex &C);
12     Complex(void){};
13     Complex(int ii,int jj):i(ii),j(jj){};
14     friend bool isEnd(Complex &C1,Complex &C2);
15 };
16 
17 bool operator > (Complex &C1,Complex &C2)
18 {
19     if(C1.i*C1.i+C1.j*C1.j>C2.i*C2.i+C2.j*C2.j) return true;
20     else return false;
21 }
22 
23 istream& operator >> (istream &input,Complex &C)
24 {
25     input>>C.i>>C.j;
26     return input;
27 }
28 
29 ostream& operator << (ostream &output,Complex &C)
30 {
31     output<<C.i;
32     if(C.j>=0) output<<"+";
33     output<<C.j<<"i";
34     return output;
35 }
36 
37 bool isEnd(Complex &C1,Complex &C2)
38 {
39     if(!C1.i&&!C1.j&&!C2.i&&!C2.j) return true;
40     else return false;
41 }
42 
43 int main()
44 {
45     Complex C1,C2;
46     cin>>C1>>C2;
47     while(!isEnd(C1,C2))
48     {
49         if(C1>C2) cout<<"true"<<endl;
50         else cout<<"false"<<endl;
51         cin>>C1>>C2;
52     }
53     return 0;
54 }

 

复数类的运算

 

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Complex
 5 {
 6     public:
 7         Complex(double r=0,double i=0):real(r), imag(i){}
 8         Complex operator+(Complex &) const;//重载双目运算符'+'
 9         Complex operator-=(Complex &); //重载双目运算符'-='
10         friend  const Complex operator-(Complex &,Complex &);//重载双目运算符'-'
11         void Display() const;
12     private:
13         double real;
14         double imag;
15 };
16 
17 Complex Complex::operator + (Complex &C) const
18 {
19     return Complex(real+C.real,imag+C.imag);
20 }
21 
22 Complex Complex::operator-=(Complex &C)
23 {
24     real=real-C.real;
25     imag=imag-C.imag;
26     return Complex(real,imag);
27 }
28 
29 Complex const operator - (Complex &C1,Complex &C2)
30 {
31     return Complex(C1.real-C2.real,C1.imag-C2.imag);
32 }
33 
34 void Complex::Display() const
35 {
36     cout << "(" << real << ", " << imag << ")" << endl;
37 }
38 
39 int main()
40 {
41     double r, m;
42     cin >> r >> m;
43     Complex c1(r, m);
44     cin >> r >> m;
45     Complex c2(r, m);
46     Complex c3 = c1+c2;
47     c3.Display();
48     c3 = c1-c2;
49     c3.Display();
50     c3 -= c1;
51     c3.Display();
52     return 0;
53 }

 

师生信息管理

 

  1 #include<iostream>
  2 #include<string>
  3 #include<iomanip>
  4 using namespace std;
  5 
  6 class Person
  7 {
  8 protected:
  9     int NO;//编号
 10 public:
 11     Person() {};
 12     Person(int N): NO(N) {};
 13     virtual void display() = 0; //输出相关信息
 14     virtual ~Person() {};
 15 };
 16 
 17 class Student: public Person
 18 {
 19     int score[5];
 20 public:
 21     Student() {};
 22     Student(int N, int S1, int S2, int S3, int S4, int S5): Person(N)
 23     {
 24         score[0] = S1, score[1] = S2, score[2] = S3, score[3] = S4, score[4] = S5;
 25     };
 26     ~Student() {};
 27     //friend istream& operator >> (istream &input,Student &S);
 28     void display();
 29 };
 30 /*
 31 istream& operator >> (istream &input,Student &S)
 32 {
 33     input>>S.NO>>S.score[0]>>S.score[1]>>S.score[2]>>S.score[3]>>S.score[4];
 34     return input;
 35 }
 36 */
 37 void Student::display()
 38 {
 39     double average(0);
 40     int count(0);
 41 
 42     for(int i(0); i < 5; i++)
 43     {
 44         if(score[i] == -1) count++;
 45         else average += score[i];
 46     }
 47 
 48     average /= (5 - count);
 49 
 50     if(count == 5) cout << NO << " " << count << endl;
 51     else cout << NO << " " << count << " " << fixed << setprecision(1) << average << endl;
 52 }
 53 
 54 class Teacher: public Person
 55 {
 56     int num[3];
 57 public:
 58     Teacher() {};
 59     Teacher(int N, int N1, int N2, int N3): Person(N)
 60     {
 61         num[0] = N1, num[1] = N2, num[2] = N3;
 62     };
 63     ~Teacher() {};
 64     //friend istream& operator >> (istream &input,Teacher &T);
 65     void display();
 66 };
 67 /*
 68 istream& operator >> (istream &input,Teacher &T)
 69 {
 70     input>>T.NO>>T.num[0]>>T.num[1]>>T.num[2];
 71     return input;
 72 }
 73 */
 74 void Teacher::display()
 75 {
 76     int number(0);
 77 
 78     for(int i(0); i < 3; i++)
 79     {
 80         number += num[i];
 81     }
 82 
 83     cout << NO << " " << number << endl;
 84 }
 85 
 86 int main()
 87 {
 88     int flag, count(0);
 89     Person *pp[10];
 90 
 91     while(cin >> flag, flag)
 92     {
 93         switch(flag)
 94         {
 95             case 1:
 96                 {
 97                     int N, S1, S2, S3, S4, S5;
 98                     cin >> N >> S1 >> S2 >> S3 >> S4 >> S5;
 99                     pp[count++] = new Student(N, S1, S2, S3, S4, S5);
100                     pp[count - 1]->display();
101                     break;
102                 }
103 
104             case 2:
105                 {
106                     int N, N1, N2, N3;
107                     cin >> N >> N1 >> N2 >> N3;
108                     pp[count++] = new Teacher(N, N1, N2, N3);
109                     pp[count - 1]->display();
110                     break;
111                 }
112         }
113     }
114 
115     return 0;
116 }

 

汽车收费

 

  1 #include<iostream>
  2 #include<string>
  3 using namespace std;
  4 
  5 class Vehicle
  6 {
  7 protected:
  8     string NO;//编号
  9 public:
 10     Vehicle() {};
 11     Vehicle(string N): NO(N) {};
 12     virtual void display() = 0; //输出应收费用
 13     virtual ~Vehicle() {};
 14 };
 15 
 16 class Car: public Vehicle
 17 {
 18     int number, weight;
 19 public:
 20     Car() {};
 21     Car(string N, int Num, int Wei): Vehicle(N), number(Num), weight(Wei) {};
 22     //friend istream& operator >> (istream &input,Car &C);
 23     void display();
 24 };
 25 /*
 26 istream& operator >> (istream &input,Car &C)
 27 {
 28     input>>C.NO>>C.number>>C.weight;
 29     return input;
 30 }
 31 */
 32 void Car::display()
 33 {
 34     cout << NO << " " << number * 8 + weight * 2 << endl;
 35 }
 36 
 37 class Truck: public Vehicle
 38 {
 39     int weight;
 40 public:
 41     Truck() {};
 42     Truck(string N, int W): Vehicle(N), weight(W) {};
 43     //friend istream& operator >> (istream &input,Truck &T);
 44     void display();
 45 };
 46 /*
 47 istream& operator >> (istream &input,Truck &T)
 48 {
 49     input>>T.NO>>T.weight;
 50     return input;
 51 }
 52 */
 53 void Truck::display()
 54 {
 55     cout << NO << " " << weight * 5 << endl;
 56 }
 57 
 58 class Bus: public Vehicle
 59 {
 60     int number;
 61 public:
 62     Bus() {};
 63     Bus(string N, int Num): Vehicle(N), number(Num) {};
 64     //friend istream& operator >> (istream &input,Bus &B);
 65     void display();
 66 };
 67 /*
 68 istream& operator >> (istream &input,Bus &B)
 69 {
 70     input>>B.NO>>B.number;
 71     return input;
 72 }
 73 */
 74 void Bus::display()
 75 {
 76     cout << NO << " " << number * 3 << endl;
 77 }
 78 
 79 int main()
 80 {
 81     int flag, count(0);
 82     Vehicle *pv[10];
 83 
 84     while(cin >> flag, flag)
 85     {
 86         switch(flag)
 87         {
 88             case 1:
 89                 {
 90                     string No;
 91                     int number, weight;
 92                     cin >> No >> number >> weight;
 93                     pv[count++] = new Car(No, number, weight);
 94                     pv[count - 1]->display();
 95                     break;
 96                 }
 97 
 98             case 2:
 99                 {
100                     string No;
101                     int weight;
102                     cin >> No >> weight;
103                     pv[count++] = new Truck(No, weight);
104                     pv[count - 1]->display();
105                     break;
106                 }
107 
108             case 3:
109                 {
110                     string No;
111                     int number;
112                     cin >> No >> number;
113                     pv[count++] = new Bus(No, number);
114                     pv[count - 1]->display();
115                     break;
116                 }
117         }
118     }
119 
120     return 0;
121 }

 

宠物的生长(多态)

 

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 class Pet   //
 6 {
 7 protected:
 8     string name;
 9     int length,weight,current;
10 public:
11     virtual void display(int day)=0;
12     virtual ~Pet(){};
13 };
14 
15 class Cat:public Pet
16 {
17 public:
18     Cat(){};
19     Cat(string N,int L,int W,int C);
20     void display(int day);
21 };
22 
23 Cat::Cat(string N,int L,int W,int C)
24 {
25     name=N,length=L,weight=W,current=C;
26 }
27 
28 void Cat::display(int day)
29 {
30     cout<<name<<" "<<length+(day-current)<<" "<<weight+(day-current)*2<<endl;
31 }
32 
33 class Dog:public Pet
34 {
35 public:
36     Dog(){};
37     Dog(string N,int L,int W,int C);
38     void display(int day);
39 };
40 
41 Dog::Dog(string N,int L,int W,int C)
42 {
43     name=N,length=L,weight=W,current=C;
44 }
45 
46 void Dog::display(int day)
47 {
48     cout<<name<<" "<<length+(day-current)*2<<" "<<weight+(day-current)<<endl;
49 }
50 
51 int main()
52 {
53     int flag,i(0);
54     Pet *pt[10];
55     string name;
56     int length,weight,time;
57     while(cin>>flag,flag==1||flag==2)
58     {
59         cin>>name>>length>>weight>>time;
60         switch(flag)
61         {
62         case 1:pt[i++]=new Cat(name,length,weight,time);break;
63         case 2:pt[i++]=new Dog(name,length,weight,time);break;
64         }
65     }
66     for(int j(0);j<i;j++)
67     {
68         pt[j]->display(flag);
69     }
70     return 0;
71 }

 

马会飞

 

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Horse
 5 {
 6 public:
 7     Horse(){cout<<"Horse 申请了空间..."<<endl;}
 8     virtual void Fly(){cout<<"Just a horse."<<endl;}
 9     virtual ~Horse(){cout<<"Horse 释放了空间..."<<endl;}
10 };
11 
12 class Pegasus:public Horse
13 {
14 public:
15     Pegasus(){cout<<"Pegasus 申请了空间..."<<endl;}
16     void Fly(){cout<<"I can fly!"<<endl;}
17     ~Pegasus(){cout<<"Pegasus 释放了空间..."<<endl;}
18 };
19 
20 int main()
21 {
22     Horse *p1 = new Horse; //输出:Horse 申请了空间...
23     Horse *p2 = new Pegasus; /*  输出两行:
24                                  Horse 申请了空间...
25                                  Pegasus 申请了空间...   
26                      */    
27     cout << endl; 
28     
29     p1->Fly(); //输出:Just a horse.
30     p2->Fly(); //输出:I can fly!
31     cout << endl; 
32     
33     delete p1; //输出:Horse 释放了空间...
34     delete p2;  /* 输出两行:
35                    Pegasus 释放了空间... 
36                    Horse 释放了空间... 
37             */
38     return 0;
39 }

 

鸡兔同笼

 

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int is_min(int);
 7     int is_max(int);
 8     int count, num;
 9     cin >> count;
10 
11     while(count--)
12     {
13         cin >> num;
14 
15         if(num % 2)
16             cout << 0 << ' ' << 0 << endl;
17         else
18             cout << is_min(num) << ' ' << is_max(num) << endl;
19     }
20 
21     return 0;
22 }
23 
24 int is_min(int number)
25 {
26     for(int i(number / 4); i >= 0; i++)
27         if(4 * i == number) return i;
28         else return i + 1;
29 
30     return 0;
31 }
32 
33 int is_max(int number)
34 {
35     return number / 2;
36 }

 

数据的最大值问题(重载+函数模板)

 

  1 #include <iostream>
  2 using namespace std;
  3 
  4 class Time
  5 {
  6 private:
  7     int hh, mm, ss;
  8 public:
  9     Time()
 10     {
 11         hh = 0;
 12         mm = 0;
 13         ss = 0;
 14     }; //构造函数
 15     Time(int h, int m, int s): hh(h), mm(m), ss(s) {};  //构造函数
 16     void settime(int h, int m, int s)
 17     {
 18         hh = h, mm = m, ss = s;
 19     };
 20     friend Time operator - (Time A, Time B);
 21     friend bool operator > (Time A, Time B);
 22     friend ostream& operator << (ostream &output, Time &A);
 23 };
 24 
 25 Time operator - (Time A, Time B)    //在该程序中无实际用途,忽略
 26 {
 27     return Time(A.hh - B.hh, A.mm - B.mm, A.ss - B.ss); //实际情况需要考虑负数情况
 28 }
 29 
 30 bool operator > (Time A, Time B)    //重载大于号,用于比较
 31 {
 32     return A.hh * 3600 + A.mm * 60 + A.ss > B.hh * 3600 + B.mm * 60 + B.ss;
 33 }
 34 
 35 ostream& operator << (ostream &output, Time &A)     //重载输出流函数,方便输出
 36 {
 37     output << A.hh << " " << A.mm << " " << A.ss;
 38     return output;
 39 }
 40 
 41 class date
 42 {
 43 private:
 44     int year, month, day;
 45 public:
 46     date() {};
 47     date(int y, int m, int d): year(y), month(m), day(d) {};
 48     void setdate(int y, int m, int d)
 49     {
 50         year = y, month = m, day = d;
 51     };
 52     friend bool operator > (date A, date B);
 53     friend ostream& operator << (ostream &output, date &A);
 54 };
 55 
 56 bool operator > (date A, date B)
 57 {
 58     return A.year * 365 + A.month * 30 + A.day > B.year * 365 + B.month * 30 + B.day;
 59 }
 60 
 61 ostream& operator << (ostream &output, date &A)
 62 {
 63     output << A.year << " " << A.month << " " << A.day;
 64     return output;
 65 }
 66 
 67 template<class T>
 68 T maxn(T x[], int len)
 69 {
 70     T temp = x[0];
 71 
 72     for(int i(1); i < len; i++)
 73         if(x[i] > temp)
 74             temp = x[i];
 75 
 76     return temp;
 77 }
 78 
 79 int main()
 80 {
 81     int intArray[100], flag;
 82     double doubleArray[100];
 83     Time timeArray[100];
 84     date dateArray[100];
 85     cin >> flag;
 86 
 87     while(flag != -1)
 88     {
 89         switch(flag)
 90         {
 91             case 1:
 92                 {
 93                     int Max, a, i(0);
 94 
 95                     while(cin >> a, a)
 96                         intArray[i++] = a;
 97 
 98                     Max = maxn(intArray, i);
 99                     cout << Max << endl;
100                     break;
101                 }
102 
103             case 2:
104                 {
105                     double Max, a;
106                     int i(0);
107 
108                     while(cin >> a, a)
109                         doubleArray[i++] = a;
110 
111                     Max = maxn(doubleArray, i);
112                     cout << Max << endl;
113                     break;
114                 }
115 
116             case 3:
117                 {
118                     Time Max;
119                     int a, b, c, i(0);
120 
121                     while(cin >> a, a)
122                     {
123                         cin >> b >> c;
124                         timeArray[i++].settime(a, b, c);
125                     }
126 
127                     Max = maxn(timeArray, i);
128                     cout << Max << endl;
129                     break;
130                 }
131 
132             case 4:
133                 {
134                     date Max;
135                     int a, b, c, i(0);
136 
137                     while(cin >> a, a)
138                     {
139                         cin >> b >> c;
140                         dateArray[i++].setdate(a, b, c);
141                     }
142 
143                     Max = maxn(dateArray, i);
144                     cout << Max << endl;
145                     break;
146                 }
147         }
148     
149         cin >> flag;
150     }
151 
152     return 0;
153 }

 

posted @ 2019-05-15 21:23  菜鸟plus  阅读(554)  评论(0编辑  收藏  举报