C++第六章习题答案

6-1

#include <iostream> using namespace std; int main()

{void swap(int *p1,int *p2); int n1,n2,n3; int *p1,*p2,*p3;

cout<<"input three integers n1,n2,n3:"; cin>>n1>>n2>>n3; p1=&n1; p2=&n2; p3=&n3;

if(n1>n2) swap(p1,p2); if(n1>n3) swap(p1,p3); if(n2>n3) swap(p2,p3); cout<<"Now,the order is:"<<n1<<" "<<n2<<" "<<n3<<endl; return 0; }

void swap(int *p1,int *p2) {int p;

p=*p1; *p1=*p2; *p2=p; }

6-2-1

#include <iostream> #include <cstring> using namespace std; int main()

{void swap(char *,char *);

char str1[20],str2[20],str3[20]; cout<<"input three line:"<<endl; 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<<endl<<"Now,the order is:"<<endl;

cout<<str1<<endl<<str2<<endl<<str3<<endl; return 0; }

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

strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p); } 6-2-2

#include <string> using namespace std; int main()

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

*p1=&str1[0],*p2=&str2[0],*p3=&str3[0]; cout<<"input three line:"<<endl; 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<<endl<<"Now,the order is:"<<endl;

cout<<str1<<endl<<str2<<endl<<str3<<endl; return 0; }

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

st=st1;st1=st2;st2=st; } 6-3

#include <iostream> 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); // 调用交换函数

你可能喜欢

  • 入党积极分子思想汇报
  • 新党章全文
  • 滑轮习题答案
  • 数据结构习题答案
  • 塑性力学习题答案
  • 补充习题答案
  • 应用光学习题答案

C++第六章习题答案相关文档

最新文档

返回顶部