C面向对象程序设计课后习题答案1~14章
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
c1=getchar(); //将输入的第一个字符赋给c1
c2=getchar(); //将输入的第二个字符赋给c2
cout<<"用putchar函数输出结果为:";
putchar(c1);
putchar(44);
putchar(c2);
cout<<endl;
cout<<"用cout语句输出结果为:";
putchar(c2);
cout<<endl;
cout<<"用cout语句输出结果为:";
cout<<c1<<c2<<endl;
return 0;
}
3-4-1、#include <iostream>
using namespace std;
int main ( )
{char c1,c2;
cout<<"请输入两个字符c1,c2:";
cout<<"please enter score of student:";
cin>>score;
while (score>100||score<0)
{cout<<"data error,enter data again.";
cin>>score;
}
switch(int(score/10))
{case 10:
else m=y;
if (z<m) m=z;
return(m);
}
1-8、#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b;
c=a+b;
cout<<"a+b="<<a+b<<endl;
return 0;
}
1-9、#include <iostream>
cin>>a>>b>>c;
temp=(a>b)?a:b; /*将a和b中的大者存入temp中*/
max=(temp>c)?temp:c; /*将a和b中的大者与c比较,最大者存入max */
cout<<"max="<<max<<endl;
return 0;
}
3-10、#include <iostream>
cout<<"s= "<<setw(10)<<s<<endl;
cout<<"sq="<<setw(10)<<sq<<endl;
cout<<"vq="<<setw(10)<<vq<<endl;
cout<<"vz="<<setw(10)<<vz<<endl;
return 0;
}
3-3、#include <iostream>
return 0;
}
第三章
3-2、#include <iostream>
#include <iomanip>
using namespace std;
int main ( )
{float h,r,l,s,sq,vq,vz;
const float pi=3.1415926;
cout<<"please enter r,h:";
ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
cout<<"original order:";
switch(place)
{case 5:cout<<ten_thousand<<","<<thousand<<","<<hundred<<","<<ten<<","<<indiv<<endl;
cout<<"reverse order:";
cout<<indiv<<ten<<hundred<<thousand<<ten_thousand<<endl;
using namespace std;
int main ()
{float c,f;
cout<<"请输入一个华氏温度:";
cin>>f;
c=(5.0/9.0)*(f-32); //注意5和9要用实型表示,否则5/9值为0
cout<<"摄氏温度为:"<<c<<endl;
return 0;
};
3-4、#include <iostream>
c1=i1;
c2=i2;
cout<<"按字符输出结果为:"<<c1<<" , "<<c2<<endl;
return 0;
}
3-8、#include <iostream>
using namespace std;
int main ( )
{ int a=3,b=4,c=5,x,y;
cout<<(a+b>c && b==c)<<endl;
cout<<c1<<","<<c2<<endl;
return;iostream>
using namespace std;
int main ( )
{char c1,c2;
int i1,i2; //定义为整型
cout<<"请输入两个整数i1,i2:";
cin>>i1>>i2;
return 0;
}
3-12、#include <iostream>
using namespace std;
int main ()
{long int num;
int indiv,ten,hundred,thousand,ten_thousand,place;
/*分别代表个位,十位,百位,千位,万位和位数*/
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<<endl;
return 0;
}
int add(int x,int y)
{int z;
z=x+y;
return(z);
cout<<"place="<<place<<endl;
//计算各位数字
ten_thousand=num/10000;
thousand=(int)(num-ten_thousand*10000)/1000;
hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;
}
else // x≥10
{y=3*x-11;
cout<<"x="<<x<<", y=3*x-11="<<y;
}
cout<<endl;
return 0;
}
3-11、#include <iostream>
using namespace std;
int main ()
{float score;
char grade;
return 0;
}
2-4、#include <iostream>
using namespace std;
int main()
{char c1='C',c2='+',c3='+';
cout<<"I say: \""<<c1<<c2<<c3<<'\"';
cout<<"\t\t"<<"He says: \"C++ is veryinteresting!\""<< '\n';
return 0;
}
2-8、#include <iostream>
using namespace std;
int main()
{char c1='C', c2='h', c3='i', c4='n', c5='a';
c1+=4;
c2+=4;
c3+=4;
c4+=4;
c5+=4;
cout<<"password is:"<<c1<<c2<<c3<<c4<<c5<<endl;
break;
cpp格式存在现暂时为大家转换第一章答案如果有需要的请通过邮件联系我我将其余章节答案发给大家
第一章
1-5、#include <iostream>
using namespace std;
int main()
{
cout<<"This"<<"is";
cout<<"a"<<"C++";
cout<<"program.";
return 0;
}
1-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;
}
1-7#include <iostream>
using namespace std;
int main ( )
{int x,y;
cout<<"enter x:";
cin>>x;
if (x<1)
{y=x;
cout<<"x="<<x<<", y=x="<<y;
}
else if (x<10) // 1≤x<10
{y=2*x-1;
cout<<"x="<<x<<", y=2*x-1="<<y;
}
第二章
2-3、#include <iostream>
using namespace std;
int main()
{char c1='a',c2='b',c3='c',c4='\101',c5='\116';
cout<<c1<<c2<<c3<<'\n';
cout<<"\t\b"<<c4<<'\t'<<c5<<'\n';
return 0;
}
2-7、#include <iostream>
using namespace std;
int main()
{int i,j,m,n;
i=8;
j=10;
m=++i+j++;
n=(++i)+(++j)+m;
cout<<i<<'\t'<<j<<'\t'<<m<<'\t'<<n<<endl;
case 9: grade='A';break;
case 8: grade='B';break;
case 7: grade='C';break;
case 6: grade='D';break;
default:grade='E';
}
cout<<"score is "<<score<<", grade is "<<grade<<endl;
cin>>r>>h;
l=2*pi*r;
s=r*r*pi;
sq=4*pi*r*r;
vq=3.0/4.0*pi*r*r*r;
vz=pi*r*r*h;
cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)
<<setprecision(2);
cout<<"l= "<<setw(10)<<l<<endl;
cout<<"enter an integer(0~99999):";
cin>>num;
if (num>9999)
place=5;
else if (num>999)
place=4;
else if (num>99)
place=3;
c2=getchar(); //将输入的第二个字符赋给c2
cout<<"用putchar函数输出结果为:";
putchar(c1);
putchar(44);
putchar(c2);
cout<<endl;
cout<<"用cout语句输出结果为:";
putchar(c2);
cout<<endl;
cout<<"用cout语句输出结果为:";
cout<<c1<<c2<<endl;
return 0;
}
3-4-1、#include <iostream>
using namespace std;
int main ( )
{char c1,c2;
cout<<"请输入两个字符c1,c2:";
cout<<"please enter score of student:";
cin>>score;
while (score>100||score<0)
{cout<<"data error,enter data again.";
cin>>score;
}
switch(int(score/10))
{case 10:
else m=y;
if (z<m) m=z;
return(m);
}
1-8、#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b;
c=a+b;
cout<<"a+b="<<a+b<<endl;
return 0;
}
1-9、#include <iostream>
cin>>a>>b>>c;
temp=(a>b)?a:b; /*将a和b中的大者存入temp中*/
max=(temp>c)?temp:c; /*将a和b中的大者与c比较,最大者存入max */
cout<<"max="<<max<<endl;
return 0;
}
3-10、#include <iostream>
cout<<"s= "<<setw(10)<<s<<endl;
cout<<"sq="<<setw(10)<<sq<<endl;
cout<<"vq="<<setw(10)<<vq<<endl;
cout<<"vz="<<setw(10)<<vz<<endl;
return 0;
}
3-3、#include <iostream>
return 0;
}
第三章
3-2、#include <iostream>
#include <iomanip>
using namespace std;
int main ( )
{float h,r,l,s,sq,vq,vz;
const float pi=3.1415926;
cout<<"please enter r,h:";
ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
cout<<"original order:";
switch(place)
{case 5:cout<<ten_thousand<<","<<thousand<<","<<hundred<<","<<ten<<","<<indiv<<endl;
cout<<"reverse order:";
cout<<indiv<<ten<<hundred<<thousand<<ten_thousand<<endl;
using namespace std;
int main ()
{float c,f;
cout<<"请输入一个华氏温度:";
cin>>f;
c=(5.0/9.0)*(f-32); //注意5和9要用实型表示,否则5/9值为0
cout<<"摄氏温度为:"<<c<<endl;
return 0;
};
3-4、#include <iostream>
c1=i1;
c2=i2;
cout<<"按字符输出结果为:"<<c1<<" , "<<c2<<endl;
return 0;
}
3-8、#include <iostream>
using namespace std;
int main ( )
{ int a=3,b=4,c=5,x,y;
cout<<(a+b>c && b==c)<<endl;
cout<<c1<<","<<c2<<endl;
return;iostream>
using namespace std;
int main ( )
{char c1,c2;
int i1,i2; //定义为整型
cout<<"请输入两个整数i1,i2:";
cin>>i1>>i2;
return 0;
}
3-12、#include <iostream>
using namespace std;
int main ()
{long int num;
int indiv,ten,hundred,thousand,ten_thousand,place;
/*分别代表个位,十位,百位,千位,万位和位数*/
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<<endl;
return 0;
}
int add(int x,int y)
{int z;
z=x+y;
return(z);
cout<<"place="<<place<<endl;
//计算各位数字
ten_thousand=num/10000;
thousand=(int)(num-ten_thousand*10000)/1000;
hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;
}
else // x≥10
{y=3*x-11;
cout<<"x="<<x<<", y=3*x-11="<<y;
}
cout<<endl;
return 0;
}
3-11、#include <iostream>
using namespace std;
int main ()
{float score;
char grade;
return 0;
}
2-4、#include <iostream>
using namespace std;
int main()
{char c1='C',c2='+',c3='+';
cout<<"I say: \""<<c1<<c2<<c3<<'\"';
cout<<"\t\t"<<"He says: \"C++ is veryinteresting!\""<< '\n';
return 0;
}
2-8、#include <iostream>
using namespace std;
int main()
{char c1='C', c2='h', c3='i', c4='n', c5='a';
c1+=4;
c2+=4;
c3+=4;
c4+=4;
c5+=4;
cout<<"password is:"<<c1<<c2<<c3<<c4<<c5<<endl;
break;
cpp格式存在现暂时为大家转换第一章答案如果有需要的请通过邮件联系我我将其余章节答案发给大家
第一章
1-5、#include <iostream>
using namespace std;
int main()
{
cout<<"This"<<"is";
cout<<"a"<<"C++";
cout<<"program.";
return 0;
}
1-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;
}
1-7#include <iostream>
using namespace std;
int main ( )
{int x,y;
cout<<"enter x:";
cin>>x;
if (x<1)
{y=x;
cout<<"x="<<x<<", y=x="<<y;
}
else if (x<10) // 1≤x<10
{y=2*x-1;
cout<<"x="<<x<<", y=2*x-1="<<y;
}
第二章
2-3、#include <iostream>
using namespace std;
int main()
{char c1='a',c2='b',c3='c',c4='\101',c5='\116';
cout<<c1<<c2<<c3<<'\n';
cout<<"\t\b"<<c4<<'\t'<<c5<<'\n';
return 0;
}
2-7、#include <iostream>
using namespace std;
int main()
{int i,j,m,n;
i=8;
j=10;
m=++i+j++;
n=(++i)+(++j)+m;
cout<<i<<'\t'<<j<<'\t'<<m<<'\t'<<n<<endl;
case 9: grade='A';break;
case 8: grade='B';break;
case 7: grade='C';break;
case 6: grade='D';break;
default:grade='E';
}
cout<<"score is "<<score<<", grade is "<<grade<<endl;
cin>>r>>h;
l=2*pi*r;
s=r*r*pi;
sq=4*pi*r*r;
vq=3.0/4.0*pi*r*r*r;
vz=pi*r*r*h;
cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)
<<setprecision(2);
cout<<"l= "<<setw(10)<<l<<endl;
cout<<"enter an integer(0~99999):";
cin>>num;
if (num>9999)
place=5;
else if (num>999)
place=4;
else if (num>99)
place=3;