c++primer4 Chapter1
Exercise1.17
#include <iostream>
int main()
{
int limit = 0, k = 0, value = 0;
std::cout << "How many integers would you like to enter?";
std::cin >> limit;
// assume we don‘t know what is EOF(End-Of-File).
while (std::cin >> value && (--limit != 0))
if (value<0) {
k++;
}
std::cout << "the number of negative values is " << k<<std::endl;
return 0;
}
Exercise1.18
#include <iostream>
using namespace std;
int main()
{
cout<<"Enter two numbers:";
int v1,v2;
cin>>v1>>v2;
int lower,upper;
if (v1>=v2) {
upper=v1;lower=v2;
}
else { upper=v2;lower=v1;
}
cout<<"Value of "<<lower<<" to "<<upper<<" inclusive are: "<<endl;
for (int value=lower;value<=upper;value++)
cout<<value<<" ";
return 0;
}
Exercise1.19
#include <iostream>
using namespace std;
int main()
{
cout<<"Enter two numbers:";
int v1,v2;
cin>>v1>>v2;
int lower,upper;
if (v1>=v2) {
upper=v1;lower=v2;
}
else { upper=v2;lower=v1;
}
cout<<"Value of "<<lower<<" to "<<upper<<" inclusive are: "<<endl;
for (int value=lower,count=1;value<=upper;value++,count++) {
cout<<value<<" ";
if (count%10==0)
cout<<endl;
}
return 0;
}
添加的变量count是为了记录已输出数的个数,若count的值是10的整数倍,则输出一个换行符。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。