C++ Primer第一章习题答案
- 练习1.3
#include <iostream> int main() { std::cout << "Hello World" << std::endl; system( "Pause" ); return 0; }
- 练习1.4
#include <iostream> int main() { int v1, v2; std::cout << "Enter two numbers:" << std::endl; std::cin >> v1 >> v2; std::cout << "The product of "<<v1<<" and "<<v2<<" is " << v1*v2<<std::endl; system( "Pause" ); return 0; }
- 练习1.9
#include <iostream> int main() { int sum=0, i=50; while( i<=100 ) { sum += i; ++i; } std::cout << "Sum of 50 to 100 inclusive is " << sum<<std::endl; system( "Pause" ); return 0; }
- 练习1.10
#include <iostream> int main() { int i=10; std::cout << "The descending order from 10 to 0 is "; while( i>=0 ) { std::cout << i<<" " ; --i; } std::cout << std::endl; system( "Pause" ); return 0; }
- 练习1.11
#include <iostream> int main() { int temp=0,v1,v2; std::cout << "Enter two numbers:" ; std::cin >> v1 >> v2; if( v1>v2 ) { temp = v2; v2 = v1; v1 = temp; } std::cout <<v1 << " to " << v2 <<" included " ; ++v1; while(v1<v2) { std::cout << v1<<" "; ++v1; } std::cout << std::endl; system( "Pause" ); return 0; }
- 练习1.12
#include <iostream> int main() { int sum = 0; for( int i = -100; i <=100; ++i ) { sum += i; } std::cout <<"Sum is "<<sum<<std::endl; system( "Pause" ); return 0; }
Sum is 0
- 练习1.13
(50-100的和)
#include <iostream> int main() { int sum = 0; for( int i = 50; i <= 100; ++i ) { sum += i; } std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl; system( "Pause" ); return 0; }
(递减打出10-0)
#include <iostream> int main() { std::cout << "The descending order from 10 to 0 is "; for( int i = 10; i >0; --i ) { std::cout << i << " "; } std::cout << std::endl; system( "Pause" ); return 0; }
(输入2个整数,打印出范围内所有整数)
#include <iostream> int main() { int temp = 0, v1, v2; std::cout << "Enter two numbers:"; std::cin >> v1 >> v2; if( v1>v2 ) { temp = v2; v2 = v1; v1 = temp; } std::cout << v1 << " to " << v2 << " included ";for( ++v1; v1 < v2; v1++ ) { std::cout << v1 << " "; } std::cout << std::endl; system( "Pause" ); return 0; }
- 练习1.14
while 循环适合循环次数不确定的情景;for 循环适合循环次数事先已知。
- 练习1.16
#include <iostream> int main() { int sum = 0, value = 0; while (std::cin >> value) { sum += value; } std::cout << "Sum is: " << sum << std::endl; return 0; }
- 练习1.20
Sales_item.h
/* * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the * copyright and warranty notices given in that book: * * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." * * * "The authors and publisher have taken care in the preparation of this book, * but make no expressed or implied warranty of any kind and assume no * responsibility for errors or omissions. No liability is assumed for * incidental or consequential damages in connection with or arising out of the * use of the information or programs contained herein." * * Permission is granted for this code to be used for educational purposes in * association with the book, given proper citation if and when posted or * reproduced.Any commercial use of this code requires the explicit written * permission of the publisher, Addison-Wesley Professional, a division of * Pearson Education, Inc. Send your request for permission, stating clearly * what code you would like to use, and in what specific way, to the following * address: * * Pearson Education, Inc. * Rights and Permissions Department * One Lake Street * Upper Saddle River, NJ 07458 * Fax: (201) 236-3290 */ /* This file defines the Sales_item class used in chapter 1. * The code used in this file will be explained in * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators) * Readers shouldn't try to understand the code in this file * until they have read those chapters. */ #ifndef SALESITEM_H // we're here only if SALESITEM_H has not yet been defined #define SALESITEM_H // Definition of Sales_item class and related functions goes here #include <iostream> #include <string> class Sales_item { // these declarations are explained section 7.2.1, p. 270 // and in chapter 14, pages 557, 558, 561 friend std::istream& operator >> ( std::istream&, Sales_item& ); friend std::ostream& operator<<( std::ostream&, const Sales_item& ); friend bool operator<( const Sales_item&, const Sales_item& ); friend bool operator==( const Sales_item&, const Sales_item& ); public: // constructors are explained in section 7.1.4, pages 262 - 265 // default constructor needed to initialize members of built-in type Sales_item() : units_sold( 0 ), revenue( 0.0 ) {} Sales_item( const std::string &book ) : bookNo( book ), units_sold( 0 ), revenue( 0.0 ) {} Sales_item( std::istream &is ) { is >> *this; } public: // operations on Sales_item objects // member binary operator: left-hand operand bound to implicit this pointer Sales_item& operator+=( const Sales_item& ); // operations on Sales_item objects std::string isbn() const { return bookNo; } double avg_price() const; // private members as before private: std::string bookNo; // implicitly initialized to the empty string unsigned units_sold; double revenue; }; // used in chapter 10 inline bool compareIsbn( const Sales_item &lhs, const Sales_item &rhs ) { return lhs.isbn() == rhs.isbn(); } // 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.isbn() == rhs.isbn(); } inline bool operator!=( const Sales_item &lhs, const Sales_item &rhs ) { return !( lhs == rhs ); // != defined in terms of operator== } // assumes that both objects refer to the same ISBN 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 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 } std::istream& operator >> ( std::istream& in, Sales_item& s ) { double price; in >> s.bookNo >> 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; } std::ostream& operator<<( std::ostream& out, const Sales_item& s ) { out << s.isbn() << " " << s.units_sold << " " << s.revenue << " " << s.avg_price(); return out; } double Sales_item::avg_price() const { if( units_sold ) return revenue / units_sold; else return 0; } #endif
main.cpp
#include <iostream> #include "Sales_item.h" int main() { Sales_item book; //读入ISBN号、售出的册数以及销售价格 std::cin >> book; //写入ISBN、售出的册数、总销售额和平均价格 std::cout << book << std::endl; system( "Pause" ); return 0; }
- 练习1.21
(Sales_item.h同1.20,代码相同)
main.cpp
#include <iostream> #include "Sales_item.h" int main() { Sales_item book1,book2; //读入ISBN号、售出的册数以及销售价格 std::cin >> book1>>book2; //写入ISBN、售出的册数、总销售额和平均价格 std::cout << book1+book2 << std::endl; system( "Pause" ); return 0; }
- 练习1.22
(Sales_item.h同1.20,代码相同)
main.cpp
#include <iostream> #include "Sales_item.h" int main() { Sales_item book,sum_book; std::cin >> sum_book; while( std::cin >> book ) { sum_book += book; } std::cout <<sum_book << std::endl; system( "Pause" ); return 0; }
- 练习1.23
(Sales_item.h同1.20,代码相同)
main.cpp
#include <iostream> #include "Sales_item.h" int main() { //currItem作为参照值,valItem后输入值 Sales_item currItem,valItem; //输入值后作为参照值 if (std::cin>>currItem) { int cnt=1; //循环获取后续值 while (std::cin>>valItem) { if (currItem.isbn()==valItem.isbn()) { ++cnt; }else{ //这里需要先输出,后赋值,否则会影响输出结果 std::cout << currItem.isbn() << " occurs " << cnt << " times " << std::endl; currItem=valItem; cnt=1; } } std::cout << currItem.isbn() << " occurs " << cnt << " times " << std::endl; } return 0; }
- 练习1.25
(Sales_item.h同1.20,代码相同)
main.cpp
#include <iostream> #include "Sales_item.h" int main() { //有交易记录的变量 Sales_item total; if( std::cin >> total ) { //记录和的变量 Sales_item trans; //循环获取后续值 while( std::cin >> trans ) { if( total.isbn() == trans.isbn() ) { //更新总销售额 total += trans; } else { //打印前一本书的结果 std::cout << total << std::endl; total = trans; } } std::cout << total << std::endl; } else { //没有输入警告 std::cerr << "No date?" << std::endl; return -1; } system( "Pause" ); return 0; }
第一章完结啦!
分类:
C++
, C++ / C++ Primer习题答案
标签:
C++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix