C++primer 第四版部分习题的解答
2016-03-02 11:30
841 查看
1.16 #include<iostream> #include<stdio.h> using namespace std; int main(){ int a, b; cout<< "Enter two number "<<endl; cin >> a>> b; if (a > b) cout << "the larger number of a and b is " << a; else cout << "the lager number of a and b is " << b; system("pause"); //return 0; } 1.17 #include<iostream> using namespace std; int main(){ int amount = 0, value; while (cin>>value) if (value <= 0){ amount ++; } system("pause"); return amount; } *** 这里有一个需要注意的地方是文件结束符的使用。 即cin在什么时候终止的问题。当你在命令行输入多个数之后,在命令行输入ctrl+z之后就可以结束输入。接着程序就会处理你所输入的数字并输出结果。*** 1.18 #include<iostream> using namespace std; int main(){ int a, b,s,l,i; cin >> a >> b; if (a >=b) s = b, l = a; else s = a, l = b; for (i = s; i <= l; i++){ cout << i << " "; } system("pause"); } 1.19 #include<iostream> using namespace std; int main(){ int a, b, lower,upper, i; cin >> a >> b; if (a >= b) lower = b, upper = a; else lower = a, upper = b; int amount=0; for (i = lower; i <= upper; i++){ cout << i << " " ; amount++; if (amount % 10 == 0) cout << endl; } system("pause"); } 1.20 #include<iostream> using namespace std; int main(){ int a, b,lower,upper; int i,sum = 0; cin >> a>> b; if (a > b) lower = b, upper = a; else lower = a, upper = b; for (i = lower; i <= upper; i++) { sum += i; } cout << "The sum between a and b is " << sum << endl; system("pause"); }
相关文章推荐
- c++编译 (.obj, .lib, .dll, .exe的关系(附:lib和DLL的区别))
- C/C++——读入一系列由空白分割的(名字,值)对,其中每个名字是由空白分隔开的一个单词,值是一个整数或者一个浮点值, 计算并打印出对应于每个名字的所有值之和与平均值..
- c++ 思考
- c语言:用函数的嵌套调用,找4个整数中最大的数。(递推)
- 1:编写虚幻引擎中的UC和C++代码
- c语言之数据类型长度
- c++ 访问父类方法
- 小型C/C++项目的makefile编写
- C++文件读写详解(ofstream,ifstream,fstream)
- 浅谈C++数组与指针(一维)
- c++的几种典型数据类型
- 卡马克:用C++进行函数式编程
- c++中cout、cerr、clog的区别
- 在C代码中调用C++接口
- C++基础总结(3)--复杂数据类型
- C++基础总结(3)--复杂数据类型
- C++ const wchar_t*转string
- c++ (P49—P68)
- C/C++从文件开头按行读取内容==并无需新建文件,获取前部分内容
- 自主输入数值求和及平均值(数组的应用)