C++ primer第五版习题 第三章

本文档详细解答了C++ Primer第五版第三章的多项习题,涉及内容包括using声明、输入输出处理、string类操作、循环与迭代器的使用、vector对象的管理和操作、字符串处理、数组及指针操作等C++基础知识。
摘要由CSDN通过智能技术生成

3.1 使用恰当的using 声明重做 1.4.1节和2.6.2节的练习

1.4.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 of 50 to 100 inclusive is "
              << sum << endl;
    
    return 0;
}
#include <iostream>
using std::cout;
using std::endl;
int main(){
   
    for(int val = 10; val >= 0; --val){
   
        cout << "val = " << val << endl;
    }
    return 0;
}
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main(){
   
    cout << "Enter two number: " << endl;
    int v = 0, v1 = 0;
    std::cin >> v >> v1;
    
    if(v <= v1){
   
        for(; v <= v1; ++v){
   
            cout << v << endl;
        }
    }
    else{
   
        for(; v1 <= v; ++v1){
   
            cout << v1 << endl;
        }
    }
}

2.6.2

#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
struct Sale_Data
{
   
    string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
};

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

int main(){
   
    Sales_Data book, book2;
    double price;
    cin >> book.bookNo >> book.units_sold >> price >> endl;
    cin >> book2.bookNo >> book2.units_sold >> price >> endl;
    if(book.bookNo == book2.bookNo){
   
        cout << book.units_sold + book2.units_sold << endl;
    }else{
   
        cerr << "输入有误" << endl;
    }
    
    return 0;
}
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
struct Sales_Data{
   
    string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
};

int main(){
   
    Sales_Data total_book;
    double total_price;
    if(cin >> total_book.bookNo >> total_book.units_sold >> total_price){
   
//        int cnt = 1;
        total_book.revenue = total_book.units_sold * total_price;
        Sales_Data book;
        double book_price;
        while(cin >> book.bookNo >> book.units_sold >> book_price){
   
            book.revenue = book.units_sold * book_price;
            if(total_book.bookNo == book.bookNo){
   
                total_book.units_sold += book.units_sold;
                total_book.revenue += book.revenue;
//                ++cnt;
            }else{
   
                cout << total_book.bookNo << " " << total_book.units_sold << " " << total_book.revenue << total_price << endl;
//                std::cout << total_book.bookNo << "has " << cnt << "recordings!";
                total_book.bookNo = book.bookNo;
                total_price = book_price;
                total_book.units_sold = book.units_sold;
                total_book.revenue = book.revenue;
            }
            
        }
        cout << total_book.bookNo << " " << total_book.units_sold << " " << total_book.revenue << total_price << endl;
//        std::cout << total_book.bookNo << "has " << cnt << "recordings!";
    }
    else{
   
        cout << " No Data" << endl;
        return -1;
    }
    return 0;
}
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
struct Sales_Data{
   
    string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
};

int main(){
   
    Sales_Data total_book;
    double total_price;
    if(cin >> total_book.bookNo >> total_book.units_sold >> total_price){
   
        int cnt = 1;
        total_book.revenue = total_book.units_sold * total_price;
        Sales_Data book;
        double book_price;
        while(cin >> book.bookNo >> book.units_sold >> book_price){
   
            book.revenue = book.units_sold * book_price;
            if(total_book.bookNo == book.bookNo){
   
//                total_book.units_sold += book.units_sold;
//                total_book.revenue += book.revenue;
                ++cnt;
            }else{
   
                cout << total_book.bookNo << "has " << cnt << "recordings!" << endl;
                total_book.bookNo = book.bookNo;
                total_price = book_price;
                total_book.units_sold = book.units_sold;
                total_book.revenue = book.revenue;
            }
           
        }
         cout << total_book.bookNo << "has " << cnt << "recordings!" << endl;
    }
    return 0;
}

3.2 编写一段程序从标准输入中一次读入一行,然后修改该程序使其一次读入一个词

一次一行

int main(
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值