zoukankan      html  css  js  c++  java
  • c++第十一天

    《c++ primer, 5E》

    第68页到第81页,笔记:

    1、读取未知量的string对象示例

    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    #include<string>
    using std::string;
    
        int main()
        {
            string word;
            while(cin >> word)
                cout << word << endl;
            return 0;
        }

    2、使用getline读取一行示例

        #include<iostream>
        using std::cin;
        using std::cout;
        using std::endl;
        #include<string>
        using std::string;
    
                    int main()
                    {
                            string line;
                            while(getline(cin , line))
                                        cout << line << endl;
                            return 0;
                    }

    3、string_A.empty()、string_A.size()

    4、string_A.size()返回值是一个无符号的整数,不要在表达式中与有符号的int型混用

    5、string比较大小按照字典序,大小写敏感!

    6、字符串字面值与string是不同的类型。不能把字面值直接相加,必须确保每个加法运算 + 两侧的运算对象至少有一个是string

    遇到的问题:

    课后练习:

    练习3.1

    1

    #include<iostream>
    using std::cout;
    using std::endl;
    int main()
    {
        int sum = 0;
        for(int val = 50; val <= 100; ++val)
            sum += val;
        cout << sum << endl;
        return 0;
    }

    2

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

    3

    #include<iostream>
    using std::cout;
    using std::endl;
    using std::cin;
    int main()
    {
        int a, b;
        cin >> a >> b;
        for(int val = a; val <= b; ++val)
            cout << val << endl;
        return 0;
    }

    4

    #include<iostream>
    #include<string>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    struct Sales_data{
        string bookNo;
        unsigned units_sold = 0;
        double price = 0.0;
        double revenue = 0.0;
    };
    int main()
    {
        Sales_data data;
        // std::cin >> data;
        cin >> data.bookNo >> data.units_sold >> data.price;
        // std::cout << data;
        cout << data.bookNo << " " << data.units_sold << " " << data.price << endl;
        return 0;
    }

    5

    #include<iostream>
    #include<string>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    struct Sales_data{
        string bookNo;
        unsigned units_sold = 0;
        double price = 0.0;
        double revenue = 0.0;
    };
    int main()
    {
        Sales_data data1, data2;
        // std::cin >> data1 >> data2;
        cin >> data1.bookNo >> data1.units_sold >> data1.price;
        cin >> data2.bookNo >> data2.units_sold >> data2.price;
        // std::cout << data1 + data2;
        cout << data1.bookNo << " " << data1.units_sold + data2.units_sold << " " << data1.price << endl;
        return 0;
    }

    6

    #include<iostream>
    #include<string>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    struct Sales_data{
        string bookNo;
        unsigned units_sold = 0;
        double price = 0.0;
        double revenue = 0.0;
    };
    int main()
    {
        Sales_data data, sum_data;
        while(cin >> data.bookNo >> data.units_sold >> data.price)
            sum_data.units_sold += data.units_sold;
        cout << data.bookNo << " " << sum_data.units_sold << " " << data.price << endl;
        return 0;
    }

    7

    #include<iostream>
    #include<string>
    using std::string;
    using std::cout;
    using std::cin;
    using std::endl;
    struct Sales_data{
        string bookNo;
        unsigned units_sold = 0;
        double price = 0.0;
        double revenue = 0.0;
    };
    int main()
    {
        int num = 0;
        string last_bookNo;
        Sales_data temp_data;
        // 读取第一个
        // std::cin >> temp_data;
        cin >> temp_data.bookNo >> temp_data.units_sold >> temp_data.price;
        last_bookNo = temp_data.bookNo;
        num = 1;
        // while(cin >> temp_data){
        while(cin >> temp_data.bookNo >> temp_data.units_sold >> temp_data.price){
            if(temp_data.bookNo == last_bookNo){
                ++num;
            }
            else{
                cout << last_bookNo << " " << num;
                last_bookNo = temp_data.bookNo;
                num = 1;
            }
        }
        cout << last_bookNo << " " << num;
        return 0;
    }

    8

    #include<iostream>
    #include<string>
    using std::cerr;
    using std::string;
    using std::cin;
    using std::cout;
    using std::endl;
    struct Sales_data{
        string bookNo;
        unsigned units_sold = 0;
        double price = 0.0;
        double revenue = 0.0;
    };
    int main()
    {
        Sales_data total;
        if(cin >> total.bookNo >> total.units_sold >> total.price){
            Sales_data trans;
            // while(cin >> total)
            while(cin >> trans.bookNo >> trans.units_sold >> trans.price){
                if(total.bookNo == trans.bookNo){
                    total.units_sold += trans.units_sold;
                }else{
                    // std::cout << total << std::endl;
                    cout << total.bookNo << " " << total.units_sold << " " << total.price << endl;
                    // total = trans;
                    total = trans;
                }
            }
            cout << total.bookNo << " " << total.units_sold << " " << total.price << endl;
        }else{
            cerr << "No data?" << endl;
            return -1;
        }
        return 0;
    }

    练习3.2

    1

        #include<iostream>
        using std::cin;
        using std::cout;
        using std::endl;
        #include<string>
        using std::string;
    
                    int main()
                    {
                            string line;
                            while(getline(cin , line))
                                        cout << line << endl;
                            return 0;
                    }

    2

    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    #include<string>
    using std::string;
    
        int main()
        {
            string word;
            while(cin >> word)
                cout << word << endl;
            return 0;
        }

    练习3.3

    string类的输入运算符将空白字符作为分隔符,

    string类的getline函数将空白符作为字符读入字符串。

    练习3.4

    1

    #include<iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    #include<string>
    using std::string;
    int main()
    {
        string s1, s2;
        cin >> s1 >> s2;
        if(s1 == s2)
            cout << "Equal" << endl;
        else if(s1 > s2)
            cout << s1 << endl;
        else
            cout << s2 << endl;
        return 0;
    }

    2

    #include<iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    #include<string>
    using std::string;
    int main()
    {
        string s1, s2;
        cin >> s1 >> s2;
        if(s1.size() == s2.size())
            cout << "Equal" << endl;
        else if(s1.size() > s2.size())
            cout << s1 << endl;
        else
            cout << s2 << endl;
        return 0;
    }

    练习 3.5

    1

    #include<iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    #include<string>
    using std::string;
    int main()
    {
        string sum_string;
        string temp_string;
        while(cin >> temp_string)
            sum_string += temp_string;
        cout << sum_string << endl;
        return 0;
    }

    2

    #include<iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    #include<string>
    using std::string;
    int main()
    {
        string sum_string;
        string temp_string;
        while(cin >> temp_string)
            sum_string += temp_string + " ";
        cout << sum_string << endl;
        return 0;
    }
  • 相关阅读:
    使用Powershell开机启动隐藏窗口的程序
    使用鼠标左键事件实现VR中的Eye Gaze Input
    在github网站上更新fork的repo
    零Web知识个性化Blog
    C#中的Attribute
    ConsoleWindow中的双击日志定位
    Hackintosh Issues 10.13.x
    开启macOS的原生写入Ntfs的功能
    Install macOS High Sierra on Any Supported Intel-based PC
    DSDT/SSDT
  • 原文地址:https://www.cnblogs.com/xkxf/p/6372707.html
Copyright © 2011-2022 走看看