谭浩强《C++⾯向对象程序设计》课后习题答案各位⼩伙伴们,满意的话记得点个关注再⾛哈,你们的关注是我继续更博的动⼒
第⼀章
5:
#include <iostream>
using namespace std;
int main()
{
cout<<"This"<<"is";
cout<<"a"<<"C++";
cout<<"program."<<endl;
return 0;
}
6:
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=10;
b=23;
c=a+b;
cout<<"a+b=";
cout<<c;
cout<<endl;
return 0;
}
7:
#include <iostream>
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<<endl;
return 0;
}
int f(int x,int y,int z)
{
int m;
if (x<y) m=x;
else m=y;
if (z<m) m=z;
return(m);
}
8: #include <iostream> using namespace std;
int main()
{
int a,b,c;
c程序设计谭浩强pdf百度网盘cin>>a>>b;
c=a+b;
cout<<"a+b="<<a+b<<endl; return 0;
}
9:
#include <iostream>
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<<endl;
return 0;
}
int add(int x,int y)
{int c;
c=x+y;
return(c);
}
10:
#include <iostream>
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);
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<x) cout<<z<<','<<x<<','<<y<<endl;
else if (z<y) cout<<x<<','<<z<<','<<y<<endl;
else cout<<x<<','<<y<<','<<z<<endl;
}
11:
#include <iostream>
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)="<<max(a,b)<<endl;
return 0;
}
int max(int a,int b,int c)
{if(b>a) a=b;
if(c>a) a=c;
return a;
}
12:
#include <iostream>
using namespace std;
int main()
{
void change(int ,int );
int a,b;
cin>>a>>b;
if(a<b) change(a,b);
cout<<"max="<<a<<" min="<<b<<endl; return 0;
}
void change(int ,int )
{
int r1,r2,temp;
temp=r1;
r1=r2;
r2=temp;
}
13:
#include <iostream>
using namespace std;
int main()
{void sort(int &,int &,int &);
int a,b,c,a1,b1,c1;
cout<<"Please enter 3 integers:";
cin>>a>>b>>c;
a1=a;b1=b;c1=c;
sort(a1,b1,c1);
cout<<a<<" "<<b<<" "<<c<<" in sorted order is "; cout<<a1<<" "<<b1<<" "<<c1<<endl;
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;
}
14:
#include <iostream>
#include <string>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论