美文网首页
第一章~编程练习解答

第一章~编程练习解答

作者: 掌灬纹 | 来源:发表于2019-09-25 19:35 被阅读0次

    第一章的友好度还是相当滴
    习题解答

    1.2习题

    1.2认为最大的收获

    注意
    // Exercise 1.3 Output Hello, World。
    #include<iostream>
    int main(){
        std::cout<< "Hello,World。" << std::endl;
        return 0;
    }
     
    // Exercise 1.4 Input two numbers by using the char of "*" to print product
    #include<iostream>
    int main(){
        int v1 = 0, v2 = 0;
        std::cin >> v1 >> v2;
        std::cout << v1 * v2 << std::endl;
        return 0;
    }
    
    // 将样例输出语句分离单个语句中 
    // std::out<<"The sum of: "<<v1 + v2<<" end!"<<std::endl
    #include<iostream>
    int main(){
        int v1 = 5, v2 = 3;
        std::cout << "The sum of: ";
        std::cout << v1 + v2;
        std::cout << " end!";
        std::cout << std::endl;
        return 0;
    }
    

    1.4习题~(循环与不定量数据读入)

    // use "while" sum 50 + 51 + 52 .... + 100 print reult
    #include<iostream>
    int main(){
        int val = 50, sum = 0;
        while(val <= 100){
            sum += val;
            val++;
        }
        std::cout << sum;
        return 0;
    }
    
    // use "while" print 10 9 8 ... 0
    #include<iostream>
    int main(){
        int val = 10;
        while(val >= 0){
            std::cout << val << " ";
            val--;
        }
        std::cout << std::endl; 
        return 0;
    }
    
    // 编写程序输入两个整数 打印这两个整数范围内的所有整数 
    #include<iostream>
    int main(){
        int left = 0, right = 0;
        std::cin >> left >> right;
        std::cout << "[left, right] includes: ";
        while(left <= right){
            std::cout << left << " ";
            left++;
        }
        std::cout << std::endl;
        return 0;
    } 
    
     // use "for" sum of 50~100 
     #include<iostream>
     int main(){
        int sum = 0;
        for(int i = 50; i <= 100; i++){
            sum += i; 
        }
        std::cout << "Sum of : " << sum << std::endl; 
        return 0;
     }
    
    // print 10 to 0 use "for" and "--"
    #include<iostream>
    int main(){
        for(int i = 10; i >= 0; --i){
            std::cout << i << " ";
        }
        std::cout << std::endl;
        return 0;
    } 
    
    // input two numbers [a, b] print a to b all numbers
    #include<iostream>
    int main(){
        int a = 0, b = 0;
        std::cin >> a >> b;
        for(int i = a; i <= b; i++){
            std::cout << i << " ";
        }
        std::cout << std::endl;
        return 0;
    }
    
    // 从cin 读取一组数 求和 不定量数据
    #include<iostream>
    int main(){
        int val = 0, sum = 0;
        while(std::cin >> val){
            sum += val;
        }
        std::cout << "sum of " << sum << std::endl;
        return 0;
    } 
    

    最后~简单的C++项目 书店程序

    收获:类的初次了解,c++项目的创建与操作 以下要点(附书中头文件源码)

    注意

    由于项目的设计,以下将练习封装为函数以便区分

    #include"Sales_item.h"
    void readAndPrint();
    void testAdd();
    void sumOfSales();
    void staSales();
    void completeRoutine();
    
    int main(void) {
        //readAndPrint();
        //testAdd();
        //sumOfSales();
        //staSales();
        completeRoutine();
        return 0;
    }
    
    // p20 1.5.1_1 读取一组图书销售记录 并且将记录打印在标准输出
    void readAndPrint(){
        Sales_item book;
        std::cin >> book;
        std::cout << book << std::endl; 
    }
    
    // 1.5.1_2 读取两个ISBN(书号)相同的Sales_item 对象, 输出和
    void testAdd(){
        Sales_item item1, item2;
        std::cout << "输入两个相同ISBN的书籍信息 (书号 售出本数 单价)" << std::endl;
        std::cin >> item1 >> item2;
        std::cout << item1 + item2 << std::endl;
    }
    
    // 1.5.1_3 读取具有多个连续相同ISBN的记录 输出所有相同记录的和
    void sumOfSales(){
        std::cout << "输入多个相同ISBN的书籍信息 (书号 售出本数 单价)" << std::endl;
        Sales_item item, sum;
        while(std::cin >> item){// ctrl+z 录入结束 
            sum += item;
        }
        std::cout << "sum of " << sum << std::endl;
        
    }
    
    // p21 1.5.2_1 读取多条销售记录 并统计每个ISBN(即每本书)有几条记录
    void staSales(){
        std::cout << "输入多条书籍销售记录 (书号 售出本数 单价)" << std::endl;
        Sales_item curItem, nextItem;
        if(std::cin >> curItem){
            int cnt = 1; // 记录条数
            while(std::cin >> nextItem){
                if(nextItem.isbn == curItem.isbn){
                    cnt++;
                }else{
                    std::cout << curItem.isbn << " has saled " << cnt << " books"<<std::endl; 
                    curItem = nextItem;
                    cnt = 1; // 初始化计数 
                }
            }
            // 打印最后一条记录 
            std::cout << curItem.isbn << " has saled " << cnt << " books"<<std::endl;  
        }
    }
        
    // 书店程序 
    // 假定读入的销售记录书号相同的已经聚在一起 输出每本书的最终销售记录
    void completeRoutine(){
        std::cout << "输入多条书籍销售记录 (书号 售出本数 单价)" << std::endl;
        Sales_item curItem, nextItem;
        if(std::cin >> curItem){
            while(std::cin >> nextItem){
                if(nextItem.isbn == curItem.isbn){
                    curItem += nextItem;// 销售信息累加 
                }else{
                    std::cout << "销售记录:" << curItem << std::endl;
                    curItem = nextItem;// 更新为下一本数的销售情况 
                }
            }
            // 最后一本书的记录 
            std::cout << "销售记录:" << curItem << std::endl;
            
        }else{
            std::cout << "无效的书籍信息!" << std::endl;
        }
        
    } 
    
    

    以上引用头文件源码

    #ifndef SALESITEM_H
    #define SALESITEM_H
    #include <iostream>
    #include <string>
    
    class Sales_item{
    public:
        Sales_item(const std::string &book):isbn(book),units_sold(0),revenue(0.0){}
        Sales_item(std::istream &is){ is >> *this;}
        friend std::istream& operator>>(std::istream &,Sales_item &);
        friend std::ostream& operator<<(std::ostream &,const Sales_item &);
    public:
        Sales_item & operator+=(const Sales_item&);
    public:
        double avg_price() const;
        bool same_isbn(const Sales_item &rhs)const{
        return isbn == rhs.isbn;
        }
        Sales_item():units_sold(0),revenue(0.0){}
    public:
        std::string isbn;
        unsigned units_sold;
        double revenue;
    };
    
    using std::istream;
    using std::ostream;
    Sales_item operator+(const Sales_item &,const Sales_item &);
    
    inline bool operator==(const Sales_item &lhs,const Sales_item &rhs){
        return lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue && lhs.same_isbn(rhs);
    }
    
    inline bool operator!=(const Sales_item &lhs,const Sales_item &rhs){
        return !(lhs == rhs);
    }
    
    inline Sales_item & Sales_item::operator +=(const Sales_item &rhs){
        units_sold += rhs.units_sold;
        revenue += rhs.revenue;
        return *this;
    }
    
    inline Sales_item operator+(const Sales_item &lhs,const Sales_item &rhs){
        Sales_item ret(lhs);
        ret += rhs;
        return ret;
    }
    
    inline istream& operator>>(istream &in,Sales_item &s){
        double price;
        in >> s.isbn >> s.units_sold >> price;
        if(in)
            s.revenue = s.units_sold * price;
        else
            s = Sales_item();
        return in;
    }
    
    inline ostream& operator<<(ostream &out,const Sales_item &s){
        out << s.isbn << "\t" <<s.units_sold << "\t" << s.revenue << "\t" << s.avg_price();
        return out;
    }
    
    inline double Sales_item::avg_price() const{
        if(units_sold)
            return revenue/units_sold;
        else
            return 0;
    }
    #endif
    

    相关文章

      网友评论

          本文标题:第一章~编程练习解答

          本文链接:https://www.haomeiwen.com/subject/sgubyctx.html