您的位置:首页 > 编程语言 > C语言/C++

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");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: