C++大学基础编程第二章部分题目答案

本答案是自己写的,如有错误,欢迎指正!

2.16

编写一个程序,要求用户输入两个数,获取用户输入的数,并打印这两个数的和,乘积,差和商。

/*计算两个数的和,积,差,商*/

 

#include

using namespace std;

int main()

{

       int x, y;

 

       cout<<"请输入x的值:";

       cin>>x;

       cout<<"\n请输入y的值:";

       cin>>y;

 

       cout<<"x+y="<

       cout<<"x*y="<

       cout<<"x-y="<

       cout<<"x/y="<

 

       return 0;

}

2.17

/*

在同一行打印数14,并且用一个空格分隔每一对相邻的数。

请用多种方式实现:

a)使用一条语句,包含1个流插入运算符;

b)使用一条语句,包含4个流插入运算符;

c)使用四条语句*/

 

#include

using namespace std;

int main()

{

       cout<<"a):12 3 4";

       cout<

       cout<<"b):1"

              <<"2"

              <<"3"

              <<"4";

       cout<

       cout<<"c):1";

       cout<<"2";

       cout<<"3";

       cout<<"4";

       cout<

 

       return0;

}


C:\Users\lenovo\Desktop\C++大学基础教程答案\2.doc

/*

用户输入两个整数,获取用户输入的数,然后打印出较大数,后面跟随“is large”。

如果这两个数相等,则打印信息“these numbers are equal

*/

 

#include

using namespace std;

int main()

{

       intx, y;

       cout<<"pleaseinput the value of x:";

       cin>>x;

       cout<<"\npleaseinput the value of y:";

       cin>>y;

       cout<

       if(x > y)

              cout<

       elseif(x < y)

              cout<

       else

              cout<<"thesenumbers are equal!";

 

       return0;

}

2.19

/*

从键盘输入3个整数,并打印他们的和、平均值、乘积、最小值和最大值。

*/

 

#include

using namespace std;

 

int main()

{

       floatx, y, z, a, b;

       cout<<"inputthree different integers:";

       cin>>x>>y>>z;

       cout<<"\nsumis "<

       cout<<"Averageis"<<(x+y+z)/3<

       cout<<"Productis"<

       if(x> y)

       {

              a= x;

              b= y;

       }

       else

       {

              a= y;

              b= x;

       }

 

       if(a< z)

              a= z;

       if(b> z)

              b= z;

       cout<<"smallestis"<

       cout<<"largestis"<

 

       return0;

}

2.20

/*

读入一个圆的半径,并打印圆的直径,周长和面积。π的值取常量3.14159.在输出语句中执行这些计算。

*/

 

#include

using namespace std;

#define PI 3.14159

int main()

{

       intr;

       cout<<"请输入圆的半径:";

       cin>>r;

       cout<<"圆的周长是:"<<2*PI*r<

       cout<<"圆的面积是:"<

 

 

       return0;

}

 

2.21

/*

编写一个程序,打印矩形,圆形,箭头,菱形

*/

 

#include

using namespace std;

#define PI 3.14159

int main()

{

       inti, j;

       cout<<"打印空心矩形:"<

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

       {

              j= 9;

              if( i == 0 || i == 9)

              {

                     while(j-- )

                            cout<<"*";

              }

              else

              {

                     cout<<"*";

                     while( j-- > 2)

                            cout<<"";

                     cout<<"*";

              }

              cout<

       }

       cout<<"\n打印空心菱形:"<

       //先打印上半部分

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

       {

              for( j = 1; j <= 9 - i; j++ )

              {

                     cout<<"";

              }

              cout<<"*";

              for(j = 1; j <= 2 * i - 2 ; j++ )

              {

                     cout<<"";

              }

              if(i!= 1)

                     cout<<"*";

              cout<

       }

       //打印后半部分

       for( i = 4; i > 0; i--)

       {

              for( j = 1; j <= 9 - i; j++)

                     cout<<"";

              cout<<"*";

              for( j = 1; j <= 2 * i - 2; j++)

                     cout<<"";

              if(i!= 1)

                     cout<<"*";

              cout<

       }

 

       cout<<"打印箭头:\n";

       for(i = 1; i < 4; i++ )

       {

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

                     cout<<"";

              for(j = 1; j <= 2 * i - 1; j++ )

                     cout<<"*";

              cout<

       }

       i= 0;

       while(i++ < 6 )

       {

              cout<<"    *"<

       }

       intr;

       cout<<"\n打印圆形:"<

       cout<<"请输入要打印的半径:";

       cin>>r;

       for(i = 0; i < 2 * r; ++i )

       {

              for(j = 0; j < 2 * r; ++j )

              {

                     if((i - r) * (i - r) + (j - r) * (j - r) <= r * r )

                            cout<<"*";

                     else

                            cout<<"";

              }

              cout<

       }

       for(i = 0; i < 2 * r; ++i )

       {

              for(j = 0; j < 2 * r; ++j )

              {

                     if((i - r) * (i - r) + (j - r) * (j - r) <= r * r )

                            cout<<"";

                     else

                            cout<<"*";

              }

              cout<

       }

 

       return0;

}

 

 


你可能感兴趣的:(C++学习笔记)