C++ 入门学习(练习+代码)—— 01


一、实验目的

1.熟悉C++程序的集成开发环境;

2.学习新建和打开控制台应用程序的方法;

3.掌握控制计算机输入与输出基本方法。

二、实验任务

1. 某公司销售人员的工资是由基本工资和销售提成两部分组成,基本工资是2000元,销售货款的18%为提成。编程实现:输入员工的销售货款,计算并输出员工的工资。(提示: C++中%为取余功能,所以18%的计算使用0.18。)

输入输出格式参见样张:

示例代码:

 1 //计算输出员工的工资 
 2 #include
 3 #include<string>
 4 #include
 5 using namespace std;
 6 int main()
 7 {   
 8     double a,s;
 9     cout<<"工资计算方法:工资=2000+销售货款*18%"<<endl;
10     cout<<"请输入销售货款:";
11     cin>>a; 
12     s=2000+a*0.18;
13     cout<<"工资为:"<""<<endl;
14     return 0; 
15  } 
16   

2. 编程实现:输入一个摄氏温度,转换成华氏温度并输出。(提示:摄氏温度=(华氏温度-32)×5/9)。输入输出格式参见样张:

 示例代码:

 1 //摄氏度转换为华氏度 
 2 #include
 3 #include<string>
 4 #include
 5 using namespace std;
 6 int main()
 7 {
 8     double t,h;
 9     cout<<"请输入摄氏温度;"; 
10     cin>>t;
11     h=(9*t)/5+32;
12     cout<<"华氏温度为:"<endl;
13     return 0; 
14 }

3. 编程实现:从键盘输入一个3位整数,计算并输出它的逆序数以及“逆序数乘以2的结果”。例如,输入258,由258分离出其百位2、十位5、个位8,然后计算8*100+5*10+2=852,852*2=1704,并输出。输入输出格式参见样张:

 示例代码:

 1 //计算并输出它的逆序数以及“逆序数乘以2的结果
 2 #include
 3 #include<string>
 4 #include
 5 using namespace std;
 6 int main()
 7 {
 8     int a,b,c,d,t,s;
 9     cout<<"请输入一个三位数:";
10     cin>>d;
11     a=d/100;
12     t=d%100;
13     b=t/10;
14     c=t%10;
15     s=100*c+10*b+a;
16     cout<"的百位是:"<endl;
17     cout<"的十位是:"<endl;
18     cout<"的个位是:"<endl;
19     cout<"的逆序数是:"<endl;
20     cout<"的逆序数乘以2是:"<2<<endl;
21     return 0;
22 }

4. 编程实现:求从键盘输入的三个整数的平均数并输出。(要求:只允许定义三个整型变量,不能再定义其他变量)(提示:当两个整型数据相除时,结果会取整)。

输入输出格式参见样张:

 示例代码:

//三个整数的平均数并输出
#include
#include<string>
#include
using namespace std;
int main()
{
    int a,b,c;
    cout<<"请输入三个整数:";
    cin>>a>>b>>c;
    cout<<"平均数是:"<<(a+b+c)/3<<"."<<(((a+b+c)*100000)/3)%100000<<endl;
    return 0; 
 } 

5.编程实现:屏幕输出一头威武雄壮的雄狮。

             ,%%%%%%

           ,%%/\%%%%/\%

          ,%%%\c "" J/%%%

 %.       %%%%/ o  o \%%%

 `%%.     %%%%    _  |%%

  `%%     `%%%%(__Y__)%

  //       ;%%%%`\-/%%%'

 ((       /  `%%%%%%%'

  \\    .'          |

   \\  /       \  | |

    \\/         ) | |

     \         /_ | |__

     (___________)))))))

提示:

1)要求使用英文符号。

2)请注意有英文符号反斜杠\ 、单引号’ 、双引号” 的地方是否正确输出。

3)英文符号\ 、’ 、”无法直接输出,必须使用\\、 \’ 、\”。

注意:示例代码可能有小错误,建议运行后自行比对,且示例只是用cout实现的,当然可以使用printf。

示例代码:

 1 //屏幕输出一头威武雄壮的雄狮
 2 #include
 3 #include<string>
 4 #include
 5 using namespace std;
 6 int main()
 7 {
 8     cout<<"             ,%%%%%%" <<endl;
 9     cout<<"           ,%%/\\%%%%/\\%"<<endl;
10     cout<<"          ,%%%\\c \"\" J/%%%"<<endl;
11     cout<<"%.       %%%%/ o  o \\%%%"<<endl;
12     cout<<"`%%.     %%%%    _  |%%"<<endl;
13     cout<<"  `%%     `%%%%(__Y__)%"<<endl;
14     cout<<"  //       ;%%%%`\\-/%%%'"<<endl;
15     cout<<" ((       /  `%%%%%%%\'"<<endl;
16     cout<<"  \\\\    .\'          |"<<endl;
17     cout<<"   \\\\  /       \\  | |"<<endl;
18     cout<<"    \\\\/         ) | |"<<endl;
19     cout<<"     \\         /_ | |__"<<endl;
20     cout<<"     (___________)))))))"<<endl;
21     return 0;
22  }