第一范文网 - 专业文章范例文档资料分享平台

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

来源:用户分享 时间:2025/5/29 4:23:14 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

.

}

void input_data() {int i;

for (i=0;i

{cout<<\ cin>>name[i]>>num[i]>>score[i];} }

6.2题

#include #include using namespace std; int main()

{void swap(char *,char *);

char str1[20],str2[20],str3[20]; cout<<\ gets(str1); gets(str2); gets(str3);

if(strcmp(str1,str2)>0) swap(str1,str2); if(strcmp(str1,str3)>0) swap(str1,str3); if(strcmp(str2,str3)>0) swap(str2,str3); cout<

cout<

void swap(char *p1,char *p2) /* 交换两个字符串 */ {char p[20];

strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p); }

6.2题另一解

#include #include using namespace std; int main()

{void change(string &,string &); string str1=\ \ str2=\ \ str3=\ \

char *p1=&str1[0],*p2=&str2[0],*p3=&str3[0]; cout<<\ gets(p1);

.

.

gets(p2); gets(p3);

if(str1>str2)change(str1,str2); if(str1>str3)change(str1,str3); if(str2>str3)change(str2,str3);

cout<

cout<

void change(string &st1,string &st2) /* 交换两个字符串 */ {string st;

st=st1;st1=st2;st2=st; }

6.3题

#include using namespace std; int main()

{ void input(int *number);

void max_min_value(int *number); void output(int *number); int number[10];

input(number); // 调用输入10个数的函数 max_min_value(number); // 调用交换函数 output(number); // 调用输出函数 return 0; }

void input(int *number) // 输入10个数的函数 {int i;

cout<<\ for (i=0;i<10;i++) cin>>number[i]; }

void max_min_value(int *number) // 交换函数 { int *max,*min,*p,temp; max=min=number;

for (p=number+1;p

if (*p>*max) max=p; // 将大数地址赋给 max else if (*p<*min) min=p; // 将小数地址赋给 min

temp=number[0];number[0]=*min;*min=temp; // 将最小数与第一数交换 temp=number[9];number[9]=*max;*max=temp; // 将最小数与第一数交换 }

.

.

void output(int *number) // 输出函数 {int *p;

cout<<\ \

for (p=number;p

#include using namespace std; int main()

{void move(int *array,int n,int m); int number[20],n,m,i;

cout<<\ // 询问共有多少个数 cin>>n;

cout<<\ // 要求输入n个数 for (i=0;i>number[i];

cout<<\ // 询问后移多少个位置 cin>>m;

move(number,n,m); //调用move 函数 cout<<\ for (i=0;i

cout<

void move(int *array,int n,int m) //使循环后移一次的函数 {int *p,array_end;

array_end=*(array+n-1);

for (p=array+n-1;p>array;p--) *p=*(p-1);

*array=array_end; m--;

if (m>0) move(array,n,m); //递归调用,当循环次数m减至为0时,停止调用 }

6.5题

#include using namespace std; int main()

.

.

{int i,k,m,n,num[50],*p;

cout<<\ cin>>n; p=num;

for (i=0;i

*(p+i)=i+1; // 以1至n为序给每个人编号 i=0; // i为每次循环时计数变量

k=0; // k为按1,2,3报数时的计数变量 m=0; // m为退出人数

while (m

{if (*(p+i)!=0) k++;

if (k==3) // 将退出的人的编号置为0 {*(p+i)=0; k=0; m++; } i++;

if (i==n) i=0; // 报数到尾后,i恢复为0 }

while(*p==0) p++;

cout<<\ return 0; }

6.6题

#include using namespace std; int main()

{int length(char *p); int len;

char str[20];

cout<<\ cin>>str;

len=length(str);

cout<<\ return 0; }

int length(char *p) //求字符串长度的函数 {int n; n=0;

while (*p!='\\0') {n++; p++; }

.

.

return(n); }

6.7题

#include using namespace std; int main()

{void copystr(char *,char *,int); int m;

char str1[20],str2[20]; cout<<\ gets(str1);

cout<<\ cin>>m;

if (strlen(str1)

cout<<\ else

{copystr(str1,str2,m);

cout<<\ }

return 0; }

void copystr(char *p1,char *p2,int m) //字符串部分复制函数*/ {int n; n=0;

while (n

while (*p1!='\\0') {*p2=*p1; p1++; p2++; }

*p2='\\0'; }

6.8题

#include using namespace std; int main()

{int upper=0,lower=0,digit=0,space=0,other=0,i=0; char *p,s[20];

cout<<\

.

《c++程序设计》谭浩强课后习题答案及解析 .doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c4tr8n37guk79ew80o94h77xpo584e200qvv_9.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top