A. 圆和圆柱体计算(继承)
#include<iostream>
using namespace std;
class CPoint
{
protected:
double x, y;
public:
CPoint (double x1, double y1) : x(x1), y(y1){ }
};
class CCircle : public CPoint
{
protected:
double r;
public:
CCircle(double x1, double y1, double r1) : CPoint(x1, y1), r(r1){}
double getArea ()
{
double S;
S = 3.14 * r * r;
return S;
}
void print()
{
cout << "Circle:(" << x << "," << y << ")," << r << endl;
cout << "Area:" << getArea() << endl;
}
};
class CCylinder : public CCircle
{
protected:
double h;
public:
CCylinder(double x1, double y1, double r1, double h1) : CCircle(x1, y1, r1), h(h1){};
double calcuVol()
{
return 3.14 * h * r * r;
}
void print()
{
cout << "Cylinder:(" << x << "," << y << ")," << r << "," << h << endl;
cout << "Volume:" << calcuVol() << endl;
}
};
int main ()
{
double x, y, r, x1, y1, r1, h;
cin >> x >> y >> r >> x1 >> y1 >> r1 >> h;
CCircle ccl(x, y, r);
CCylinder cyl(x1, y1, r1, h);
ccl.print();
cyl.print();
}
B 除了倒数第三行II=III2以外其他是自己一遍过
#include<iostream>
#include<cmath>
using namespace std;
class C2D
{
protected:
double x, y;
public:
C2D(double x1, double y1) : x(x1), y(y1){ }
double getDistance()
{
double dis;
dis = sqrt( x*x + y*y );
}
void print2()
{
cout << getDistance() << endl;
}
};
class C3D : public C2D
{
protected:
double z;
public:
C3D(double x1, double y1, double z1) : C2D(x1, y1), z(z1){}
double getDis()
{
return sqrt( x*x + y*y + z*z );
}
void print3()
{
cout << getDis() << endl;
}
};
int main()
{
int x, y, z;
cin >> x >> y;
C2D II(x, y);
II.print2();
cin >> x >> y >> z;
C3D III1(x, y, z);
III1.print3();
cin >> x >> y >> z;
C3D III2(x, y, z);
III2.print3();
II = III2;
II.print2();
}
C
#include<iostream>
#include<cmath>
using namespace std;
class Count
{
protected:
int value;
public:
Count(int v){
value = v;
}
void increment()
{
value++;
}
int getValue()
{
return value;
}
};
class Cycle : public Count
{
private:
int minValue, maxValue;
public:
Cycle(int min, int max, int v) : Count(v), minValue(min), maxValue(max){ }
void increment()
{
value++;
if(value == maxValue) value = minValue;
}
};
class Clk
{
private:
Cycle hour, minute, second;
public:
Clk(int h, int m, int s) : hour(0, 24, h), minute(0, 60, m), second(0, 60, s){ }
void ctime(int s)
{
for(int i = 0; i < s; i++)
{
second.increment();
if(second.getValue() == 0)//因为没继承,所以:1.getValue! 2.不是minValue是0
{
minute.increment();
if(minute.getValue() == 0)
{
hour.increment();
}
}
}
}
void print()
{
cout << hour.getValue() << ":" << minute.getValue() << ":" << second.getValue() << endl;
}
};
int main()
{
int n;
cin >> n;
while(n--)
{
int h, m, s, scnd;
cin >> h >> m >> s >> scnd;
Clk clk (h, m, s);
clk.ctime(scnd);
clk.print();
}
}
E
#include<iostream>
#include<cmath>
using namespace std;
class Person
{
protected:
string name;
int age;
public:
Person(string n, int a)
{
age = a;
name = n;
}
void display()
{
cout << name << " " << age << " ";
}
};
class R : public Person
{
protected:
int usual, exam;
char gpa;
public:
R(string n, int age1, int u, int e) : Person(n, age1)
{
usual = u;
exam = e;
}
char calculate()
{
double sum = 0.4 * usual + 0.6 * exam;
if( sum < 60 ) gpa = 'F';
if( sum >= 60 ) gpa = 'D';
if( sum >= 65 ) gpa = 'C';
if( sum >= 75 ) gpa = 'B';
if( sum >= 85 ) gpa = 'A';
}
void display()
{
cout << name << " " << age << " " << gpa << endl;
}
};
class S : public Person
{
protected:
int exam;
char gpa;
public:
S(string n, int age1, int e) : Person(n, age1)
{
exam = e;
}
char calculate()
{
int sum = exam;
if( exam < 60 ) gpa = 'F';
if( exam >= 60 ) gpa = 'D';
if( exam >= 65 ) gpa = 'C';
if( exam >= 75 ) gpa = 'B';
if( exam >= 85 ) gpa = 'A';
}
void display()
{
cout << name << " " << age << " " << gpa << endl;
}
};
int main()
{
int t;
cin >> t;
while(t--)
{
char x;
cin >> x;
if(x == 'R')
{
string name;
int u, e, age;
cin >> name >> age >> u >> e;
R r(name, age, u, e);
r.calculate();
r.display();
}
if(x == 'S')
{
string name;
int age, e;
cin >> name >> age >> e;
S s(name, age, e);
s.calculate();
s.display();
}
}
}
F
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
class Date
{
protected:
int year, month, day;
public:
Date(int year1, int month1, int day1)
{
year = year1;
month = month1;
day = day1;
}
};
class Time
{
protected:
int hour, minute, second;
public:
Time(int h, int m, int s){
hour = h;
minute = m;
second = s;
}
};
class Schedule : public Date, public Time
{
protected:
int ID;
public:
Schedule(int id, int y, int m, int d, int h, int min, int s) : Date(y, m, d), Time(h, min, s)
{
ID = id;
}
void print()
{
printf("The urgent schedule is No.%d: %04d/%02d/%02d %02d:%02d:%02d", ID, year, month, day, hour, minute, second);
}
friend bool before(const Schedule & s1,const Schedule & s2);//判断日程s1时间是否早于日程s2。
};
bool before(const Schedule & s1,const Schedule & s2)
{
int d1, d2;
d1 = s1.year * 10000 + s1.month * 100 + s1.day;
d2 = s2.year * 10000 + s2.month * 100 + s2.day;
if(d1 < d2) return 1;
if(d1 > d2) return 0;
else
{
int t1, t2;
t1 = s1.hour * 10000 + s1.minute * 100 + s1.second;
t2 = s2.hour * 10000 + s2.minute * 100 + s2.second;
if(t1 < t2) return 1;
else return 0;
}
}
int main()
{
Schedule mini(1,9999, 99, 99, 99, 99, 99);
int id;
cin >>id;
while(id != 0)
{
if(id==0) break;
else{
int year, month, day, hour, min, second;
cin >> year >> month >> day >> hour >> min >> second;
Schedule s(id, year, month, day, hour, min, second);
if(before (s, mini) ) mini = s;
cin >> id;
}
}
mini.print();
return 0;
}