📄 c语言.txt
字号:
8.在for循环中,是先执行循环体后再判断循环条件。 ( × )
9.C++中,如果条件表达式值为-1, 则表示逻辑为假。 ( × )
10. 在C++中用new分配的内存空间,在不需要时一般用free将该空间释放。 ( × )
四、 填空题(每空2分,共20分)
1.以下函数完成求表达式 的值,请填空使之完成此功能。
float sum( float x )
{ float s=0.0;
int sign=1;
float t=1.0;
for(int i=1; i<=100; i++)
{
t=t*x;
s=s+-sign*i/(t+sign*i);
sign=-sign;
}
return s;
}
2.以下程序中实现类CSort, 完成对其成员p所指向的整数数组进行从小到大排序,该数组的元素个数由num表示,请填空完善该程序。
#i nclude
class CSort
{
int *p;
int num;
public:
void Order();
CSort(int *, int);
void Disp();
};
CSort::CSort(int *arry, int n)
:p(arry), num(n)
{ }
void CSort::Order() //函数Order原型
{ int m, tmp;
for(int i=0; i
{ m=i;
for(int j=i+1; j
{ if(p[j]
m=j;
}
if(m!=i)
{ tmp=p[i];
p[i]=p[m];
p[m]=tmp;
}
}
}
void CSort::Disp()
{ for(int i=0; i
cout<
cout<
}
void main( )
{ static int a[]={10, -15, -3, 5, -4, 7,2};
CSort obj(a,2);
obj.Disp(); //应输出一行:10,-15,-3,5,-4,7,2
obj.Order(); //对数组排序
obj.Disp(); //应输出一行:-15,-4,-3,2,5,7,10
}
3.以下函数完成求两个数n1和n2的最大公约数。
#i nclude
int fac(int n1, int n2)
{ int tmp;
if( n1 < n2 )
{ tmp=n1;
n1=n2 ;
n2=tmp ;
}
while(n1%n2!=0)
{ tmp=n1%n2; n1=n2; n2=tmp;
}
return n2;
}
五、 阅读程序题(每个小题5分,共20分)
1.阅读以下程序,概括地写出程序的功能。
#i nclude
double Exp(double x)
{ double sum=1.0;
double term=x;
double i=1 ;
while (term>=1.0E-8)
{ sum+=term ;
i++;
term=term*x/i ;
}
return sum ;
}
void main()
{ double s;
s=Exp(1.0)+Exp(2.0);
cout.precision(8);
cout<<"s="<
}
zz
2. 阅读程序,写出程序执行时输出结果。
#i nclude
const int SIZE=10;
class stack
{ char stck[SIZE];
int top;
public:
void init();
void push(char ch);
char pop();
};
void stack::init()
{ top=0; }
void stack::push(char ch)
{ if(top==SIZE)
{ cout<<"Stack is full.\n";
return ;
}
stck[top++]=ch;
}
char stack::pop()
{ if(top==0)
{ cout<<"Stack is empty.\n";
return 0;
}
return stck[--top];
}
void main()
{ stack s1, s2;
s1.init();
s2.init();
s1.push('a');
s1.push('b');
s1.push('c');
s2.push('x');
s2.push('y');
s2.push('z');
for(int i=0; i<3; i++)
cout<<"Pop s1:"<
for(i=0; i<3; i++)
cout<<"Pop s2:"<
}
程序结果:
Pop s1: c
Pop s1: b
Pop s1: a
Pop s2: z
Pop s2: y
Pop s2: z
3.阅读程序,写出程序运行时输出结果。
#i nclude
class Tdate
{ public:
Tdate();
Tdate(int d);
Tdate(int m, int d);
Tdate(int m, int d, int y);
protected:
int month;
int day;
int year;
};
Tdate::Tdate()
{ month=4;
day=15;
year=1995;
cout<
}
Tdate::Tdate(int d)
{ month=4;
day=d;
year=1996;
cout<
}
Tdate::Tdate(int m, int d)
{ month=m;
day=d;
year=1997;
cout<
}
Tdate::Tdate(int m, int d, int y)
{ month=m;
day=d;
year=y;
cout<
}
void main()
{ Tdate aday;
Tdate bday(10);
Tdate cday(2,12);
Tdate dday(1,2,1998);
}
运行结果:
4/15/1995
4/10/1996
2/12/1997
1/2/1998
4.阅读程序,写出程序运行时输出结果。
#i nclude
#i nclude
class shape
{ public:
shape(double x, double y):xCoord(x), yCoord(y){}
virtual double Area()const {return 0.0; }
protected:
double xCoord, yCoord;
};
class AA :public shape
{ public:
AA(double x, double y, double r): shape(x,y), rad(r){}
virtual double Area()const { return 3.0 * rad * rad; }
protected:
double rad;
};
class BB :public shape
{ public:
BB(double x1, double y1, double x2, double y2)
:shape(x1, y1), x2Coord(x2), y2Coord(y2){ }
virtual double Area()const;
protected:
double x2Coord, y2Coord;
};
double BB:Area()const
{ return fabs((xCoord-x2Coord)* (yCoord - y2Coord));
//库函数fabs(double t)求得t的绝对值
}
void fun(const shape& sp)
{ cout<
}
void main()
{ AA aa(2.0, 5.0, 4.0);
fun(aa);
BB bb(2.0, 8.0, 12.0, 17.0);
fun(bb);
}
运行结果:
48
30
六、 编写程序题(每小题10分,共20分)
1.编写一个函数int Judge(int *pArray, int n),判断一个n×n二维整数数组pArray 是否为“魔方阵”,若是返回1,否则返回0。所谓 魔方阵就是将1到n2的各个数字组成的方阵,它的每一行、每一列以及两个对角线上数字之和均相等。例如,3×3的 中,A是魔方阵,而B不是魔方阵。然后在主程序中调用Judge函数判断数组A是否为魔方阵。
参考程序
#i nclude
int Judge(int *pArray, int n)
{ int s1, s2, s3,s4,sum=0;
int *p=pArray;
for(int i=1; i<= n*n; i++)
{ int Found=0; //为0,不在方阵中;
for(int j=0; j
if(p[j]==i)
{ Found=1; //为1,在方阵中
break;
}
if(Found==0) return 0; // 值为 i 的元素不在数组中,显然不是魔方阵
}
for( i=1; i<=n*n; i++)
sum=sum+i;
sum=sum / n; // 各行、各列、对角线元素应当得到的和
s3=0;
s4=0;
for( i=0; i
{ s1=0, s2=0;
p=pArray;
for(int j=0; j
{ s1=s1+p[i*n+j]; //第i行的元素和
s2=s2+p[j*n+i]; //第i列的元素和
}
if ( s1!=sum)
return 0;
if ( s2!=sum)
return 0;
s3=s3+pArray[i*n+i]; // 对角线一元素和
s4=s4+pArray[i*n+(n-1-i)]; // 对角线二元素和
}
if(s3!=sum)
return 0;
if(s4 != sum)
return 0;
return 1;
}
void main()
{ int Array[3][3]={{ 8, 1, 6},{ 3, 5, 7},{ 4, 9, 2}};
if(Judge((int*)Array, 3))
cout<<"Yes, it's a magic array"<
else
cout<<"No, it isn't a magic array"<
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -