当前位置:文档之家› c__面向对象程序设计课后习题解答-谭浩强

c__面向对象程序设计课后习题解答-谭浩强

c__面向对象程序设计课后习题解答-谭浩强
c__面向对象程序设计课后习题解答-谭浩强

第1章

4.请说明编辑、编译、连接的作用。在编译后得到的目标文件为什么不能直接运行?

【解】 编译是以源程序文件为单位进行的,而一个完整的程序可能包含若干个程序文件,在分别对它们编译之后,得到若干个目标文件(后缀一般为.obj ),然后要将它们连接为一个整体。此外,还需要与编译系统提供的标准库相连接,才能生成一个可执行文件(后缀为.exe )。不能直接运行后缀为.obj 的目标文件,只能运行后缀为.exe 的可执行文件。

5.分析下面程序运行的结果。

#include

using namespace std;

int main( )

{

cout<<" This "<<" is ";

cout<<" a "<<" C++ ";

cout<<"program. " << endl;

return 0;

}

【解】 输出的结果为ThisisaC++program.

6.分析下面程序运行的结果。

#include

using namespace std;

int main( )

{

int a,b,c;

a=10;

b=23;

c=a+b;

cout<<" a+b=";

cout<

cout<

return 0;

}

【解】 前两个cout 语句在输出数据后不换行,第3个cout 语句输出一个换行,因此输出的结果为

a+b=33

7.分析下面程序运行的结果。请先阅读程序写出程序运行时应输出的结果,然后上机运行程序,验证自己分析的结果是否正确。以下各题同。

#include

using namespace std;

int main( )

{

int a,b,c;

int f(int x,int y,int z);

cin>>a>>b>>c;

c=f(a,b,c);

cout<

return 0;

}

int f(int x,int y,int z)

{

int m;

if (x

else m=y;

if (z

return(m);

}

【解】 程序的作用是:输入3个整数,然后输出其中值最小的数。在主函数中输入3个整数,然后调用f 函数,在f 函数中实现找最小的整数,用if 语句比较两个数,将小者存放在变量m 中,经过两个if 语句的比较,m 中存放的是3个整数中最小的数。运行情况如下:

1 5 3↙ (输入3个整数)

1 (输出其中最小的数)

8.在你所用的C++系统上,输入以下程序,进行编译,观察编译情况,如果有错误,请修改程序,再进行编译,直到没有错误,然后进行连接和运行,分析运行结果。

int main( );

{

int a,b;

c=a+b;

C++面向对象程序设计题解与上机指导

4

cout >>" a+b=" >> a+b;

}

【解】上机编译出错,编译出错信息告知在第2行出错,经检查,发现第1行的末尾多了一个分号,编译系统无法理解第2行的花括号,导致报告第2行出错。将第1行的末尾的分号去掉,重新编译,编译出错信息告知在第5行和第6行出错。

第5行出错原因是cout未经声明,因为cout不是C++语言提供的系统的关键字,而是输出流的对象,必须使用头文件iostream。

第6行出错原因是main是int型函数,应返回一个整型值。将程序改为

#include

int main( )

{

int a,b;

c=a+b;

cout >>" a+b=" >> a+b;

return 0;

}

重新编译。编译出错信息告知在第5行和第6行出错。第5行出错原因是变量c未定义,第6行出错原因是cout未经声明,说明#include 命令行未能起作用,原因是未指明命名空间。将程序改为

#include

using namespace std;

int main( )

{

int a,b,c;

c=a+b;

cout>>" a+b=" >>a+b;

return 0;

}

重新编译。编译出错信息告知在第7行出错,经检查,是“>>”用得不当,“>>”是提取运算符,应与cin联合使用,用来从输入流中提取数据,输出时应该用插入运算符“<<”。把两处“>>”都改为“<<”,再重新编译,发现没有error错误,但有两个警告(warning),原因是定义了a和b,但未对它们赋值。应增加赋值语句或输入语句,使a和b获得值,将程序改为#include

using namespace std;

int main( )

{

int a,b,c;

cin>>a>>b;

c=a+b;

cout>>" a+b=" >>a+b;

return 0;

}

重新编译,没有编译错误,能通过编译和连接,可以正常运行,在V isual C++ 6.0环境下运行时屏幕显示如下:

5 9↙

a+b=14Press any key to continue

显然这样的输出不理想,“Press any key to continue”是V isual C++系统在输出了运行结果后自动显示的一个信息,告诉用户“如果想继续工作,请按任何一个键”。当用户按任何一个键后,显示运行结果的窗口消失,屏幕显示回到V isual C++的主窗口,显示出源程序和编译信息。

为了解决以上输出不理想的情况,可以在最后一个输出语句中增加输出一个换行符。最后的程序如下:

#include

using namespace std;

int main( )

{

int a,b,c;

cin>>a>>b;

c=a+b;

cout<<"a+b="<

return 0;

}

运行时屏幕显示如下:

5 9↙

a+b=14

Press any key to continue

这就完成了程序的调试。

(1)编译系统给出的编译出错信息,只是提示性的,引导用户去检查错误,用户必须根据程序的上下文和编译出错信息,

第1章C++ 的初步知识

5

全面考虑,找出真正出错之处。例如编译出错信息通知第2行出错,其实可能是第1行出错。

(2)有时,有的错误开始时未被检查出来并告知用户(例如未定义变量c),由于其他错误未解决,掩盖了这个错误。当

解决了其他错误后,这个错误会被检查出来。有时在调试过程中会不断检查出新的错误,这是不奇怪的。一一处理,问题会迎

刃而解。

(3)为了说明调试过程,这里全部依靠计算机系统来检查错误,其实有些明显的错误,完全可以由人工查出,这样可以

提高调试效率。由人工在纸面或屏幕上检查错误,称为静态查错,用计算机编译系统检查错误,称为动态查错。建议尽量先用

静态查错的方法排除错误,只有人工检查不出来的错误才让计算机检查。

9.输入以下程序,进行编译,观察编译情况,如果有错误,请修改程序,再进行编译,直到没有错误,然后进行连接和

运行,分析运行结果。

#include

using namespace std;

int main( )

{

int a,b;

c=add(a,b)

cout<<"a+b="<

return 0;

}

int add(int x,int y);

{

z=x+y;

retrun(z);

}

【解】发现7个错误:

(1)对add函数未声明就调用,应在main函数中对add函数进行声明。

(2)定义add函数时,函数首行末尾不应有分号。

(3)变量c未经定义。

(4)add函数中的变量z未经定义。

(5)第6行末尾少了一个分号。

(6)add函数中的retrun拼写错误,应为return。编译系统把retrun作为未声明的标识符而报错,因为retrun(z)会被认为是函数调用的形式。

(7)变量a和b未被赋值。

改正后的程序如下:

#include

using namespace std;

int main( )

{int add(int x,int y);

int a,b,c;

cin >> a >> b;

c=add(a,b);

cout <<" a+b=" << c <

return 0;

}

int add(int x,int y)

{int z;

z=x+y;

return(z);

}

运行情况如下:

5 8↙

13

10.输入以下程序,编译并运行,分析运行结果。

#include

using namespace std;

int main( )

{ void sort(int x,int y,int z);

int x,y,z;

cin >> x >> y >> z;

sort(x,y,z);

C++面向对象程序设计题解与上机指导

6

return 0;

}

void sort(int x, int y, int z)

{

int temp;

if (x>y) {temp=x;x=y;y=temp;} //{ }内3个语句的作用是将x和y的值互换

if (z

else if (z

else cout << x << ',' << y << ',' << z << endl;

}

请分析此程序的作用。sort函数中的if语句是一个嵌套的if语句。

运行时先后输入以下几组数据,观察并分析运行结果。

①3 6 10↙

②6 3 10↙

③10 6 3↙

④10,6,3↙

【解】程序的作用是对输入的3个整数按由小到大的顺序进行排序。sort函数中的第1个if语句的作用是先将x和y排序,使x小于或等于y。第2个if语句是一个嵌套的if语句,先比较z和x,如果z

按题目要求分别输入以下几组数据,运行结果如下:

①3 6 10↙

3,6,10

②6 3 10↙

3,6,10

③10 6 3↙

3,6,10

④10,6,3↙

-858993460,-858993460,10

以上是在V isual C++ 6.0环境下运行的情况,前3次运行正常,表明当输入不同的数据时,程序能实现由小到大的排序功能。

第4次运行的结果显然不正常,这是由于输入数据时出了问题,本来要求在输入数据时,数据之间以空格或换行相隔,而现在却以逗号相隔,只有第一个整数能正常赋给变量x,第二和第三个数据均无法正常赋给变量y和z,y和z的值来自输入流中相应字节的内容。

11.求2个或3个正整数中的最大数,用带有默认参数的函数实现。

【解】可以编写出以下程序:

#include

using namespace std;

int main( )

{int max(int a,int b,int c=0);

int a,b,c;

cin >> a >> b >> c;

cout << " max(a,b,c)= " << max(a,b,c) << endl;

cout << " max(a,b)= " <

return 0;

}

int max(int a,int b,int c)

{if(b>a) a=b;

if(c>a) a=c;

return a;

}

运行情况如下:

13 5 76↙

max(a,b,c)=76 (从3个数中找最大者)

max(a,b)=13 (从前2个数中找最大者)

如果想从3个数中找大者,可以在调用时写成“max(a,b,c)”形式,如果只想从2个数中找大者,则在调用时写成“max(a,b)”形式,此时c自动取默认值0,由于0比任何正整数都小,因此从14,5,0中选最大者和从14,5中选大者的结果是一样的。

12.输入两个整数,将它们按由大到小的顺序输出。要求使用变量的引用。

第1章C++ 的初步知识

7

【解】可以编写出以下程序:

#include

using namespace std;

int main( )

{ void change(int &,int &);

int a,b;

cin>>a>>b;

if(a

cout<<"max="<

cin>>a>>b>>c;

a1=a;b1=b;c1=c;

sort(a1,b1,c1);

cout<

cout<

return 0;

}

void sort(int &i,int &j,int &k)

{ void change(int &,int &);

if (i>j) change(i, j);

if (i>k) change(i, k);

if (j>k) change(j, k);

}

void change(int &x,int &y)

{ int temp;

temp=x;

x=y;

y=temp;

}

运行情况如下:

Please enter 3 integers:23 67 -55↙

23 67 –55 in sorted order is –55 23 67

这个程序很容易理解,不易出错。由于在调用sort函数时虚实结合使形参i,j,k成为实参a1,b1,c1的引用(别名),因此通过

调用函数sort(a1,b1,c1)既实现了对i,j,k排序,也就同时实现了对a1,b1,c1排序。同样,执行change(i,j)函数,可以实现对实

参i和j的互换。

14.编一程序,将两个字符串连接起来,结果取代第一个字符串。要求用string方法。

【解】可以编写出以下程序:

#include

#include //程序中若使用字符串变量,必须包含头文件string

using namespace std;

int main( )

{ string s1= " week ",s2= " end ";

cout << " s1= " << s1 << endl;

cout << "s2=" << s2 << endl;

C++面向对象程序设计题解与上机指导

8

s1=s1+s2;

cout<<" The new string is: "<

return 0;

}

运行情况如下:

s1=week

s2=end

The new string is: weekend

15.输入一个字符串,把其中的字符按逆序输出。如输入LIGHT,输出THGIL。要求用string方法。

【解】可以编写出以下程序:

#include

#include

using namespace std;

int main( )

{ string str; //定义字符串变量str

int i,n;

char temp; //定义字符变量temp

cout<<" please input a string: ";

cin>>str; //输入一个字符串赋给字符串变量str

n=str.size( ); //测量str的长度n

for(i=0;i

{temp=str[i];str[i]=str[n-i-1];str[n-i-1]=temp;}

cout << str << endl;

return 0;

}

运行情况如下:

please input a string:

LIGHT↙

THGIL

注意:输入的字符串中不能含有空格。

16.有5个字符串,要求将它们按由小到大的顺序排列,用string方法。

【解】可以编写出以下程序:

#include

#include

using namespace std;

int main( )

{ int i;

string str[5]={" BASIC"," C"," FORTRAN"," C++","PASCAL"};

void sort(string [ ]);

sort(str); //对字符串排序

cout<<" the sorted strings : "<

for(i=0;i<5;i++)

cout<

cout<

return 0;

}

void sort(string s[ ])

{int i, j;

string t;

for (j=0; j<5; j++)

for(i=0; i<5-j; i++)

if (s[i]>s[i+1])

{t=s[i];s[i]=s[i+1];s[i+1]=t;}

}

运行结果如下:

the sorted strings :

BASIC C C++ FORTRAN PASCAL

17.编一个程序,用同一个函数名对n个数据进行从小到大排序,数据类型可以是整型、单精度型、双精度型。用重载函数实现。

【解】可以编写出以下两个程序:

(1)建立3个函数,分别用于处理整型、单精度型、双精度型数据的排序,在3个函数中都采用选择法排序方法。

#include

第1章C++ 的初步知识

9

#include

using namespace std;

int main( )

{

long a[5]={10100,-123567, 1198783,-165654, 3456};

int b[5]={1,9,0,23,-45};

float c[5]={2.4, 7.6, 5.5, 6.6, -2.3 };

void sort(long [ ]);

void sort(int [ ]);

void sort(float [ ]);

sort(a);

sort(b);

sort(c);

return 0;

}

void sort(long a[ ])

{int i, j;

long t;

for (j=0; j<5; j++)

for(i=0;i<5-j;i++)

if (a[i]>a[i+1])

{t=a[i];a[i]=a[i+1];a[i+1]=t;}

cout<<" the sorted numbers : "<

for(i=0;i<5;i++)

cout<

cout<

}

void sort(int a[ ])

{int i, j, t;

for (j=0; j<5; j++)

for(i=0;i<5-j;i++)

if (a[i]>a[i+1])

{t=a[i];a[i]=a[i+1];a[i+1]=t;}

cout<<" the sorted numbers : "<

for(i=0;i<5;i++)

cout<

cout<

}

void sort(float a[ ])

{int i, j;

float t;

for (j=0;j<5;j++)

for(i=0;i<5-j;i++)

if (a[i]>a[i+1])

{t=a[i];a[i]=a[i+1];a[i+1]=t;}

cout<<" the sorted numbers : "<

for(i=0;i<5;i++)

cout<

cout<

}

运行结果如下:

the sorted numbers :

-123567 -165654 10100 3456 1198783 (长整型数据排序)

the sorted numbers : (整型数据排序)

-45 0 1 9 23

the sorted numbers :

-2.3 2.4 5.5 6.6 7.6 (单精度型数据排序)

(2)在第1种方法中,3个函数的函数体基本上是相同的,都是采用选择法排序,在下面的程序中,3个函数的函数体不

全相同,前两个函数采用选择法排序,最后一个函数采用起泡法排序。

#include

#include

using namespace std;

C++面向对象程序设计题解与上机指导

10

int main( )

{ long a[5]= {10100,-123567, 1198783,-165654, 3456};

int b[5]={1,9,0,23,-45};

float c[5]={2.4, 7.6, 5.5, 6.6, -2.3 };

void sort(int [ ]);

void sort(float [ ]);

void sort(long [ ]);

sort(a); //对长整型数据排序

sort(b); //对整型数据排序

sort(c); //对单精度型数据排序

return 0;

}

void sort(long a[ ]) //对长整型数据用选择法排序的函数

{int i,j,min;

long t;

for(i=0;i<5;i++)

{min=i;

for (j=i+1;j<5;j++)

if(a[min]>a[j]) min=j;

{t=a[i]; a[i]=a[min]; a[min]=t; }

cout<<" the sorted numbers : "<

for(i=0;i<5;i++)

cout<

cout<

}

void sort(int a[ ]) //对整型数据用选择法排序的函数

{int i, j, t;

for (j=0; j<5; j++)

for(i=0;i<5-j;i++)

if (a[i]>a[i+1])

{t=a[i];a[i]=a[i+1];a[i+1]=t;}

cout<<" the sorted numbers : "<

for(i=0;i<5;i++)

cout<

cout<

}

void sort(float a[ ]) //对单精度型数据用起泡法排序的函数

{int i, j;

float t;

for (j=0;j<5;j++)

for(i=0;i<5-j;i++)

if (a[i]>a[i+1])

{t=a[i];a[i]=a[i+1];a[i+1]=t;}

cout<<" the sorted numbers : "<

for(i=0;i<5;i++)

cout<

cout<

}

运行结果如下:

the sorted numbers :

-123567 -165654 10100 3456 1198783 (长整型数据排序结果)

the sorted numbers : (整型数据排序结果)

-45 0 1 9 23

the sorted numbers :

-2.3 2.4 5.5 6.6 7.6 (单精度型数据排序结果)

对比两种方法,可以看到,并不要求重载函数的函数体相同,在本例中,采用不同的排序方法,结果是相同的。从理论上说,重载的函数可以用来实现完全不同的功能,但是应该注意:同一个函数名最好用来实现相近的功能,而不要用来实现完全不相干的功能,以方便用户理解和使用。

18.对第17题改用函数模板实现。并与17题程序进行对比分析。

#include

#include

using namespace std;

第1章C++ 的初步知识

11

template

void sort(T a[ ]) //函数模板,采用选择法排序

{int i, j, min;

T t;

for(i=0;i<5;i++)

{min=i;

for (j=i+1; j<5; j++)

if(a[min]>a[j]) min=j;

t=a[i]; a[i]=a[min]; a[min]=t;

}

cout<<" the sorted numbers : "<

for(i=0;i<5;i++)

cout<

cout<

}

int main( )

{ long a[5]={10100,-123567, 1198783,-165654, 3456};

int b[5]={1,9,0,23,-45};

float c[5]={2.4, 7.6, 5.5, 6.6, -2.3 };

sort(a);

sort(b);

sort(c);

return 0;

}

运行结果如下:

the sorted numbers :

-123567 -165654 10100 3456 1198783 (长整型数据排序)

the sorted numbers : (整型数据排序)

-45 0 1 9 23

the sorted numbers :

-2.3 2.4 5.5 6.6 7.6 (单精度型数据排序)

对比第17题和18题,可以看到,如果重载函数的函数体基本相同的话,用函数模板显然更方便,可以压缩程序篇幅,使

用方便。

《c++程序设计》谭浩强课后习题答案

第一章 1.5题 #include using namespace std; int main() { cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."; return 0; 1.6题 #include using namespace std; int main() { int a,b,c; a=10; b=23; c=a+b; cout<<"a+b="; cout< using namespace std; int main() { int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout< using namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; cout<<"a+b="< using namespace std; int main() { int a,b,c; int add(int x,int y); cin>>a>>b; c=add(a,b);

(完整版)谭浩强c程序设计课后习题答案

谭浩强c++程序设计课后答案 娄警卫

第一章1.5题 #include using namespace std; int main() { cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."; return 0; 1.6题 #include using namespace std; int main() { int a,b,c; a=10; b=23; c=a+b; cout<<"a+b="; cout< using namespace std; int main() { int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout< using namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; cout<<"a+b="< using namespace std; int main() { int a,b,c; int add(int x,int y); cin>>a>>b; c=add(a,b); cout<<"a+b="<

C语言程序设计第三版谭浩强课后习题答案完整版

1.5 #include void main() { printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"); printf(" Very good! \n"); printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"); } 1.6 #include void main() { float max(float x,float y,float z); float a,b,c; printf("请分别输入a,b,c:\n"); scanf("%f,%f,%f",&a,&b,&c); printf("max=%f",max(a,b,c)); } float max(float x,float y,float z) { float m,n; m=x>y? x: y; n=m>z? m: z; return(n); } 3.6 #include void main() { char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’; printf(“a%c b%c\tc%c\tabc\n”,c1,c2,c3); printf(“\t\b%c %c\n”,c4,c5);

} 3.9.1 #include void main() { double x=2.5,y=4.7,z; int a=7; z=x+a%3*(int)(x+y)%2/4; printf("该表达式的值为:%f",z); } 3.9.2 #include void main() { int a=2,b=3; float x=3.5,y=2.5,z; z=(float)(a+b)/2+(int)x%(int)y; printf("该表达式的值为:%f",z); } 4.5 #include void main() { int a=5,b=7; double x=67.8564,y=-789.124; char c='A'; long n=1234567; unsigned u=65535; printf("%d%d\n",a,b); printf("%3d%3d\n",a,b); printf("%f,%f\n",x,y); printf("%-10f,%-10f\n",x,y); printf("%8.2f,%8.2f,%.4f,%.4f,%3f,%3f\n",x,y,x,y,x,y); printf("%e,%10.2e\n",x,y); printf("%c,%d,%o,%x\n",n,n,n); printf("%ld,%lo,%x\n",n,n,n); printf("%u,%o,%x,%d\n",u,u,u,u);

C语言程序设计第三版谭浩强课后习题答案完整版

1.6 编写一个程序,输入a、b、c 三个值,输出其中最大值。 课后习题答案完整版 第一章 1.5 请参照本章例题,编写一个C 程序,输出以下信息: ************************** Very Good! ************************** 解:mian() {int a,b,c,max; printf( “请输入三个数a,b,c:\n ” ); scanf( “%d,%d,%”d ,&a,&b,&c); C语言程序设计第三版谭浩强 解: mian() {printf( ”); “************************** printf( “”X “ n” ); printf( “Very Good!” \ n”); printf( “”X “ n” ); printf( “************************** ); max=a; if(max

{char #include c1='a',c2='b',c3= 'c',c4= ' \101 ',c5= ' 116'; printf( “a%cb%c n”,c1,c2,c 3); printf( “ b%c %c” ,c4,c5); } 解: aa 口bb 口口口cc 口口口口口口abc A 口N 3.7 要将"China" 译成密码,译码规律是:用原来字母后面的第 4 个字母代替原来的字母.例如,字母"A" 后面第 4 个字母是"E" . "E"代替"A"。因此,"China"应译为"Glmre" 。请编一程序,用赋初值的方法使cl 、c2、c3、c4、c5 五个变量的值分别为, ' C'、h'、i '、n'、a'经过运算,使cl、c2、c3、c4、c5分别变为'G'、' I '、' m >' r'、’ e',并输出。main() { char c1=' C' ,c2=' h' ,c3= ' i ' ,c4= ' n' ,c 5=' a' ; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; printf(" 密码是%c%c%c%c%c\n",c1,c2,c3,c4,c5); } 运行结果: 密码是GImre 3.9 求下面算术表达式的值。 解: 1 )x+a%3*(int)(x+y)%2/4

C语言程序设计第三版谭浩强课后习题答案完整版

C语言程序设计第三版谭浩强课后习题答案完整版Last revision on 21 December 2020

C语言程序设计第三版谭浩强 课后习题答案完整版 第一章 请参照本章例题,编写一个C程序,输出以下信息: ************************** Very Good! ************************** 解: mian() {printf(“**************************”); printf(“\n”); printf(“Very Good!\n”); printf(“\n”); printf(“**************************”); } 编写一个程序,输入a、b、c三个值,输出其中最大值。 解: mian() {int a,b,c,max; printf(“请输入三个数a,b,c:\n”); scanf(“%d,%d,%d”,&a,&b,&c); max=a; if(max main() { char c1=’C’,c2=’h’,c3=’i’,c4=’n’,c5=’a’; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; printf("密码 是%c%c%c%c%c\n",c1,c2,c3,c4,c5); } 运行结果: 密码是Glmre 求下面算术表达式的值。

C程序设计第四版谭浩强完整版课后习题答案

C程序设计第四版谭浩强完整版课后习题答案集团标准化办公室:[VV986T-J682P28-JP266L8-68PNN]

C程序设计(第四版)(谭浩强)第一章课后习题答案 P006 向屏幕输出文字. #include<>代码均调试成功,若有失误大多不是代码问题.自已找找. int main() { printf("Welcome to \n"); return 0; } P008 求两个数的和. #include<> int main() { int a,b,sum; a=5; b=4; sum=a+b; printf("The sum is %d .\n",sum);

return 0; } P008 调用函数比较两个数的大小. #include<> int main() { int max(int x,int y); int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("The max is %d .\n",c); return 0; } int max(int x,int y) { int z; if (x>y) z=x; else z=y; return(z); }

P015 三个数的大小.(数字0表示课后练习题) #include<> int main() { int a,b,c,d; int max(int x , int y , int z); printf("Please input 3 numbers :\n"); scanf("%d %d %d",&a,&b,&c); d=max(a,b,c); printf("The max is :%d .\n",d); } int max(int x , int y , int z) { int m; if (x>y && x>z) m=x; if (y>x && y>z) m=y; if (z>y && z>x) m=z; return (m); }

《C语言程序设计》课后习题答案(第四版)谭浩强

第1章程序设计和C语言1 1.1什么是计算机程序1 1.2什么是计算机语言1 1.3C语言的发展及其特点3 1.4最简单的C语言程序5 1.4.1最简单的C语言程序举例6 1.4.2C语言程序的结构10 1.5运行C程序的步骤与方法12 1.6程序设计的任务14 1-5 #include int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); printf ("**************************\n"); return 0; } 1-6#include int main() {int a,b,c,max; printf("please input a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); max=a; if (max

2.5结构化程序设计方法34 习题36

C语言程序设计第三版谭浩强课后习题答案完整版

C语言程序设计第三版谭浩强 课后习题答案完整版 第一章 1.5请参照本章例题,编写一个C程序,输出以下信息:************************** V ery Good! ************************** 解: mian() {printf(“**************************”); printf(“\n”); printf(“V ery Good!\n”); printf(“\n”); printf(“**************************”); } 1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。解: mian() {int a,b,c,max; printf(“请输入三个数a,b,c:\n”); scanf(“%d,%d,%d”,&a,&b,&c); max=a; if(max main() { char c1=?C?,c2=?h?,c3=?i?,c4=?n?,c5=?a?; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; printf("密码是%c%c%c%c%c\n",c1,c2,c3,c4,c5); } 运行结果: 密码是Glmre 3.9求下面算术表达式的值。 (1)x+a%3*(int)(x+y)%2/4 设x=2.5,a=7,y=4.7 (2)(float)(a+b)/2+(int)x%(int)y 设a=2,b=3,x=3.5,y=2.5 (1)2.5 (2)3.5 3.10写出程序运行的结果。 main() {int i,j,m,n; i=8; j=10; m=++i; n=j++; printf(“%d,%d,%d,%d”,i,j,m,n); } 解: 9,11,9,10 3.12 写出下面表达式运算后a的值,设原来a=12。设a和n都已定义为整型变量。 (1)a+=a (2)a-=2 (3)a*=2+3 (4)a/=a+a (5)a%=(n%=2),n的值等于5 (6)a+=a-=a*=a 解: (1) 24 (2) 10 (3) 60 (4) 0 (5) 0 (6) 0 第四章 4.4若a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1=’a’,c2=’b’。想得到以下输出格式和结果,请写出程序(包括定义变量类型和设计输出)。 a=_3_ _b=_4_ _c=_5 x=1.200000,y=2.400000,z=-3.600000 x+y=_3.600_ _y+z=-1.20_ _z+x=-2.40 c1=ˊaˊ_or_97(ASCII)

C程序设计第四版谭浩强完整版课后习题答案

C程序设计第四版谭浩强完整版课后习题答案 Document serial number【LGGKGB-LGG98YT-LGGT8CB-LGUT-

C程序设计(第四版)(谭浩强)第一章课后习题答案 #include<>代码均调试成功,若有失误大多不是代码问题.自已找找. int main() { printf("Welcome to \n"); return 0; } #include<> int main() { int a,b,sum; a=5; b=4; sum=a+b; printf("The sum is %d .\n",sum); return 0; } P008 调用函数比较两个数的大小. #include<> int main() { int max(int x,int y); int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("The max is %d .\n",c); return 0; } int max(int x,int y) { int z; if (x>y) z=x; else z=y; return(z); }

P015 三个数的大小.(数字0表示课后练习题) #include<> int main() { int a,b,c,d; int max(int x , int y , int z); printf("Please input 3 numbers :\n"); scanf("%d %d %d",&a,&b,&c); d=max(a,b,c); printf("The max is :%d .\n",d); } int max(int x , int y , int z) { int m; if (x>y && x>z) m=x; if (y>x && y>z) m=y; if (z>y && z>x) m=z; return (m); } C程序设计(第四版)(谭浩强)第2章课后 习题答案 算法——程序的灵魂 P017 计算机1-5相乘的积. #include<> int main() { int i,s=1; for(i=1;i<6;i++) { s=s*i; n",s); return 0; } #include<> int main() { int i,s=1; for(i=1;i<12;i++) 可以是i=i+2 { if(i%2!=0) s=s*i; else continue; }

《C语言程序设计》课后习题答案(第四版)谭浩强

第1章程序设计和C语言1 什么是计算机程序1 什么是计算机语言1 语言的发展及其特点3 最简单的C语言程序5 最简单的C语言程序举例6 语言程序的结构10 运行C程序的步骤与方法12 程序设计的任务14 1-5 #include <> int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); printf ("**************************\n"); return 0; } 1-6#include <> int main() {int a,b,c,max; printf("please input a,b,c:\n");

scanf("%d,%d,%d",&a,&b,&c); max=a; if (max

数据类型42 整型数据44 字符型数据47 浮点型数据49 怎样确定常量的类型51 运算符和表达式52 语句57 语句的作用和分类57 最基本的语句——赋值语句59 数据的输入输出65 输入输出举例65 有关数据输入输出的概念67 用printf函数输出数据68 用scanf函数输入数据75 字符数据的输入输出78 习题82 3-1 #include <> #include <> int main() {float p,r,n; r=; n=10; p=pow(1+r,n); printf("p=%f\n",p); return 0;

c面向对象程序设计课后习题答案(谭浩强版)

第一章5: #include using namespace std; int main() { cout<<"This"<<"is"; cout<<"a"<<"C++"; cout<<"program."< using namespace std; int main() { int a,b,c; a=10; b=23; c=a+b; cout<<"a+b="; cout< using namespace std; int main() { int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout<

else m=y; if (z using namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; cout<<"a+b="< using namespace std; int main() {int add(int x,int y); int a,b,c; cin>>a>>b; c=add(a,b); cout<<"a+b="< using namespace std; int main() {void sort(int x,int y,int z); int x,y,z; cin>>x>>y>>z;

谭浩强C++课后习题答案

第1章 谭浩强C++课后习题答案 1.请根据你的了解,叙述C++ 的特点。C++ 对C有哪些发展? 【解】略。 2.一个C++的程序是由哪几部分构成的?其中的每一部分起什么作用? 【解】略。 3.从拿到一个任务到得到最终结果,一般要经过几个步骤? 【解】略。 4.请说明编辑、编译、连接的作用。在编译后得到的目标文件为什么不能直接运行? 【解】编译是以源程序文件为单位进行的,而一个完整的程序可能包含若干个程序文件,在分别对它们编译之后,得到若干个目标文件(后缀一般为.obj),然后要将它们连接为一个整体。此外,还需要与编译系统提供的标准库相连接,才能生成一个可执行文件(后缀为.exe)。不能直接运行后缀为.obj的目标文件,只能运行后缀为.exe的可执行文件。 5.分析下面程序运行的结果。 #include using namespace std; int main( ) { cout<<" This "<<" is "; cout<<" a "<<" C++ "; cout<<"program. " << endl; return 0; } 【解】输出的结果为 ThisisaC++program.

C++面向对象程序设计题解与上机指导 4 6.分析下面程序运行的结果。 #include using namespace std; int main( ) { int a,b,c; a=10; b=23; c=a+b; cout<<" a+b="; cout< using namespace std; int main( ) { int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout<

C程序设计(第四版)(谭浩强)完整版 课后习题答案

C程序设计(第四版)(谭浩强)第一章课后习题答案 P006 1.1 向屏幕输出文字. #include//预编译. 代码均调试成功,若有失误大多不是代码问题.自已找找. int main() { printf("Welcome to https://www.doczj.com/doc/eb6004965.html,\n"); return 0; //与int main对应,为了程序可移植性,建议全用int main + return 0;. } P008 1.2 求两个数的和. #include int main() { int a,b,sum; a=5; b=4; sum=a+b; printf("The sum is %d .\n",sum); return 0; } P008 1.3 调用函数比较两个数的大小. #include int main() { int max(int x,int y); //被调用函数在主函数后面,用前先声明. int a,b,c; scanf("%d,%d",&a,&b); //输入时要按格式来,此处的逗号,用空格会发生错误. c=max(a,b); //a,b作为实参传入被调用函数中. printf("The max is %d .\n",c); return 0;

} int max(int x,int y) //定义了两个形参. { int z; //z属于局部变量,可与主函数中相同名字. if (x>y) z=x; else z=y; return(z); //z作为整个程序的出口值,赋给主函数中的c. } P015 0.6 三个数的大小.(数字0表示课后练习题) #include int main() { int a,b,c,d; //d是用于存储最大值的. int max(int x , int y , int z); //测试可知,在VS2008中,可以不预先声明. printf("Please input 3 numbers :\n"); scanf("%d %d %d",&a,&b,&c); d=max(a,b,c); //调用函数中有三个形参,这里需要传入三个实参,才可运算. printf("The max is :%d .\n",d); // d可以换成max(a,b,c). } int max(int x , int y , int z) { int m; if (x>y && x>z) //求三者之大的一种方法. m=x; if (y>x && y>z) m=y; if (z>y && z>x) m=z; return (m); //返回值m给主函数中的d. } C程序设计(第四版)(谭浩强)第2章课 后习题答案 算法——程序的灵魂

c语言程序设计第三版谭浩强课后习题答案.doc

c 语言程序设计第三版谭浩强课后习题 答案 【篇一:c 语言程序设计第三版谭浩强课后习题答案完 整版】 ude stdio.h void main() { printf(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n); printf(very good! \n); printf(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n); } 1.6 #include stdio.h void main() { float max(float x,float y,float z); float a,b,c; printf( 请分别输入a,b,c:\n); scanf(%f,%f,%f,a,b,c); printf(max=%f,max(a,b,c)); } float max(float x,float y,float z) { float m,n; m=xy? x: y; n=mz? m: z; return(n); } 3.6 #include stdio.h void main() { char c1= ’a’,c2= ’b’,c3= ’1c0’1’,c4,c=5’= 1’16’; printf( “a%c b%c n”,c1,c2,c3); printf( “n”,c4,c5); }

3.9.1 #include stdio.h void main() { double x=2.5,y=4.7,z; int a=7; z=x+a%3*(int)(x+y)%2/4; printf( 该表达式的值为:%f,z); } 3.9.2 #include stdio.h void main() { int a=2,b=3; float x=3.5,y=2.5,z; z=(float)(a+b)/2+(int)x%(int)y; printf( 该表达式的值为:%f,z); } 4.5 #include stdio.h void main() { int a=5,b=7; double x=67.8564,y=-789.124; char c=a; long n=1234567; unsigned u=65535; printf(%d%d\n,a,b); printf(%3d%3d\n,a,b); printf(%f,%f\n,x,y); printf(%-10f,%- 10f\n,x,y); printf(%8.2f,%8.2f,%.4f,%.4f,%3f,%3f\n,x,y,x,y,x,y); printf(%e,%10.2e\n,x,y); printf(%c,%d,%o,%x\n,n,n,n); printf(%ld,%lo,%x\n,n,n,n); printf(%u,%o,%x,%d\n,u,u,u,u); printf(%s,%5.3s\n,computer,computer); } 4.6 #include stdio.h

C程序设计(第四版)_谭浩强_第四章_课后习题答案

选择结构程序设计 P086 4.1 一无二次方程求根的二分支. #include #include int main() { double a,b,c,disc,x1,x2,p,q; scanf("%lf %lf %lf",&a,&b,&c); disc=b*b-4*a*c; if(disc<0) //这是选择结构和其判断条件的示例. printf("This equation hasn't real roots\n"); else { p=-b/(2.0*a); q=sqrt(disc)/(2.0*a); x1=p+q; x2=p-q; printf("x1=%7.2f\nx2=%7.2f",x1,x2); } return 0; } P087 4.2 二个数按大小输出. #include int main() //此程序代表按大小顺序输出. { float a,b,t; scanf("%f %f",&a,&b); //出错时,注意检查这里是否按格式输入了.比如有个逗号. if(a>b) { t=a; a=b; b=t; } printf("%5.2f,%5.2f\n",a,b);

return 0; } P088 4.3 三个数按大小输出. #include int main() //此程序代表按大小顺序输出. { float a,b,c,t; scanf("%f %f %f",&a,&b,&c); if(a>b) //此处执行后,a为小者. { t=a; a=b; b=t; } if(a>c) //此处执行后,a为小者. { t=a; a=c; c=t; } if(b>c) //上面已经搞定a是最小者,现在对比得出次小者,并且已经归到变量中. { t=b; b=c; c=t; } printf("%5.2f,%5.2f%5.2f\n",a,b,c); return 0; } P099 4.4 判断输入字符,并最终按小写输出. #include int main() { char ch; scanf("%c",&ch);

语言程序设计第二版谭浩强章课后习题答案清华大学出版社

原题:打印出下题的结果 main() { int a=5,b=7; float x=,y=; char c='A'; long n=1234567; unsigned u=65535; printf("%d%d\n",a,b); printf("%3d%3d\n",a,b); printf("%f,%f\n",x,y); printf("%-10f,%-10f\n",x,y); printf("%,%,%4f,%4f,%3f,%3f\n",x,y,x,y,x,y); printf("%e,%\n",x,y); printf("%c,%d,%o,%x\n",c,c,c,c); printf("%ld,%lo,%x\n",n,n,n); printf("%u,%o,%x,%d\n",u,u,u,u); printf("%s,%\n","COMPUTER","COMPUTER"); } 结果: 57 5 7 , , , ,,,, +01, +02 A,65,101,41 1234567,4553207,d687 65535,177777,ffff,-1 COMPUTER, COM 4-6 原题: 用下面的scanf函数输入数据,使a=3,b=7,x=,y=,c1='A',c2='a'。问在键盘上如何输入main() { int a,b; float x,y; char c1,c2; scanf("a=%d,_b=%d",&a,&b); scanf("_%f_%e",&x,7y); scanf("_%c_%c",&c1,&c2);

答案: 输入格式为: a=3,b=7 A a 4-7 原题:用下面的scanf函数输入数据使a=10,b=20,c1='A',c2='a',x=,y= z=,请问在键盘上如何输入数据 scanf("%5d%5d%c%c%f%f%*f,%f",&a,&b,&c1,&c2,&x,&y,&z); 答案: 输入格式为: 10 , 友情提示:10与20之间是3个空格,而那个是随便一个浮点数即可。 4-8 原题: 设圆半径r=,圆柱高h=3,求圆周长,圆面积,圆球表面积,圆球体积,圆柱体积,用scanf输入数据,输出结果,输出时要求有文字说明,取小数点后2位数字,请编写程序。 答案: #define PI main() { int h; float r; float cubage1,cubage2,per,area,facearea; clrscr(); printf("please input the circle's radii\n"); scanf("%f",&r); clrscr(); printf("please input the cylinder's height\n"); scanf("%d",&h); per=2*PI*r; area=PI*r*r; facearea=4*PI*r*r; cubage1=4/3*PI*r*r*r; cubage2=area*h; clrscr();

c++程序设计谭浩强课后习题答案(完整版)

c++程序设计谭浩强课后习题答案(完整版) -CAL-FENGHAI-(2020YEAR-YICAI)_JINGBIAN

第八章 #include using namespace std; class Time {public: //成员改为公用的 int hour; int minute; int sec; }; Time t; void set_time(void) //在 main 函数之前定义 { cin>>t.hour; cin>>t.minute; cin>>t.sec; } void show_time(void) //在 main 函数之前定义 { cout< using namespace std; class Time {public: void set_time(void) {cin>>hour; cin>>minute; cin>>sec; } void show_time(void)

{cout< using namespace std; class Time {public: void set_time(void); void show_time(void); private: int hour; int minute; int sec; }; void Time::set_time(void) {cin>>hour; cin>>minute; cin>>sec; } void Time::show_time(void) {cout<

相关主题
文本预览
相关文档 最新文档