C++Primer 学习/习题记录——第一章(郎不器)

近期开始系统学习C++了,目的是为之后学习SLAM打好基础。教材是C++Primer第四版。主要是为了记录自己做习题的感想和理解。
  1. 习题1.3
    最经典的Hello World作为开端。
// 编写程序在标准输出上打印 Hello World。
#include <iostream>  //引用标准输入输出流库
int main()
{
    std::cout <<  "Hello, World !"  << std::endl;
    return 0;
}
  1. 习题1.4
    乘法操作,直接输出。
//编写程序,使用乘法操作符“*”来产生两个数的积。
#include <iostream>
int main()
{
    std::cout <<  "Enter the two numbers : "  << std::endl;
    int v1, v2;
    std::cin >> v1 >> v2;
    std::cout << "The product of " << v1 << " and " << v2 << " is " << v1*v2 << std::endl;
    return 0;
}
  1. 习题1.5
// 重写习题1.4的程序,用单独的语句打印每一个操作数。
#include <iostream>
int main()
{
    std::cout <<  "Enter the two numbers : "  << std::endl;
    int v1, v2;
    std::cin >> v1;
    std::cin >> v2;
    std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1*v2 << std::endl;
    return 0;
}
  1. 习题1.9
// 解释下列代码,for循环。
#include <iostream>
int main()
{
    int sum = 0;
    for(int i=-100; i<=100; ++i)  // 不要忘记分号。
    {
        sum += i;
    }
    std::cout << sum << std::endl;
    return 0;
}
  1. 习题1.10
// 用for循环编程,求从50到100的所有自然数的和.
#include <iostream>
int main()
{
    int sum = 0;
    for(int i=50; i<=100; ++i)
    {
        sum += i;
    }
    std::cout << sum << std::endl;
    return 0;
}
// 用while循环编程,求从50到100的所有自然数的和.
#include <iostream>
int main()
{
    int sum = 0;
    int i=50;
    while( i<=100)
    {
        sum += i;
         ++i;
    }
    std::cout << sum << std::endl;
    return 0;
}
  1. 习题1.11
// 用while循环变成,输出10-0递减的自然数
#include <iostream>
int main()
{
    int sum = 0;
    int i=10;
    while( i>=0)
    {
        std::cout << i << std::endl;
        --i;
    }
    return 0;
}
  1. 习题1.16
// 编写程序,输出用户输入的两个数中的较大者。
#include <iostream>
int main()
{
    std::cout << "Enter two numbers, I will give the bigger back" << std::endl;
    int a,b;
    std::cin >> a >> b ;
    if (a>=b)
    {
        std::cout << "the bigger one is "<< a << std::endl;
    } else
    {
        std::cout << "the bigger one is "<< b << std::endl;
    }
    return 0;
}
  1. 习题1.17
// 编写程序,要求用户输入一组数,输出信息说明其中有多少个负数。
#include <iostream>
int main()
{
    int amount =0, value;
    while (std::cin >> value)
    {
        if (value <= 0)
        {
            ++amount;
        }
    }
    std::cout << "Amount of all negative values read is " << amount << std::endl;
    return 0;
}
  1. 习题1.21
// 编写程序,循环遍历一组书的销售交易,读入每笔交易并将交易写入标准输出。
#include <iostream>
#include "Sales_item.h"
int main()
{
    Sales_item book;
    std::cout << "Enter transactions: " << std::endl; // 提醒输入数据
    while (std::cin >> book)  // 循环持续读入书的信息,直到碰到文件终止符,Ubuntu中是Ctrl+D
    {
        std::cout << "ISBN, number of copies sold, total revenue, and average price are" << std::endl;
        std::cout << book << std::endl; 
    }
    return 0;
}
  1. 习题1.22
// 编写程序,读入两个具有相同ISBN的Sales_item对象并产生他们的和。
#include <iostream>
#include "Sales_item.h"
int main()
{
    Sales_item book1, book2, book;
    std::cout << "Enter the transaction: " << std::endl;
    std::cin >> book1;
    std::cout << "Enter another transaction: " << std::endl;
    std::cin >> book2;

    book = book1 + book2; // 求和

    std::cout << "ISBN, number of copies sold, total revenue, and average price are" << std::endl;
    std::cout << book << std::endl; 
    return 0;
}
  1. 习题1.23
// 编写程序,读入几个具有相同ISBN的交易,输出所有读入交易的和。
#include <iostream>
#include "Sales_item.h"
int main()
{
    Sales_item book1,book;
    std::cout << "Enter the transaction: " << std::endl;
    while (std::cin >> book1)
    {
        book = book + book1;
    }
    std::cout << "ISBN, number of copies sold, total revenue, and average price are" << std::endl;
    std::cout << book << std::endl; 
    return 0;
}
  1. 习题1.24
/*
编写程序,读入几笔不同的交易。
对于每笔新读入的交易,要确定它的ISBN是否和以前的交易的ISBN一样。
记录下每一个ISBN交易的总数。
通过给定多笔不同的交易来测试程序。
这些交易必须代表多个不同的ISBN,但每个ISBN的记录应分在同一组。
*/
#include <iostream>
#include "Sales_item.h"
int main()
{
    Sales_item trans1, trans2;
    int amount;
    std::cout << "Enter transactions: " << std::endl;
    std::cin >> trans1; // 读取第一个
    amount  = 1;
    while (std::cin >> trans2) // 循环读取之后每一个
    {
        if (trans1.same_isbn(trans2)) // 看与上一次读到的是否为同一本书
        {
            ++amount;
        }
        else // 若读到了新书
        {
            std::cout << "Transaction amount of previous ISBN: " << amount << std::endl;
            trans1 = trans2;
            amount = 1;
        }        
    }
    std::cout << "Transaction amount of the last ISBN: " << amount << std::endl; // 输出最后一本书
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值