教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

C++ Primer第一章 快速入门课后习题代码(2)

来源:网络收集 时间:2026-08-23
导读: {std::cout std::cout system(\ return 0; } #include #include #include \int main() { Sales_item book; //读入ISBN,售出书的本数,销售价格 std::cout >book) { //输出ISBN,售出书的本数,总收入,平均价格 st

{std::cout<

std::cout<

system(\ return 0; }

#include #include

#include \int main() {

Sales_item book;

//读入ISBN,售出书的本数,销售价格 std::cout<<\ while(std::cin>>book) {

//输出ISBN,售出书的本数,总收入,平均价格

std::cout<<\of copies sold,\revenue,and average price:\ <

std::cout<

system(\ return 0; }

//习题-1.22

#include #include\int main() {

Sales_item trans1,trans2; //读入交易

std::cout<<\ std::cin>>trans1>>trans2; if(trans1.same_isbn(trans2))

std::cout<<\ <<\

<<\ < std::cout<<\ <

//习题-1.23

#include #include\int main() {

Sales_item total,trans; if(std::cin>>total) {

while(std::cin>>trans)

if(total.same_isbn(trans)) total=total+trans; else{

std::cout<<\ return -1; }

std::cout<<\ <<\

<<\ <

std::cout<<\ return -1; }

system(\ return 0; }

//习题-1.24

#include #include\int main() {

Sales_item trans1,trans2; int amount;

std::cout<<\ std::cin>>trans1; amount=1;

while(std::cin>>trans2)

if(trans1.same_isbn(trans2))//ISBN相同 ++amount; else{//ISBN不同

std::cout<<\ <

std::cout<<\ <

//习题-1.25

#include #include \

int main() {

// declare variables to hold running sum and data for the next record

Sales_item total, trans;

// is there data to process? if (std::cin >> total) {

// if so, read the transaction records while (std::cin >> trans)

if (total.same_isbn(trans))

// match: update the running total total = total + trans; else {

// no match: print & assign to total std::cout << total << std::endl; total = trans; }

// remember to print last record std::cout << total << std::endl; } else {

// no input!, warn the user

std::cout << \ return -1; // indicate failure }

return 0; }

/* 习题1.21~1.25要用到的头文件*/ #ifndef SALESITEM_H #define SALESITEM_H

// Definition of Sales_item class and related functions goes here

#include #include

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

C++ Primer第一章 快速入门课后习题代码(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/592031.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)