大连海事大学C语言与Windows程序设计6道课后题答案 下载本文

内容发布更新时间 : 2024/5/21 13:47:31星期一 下面是文章的全部内容请认真阅读。

大连海事大学C语言与Windows程序设计6道课后题答案 1.(1)设计具有以下功能的程序:从键盘上输入10个非负整数,统计并输出最大数和最小数,当输入负数时结束输入。

#include void main() {

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 void main() {

inti,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 class Point {

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(){} intGetX() {

return this->X; }

intGetY() {

return this->Y; }

voidSetX(int X) {

this->X = X; }

voidSetY(int Y) {

this->Y = Y; }

voidMoveTo(int X, int Y) {

SetX(X); SetY(Y);