zoukankan      html  css  js  c++  java
  • [C++ Primer Plus] 第2章、开始学习c++

    一、程序清单2.1(代码和书略不一样)

    1 #include<iostream>
    2 using namespace std;//使用std这个命名空间,才能正确找到cin和cout,如果不使用命名空间,那么使用时需要 std::cout<<"hello"<<std::endl;
    3 void main()
    4 {
    5     cout<<"hello world"<<endl;//endl表示重起一行,信息从“hello”流向cout
    6     cout<<"hey man!"<<endl;
    7     //getchar();
    8     cin.get();//按下enter键结束
    9 }

    二、程序清单2.3

     1 #include<iostream>
     2 using namespace std;
     3 void main()
     4 {
     5     int carrots;
     6     cout<<"你有多少胡萝卜?"<<endl;
     7     cin>>carrots;//输入,信息从cin流向carrots
     8     cout<<"这里还有两个。";
     9     carrots=carrots+2;
    10     cout<<"现在你有"<<carrots<<"个胡萝卜。"<<endl;
    11     cin.get();//按下enter键结束
    12     cin.get();//  两条cin.get()  语句可以替换成一条语句    system("pause");
    13 }

    注意:只有一条  cin.get()   语句的话,在屏幕输入数字后就会一闪而过,只有两条  cin.get() 才能在屏幕看到输出

    三、程序清单2.4(调用函数)

     1 #include<iostream>
     2 #include<cmath>//or math.h
     3 using namespace std;
     4 void main()
     5 {
     6     double area,side;
     7     cout<<"你家房子多少平米:"<<endl;
     8     cin>>area;
     9     side=sqrt(area);//开平方根
    10     cout<<"这相当于"<<side<<"米的正方形"<<endl;
    11     system("pause");//暂停,使屏幕处于等待状态
    12 }

    四、程序清单2.5(用户自定义无返回函数)

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void simon(int n);//函数声明,simon函数在main函数之后,若不提前声明则无法在main函数中使用
     5 
     6 void main()
     7 {
     8     int n;
     9     simon(3);
    10     cout<<"请输入一个正整数:";
    11     cin>>n;
    12     simon(n);//此处的n为实际参数(简称“实参”)
    13     system("pause");
    14 }
    15 
    16 void simon(int n){//此处的n为形式参数(简称“形参”)
    17     cout<<"Simon says touch your toes "<<n<<" times"<<endl;
    18 }

    五、程序清单2.6(用户自定义有返回函数)

     1 #include<iostream>
     2 using namespace std;
     3 
     4 int simon(int n);
     5 
     6 void main()
     7 {
     8     int n;
     9     cout<<"请输入青蛙数量:";
    10     cin>>n;
    11     cout<<"共有"<<simon(n)<<"只腿"<<endl;
    12     system("pause");
    13 }
    14 
    15 int simon(int n){
    16     return 4*n;
    17 }

    六、章节后的编程练习

    1.显示姓名和地址

    1 #include<iostream>
    2 using namespace std;
    3 
    4 void main()
    5 {
    6     cout<<"XXX"<<endl;
    7     cout<<"xxx university"<<endl;
    8     cin.get();
    9 }

    2.用户输入一个以 long 为单位的距离,然后将它转换为码(一long等于 220 码)。

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void main()
     5 {
     6     long l;
     7     cout<<"请输入距离(long):";
     8     cin>>l;
     9     cout<<l*220<<""<<endl;
    10     system("pause");
    11 }

    3.使用 3 个用户定义的函数(包括 m a iri()), 并生成下面的输出:

    Three blind mice
    Three blind mice
    See how they run
    See how they run

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void mice(){
     5     cout<<"Three blind mice"<<endl;
     6 }
     7 void run(){
     8     cout<<"See how they run"<<endl;
     9 }
    10 
    11 void main()
    12 {
    13     mice();
    14     mice();
    15     run();
    16     run();
    17     system("pause");
    18 }

    4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:

    Enter your age : 29

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void main()
     5 {
     6     int age;
     7     cout<<"Enter your age :";
     8     cin>>age;
     9     cout<<age*12<<endl;
    10     system("pause");
    11 }

    5.编写一个程序,其 中 的 m ain() 调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华

    氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
    Please enter a Celsius value : 20
    20 degrees Celsius is 68 degrees Fahrenheit.
    下面是转换公式:

    华氏温度 = 1.8 X 摄氏温度十 32.0

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void main()
     5 {
     6     double Celsius;
     7     cout<<"Please enter a Celsius value:";
     8     cin>>Celsius;
     9     cout<<Celsius<<" degrees Celsius is "<<Celsius*1.8+32<<" degrees Fahrenheit."<<endl;
    10     system("pause");
    11 }

    6 . 编写一个程序,其 main() 调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。
    该程序按下面的格式要求用户输入光年值,并显示结果:
    Enter the number of light years : 4.2
    4.2 light years = 265608 astronomical units .
    天文单位是从地球到太阳的平均距离(约 150000000 公里或 93000000 英里),光年是光一年走的距离
    (约 10 万亿公里或 6 万亿英里)(除太阳外,最近的恒星大约离地球 4 .2 光年)。请使用 double 类 型 (参见
    程序淸单 2 .4 ) , 转换公式为:
    1 光年 =63240 天文单位

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void main()
     5 {
     6     double light;
     7     cout<<"Enter the number of light years:";
     8     cin>>light;
     9     cout<<light<<" light years="<<light*63240<<" astronomical units."<<endl;
    10     system("pause");
    11 }

    7 . 编写一个程序,要求用户输入小时数和分钟数。在 main() 函数中,将这两个值传递给一个 void 函
    数,后者以下面这样的格式显示这两个值:
    Enter the number of hours : 9
    Enter the number of m inutes : 28
    Time : 9:28

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void show(int hours,int minutes){
     5     cout<<hours<<":"<<minutes<<endl;
     6 }
     7 
     8 void main()
     9 {
    10     int hours,minutes;
    11     cout<<"Enter the number of hours:";
    12     cin>>hours;
    13     cout<<"Enter the number of minutes:";
    14     cin>>minutes;
    15     show(hours,minutes);
    16     system("pause");
    17 }
  • 相关阅读:
    1386. 安排电影院座位
    1123. 最深叶节点的最近公共祖先
    375. 猜数字大小 II
    未能在system.drawing中找到bitmap
    856. 括号的分数
    140. 单词拆分 II
    5909. 并行课程 III
    typora + ImgHosting (一款不会存储图片的图床实现)
    IDEA插件:search with bing、search with baidu
    多吉搜索美化
  • 原文地址:https://www.cnblogs.com/little-monkey/p/7196571.html
Copyright © 2011-2022 走看看