#include#include #include using namespace std;class polygon{ protected: int number; private: int side_length[100]; public: polygon(int a = 0):number(a){memset(side_length, 0, sizeof(side_length));}; int perimeter();//计算多边形边长 void display();//输出多边形边数和周长 int* reachsidelen();};int polygon::perimeter(){ int i; int tot = 0; for (i = 1; i <= number; i++) { tot += side_length[i]; } return tot;}void polygon::display(){ cout << number << " " << perimeter() << endl;}int* polygon::reachsidelen(){ return side_length;}class rectangle : protected polygon{ protected: int height; int width; public: rectangle(int a = 0, int b = 0, int c = 0):polygon(a), height(b), width(c){}; int perimeter();//计算矩形边长 void display();//输出多边形边数和周长};int rectangle::perimeter(){ int tot = 0; tot = height * 2 + width * 2; return tot;}void rectangle::display(){ cout << number << " " << perimeter() << endl;}class equal_polygon : protected polygon{ protected: int side_len; public: equal_polygon(int a = 0, int b = 0):polygon(a), side_len(b){}; int perimeter();//计算等边多边形边长 void display();//输出多边形边数和周长};int equal_polygon::perimeter(){ int tot = 0; tot = number * side_len; return tot;}void equal_polygon::display(){ cout << number << " " << perimeter() << endl;}int main(){ int b[105]; int i,j; int cnt = 0; cin >> cnt; int ope; while(cnt --) { memset(b, 0, sizeof(b)); cin >> ope; if(ope == 0) { int tot = 1; int idata; while(cin >> idata) { if(idata == -1)break; b[tot++] = idata; } tot --; polygon P(tot); int *p = P.reachsidelen(); for (i = 1; i <= tot; i++) { *(p + i) = b[i]; } P.display(); } else if(ope == 1) { int iwide, ilen; cin >> iwide >> ilen; rectangle R(4, ilen, iwide); R.display(); } else if(ope == 2) { int inumber, ilen; cin >> inumber >> ilen; equal_polygon E(inumber, ilen); E.display(); } } return 0;}
#include#include #include using namespace std;class Point_1D{ protected: float x;//1D 点的x坐标public: Point_1D(float a = 0): x(a){}; float distance( );//计算当前点到原点的距离};float Point_1D::distance(){ float d = x; if(d < 0) d = -d; return d;}class Point_2D : protected Point_1D{ protected: float y; public: Point_2D(float a = 0, float b = 0): Point_1D(a), y(b){}; float distance( );};float Point_2D::distance(){ float d; d = sqrt(x * x + y * y); return d;}class Point_3D : protected Point_2D{ protected: float z; public: Point_3D(float a = 0, float b = 0, float c = 0): Point_2D(a, b), z(c){}; float distance( );};float Point_3D::distance(){ float d; d = sqrt(x * x + y * y + z * z); return d;}int main(){ int ope; while(cin >> ope) { if(ope == 0)break; if(ope == 1) { float ix; cin >> ix; Point_1D p1(ix); cout << "Distance from Point " << ix << " to original point is " << p1.distance() << endl; } else if(ope == 2) { float ix, iy; cin >> ix >> iy; Point_2D p2(ix, iy); cout << "Distance from Point(" << ix << "," << iy << ") to original point is " << p2.distance() << endl;; } else if(ope == 3) { float ix, iy, iz; cin >> ix >> iy >> iz; Point_3D p3(ix, iy, iz); cout << "Distance from Point(" << ix << "," << iy << "," << iz << ") to original point is " << p3.distance() << endl;; } } return 0;}
用了sstream,主函数太长了。。
#include#include #include using namespace std;class Date{ protected: int year; int month; int day; public: Date(int a = 0, int b = 0, int c = 0):year(a), month(b), day(c){};};class Time{ protected: int hour; int minute; int second; public: Time(int a = 0, int b = 0, int c = 0):hour(a), minute(b), second(c){};};class Schedule : protected Date, protected Time{ protected: int ID; public: Schedule(int y = 10000, int m = 0, int d = 0, int h = 0, int min = 0, int s = 0, int id = 0): Date(y, m, d), Time(h, min, s), ID(id){}; bool operator < (const Schedule & s2); void display();};void Schedule::display() //The urgent schedule is No.1: 2014/6/27 8:0:1{ cout << ID << ": " << year << "/" << month << "/" << day << " " << hour << ":" << minute << ":" << second << endl;}bool Schedule::operator < (const Schedule & s2){ int dtot1 = 0, tot1 = 0; int dtot2 = 0, tot2 = 0; dtot1 = year * 365 + month * 30 + day; dtot2 = s2.year * 365 + s2.month * 30 + s2.day; tot1 = hour * 3600 + minute * 60 + second; tot2 = s2.hour * 3600 + s2.minute * 60 + s2.second; if(dtot1 < dtot2)return true; else if(dtot1 > dtot2)return false; else { if(tot1 < tot2)return true; else return false; }}int main(){ int id; int i; int record; Schedule sch; while(cin >> id) { if(id == 0)break; string date, time; cin >> date >> time; stringstream stream; string s1, s2; int y = 0, m = 0, d = 0, h = 0, min = 0, s = 0; bool flag = false; for(i = 0; i < date.length(); i++) { if(date[i] == '/') { if(flag) { stream << s1; stream >> m; stream.clear(); s1 = ""; } else { stream << s1; stream >> y; stream.clear(); s1 = ""; flag = true; } } else s1 += date[i]; } stream << s1; stream >> d; stream.clear(); bool flag2 = false; for(i = 0; i < time.length(); i++) { if(time[i] == ':') { if(flag2) { stream << s2; stream >> min; stream.clear(); s2 = ""; } else { stream << s2; stream >> h; stream.clear(); s2 = ""; flag2 = true; } } else s2 += time[i]; } stream << s2; stream >> s; stream.clear(); Schedule sch1(y, m, d, h, min, s, id); if(sch1 < sch)sch = sch1; } cout << "The urgent schedule is No."; sch.display(); return 0;}