zoukankan      html  css  js  c++  java
  • C++primer习题--第1章

    本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter1-ans.html,转载请注明源地址。

    【习题 1.3】

    编一个程序,在标准输出上打印“Hello, World”。

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

    【习题 1.4】
    我们的程序利用内置加法操作符“+”来产生两个数的和。编写程序,使用乘法操作符“*”产生两个数的积。

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

    【习题 1.5】
    我们的程序使用了一条较长的输出语句。重写程序,使用单独的语句打印每一个操作数。

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

    【习题 1.10】
    用 for 循环编程,求从 50~100 的所有自然数的和。然后用 while 循环重写该程序。
    用for循环:

    #include <iostream>
    using namespace std;
    int main( )
    {
        int i, sum=0;
        for(i=50; i<=100; ++i)
            sum+=i;
        cout<<"sum of 50 to 100 is "<<sum<<endl;
        system("PAUSE");
        return 0;
    }

    用 while 循环:

    #include <iostream>
    using namespace std;
    int main( )
    {
        int i, sum;
        i=50;
        sum=0;
        while(i++<=100)
            sum+=i;
        cout<<"sum of 50 to 100 is "<<sum<<endl;
        system("PAUSE");
        return

    【习题 1.11】
    用 while 循环编程,输出 10~0 递减的自然数。然后用 for循环重写该程序。

    用 while 循环:

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

    用 for循环:

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

    【习题 1.16】
    编写程序,输出用户输入的两个数中的较大者。

    #include <iostream>
    using namespace std;
    int main( )
    {
        int a, b;
        cout<<"请输入两个数: ";
        cin>>a>>b;
        cout<<"较大的一个数是: "<< ((a >= b)? a : b)<<endl;
        system("PAUSE");
        return 0;
    }

    【习题 1.17】
    编写程序,要求用户输入一组数。输出信息说明其中有多少个负数

    #include <iostream>
    using namespace std;
    int main( )
    {
        int num, a;
        num=0;
        while(cin>>a) 
            if(a < 0) num++;
        cout<<"输入的负数数量为: "<<num<<endl;
      return 0; }

    【习题 1.18】
    编写程序,提示用户输入两个数幵将这两个数范围内的每个数写到标准输出。

    #include <iostream>
    using namespace std;
    void print(int a, int b)
    {
        a++;
        while(a < b)
            cout<<a++<<" ";
        cout<<endl;
    }
    int main( )
    {
        int a,b,i;
        cout<<"请输入两个数: ";
        cin>>a>>b;
        if(a < b)
            print(a,b);
        else
            print(b,a);
      return 0; }

    【习题 1.21】
    本书配套网站(http://www.awprofessional.com/cpp_primer)的第1章的代码目录下有 Sales_ item.h 源文件。复制该文件到你的工作目录。编写程序,循环遍历一组书的销售交易, 读入每笔交易幵将交易写至标准输出。

    #include <iostream>
    #include "Sales_item.h"
    using namespace std;
    int main( )
    {
        Sales_item book;
        cout<<"输入交易:"<<endl;
        while(cin>>book) {
            cout<<"售出书的本数、总收入、平均价格:"
                <<endl;
            cout<<book<<endl;
        }
        system("PAUSE");
        return 0;
    }

    附上 Sales_ item.h 源文件:

    #ifndef SALESITEM_H
    #define SALESITEM_H
    
    // Definition of Sales_item class and related functions goes here
    
    
    #include <iostream>
    #include <string>
    
    class Sales_item {
    friend bool operator==(const Sales_item&, const Sales_item&);
    // other members as before
    public:
        // added constructors to initialize from a string or an istream
        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:
        // operations on Sales_item objects
        // member binary operator: left-hand operand bound to implicit this pointer
        Sales_item& operator+=(const Sales_item&);
        // other members as before
        
    public:
        // operations on Sales_item objects
        double avg_price() const;
        bool same_isbn(const Sales_item &rhs) const
            { return isbn == rhs.isbn; }
        // default constructor needed to initialize members of built-in type
        Sales_item(): units_sold(0), revenue(0.0) { }
    // private members as before
    private:
        std::string isbn;
        unsigned units_sold;
        double revenue;
    
    };
    
    
    // nonmember binary operator: must declare a parameter for each operand
    Sales_item operator+(const Sales_item&, const Sales_item&);
    
    inline bool 
    operator==(const Sales_item &lhs, const Sales_item &rhs)
    {
        // must be made a friend of Sales_item
        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); // != defined in terms of operator==
    }
    
    using std::istream; using std::ostream;
    
    // assumes that both objects refer to the same isbn
    inline
    Sales_item& Sales_item::operator+=(const Sales_item& rhs) 
    {
        units_sold += rhs.units_sold; 
        revenue += rhs.revenue; 
        return *this;
    }
    
    // assumes that both objects refer to the same isbn
    inline
    Sales_item 
    operator+(const Sales_item& lhs, const Sales_item& rhs) 
    {
        Sales_item ret(lhs);  // copy lhs into a local object that we'll return
        ret += rhs;           // add in the contents of rhs 
        return ret;           // return ret by value
    }
    
    inline
    istream& 
    operator>>(istream& in, Sales_item& s)
    {
        double price;
        in >> s.isbn >> s.units_sold >> price;
        // check that the inputs succeeded
        if (in)
            s.revenue = s.units_sold * price;
        else 
            s = Sales_item();  // input failed: reset object to default state
        return in;
    }
    
    inline
    ostream& 
    operator<<(ostream& out, const Sales_item& s)
    {
        out << s.isbn << "	" << s.units_sold << "	" 
            << s.revenue << "	" <<  s.avg_price();
        return out;
    }
    
    inline
    double Sales_item::avg_price() const
    {
        if (units_sold) 
            return revenue/units_sold; 
        else 
            return 0;
    }
    #endif
    View Code

    【习题 1.22】
    编写程序,读入两个具有相同 ISBN 的 Sales_item 对象幵产生它们的和。

    #include <iostream>
    #include "Sales_item.h"
    using namespace std;
    int main( )
    {
        Sales_item trans1, trans2;
        cout<<"读入交易:"<<endl;
        cin>>trans1>>trans2;
        if(trans1.same_isbn(trans2))
            cout<<"两笔交易具有相同的ISBN,和为:"<<endl<<trans1 + trans2;
        else
            cout<<"两笔交易没有相同的ISBN";
        return 0;
    }

    【习题 1.23】
    编写程序,读入几个具有相同 ISBN 的交易,输出所有读入交易的和。

    #include <iostream>
    #include "Sales_item.h"
    using namespace std;
    int main( )
    {
        Sales_item sum, trans;
        cout<<"读入交易:"<<endl;
        if(cin>>sum) {
            while(cin>>trans) {
                if(sum.same_isbn(trans))
                    sum += trans;
                else {
                    cout<<"不同的ISBN!"<<endl;
                    return -1;
                }
            }
        } else {
            cout<<"no data!"<<endl;
            return -1;
        }
        return 0;
    }

    【习题 1.24】
    编写程序,读入几笔不同的交易。对于每笔新读入的交易,要确定它的 ISBN 是否和以前的交易的 ISBN 一样,并且记下每一个 ISBN 的交易的总数。

    通过给定多笔不同的交易来测试程序。这些交易必须代表多个不同的 ISBN,但是每个ISBN 的记录应分在同一组。

    #include <iostream>
    #include <map>
    #include <string>
    #include "Sales_item.h"
    using namespace std;
    int main( )
    {
        Sales_item trans;
        cout<<"读入交易:"<<endl;
        cin>>trans;
        map<string, int> count;
        count[trans.getIsbn()]++;
        while(cin>>trans) {
            ++count[trans.getIsbn()];
        }
        map<string, int>::iterator it;
        for(it=count.begin(); it!=count.end(); it++)
            cout<<it->first<<":"<<it->second<<endl;
        system("PAUSE");
        return 0;
    }

     

  • 相关阅读:
    泛型的模板思想
    GTD:是一种态度
    如何debug android cts
    POJ 3352 无向图边双连通分量,缩点,无重边
    Oracle—用户管理的备份(一)
    Retinex processing for automatic image enhancement 翻译
    myBatis抛出异常Result Maps collection already contains value ...
    xxx cannot be resolved to a type 错误解决方法
    Cannot change version of project facet Dynamic Web Module to 3.0
    mysql JDBC URL格式各个参数详解
  • 原文地址:https://www.cnblogs.com/wuyudong/p/cpp-primer-chapter1-ans.html
Copyright © 2011-2022 走看看