C++程序设计基础课后答案 第五章

合集下载

程序设计基础课后答案 第五章

程序设计基础课后答案 第五章
{
int i,m,n;
m=strlen(dest);
n=strlen(a)-m;
try//用于抛出错误
{
for(i=0;i<=n;i++)
{
int k=i;/r(int j=0;j<=m-1;j++)
{
if(a[k]==dest[j])
cout<<"values after function"<<big<<"and"<<small<<endl;
}
分析:输出结果:Values before function 2002 and 0
Values after function 2002 and 0
在函数调用中,可以通过值传递方式在函数间传递数据。但是,这种传递无法改变实参的值。只是因为在进入被调用函数时,变量进行了复制。被调用函数的任何更改和实参没有任何联系。所以,在主程序中打印它的局部变量时,变量值没有任何改变。
double add(double,double);
void main()
{
cout<<add(5,10)<<endl;
cout<<add(5.0,10.6)<<endl;
}
int add(int x,int y)
{
return x+y;
}
double add(double a, double b)
四、编程题
2、编写一个函数,在一个字符串数组中查找"Hello",如果找回返回指向“Hello”开始出的指针;否则抛出一个异常“Not Found”.

c语言程序设计基础教程课后答案

c语言程序设计基础教程课后答案

c语言程序设计基础教程课后答案C语言程序设计基础教程课后答案第一章:C语言概述1. 简述C语言的特点。

- C语言是一种结构化编程语言,具有高效、灵活、功能强大等特点。

- 它支持多种编程范式,如过程式编程、结构化编程、面向对象编程等。

- C语言具有较低的抽象级别,接近硬件,因此执行效率较高。

2. C语言的发展历史。

- C语言在1972年由丹尼斯·里奇(Dennis Ritchie)在贝尔实验室开发。

- 它最初是作为UNIX操作系统的系统编程语言而设计的。

第二章:C语言环境和基本语法1. 如何设置C语言的开发环境?- 选择一个C语言编译器,如GCC、MSVC等。

- 安装编译器并配置环境变量,以便在命令行中直接调用编译器。

2. C语言的基本语法结构是什么?- C语言的基本结构包括预处理指令、函数定义、变量声明和语句。

第三章:数据类型、运算符和表达式1. C语言中的基本数据类型有哪些?- 整型(int)、字符型(char)、浮点型(float和double)等。

2. 运算符的分类及其用法。

- 算术运算符:+、-、*、/、%等。

- 赋值运算符:=、+=、-=等。

- 比较运算符:==、!=、>、<等。

- 逻辑运算符:&&、||、!等。

第四章:控制语句1. 条件语句的用法。

- if语句用于基于条件执行代码块。

- switch语句用于多条件选择。

2. 循环语句的用法。

- while循环用于在满足条件时重复执行代码块。

- for循环用于已知迭代次数的循环。

- do-while循环先执行代码块再判断条件。

第五章:数组和字符串1. 数组的基本概念和使用。

- 数组是相同数据类型的元素集合,存储在连续的内存位置。

- 声明数组:int arr[10]; 表示一个包含10个整数的数组。

2. 字符串的表示和操作。

- 在C语言中,字符串是以空字符('\0')结尾的字符数组。

- 使用标准库函数如strcpy、strlen等操作字符串。

C语言程序设计(第3版)第5章习题参考答案

C语言程序设计(第3版)第5章习题参考答案
printf(" 1: input 2:find 3:sort \n");
printf(" 4: insert 5:delete 6:exit \n");
while(1)
{ printf("\nplease input a menu option:");
scanf("%d",&iMenu);
switch(iMenu)
break;
case 4: printf("\nplease input the data to be inserted:");
scanf("%d",&iInsert);
for(i=0;i<10;i++)
{
if(iInsert>iScore[i])
{
iFlag=i;
break;
}
}
for(i=10;i>iFlag;i--)
{
if(iScore[j]<iScore[j+1])
{
iTemp = iScore[j];
iScore[j] = iScore[j+1];
iScore[j+1] = iTemp;
}
}
}
printf("\nthe sorted datas :");
for(i=0;i<10;i++)
printf("%d ",iScore[i]);
printf("%d\t",s[i][0]);
for(j=1;j<=i;j++)

C语言程序设计教程 第五章 课后习题参考答案

C语言程序设计教程 第五章 课后习题参考答案
printf("%d\t",f);
k++;
if(k%5==0)
printf("\n");
f1=f2;
f2=f;
}
printf("\n");
return 0;
}
P124 3统计一个整数的位数
#include<stdio.h>
int main()
{
int n,k=0;
printf("请输入n的值:");
int main()
{
int x,y,z,i=0;
for(x=1;x<=20;x++)
{
for(y=1;y<=33;y++)
{
for(z=3;z<=99;z=z+3)
{
if((5*x+3*y+z/3==100)&&(x+y+z==100))
i++;
}
}
}
printf("共有%d种买法:\n",i);
c语言程序设计教程第五章课后习题参考答案
P1242古典问题:兔子总数(斐波那契数列)
#include<stdio.h>
int main()
{
int f1=1,f2=1,f,i,k=0;
printf("%d\t %d\t",f1,f2);
k=k+2;
for(i=3;i<=20;i++)
{
f=f1+f2;
do
{
printf("请输入第%d个整数x=",i);

C++程序设计基础课后答案--第五章教学提纲

C++程序设计基础课后答案--第五章教学提纲

C++程序设计基础课后答案--第五章5.1 阅读下列程序,写出执行结果1.#include < iostream.h >struct data{ int n ;float score ;} ;void main(){ data a[3] = { 1001,87,1002,72,1003,90 } , *p = a ;cout << (p++)->n << endl ;cout << (p++)->n << endl ;cout << p->n++ << endl ;cout << (*p).n++ << endl ;}2.#include < iostream.h >struct employee{ char name[ 20 ] ;char sex ;} ;void fun( employee *p ){ if( (*p).sex == 'm' )cout << (*p).name << endl ;}void main(){ employee emp[5] = { "Liming", 'm', "Wangxiaoping", 'f', "Luwei", 'm' } ;int i ;for( i=0; i<3; i++ )fun( emp+i ) ;}3. #include < iostream.h >struct node{ char * s ;node * q ;} ;void main(){ node a[ ] = { { "Mary", a+1 }, { "Jack", a+2 }, { "Jim", a } } ;node *p = a ;cout << p->s << endl ;cout << p->q->s << endl ;cout << p->q->q->s << endl ;cout << p->q->q->q->s << endl ;}4.#include < iostream.h >class A{ public :int f1();int f2();void setx( int m ) { x = m ; cout << x << endl; }void sety( int n ) { y = n ; cout << y << endl; }int getx() { return x ; }int gety() { return y ; }private :int x, y ;};int A::f1() { return x + y ; }int A::f2() { return x - y ; }void main(){ A a ;a.setx( 10 ) ; a.sety( 5 ) ;cout << a.getx() << '\t' << a.gety() << endl ;cout << a.f1() << '\t' << a.f2() << endl ;}5. #include < iostream.h >class T{ public :T( int x, int y ){ a = x ; b = y ;cout << "调用构造函数1." << endl ;cout << a << '\t' << b << endl ;}T( T &d ){ cout << "调用构造函数2." << endl ;cout << d.a << '\t' << d.b << endl ;}~T() { cout << "调用析构函数."<<endl; }int add( int x, int y = 10 ) { return x + y ; } private :int a, b ;};void main(){ T d1( 4, 8 ) ;T d2( d1 ) ;cout << d2.add( 10 ) << endl ;}6. #include < iostream.h >class T{ public:T(int x){ a=x; b+=x;};static void display(T c){ cout<<"a="<<c.a<<'\t'<<"b="<<c.b<<endl; } private:int a;static int b;} ;int T::b=5;void main(){ T A(3),B(5);T::display(A);T::display(B);}7. #include < iostream.h >#include < math.h >class point{ public :point( float x, float y ){ a = x; b = y;? cout << "点( " << a << ", " << b << " )" ; } friend double d( point &A , point &B ){ return sqrt((A.a-B.a) * (A.a-B.a) + (A.b-B.b) * (A.b-B.b)) ; } private:double a, b ;};void main(){ point p1( 2, 3 ) ;cout << " 到 " ;point p2( 4, 5 ) ;cout << "的距离是:" << d( p1,p2 ) << endl ;}8. #include < iostream.h >class A{ public :A() { a = 5 ; }void printa() { cout << "A:a = " << a << endl ; } private :int a ;friend class B ;} ;class B{ public:void display1( A t ){ t.a ++ ; cout << "display1:a = " << t.a << endl ; } ;void display2( A t ){ t.a -- ; cout << "display2:a = " << t.a << endl ; } ;};void main(){ A obj1 ;B obj2 ;obj1.printa() ;obj2.display1( obj1 ) ;obj2.display2( obj1 ) ;obj1.printa() ;}5.2 思考题1.结构和类有什么区别?如果把程序中定义结构的关键字struct直接改成class,会有什么问题?用教材上的一个例程试一试,想一想做什么修改能使程序正确运行?2.分析以下说明结构的语句struct node{ int data;node error; //错误node * ok; //正确};error和ok分别属于什么数据类型?有什么存储要求?error出错的原因是什么?3.有说明class A{ int a;double x;public:funMember();};A a1, a2, a3 ;编译器为对象a1,a2,a3开辟了什么内存空间?它们有各自的funMember函数的副本吗?C++通过什么机制调用类的成员函数?4.C++提供了默认版本的构造函数,为什么还需要用户自定义构造函数?编写一个验证程序,说明自定义构造函数的必要性。

C 程序设计与应用基础第五章 多态性习题答案

C  程序设计与应用基础第五章 多态性习题答案
Base's des.
根据结果将程序补充完整。
#incude <iostream.h>
class Base
{
public:
Base(){cout<<"Base's cons."<<endl;}
___varitual ~Base()___{cout<<"Base's des."<endl;}
};
class Derived:public Base
第五章多态性
1、填空题
1)在一个成员函数内调用一个虚函数时,对该虚函数的调用进行___动态______联编。
2)动态联编是在__虚函数___的支持下实现的,它通过___指针和引用__来调用该函数操作。
3)下列程序的运行结果如下:
Base's cons.
Derived's cons.
Derived's des.
salesman s1;
employee *emp[4]={&m1,&t1,&sm1,&s1};
int i;
for(i=0;i<4;i++)
{
emp[i]->promote();
emp[i]->pay();
emp[i]->display();
}
}
2)编写一个程序,建立两种类型的表:队列与堆钱,使它们可以共用一个接口访问。
答案:
#include <iostream.h>
#include <string.h>
class employee

C程序设计第五章课后习题答案

C程序设计第五章课后习题答案

循环结构程序设计P115 5.1 用while计算1至100的合.#include<stdio.h>int main(){int i=1,sum=0;while(i<=100) //对于需要运算的值,要么在运算前可以赋值,要么一开始要指定.{sum=sum+i;i++;}printf("The sum is %d .\n",sum);return 0;}P117 5.2 用do-while来做1至100的合.#include<stdio.h>int main(){int i=1,sum=0;do// do-while可以做的事,用while都可以做到.{ //do-while先做一次执行,再判断条件,而while却是先做一个条件,再执行.sum=sum+i;i++;}while(i<=100);printf("The sum is %d .\n",sum);return 0;}P118 5.3 比较do-while与while的差别.#include<stdio.h>{int i,sum=0;printf("Please input a number :");scanf("%d",&i); //输入10以内,正常,11的话,则sum仍然是0.while(i<=10){sum=sum+i;i++;}printf("The sum of 1-10 is %d .\n",sum);return 0;}#include<stdio.h>int main(){int i,sum=0;printf("Please input a number :");scanf("%d",&i); //输入10以内,结果一样.输入11的话,先做操作,所以sum=11.do{sum=sum+i;i++;}while(i<=10); //此重点在于理解二者的差别.printf("The sum of 1-10 is %d .\n",sum);return 0;}P126 5.4 break的例子.#include<stdio.h>int main(){int i,b=0,a,c;for(i=0;i<=1000;i++){printf("Please input amount :"); //循环体内套有输出语句以及跳出语句.scanf("%d",&a);b=b+a;if(b>=100)break; //break是用于跳出循环,对if无效,对while for switch 这一类.}}c=b/i;printf("conut is %d , aver is %d ",i+1,c); //注意%号后的形式,否则可能输出错误.return 0;}P127 5.5 continue的例子.#include<stdio.h>int main(){int i;for(i=1;i<20;i++){if(i%3!=0){continue; //跳过本次I,执行下一个i.}printf("%d ",i);}printf("\n");return 0;}P128 5.6 形成一个4*5的矩阵.#include<stdio.h>int main(){int i,j,a=0; //没有给初值,会出现警告: 使用了未初始化的局部变量“a”.for(i=1;i<=4;i++){for(j=1;j<=5;j++,a++) // a用来控制换行.{if(a%5==0){printf("\n");}printf("%d\t",i*j);}}printf("\n");return 0;}P131 5.7 用一个交错的式子求哌的近似值.#include<stdio.h>#include<math.h>int main() //四分之哌等于(1)-(1/3)+(1/5)-(1/7)+(1/9)-(1/11).{float s=1,n=1,m,sum=0,t;for(m=1;;m=m+2) //不确定哪项才会小于等于十的负六次方,所以不指定,无限下去.{ //不指定第二项,执行语句中应该有控制跳出的句子,否则死循环.t=(s)*(n/m); //这是第一项,s是符号,if(fabs(t)<=1e-6) //应该写在这里,题目要求这一项不累加进去.{break;}sum=sum+t; //因为累加项在这里,所以,一旦跳出就不会累加进来了.s=s*(-1); //变号一次.}printf("四分之一哌的值是%f.\n",sum);printf("一个完整哌的值是%f.\n",sum*4);return 0;}//下面这段小代码用来验证关于数值型数据类型的关系.去掉注释,可运行.//如果出现类似值为全1或是全零的话,一般可以考虑数据类型赋值或是定义错了的问题.//#include <stdio.h> //这是关于int float double三者关复杂关系的,乱啊,如果看不懂,可以通过实验自己明白来.////int main() //在C语言中,1.0认为是实数,即是double型,所以,如果你把它用float输出的话,会有警告:警告 1 warning C4305: “=”: 从“double”到“float”截断.//{// float m,a,b,c; //一旦定义了是这种类型的话,输出或是赋值的时候只能扩展不能截断,意思就是能变成double型,不能变成int型啦.而且后面的赋值会跟着它变成相应的类型.比如下面的m=1,其实得到的是m=1.0.// int d,e,f;// m=1;// a=1.0/3;// b=1/3;// c=m/3;// d=1.0;// e=1/4;// f=1.0/4;// printf("%lf(float用double的%lf来输出是可以的.)\n%f\n%f\n%f\n",m,a,b,c); //不管上面定义什么,这边写的输出类型是什么,就按相应的类型输出,有可能会出错,所以建议按定义的类型来输出.当然扩展的是不会错的,截断的是会错的,比如float可以用%lf来输出,而不能用%d来输出.// printf("%d\n%d\n%d\n",d,e,f); //但是,不相应的int型不可以用%f来输出的.因为int float就不同种类,一个是整数,一个是小数,float double同样是有小数点的!!!!// return 0;//}#include<stdio.h>int main() //这个就是著名的Fibonacci(费波那契数列问题){int f1=1,f2=1,f3,i;printf("%12d\n%12d\n",f1,f2);for(i=1;i<=38;i++) //注意,这是个基础问题,(i=1;i<=5;i++)这里其实进行了次运算,因为有f1,f2,要求有个,所以要有个.要么写<=38,要么写<39,边界问题一定要注意,不可以太随意!!!!{f3=f1+f2;printf("%12d\n",f3); //这个问题同样适用于"一对兔子一个月生一对"的问题.,f1=f2; //f1=f1+f2;此时它们各是,所以,现在的f1是.f2=f3; //f2=f2+f1;此时的f1已经是最先二者之和了.可以不用到f3.}return 0;}#include<stdio.h>#include<math.h>int main(){double num;int i;printf("Please input a number :");scanf("%lf",&num); //因为sqrt要求是浮点型,那就给它浮点型,需要时再强制转换.for(i=2;i<=sqrt(num);i++) //这边是<=号没错.{if((int)num%i==0) //如果在这期间有任何一个可以为零的话,则不是素数.{break; //当然跳出.}} //执行到这里的时候,i=5,已经变成了!!if(i<=sqrt(num)){printf("Not %d",(int)num);}else//如上所述,i=5,超出了求根的值,所以是素数.{printf("Yes %d",(int)num);}return 0;}P137 5.10 求100至200间的素数.#include<stdio.h>//不解释,HOHO>>>>...#include<math.h>int main(){double j;int i,k=0;for(j=100;j<=200;j++){for(i=2;i<=sqrt(j);i++){if((int)j%i==0){break;}}k=k+1; //这里是布局的开头.学习一下,有助逻辑.if(i<=sqrt(j)){printf("Not %d ",(int)j);if(k%5==0) //5个换一次行.{printf("\n");}}else{printf("Yes %d ",(int)j);if(k%5==0){printf("\n");}}}return 0;}P139 5.11 密码转换.#include<stdio.h>int main(){char c;c=getchar();while(c!='\n') //这也可以用数组来实现.{if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){if((c>='w'&&c<='z')||(c>='W'&&c<='Z')){c=c-22;}else{c=c+4;}printf("%c",c);c=getchar(); //套在循环里,依次得到字母,而while中判断回车为结束.}}printf("\n"); //这是布局问题.return 0;}P140 0.3 最大公约数和最小公倍数.#include<stdio.h>//最大公约数用累除法,除到无余数时的被除数是最大公约数.main (){int m, n, c, d;int gcd(); //这是最大公约数的缩写,此处调用函数,可以不写里面的实参.int lcm(); //这是最小公倍数的缩写,此处调用函数,可以不写里面的实参.printf("Please input two number :\n");scanf("%d %d",&m,&n);c=gcd(m,n); //c获取最大公约数d=lcm(m,n); //d获取最小公倍数printf("The GCD of %d and %d is : %d !\n", m, n, c);printf("The LCM of %d and %d is : %d !\n", m, n, d);return 0;}int gcd(int x, int y) //最大公约数Greatest Common Divisor{int temp;while(x%y!=0){temp=y; //y在下一轮中作为除数,即是下一轮中的X,所以先闪一边去.y=x%y; //x,y的余数作为下一轮中的Y,由x%y来取得.x=temp; //刚才temp中存储了y的值,现在拿出来作为下一轮中的X使用.}return y; //这是每一轮中的被除数,按原理来,这就是最大公约数,即累除法的原理. }int lcm(int x, int y) //最小公倍数Lowest Common Multiple{int i, temp;if(x<y) //此段代码结果是保证二者大的数在X上,小的数在Y上.即小于号降序.{ //以下为经典三行码,实现两个数的互换.temp=x;x=y;y=temp;}for(i=1; i<=y; i++) //设定一个区间,从1至大的数之间的循环.{if(!((x*i)%y)) //此式子如有余数,加上"!"号,会是假,则不返回,进行下一轮.{ //如此往复,直到取模无余数,那么小的数X乘以区间当前的I值,就是最小公倍数.return x*i;}}}P140 0.4 判断一串输入的字符.#include<stdio.h>int main(){char ch;int a=0,b=0,c=0,d=0,e=0;printf("Please input the string\n");while((ch=getchar())!='\n') //直到回车.{if(ch<='z'&&ch>='a'){a++;}else if(ch==' '){c++;}else if(ch<58&&ch>47){d++;}else if(ch<='Z'&&ch>='A'){b++;}else{e++;}}printf("大写%d 小写%d 空格%d 数字%d 其它%d\n",a,b,c,d,e);}#include<stdio.h>//不理解时可以百度或是谷歌更多的信息.int main() //想办法既快速做完,又要消化理解!!!{int temp,i,a,n,sum=0; //主逻辑,友好性暂时放松.scanf("%d %d",&a,&n); //a是数字,n是要乘的个数.temp=a; //先把第一阶的值存起来.for(i=0;i<n;i++){sum=sum+a;printf("%d + ",a); //事关布局.a=a*10+temp; //重点是每次乘,然后加上上一个数.}printf("= %d .",sum);return 0;}P140 0.6 1!+2!+3!+4!.....的值. #include<stdio.h>int main() //1!+2!+3!+4!.....{int i,j,k,sum=0,m=1;scanf("%d",&k); //比如设定为,值为.for(i=1;i<=k;i++) //第一层循环,指定到.{for(j=1;j<=i;j++) //第二层循环,指定至当前数.{m=m*j;} //到此是阶乘的结构.sum=sum+m;m=1;}printf("%d",sum); //完全不理解时,搜索并参考.return 0; //尝试自己做,第一次做出来就是自己的东西了. }#include<stdio.h>int main(){int a,b;double c,asum=0,bsum=0,csum=0;for(a=1;a<=100;a++) //三个块分别注释验证结果.{asum=asum+a;}for(b=1;b<=50;b++) //在VS运行中,注意*.cpp为C++语言.{bsum=bsum+b*b; //为了避免语言差别,请注意文件名为*.c.}for(c=1;c<=10;c++) //c作浮点运算,所以定义在double类型中.{csum=csum+1/c;}printf("%lf",asum+bsum+csum);return 0;}PP#include<stdio.h>#include<math.h>int main(){int j,k,s[6],x=100,y,sum=0;for(j=153;j<=154;j++){for(k=2;k>=0;k--){s[k]=j%(int)pow(10,k+1)/(int)pow(10,k);y=pow(s[k],3);sum+=y;printf("%d-%d--%d\t",k,s[k],j);}printf("%d\n",sum);}return 0;}P140 0.8 水仙花数.//#include <stdio.h> //一步步的发现问题.////int main() //在%和/号之间,以前pow.以后再做.//{// int i,j,k,a,b,c,sum=0; //这里逻辑对,算出来却错了.// for(i=2;i<=4;i++) //计算机在想什么,看来它的大脑难以模拟.// {// for(j=pow(10,i);j<=pow(10,i+1)-1;j++) //我不完全明白它遵守的逻辑. // {// for(k=0;k<=i;k++)// {// sum+=pow((j%pow(10,i+1)/pow(10,i)),3);// }// if(sum==j)// {// printf("%d 是水仙花数!\n",j);// }// sum=0;// }// }// return 0;//} //为什么还是无法实现?!#include<stdio.h>#include<math.h>int main(){int a,b,c,i,sum=0; //这里只计算三位数的.for(i=100;i<1000;i++){a=i/100;b=i%100/10;c=i%10;sum+=a*a*a+b*b*b+c*c*c;if(sum==i){printf("%d 是水仙花数.\n",i);}sum=0;}return 0;}P141 0.9 完数.#include<stdio.h>int main(){int i,j,r;for(i=1;i<=1000;i++) //零是个临界值,不能包括它.{r=0; //每次清零重来.类似水仙中的sum.for(j=1;j<i;j++){if(i%j==0) //除得尽即是因子.{r=r+j; //然后累加进去.}}if(r==i) //若相等.{printf("%d 是完数.\n",i);}}return 0;}P141 0.10 2/1+3/2+5/3+8/5+13/8…#include<stdio.h>int main() //10.007051{double i,a=2,b=1,c,s=0; //a是分子,b是分母.for(i=0;i<6;i++) //二十可以用户指定.{s+=a/b; //中间储值变量.c=a+b;b=a;a=c;}printf("%lf\n",s);return 0;}#include<stdio.h>int main() //从一百开始,减一半再自加两次,下降一次,反弹一次.{double sum=100,high=100,up,donw,i;for(i=1;i<10;i++) //不管指定到哪个数,都不会超过三百.{up=high/2;donw=up;high=donw; //自咬尾巴问题.sum+=donw*2;}printf("%lf %lf\n",donw,sum);return 0;}P141 0.12 猴子吃桃子.#include<stdio.h>int main() //从一百开始,减一半再自加两次,下降一次,反弹一次.{int i,sum=1;for(i=1;i<=10;i++) //临界要清楚,结果要与手算的前几个实例相匹配.{printf("倒数第%d天还剩有%d个桃子.\n",i,sum);sum=(sum+1)*2;}//printf("%d\n",sum);return 0;}#include<stdio.h>#include<conio.h>main() //只关注左半部分.右半部分无视空格.{int i,j,k,m=6; //m可指定,指定中心点位置.可任意奇偶.for(i=1;i<=m;i++) //上半部分.其实也是正三角.{for(j=1;j<=m-i;j++) //一到中心点前i个位置填充空格.printf(" ");for(k=1;k<2*i;k++) //空格后向前填充星号的个数.是奇数.一,三,五... printf("*");printf("\n");}for(i=m-1;i>0;i--) //下半部分.其实也是倒三角.{ //减一是因为行数问题.这是中心行以下的.for(j=m-1;j>=i;j--)printf(" ");for(k=1;k<2*i;k++)printf("*");printf("\n");}getch(); //用户反应后结束.但,没必要.}。

c语言《程序设计基础》课后习题参考答案与解析

c语言《程序设计基础》课后习题参考答案与解析

《程序设计基础》习题参考答案与部分解析第1章 C 语言概述一、填空a) C源程序的基本单位是函数。

b) 一个C程序中至少应包括一个main函数。

c) 在C语言中,输出操作是有库函数printf( )函数完成。

二、单选题1、A2、C3、B解析:第1题答案:A 。

因为一个C程序总是从main函数开始执行的,而不论main函数在程序中的位置。

且到main函数结束。

第2题答案:C 。

因为main函数没有限制必须位于程序的最前面。

C程序书写自由,一行内可写几个语句。

在对一个C 程序进行编译的过程中,无法检查注释当中的拼写错误。

不过C语言本身并没有输入输出语句,输入输出是由函数完成的。

第3题答案:B。

因为一个C语言程序是由若干个函数组成的。

但至少包含一个main 函数,且main函数的位置不限。

三、编程题1、编写一个输出“Welcome to C!”信息的小程序。

解:程序如下#include “stdio.h”main( ){printf(“Welcome to C!”) ;}2、已知三角形的三边长分别为3,4,5,试用海轮公式编程求其面积。

海伦公式为:S△= ,其中s= (a+b+s)/2 ;解:程序如下#include “math.h”#include “stdio.h”main( ){int a , b , c ; /* a ,b,c 3个整型变量表示三角形的3条边。

*/float s ,s1 ; /* s1作为面积变量,s 作为中间变量是都应该是实形*/a=3 ; b= 4; c=5 ;s= (a+b+c)/2.0 ;s1= sqrt(s*(s-a)*(s-b)*(s-c)); /* sqrt函数完成开平方根功能。

*/printf(“area=%f\n”,s1);}第2章程序设计基础知识一、单选题1、C2、A3、C4、A5、C6、C7、D8、C9、D 10、A 11、D 12、A 13、C 14、C 15、B A16、B 17 D解析:1. 答案:C。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

5.1 阅读下列程序,写出执行结果1.#include < iostream.h >struct data{ int n ;float score ;} ;void main(){ data a[3] = { 1001,87,1002,72,1003,90 } , *p = a ; cout << (p++)->n << endl ;cout << (p++)->n << endl ;cout << p->n++ << endl ;cout << (*p).n++ << endl ;}2.#include < iostream.h >struct employee{ char name[ 20 ] ;char sex ;} ;void fun( employee *p ){ if( (*p).sex == 'm' )cout << (*p).name << endl ;}void main(){ employee emp[5] = { "Liming", 'm', "Wangxiaoping", 'f', "Luwei", 'm' } ; int i ;for( i=0; i<3; i++ )fun( emp+i ) ;}3. #include < iostream.h >struct node{ char * s ;node * q ;} ;void main(){ node a[ ] = { { "Mary", a+1 }, { "Jack", a+2 }, { "Jim", a } } ;node *p = a ;cout << p->s << endl ;cout << p->q->s << endl ;cout << p->q->q->s << endl ;cout << p->q->q->q->s << endl ;}4.#include < iostream.h >class A{ public :int f1();int f2();void setx( int m ) { x = m ; cout << x << endl; } void sety( int n ) { y = n ; cout << y << endl; } int getx() { return x ; }int gety() { return y ; }private :int x, y ;};int A::f1() { return x + y ; }int A::f2() { return x - y ; }void main(){ A a ;a.setx( 10 ) ; a.sety( 5 ) ;cout << a.getx() << '\t' << a.gety() << endl ;cout << a.f1() << '\t' << a.f2() << endl ;}5. #include < iostream.h >class T{ public :T( int x, int y ){ a = x ; b = y ;cout << "调用构造函数1." << endl ;cout << a << '\t' << b << endl ;}T( T &d ){ cout << "调用构造函数2." << endl ;cout << d.a << '\t' << d.b << endl ;}~T() { cout << "调用析构函数."<<endl; }int add( int x, int y = 10 ) { return x + y ; } private :int a, b ;};void main(){ T d1( 4, 8 ) ;T d2( d1 ) ;cout << d2.add( 10 ) << endl ;}6. #include < iostream.h >class T{ public:T(int x){ a=x; b+=x;};static void display(T c){ cout<<"a="<<c.a<<'\t'<<"b="<<c.b<<endl; }private:int a;static int b;} ;int T::b=5;void main(){ T A(3),B(5);T::display(A);T::display(B);}7. #include < iostream.h >#include < math.h >class point{ public :point( float x, float y ){ a = x; b = y;? cout << "点( " << a << ", " << b << " )" ; }friend double d( point &A , point &B ){ return sqrt((A.a-B.a) * (A.a-B.a) + (A.b-B.b) * (A.b-B.b)) ; } private:double a, b ;};void main(){ point p1( 2, 3 ) ;cout << " 到 " ;point p2( 4, 5 ) ;cout << "的距离是:" << d( p1,p2 ) << endl ;}8. #include < iostream.h >class A{ public :A() { a = 5 ; }void printa() { cout << "A:a = " << a << endl ; }private :int a ;friend class B ;} ;class B{ public:void display1( A t ){ t.a ++ ; cout << "display1:a = " << t.a << endl ; } ; void display2( A t ){ t.a -- ; cout << "display2:a = " << t.a << endl ; } ; };void main(){ A obj1 ;B obj2 ;obj1.printa() ;obj2.display1( obj1 ) ;obj2.display2( obj1 ) ;obj1.printa() ;}5.2 思考题1.结构和类有什么区别?如果把程序中定义结构的关键字struct直接改成class,会有什么问题?用教材上的一个例程试一试,想一想做什么修改能使程序正确运行?2.分析以下说明结构的语句struct node{ int data;node error; //错误node * ok; //正确};error和ok分别属于什么数据类型?有什么存储要求?error出错的原因是什么?3.有说明class A{ int a;double x;public:funMember();};A a1, a2, a3 ;编译器为对象a1,a2,a3开辟了什么内存空间?它们有各自的funMember函数的副本吗?C++通过什么机制调用类的成员函数?4.C++提供了默认版本的构造函数,为什么还需要用户自定义构造函数?编写一个验证程序,说明自定义构造函数的必要性。

5.试从定义方式、访问方式、存储性质和作用域分析类的一般数据成员和静态数据成员的区别,并编写一个简单程序验证之。

6.试从定义方式、调用方式分析静态成员函数和友员函数的区别。

考察教材的例5-22,若class Goods 的指针域Goods * next;被声明为私有(private)成员,程序会出现什么错误?做什么最小修改能使程序正确运行?请你试一试。

1.分析以下说明结构的语句struct Node{ int data;Node error; //错误Node * ok; //正确};error和ok分别属于什么数据类型?有什么存储要求?error出错的原因是什么?【答案】error是Node结构类型数据成员,错误。

原因是结构定义的数据成员若为本身的结构类型,是一种无穷递归。

ok是指向Node类型的指针,定义正确,占4字节。

2.本章例5-5中用辅助数组对结构数组做关键字排序,有定义person *index[100];index数组存放结构数组元素的地址。

如果把index定义改为int index[100];用于存放结构数组元素的下标。

可以实现对结构数组的索引排序吗?如何修改程序?请你试一试。

【答案】可以。

关键是通过整型索引数组元素作为下标访问结构数组。

表示为:all[pi[i]].name all[pi[i]].id all[pi[i]].salary有关程序如下:#include<iostream>using namespace std;struct person //说明结构类型{ char name[10];unsigned int id;double salary;} ;void Input( person[], const int );void Sort( person[], int[],const int );void Output( const person[], int[],const int );int main(){ person allone[100] ; //说明结构数组int index[100]; //说明索引数组int total ;for(int i=0; i<100; i++) //索引数组元素值初始化为结构数组元素下标index[i]=i ;cout<<"输入职工人数:";cin>>total;cout<<"输入职工信息:\n";Input(allone,total);cout<<"以工资做关键字排序\n";Sort(allone,index, total);cout<<"输出排序后信息:\n";Output(allone,index,total);}void Input( person all[], const int n ){ int i ;for( i=0; i<n; i++ ) // 输入数据{ cout<<i<<": 姓名: ";cin>>all[i].name;cout<<"编号: ";cin >> all[i].id;cout<<"工资: ";cin >> all[i].salary ;}}void Sort(person all[], int pi[], const int n){ int i,j;int t; //交换用中间变量for(i=1; i<n; i++) //以成员salary做关键字排序{ for(j=0; j<=n-1-i; j++)if(all[pi[j]].salary>all[pi[j+1]].salary) //通过索引数组访问结构数组元素{ t=pi[j]; //交换索引数组元素值pi[j]=pi[j+1];pi[j+1]= t;}}}void Output(const person all[], int pi[], const int n){ for( int i=0; i<n; i++ ) // 输出排序后数据cout<<all[pi[i]].name<<'\t'<<all[pi[i]].id<<'\t'<<all[pi[i]].salary<<endl;}3.有以下结构说明和遍历单向链表的函数。

相关文档
最新文档