在我自学C++过程中,我选择了C++Primer这本书,并对部分代码习题进行了求解以及运行结果。接下来几个月我将为大家定时按章节更新习题答案与运行结果:
C++ Primer (第五版)习题答案总目录:https://blog.csdn.net/weixin_48467622/article/details/136418131?spm=1001.2014.3001.5501
目录
1.10 除了++运算符将运算对象的值增加1之外,还有一个递减运算符(–)实现将值减少1.编写程序,使用递减运算符在循环中按递减顺序打印出10到0之间的整数.
1.11编写程序,提示用户输入两个整数,打印出这两个整数所指定的范围内的所有整数.
1.12下面的for循环完成了什么功能?sum的终值是多少?
1.18编译并运行本节的程序,给它输入全都相等的值.再次运行程序,输入没有重复的值
1.19修改你为1.4.1节练习1.10(第11页)所编写的程序(打印一个范围内的数),使其能处理用户输入的第一个数比第二个数小的情况.
1.21编写程序,读取两个ISBN相同的Sales_item对象,输出它们的和.
1.22编写程序,读取多个具有相同ISBN的销售记录,输出所有记录的和.
1.23编写程序,读取多条销售记录,并统计每个ISBN(每本书)有几条销售记录.
1.24输入表示多个ISBN的多条销售记录来测试上一个程序,每个ISBN的记录应该聚在一起
1.25借助网站上的Sales_item.h头文件,编译并运行本节给出的书店程序.
- #include
- int main()
-
- {
- int n1=0, n2=50;
- while (n2 < 101)
- {
- n1 = n1 + n2;
- n2++;
- }
- std::cout << "The sum of 50 to 100 is " << n1 << std::endl;
- return 0;
- }

- #include
- int main()
-
- {
- int n1=10;
- while (n1 >= 0)
- {
- std::cout << n1 << std::endl;
- n1--;
- }
- return 0;
- }

- #include
- int main()
-
- {
- int n1 = 0, n2 = 0;
- std::cout << "Please input two numbers:";
- std::cin >> n1 >> n2;
- if (n1 > n2)
- while (n1 >= n2)
- std::cout << n2++ << " ";
- else
- while (n1 <= n2)
- std::cout << n1++ << " ";
- std::cout << std::endl;
- return 0;
- }

- #include
- int main()
-
- {
- int sum = 0;
- for (int i = 0; i <= 100; ++i)
- sum += i;
- std::cout << "The final sum is " << sum << std::endl;
- return 0;
- }

由运行结果可知,for循环完成了0到100内的整数求和,sum终值为5050.
- #include
- int main()
-
- {
- int sum = 0;
- for (int i = 50; i <= 100; ++i)
- sum += i;
- std::cout << "The sum is " << sum << std::endl;
- return 0;
- }

- #include
- int main()
-
- {
- for (int i = 10; i >= 0; i--)
- std::cout << i << " ";
- std::cout << std::endl;
- return 0;
- }

- #include
- int main()
-
- {
- int n1 = 0, n2 = 0;
- std::cout << "Please input two numbers:";
- std::cin >> n1 >> n2;
- if (n1 > n2)
- for(;n2 <= n1; n2++)
- std::cout << n2 << " ";
- else
- for(;n1 <= n2; n1++)
- std::cout << n1<< " ";
- std::cout << std::endl;
- return 0;
- }

- #include
- int main()
-
- {
- int n1 = 0, n2 = 0;
- std::cout << "请输入一些数字求和:";
- while (std::cin >> n2)
- n1 = n1 + n2;
- std::cout << "The sum is " << n1 << std::endl;
- return 0;
- }

- #include
- int main()
-
- {
- int n1 = 0, n2 = 0;
- if (std::cin >> n1)
- {
- int cnt = 1;
- while (std::cin >> n2)
- {
- if (n1 == n2)
- cnt++;
- else
- {
- std::cout << n1 << " occurs " << cnt << " times" << std::endl;
- n1 = n2;
- cnt = 1;
- }
- }
- std::cout << n1 << " occurs " << cnt << " times" << std::endl;
- }
- return 0;
- }


- #include
- int main()
- {
- std::cout << "请输入俩个数(第一个数比第二个数小):" << std::endl;
- int v1 = 0, v2 = 0;
- std::cin >> v1 >> v2;
- if (v1 < v2)
- {
- while (v1 <= v2)
- {
- std::cout << v1++ << " ";
- }
- }
- else
- {
- std::cout << "输入有误." << std::endl;
- }
- std::cout << std::endl;
- return 0;
- }

下载地址https://download.csdn.net/download/weixin_48467622/88350222?spm=1001.2014.3001.5501
- #include<iostream>
- #include"Sales_item.h"
- int main()
- {
- Sales_item s;
- while (std::cin >> s)
- std::cout << s << std::endl;
- return 0;
- }

- #include
- #include"Sales_item.h"
- int main()
- {
- Sales_item s1,s2;
- std::cout << "请输入ISBN相同的Sales_item对象:" << std::endl;
- std::cin >> s1 >> s2;
- if (compareIsbn(s1, s2))
- std::cout << std::endl << "汇总信息:ISBN 售出本书 销售额 和 平均售价为:" << std::endl << s1 + s2 << std::endl;
- else
- std::cout << "您输入的ISBN值不同!" << std::endl;
- return 0;
- }

- #include
- #include"Sales_item.h"
- int main()
- {
- Sales_item s,total;
- std::cout << "请输入ISBN相同的Sales_item对象:" << std::endl;
- if (std::cin >> total)
- {
- while (std::cin >> s)
- {
- if (compareIsbn(s, total))
- total = total + s;
- else
- {
- std::cout << "输入ISBN与历史数据不同!" << std::endl;
- return -1;
- }
- }
- }
- else
- {
- std::cout << "未输入数据!" << std::endl;
- return -1;
- }
- std::cout << std::endl << "汇总信息:ISBN 售出本书 销售额 和 平均售价为:" << std::endl << total << std::endl;
- return 0;
- }

- #include
- #include "Sales_item.h"
- int main()
- {
- Sales_item tran1, tran2;
- int num = 0;
- std::cout << "请输入销售记录:" << std::endl;
- if (std::cin >> tran1)
- {
- num++;
- while (std::cin >> tran2)
- {
- if (tran1.isbn()==tran2.isbn())
- {
- num++;
- }
- else
- {
- std::cout << std::endl << "ISBN:" << tran1.isbn() <<
- "的销售记录有" << num << "条" << std::endl;
- tran1 = tran2;
- num = 1;
- }
- }
- std::cout << std::endl << "ISBN:" << tran1.isbn() << "的销售记录有" << num << "条" << std::endl;
- }
- else
- {
- std::cout << "没有输入" << std::endl;
- return -1;
- }
- return 0;
- }

- #include
- #include "Sales_item.h"
- int main()
- {
- Sales_item tran1, tran2;
- int num = 0;
- std::cout << "请输入销售记录:" << std::endl;
- if (std::cin >> tran1)
- {
- num++;
- while (std::cin >> tran2)
- {
- if (tran1.isbn()==tran2.isbn())
- {
- num++;
- }
- else
- {
- std::cout << std::endl << "ISBN:" << tran1.isbn() <<
- "的销售记录有" << num << "条" << std::endl;
- tran1 = tran2;
- num = 1;
- }
- }
- std::cout << std::endl << "ISBN:" << tran1.isbn() << "的销售记录有" << num << "条" << std::endl;
- }
- else
- {
- std::cout << "没有输入" << std::endl;
- return -1;
- }
- return 0;
- }

- #include
- #include "Sales_item.h"
- int main()
- {
- Sales_item total, trans;
- std::cout << "请输入销售记录:" << std::endl;
- if (std::cin >> total)
- {
- while (std::cin >> trans)
- {
- if (total.isbn()==trans.isbn())
- {
- total = total + trans;
- }
- else
- {
- std::cout << total << std::endl;
- total = trans;
- }
- }
- std::cout << total << std::endl;
- }
- else
- {
- std::cerr << "No data!" << std::endl;
- return -1;
- }
- return 0;
- }
