C++primer 第四版部分习题的解答

来源:互联网 发布:初中英语背单词软件 编辑:程序博客网 时间:2024/06/05 05:16
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");}
0 0
原创粉丝点击