C++primer习题--第1章

简介:

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

#include <iostream>
using namespace std;
int main()
{
    cout<<"Hello, World\n";
    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 源文件:

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;
}
目录
相关文章
|
6月前
|
算法 测试技术 C++
C++二分算法习题:判断是否是完全平方数[容易]和排列箱子[容易]
C++二分算法习题:判断是否是完全平方数[容易]和排列箱子[容易]
|
3月前
|
C++
来自C++ Primer 5的函数指针的定义,调用等
来自C++ Primer 5的函数指针的定义,调用等
14 0
|
5月前
|
编译器 Linux C语言
C++Primer 【学习笔记】第一章 深思
main 函数在很多方面都比较特别,其中最重要的是每个 0+程序必须含有 main 函数,且 main 函数是(唯一)被操作系统显式调用的函数。
33 0
|
6月前
|
存储 编译器 程序员
C++ Primer Plus 第6版 读书笔记(10) 第十章 类与对象
C++ Primer Plus 第6版 读书笔记(10) 第十章 类与对象
37 0
|
6月前
|
存储 关系型数据库 编译器
C++ Primer Plus 第6版 读书笔记(9)第 9章 函数——内存模型和名称空间
C++ Primer Plus 第6版 读书笔记(9)第 9章 函数——内存模型和名称空间
61 1
|
6月前
|
存储 算法 编译器
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(二)
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(二)
32 1
|
6月前
|
存储 Java 编译器
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(一)
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(一)
23 0
|
7月前
|
存储 算法 前端开发
C++Primer 第一章 开始
C++Primer 第一章 开始
61 0
|
8月前
|
存储 编译器 C++
【C++ Primer Plus】基础知识 2
【C++ Primer Plus】基础知识
62 0
|
8月前
|
编译器 C++
【C++ Primer Plus】基础知识 1
【C++ Primer Plus】基础知识
96 0
【C++ Primer Plus】基础知识 1