本博客主要记录C++ Primer(第五版)中的练习题答案与解析。 参考:C++ Primer C++ Primer
使用while循环将50到100的整数相加
测试:
#include <iostream> int main(){ int num = 50; int sum = 0; while (num <= 100) { /* code */ std::cout<<"num:"<<num<<std::endl; sum += num; ++num; } return 0; }使用递减(–)运算符在循环中按顺序递减打印出10到0之间的整数。
#include <iostream> int main(){ int num = 10; while (num >= 0) { std::cout<<"num:"<<num<<std::endl; --num; } return 0; }提示用户输入两个整数,打印出这两个整数所指定的范围内所有整数。
测试:
#include <iostream> using std::cout; using std::cin; void print_range(int lo, int hi) { if (lo > hi) { print_range(hi, lo); return; } for (int i = lo; i != hi; ++i) cout << i << " "; } int main() { int low = 0, high = 0; cout << "please input two integers:\n"; cin >> low >> high; print_range(low, high); return 0; }输出:
please input two integers: 5 15 5 6 7 8 9 10 11 12 13 14下面for循环完成什么功能?sum终值是多少?
#include <iostream> int main() { int sum = 0; for(int i = -100; i <= 100; ++i) sum += i; std::cout<<"sum:"<<sum; return 0; }输出:sum:0 从-100加到100.
for循环实现求1到10这10个数之和
#include <iostream> int main() { int sum = 0; int num = 1; for(int i = num; i <= 10; ++i) sum += i; std::cout<<"sum:"<<sum; return 0; }对比for循环和while循环,两种形式的优缺点各是什么?
在for循环中,循环控制变量的初始化和修改都放在语句头部分,形式较简洁,且特别适用于循环次数已知的情况。 在while循环中,循环控制变量的初始化一般放在while语句之前,循环控制变量的修改一般放在循环体中,形式上不如for语句简洁,但它比较适用于循环次数不易预知的情况(用某一条件控制循环)。 两种形式各有优点,但它们在功能上是等价的,可以相互转换。
参考:https://blog.csdn.net/u012317833/article/details/23946303
从cin读取一组数,输出其和
#include <iostream> using std::cin; using std::cout; using std::endl; int main() { int sum = 0; for (int i = 0; cin >> i; sum += i); cout << "Sum is: " << sum << endl; return 0; }输出:
5 1 3 4 2^Z Sum is: 15注意:从键盘输入文件结束符
windows系统,Ctrl + Z,然后按enter 或者 return。UNIX系统,或者Mac OS X系统中,Ctrl + D。根据Sales_item.h ,编写一组数的销售记录,将每条记录打印到标准输出上。
Sales_item.h
#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() = default; Sales_item(const std::string &book): bookNo(book) { } 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 = 0; // explicitly initialized double revenue = 0.0; }; // 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测试代码:
#include <iostream> #include "include/Sales_item.h" using std::cin; using std::cout; using std::endl; int main() { for (Sales_item item; cin >> item; cout << item << endl); return 0; }输出:
0-2010-201-78345-x 3 20.00 0-2010-201-78345-x 3 60 20 0-2010-201-78000-x 10 55.5 0-2010-201-78000-x 10 555 55.5读取两个IBSN相同的Sales_item对象,输出它们的和。
测试:
#include <iostream> #include "include/Sales_item.h" using std::cin; using std::cout; using std::endl; using std::cerr; int main() { Sales_item item1, item2; cin >> item1 >> item2; if (item1.isbn() == item2.isbn()) cout << item1 + item2 << endl; else cerr << "Different ISBN." << endl; }输出:
0-2010-201-78345-x 3 20.00 0-2010-201-78345-x 5 20.00 0-2010-201-78345-x 8 160 20 0-2010-201-78345-x 3 20.00 0-2010-201-78000-x 10 55.5 Different ISBN.读取多个具有相同的IBSN的销售记录,输出所有记录的和。
#include <iostream> #include "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;//total表示下一本书的销售额 } } std::cout << total << std::endl;//打印最后一本书的结果 } else { std::cerr << "No data?!" << std::endl; return -1; } return 0; }测试:
0-2010-201-78345-x 3 20 0-2010-201-78345-x 6 20 0-2010-201-78000-x 10 55.5 0-2010-201-78345-x 9 180 20读取多个销售记录,并统计每个ISBN(每本书)有几条销售记录。
#include <iostream> #include "include/Sales_item.h" int main() { Sales_item currItem, valItem; if (std::cin >> currItem) { int cnt = 1; while (std::cin >> valItem) { if (valItem.isbn() == currItem.isbn()) { ++cnt; } else { std::cout << currItem << " occurs " << cnt << " times " << std::endl; currItem = valItem; cnt = 1; } } std::cout << currItem << " occurs "<< cnt << " times " << std::endl; } return 0; }测试:
0-2010-201-78345-x 3 20 0-2010-201-78345-x 6 20 0-2010-201-78000-x 10 55.5 0-2010-201-78345-x 3 60 20 occurs 2 times ^Z 0-2010-201-78000-x 10 555 55.5 occurs 1 times