大连海事大学C语言与Windows程序设计6道课后题答案 1.(1)设计具有以下功能的程序:从键盘上输入10个非负整数,统计并输出最大数和最小数,当输入负数时结束输入。
#include
int a, max=-1, min=32767,num=1;
cout<<\ do{
cin>>a;
if(a<0) break; //输入负数结束输入 if(a>max) max =a; if(a }while (a>=0&&num<=10); if(max>=0) cout<<\ else cout<<\} 1.(2)编程序实现求和:S=1-1/3+1/5-1/7+……+1/n。要求:在程序运行时从键盘输入。 #include int i,n,sign=1; float sum=0.0; printf(\请输入n:\ scanf(\ for(i=1;i<=n;i+=2) { sum+=sign*(1.0/i); sign=-sign; } printf(\ } n值2.设计具有以下功能的程序:从键盘输入10个整数,然后排序。(要求:利用函数调用实现并以数组作为函数的参数) 3. 编写几何点(二维平面上)的类Point,包括位置属性(二维坐标x,y), 成员函数包括: 点的位置获取函数GetX()和GetY(), 点的位置设置函数SetX()和SetY(), 点的位置移动函数MoveTo() 点的信息打印函数Display()。 void main() { Point p(100,100); p.Display(); p.MoveTo(200,200); cout<<\…\ p.Display(); } 程序输出结果如下: X: 100 Y: 100 after moving… X: 200 Y: 200 #include private: int X; int Y; public: Point(int X, int Y) { this->X = X; this->Y = Y; } Point() { this->X = 0; this->Y = 0; } ~Point(){} int GetX() { return this->X; } int GetY() { return this->Y; } void SetX(int X) { this->X = X; } void SetY(int Y) { this->Y = Y; } void MoveTo(int X, int Y) { SetX(X); SetY(Y); } void Display() { cout<<\ cout<<\ } }; 4. 编写几何图形圆的类Circle,包括两个属性:圆心的Point类实现)和半径R。 成员函数包括: 圆心位置获取函数GetO() 半径获取函数GetR() 半径位置设置函数SetR() 圆的位置移动函数MoveTo() 圆的半径设置函数SetR() 圆的信息打印函数Display() void main() { Point p(100,100); Point p2(200,200); Circle c(p, 100); c.Display(); c.MoveTo(p2); cout<<\ c.Display(); O(用上题中c.SetR(200); cout<<\ c.Display(); } 程序输出结果如下: Circle: (100,100),100 after moving Circle: (200,200),100 after altering r Circle: (200,200),200 #include private: int X; int Y; public: Point(int X, int Y) { this->X = X; this->Y = Y; } Point() { this->X = 0; this->Y = 0; } ~Point(){} int GetX() { return this->X; } int GetY() { return this->Y; } void SetX(int X) { this->X = X; } void SetY(int Y) { this->Y = Y; } void MoveTo(int X, int Y) { SetX(X); SetY(Y); } /* void Display() { cout<<\ cout<<\ } */ }; ///////////////////////// class Circle { private: Point O; int R; public: Circle(Point& p, int R) { O.MoveTo(p.GetX(),p.GetY()); this->R = R; } ~Circle(){} Point GetO() { return this->O; } int GetR() { return this->R; } void MoveTo(Point& p) { O.MoveTo(p.GetX(), p.GetY()); } void SetR(int R) { this->R = R; } void Display() { cout<<\ } }; void main() { Point p(100,100); Point p2(200,200); Circle c(p, 100); c.Display(); c.MoveTo(p2); cout<<\ c.Display(); c.SetR(200); cout<<\ c.Display(); } 5. 编写一个有关日期(年、月、日)和时间(时、分、秒)的程序。该程序建立三个类,其中一个是日期的类Date,一个是时间的类Time,另一个是日期和时间类TimeDate,它是前面两个类为基类的派生类。 void main() { TimeDate date1, date2(1998, 8, 12, 12, 45, 10); date1.SetDate(1998, 8, 7); date1.SetTime(10, 30, 45); cout<<\ date1.GetDT(); cout<<\ date1.GetDate(); cout<<\ date1.GetTime(); cout<<\ date2.GetDT(); } #include class Date { public: Date() {} Date(int y, int m, int d) { SetDate(y, m, d); } void SetDate(int y, int m, int d) { Year = y; Month = m; Day = d; } void GetDate() { cout< protected: int Year, Month, Day; }; class Time { public: Time() {} Time(int h, int m, int s) { SetTime(h, m, s); } void SetTime(int h, int m, int s) { Hours = h; Minutes = m; Seconds = s; } void GetTime() { cout< protected: int Hours, Minutes, Seconds; }; class TimeDate:public Date, public Time { public: TimeDate(){} TimeDate(int y, int mo, int d, int h, int mi, int s):Date(y, mo, d), Time(h, mi, s) {} void GetDT() { cout< void main() { TimeDate date1, date2(1998, 8, 12, 12, 45, 10); } date1.SetDate(1998, 8, 7); date1.SetTime(10, 30, 45); cout<<\date1.GetDT(); cout<<\date1.GetDate(); cout<<\date1.GetTime(); cout<<\date2.GetDT(); 6. 生成一个Object抽象类,在其中声明double CalArea()为纯虚函数,从Object派生出: Rect类(其中包含成员变量Point topleft,Point bottomright。其中Point为其子对象)、 Circle类(其中包含成员变量Point cc,double radius ),实现如下功能 void main() { Object *obj[2]; obj[0]=new Rect(Point(0,0),Point(100,200)); obj[1]=new Circle(Point(100,200),100.0); int i; for (i=0;i<2;i++) { cout<<\ } } #include Point(int a,int b) { x=a; y=b; } int x; int y; }; class Object { public: virtual double calarea()=0; }; class Rect :public Object { Point topleft; Point bottomright; public: Rect(Point p1,Point p2): topleft(p1),bottomright(p2) {} double calarea() { return fabs((topleft.x-bottomright.x)* (topleft.y-bottomright.y)); } }; class Circle :public Object { Point cc; double radius; public: Circle(Point p,double r):cc(p),radius(r) {} double calarea() { return pi*radius*radius; } }; void main() { Object *obj[2]; obj[0]=new Rect(Point(0,0),Point(100,200)); } obj[1]=new Circle(Point(100,200),100.0); int i; for (i=0;i<2;i++) { cout<<\}