zoukankan      html  css  js  c++  java
  • c++primer(第五版) 阅读笔记_第1章

    一、结构图:

     二、代码及练习题

    1.1节 编写一个简单的C++程序

    1.1节练习

    练习1.1:

    查阅使用的编译器的文档:我使用的是vs2013,其文档为https://docs.microsoft.com/zh-cn/previous-versions/visualstudio/visual-studio-2013/60k1461a(v=vs.120)

    编译运行第2页程序:

    #include <iostream>
    using namespace std;
    int main()
    {
        return 0;
    }

    返回值为 0 (0x0)

    练习1.2:

    改写程序,让它返回-1.

    #include <iostream>
    using namespace std;
    int main()
    {
        return -1;
    }

    返回值为 -1 (0xffffffff)

    0xffffffff ,0x表示十六进制,计算机里用补码表示。-1的补码(32位系统,每四位二进制可以由一位十六进制代替)为ffffffff

    原码、反码、补码,这篇文章讲的不错:https://www.imooc.com/article/16813?block_id=tuijian_wz

    正数的原码、反吗、补码一样。

    以-1(8位)为例,最高位符号位,1为负数、0为正数。

    原码:1000 0001

    反码:1111 1110(反码在原码基础上按位取反,符号位不变)

    补码:1111 1111(补码在反码的基础上加1)

    补码转原码:补码基础上按位取反后加一,符号位在取反时不变,加一时最高位符号位有进位的,进位忽略

    1.2 初识输入输出

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << "Enter two numbers:" << endl;
        int v1 = 0, v2 = 0;
        cin >> v1 >> v2;
        cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << endl;
        return 0;
    }

    1.2节练习

    练习1.3:

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << "Hello Word" << endl;
        return 0;
    }

    练习1.4:

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << "Enter two numbers:" << endl;
        int v1 = 0, v2 = 0;
        cin >> v1 >> v2;
        cout << "The product of " << v1 << " and " << v2 << " is " << v1 * v2 << endl;
        return 0;
    }

    练习1.5:

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << "Enter two numbers:" << endl;
        int v1 = 0, v2 = 0;
        cin >> v1 >> v2;
        cout << "The product of ";
        cout << v1 ;
        cout << " and " ;
        cout << v2 ;
        cout << " is ";
        cout<<v1 * v2 << endl;
        return 0;
    }

    练习1.6:

        cout << "The sum of "<< v1 ;
             << " and "<< v2 ;
             << " is "<<v1 + v2 << endl;

    程序不合法,原因是<<运算符接受两个运算对象:左侧的运算对象必须是一个ostream对象,右侧的运算对象是要打印的值。显然第二、三行缺少ostream对象。

    可以修改为:

    cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << endl;

    或者:

        cout << "The sum of "<< v1 ;
        cout << " and "<< v2 ;
        cout << " is "<<v1 + v2 << endl;

    1.3注释简介

    c++有两种注释:单行注释(//)和界定符对注释(/**/)

    #include <iostream>
    using namespace std;
    /*
    * 简单主函数:
    * 读取两个数,求它们的和
    */
    int main()
    {
        //提示用户输入两个数
        cout << "Enter two numbers:" << endl;
        int v1 = 0, v2 = 0; //保存我们读入的输入数据的变量
        cin >> v1 >> v2;    //读取输入数据
        cout << "The sum of "<< v1 << " and "<< v2 
             << " is "<<v1 + v2 << endl;
        return 0;
    }

    注释界定符不能嵌套

    #include <iostream>
    using namespace std;
    /*
    * 注释对/* */ 不能嵌套
    *"不能嵌套"几个字会被认为是源码
    * 像剩余程序一样处理
    */
    int main()
    {
        return 0;
    }
    ///*
    //* 单行注释中的任何内容都会被忽略
    //* 包括嵌套的注释对也一样会被忽略
    //*/

    1.3节练习

    练习1.7,包含不正确嵌套的程序,返回错误信息

     练习1.8

    cout << "/*"; //正确
    cout << "*/"; //正确
    cout << /*"*/"*/; //错误
    cout << /* "*/" /* "/*" */; //正确

    改为:

    cout << /*"*/"*/"; 

    1.4 控制流

    1.4.1 while语句

    #include <iostream>
    using namespace std;
    int main()
    {
        int sum = 0, val = 1;
        //只要val的值小于等于10,while循环就会持续执行
        while(val <=10){
            sum += val; //将sum+val赋予sum
            ++val;   //将val加1
        }
        cout << "Sum of 1 to 10 inclusive is " << sum << endl;
        return 0;
    }

     练习1.9

    #include <iostream>
    using namespace std;
    int main()
    {
        int sum = 0, val = 50;
        //只要val的值小于等于100,while循环就会持续执行
        while(val <=100){
            sum += val; //将sum+val赋予sum
            ++val;   //将val加1
        }
        cout << "Sum of 50 to 100 inclusive is " << sum << endl;
        return 0;
    }

    练习1.10

    #include <iostream>
    using namespace std;
    int main()
    {
        int  val = 10;
        
        while(val >=0){
            
            cout << val << endl;
            --val;
        }
        return 0;
    }

    练习 1.11

    #include <iostream>
    using namespace std;
    void fun(int v1,int v2)
    {
        while (v1 >= v2){
    
            cout << v1<<" ";
            --v1;
        }
        cout << endl;
    }
    int main()
    {
        int  v1,v2;
        cout << "请输入两个整数:" << endl;
        cin >> v1 >> v2;
        if (v1 >= v2) fun(v1, v2);
        else  fun(v2, v1);
        return 0;
    }

     1.4.2 for语句

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int  sum =0;
        //从1加到10
        for (int val = 1; val <= 10; ++val)
            sum += val; //等价于sum =sum+val
        cout << "Sum of 1 to 10 inclusive is " << sum << endl;
        return 0;
    }

     练习1.12

        int  sum =0;
        for (int val = -100; val <= 100; ++val)
            sum += val; 

    for循环完成-100到100的求和,sum终值为0

    练习1.13

    #include <iostream>
    using namespace std;
    int main()
    {
        int sum = 0, val = 50;
        for (int val = 50; val <= 100;++val)
            sum += val; 
        cout << "Sum of 50 to 100 inclusive is " << sum << endl;
        return 0;
    }
    #include <iostream>
    using namespace std;
    int main()
    {
        for (int val = 10; val >= 0;--val)
            cout << val << endl;
        return 0;
    }
    #include <iostream>
    using namespace std;
    void fun(int v1, int v2)
    {
        for (; v1 >= v2; --v1)
            cout << v1 << " ";
        cout << endl;
    }
    int main()
    {
        int  v1, v2;
        cout << "请输入两个整数:" << endl;
        cin >> v1 >> v2;
        if (v1 >= v2) fun(v1, v2);
        else  fun(v2, v1);
        return 0;
    }

     练习1.14 对比for 循环和while循环,两种形式的优缺点各是什么?

    for循环和while循环的优缺点如下: 

    1、在for循环中,循环控制变量的初始化和修改都放在语句头部分,形式较简洁,且特别适用于循环次数已知的情况。

    2、在while循环中,循环控制变量的初始化一般放在while语句之前,循环控制变量的修改一般放在循环体中,形式上不如for语句简洁,但它比较适用于循环次数不易预知的情况(用某一条件控制循环)。

    3、两种形式各有优点,但它们在功能上是等价的,可以相互转换。

     https://blog.csdn.net/sixabs/article/details/82708318

    练习1.15 熟悉编译器生成的错误信息。

    1.4.3 读取数量不定的输入数据

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int sum = 0, value = 0;
        //读取数据直到遇到文件尾,计算所有读入的值的和
        while (cin >> value)
            sum += value; //等价于sum =sum+value
        cout << "Sum is: " <<sum << endl;
        return 0;
    }

    键盘输入文件结尾符:windows中先“Ctrl+Z”,然后再“换行Enter”

    1.4.4  if语句

    #include <iostream>
    using namespace std;
    int main()
    {
        //currVal是我们正在统计的数;我们将读入的新值存入val
        int currVal=0, val=0;
        //读取第一个数,并确保确实有数据可以处理
        if (cin >> currVal){
            int cnt = 1;   //保存我们正在处理的当前值的个数
            while (cin >> val){  //读取剩余的数
                if (val == currVal) //如果值相同
                    ++cnt;  //将cnt加1
                else{      //否则,打印前一个值的个数
                    cout << currVal << " occurs " << cnt << " times" << endl;
                    currVal = val; //记住新值
                    cnt = 1; //重置计数器
                }
            }//while 循环在这里结束
            //记住打印文件中最后一个值的个数
            cout << currVal << " occurs " << cnt << " times " <<endl;
        }//最外层的if语句在这里结束
        return 0;
    }

     1.5 类简介

     1.5.1 Sale_item类

    #include <iostream>
    #include "Sales_item.h"
    using namespace std;
    int main()
    {
        Sales_item book;
        //读入ISBN号、售出的册数以及销售价格
        cin >> book;
        //写入ISBN、售出的册数、总销售额和平均价格
        cout << book << endl;
        return 0;
    }
    #include <iostream>
    #include "Sales_item.h"
    using namespace std;
    int main()
    {
        Sales_item item1,item2;
        cin >> item1>>item2;   //读取一对交易记录
         cout << item1 + item2 << endl;  //打印它们的和
        return 0;
    }

    练习1.22 编写程序,读取多个具有相同ISBN的销售记录,输出所有记录的和。

    #include <iostream>
    #include "Sales_item.h"
    using namespace std;
    int main()
    {
        Sales_item item1, item2;
        while (cin >> item1)
        {
            item2 = item1 + item2;
        }
        
        cout <<  item2 << endl;  //打印它们的和
        return 0;
    }

     1.5.2 初始成员函数

    #include <iostream>
    #include "Sales_item.h"
    using namespace std;
    int main()
    {
        Sales_item item1, item2;
        cin >> item1 >> item2;
        //首先检查item1和item2是否表示相同的书
        if (item1.isbn() == item2.isbn()){
            cout << item1 + item2 << endl;
            return 0;  //表示成功
        }
        else {
            cerr << "Data must refer to same isbn" << endl;
            return -1;
        }
    }

     1.6 书店程序

    #include <iostream>
    #include "Sales_item.h"
    using namespace std;
    int main()
    {
        Sales_item total; //保存下一条交易记录的变量
        //读入第一条交易记录,并确保有数据可以处理
        if (cin >> total){
            Sales_item trans; //保存和的变量
            //读入并处理剩余交易记录
            while (cin >> trans){
                //如果我们仍在处理相同的书
                if (total.isbn() == trans.isbn())
                    total += trans;  //更新总销售额
                else{
                //打印前一本书的结果
                    cout << total << endl;
                    total = trans; //total 现在表示下一本书的销售额
                }
            }
            cout << total << endl; //打印最后一本书的结果
        }
        else {
            //没有输入!警告读者
            cerr << "No data?!" << endl;
            return -1;
        }
        return 0;
    }

      小结

    术语表

  • 相关阅读:
    Open source cryptocurrency exchange
    Salted Password Hashing
    95. Unique Binary Search Trees II
    714. Best Time to Buy and Sell Stock with Transaction Fee
    680. Valid Palindrome II
    Java compiler level does not match the version of the installed Java project facet.
    eclipse自动编译
    Exception in thread "main" java.lang.StackOverflowError(栈溢出)
    博客背景美化——动态雪花飘落
    java九九乘法表
  • 原文地址:https://www.cnblogs.com/a1397240667/p/11678560.html
Copyright © 2011-2022 走看看