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

C++ Primer Plus 6书后习题 第5章

2018-02-23 00:50 411 查看






5-1
可以用循环做,也可以用数列求和公式算
#include<iostream>
#include<string>
using namespace std;

int main()
{
cout << "enter two number: " << endl;
int a, b;
cin >> a >> b;
int sum = 0;
for (int i = a; i <= b; i++)
{
sum += i;
}
cout << "sum=" << sum << endl;
return 0;
}5-2#include<iostream>
#include<string>
#include<array>
using namespace std;

int main()
{
const int arsize = 16;
array<long double, arsize> a = { 1 };
for (int i = 1; i < a.size(); i++)
{
a[i] = i*a[i - 1];
}
for (int i = 0; i < a.size(); i++)
{
cout << i << "!=" << a[i] << endl;
}
return 0;
}5-3#include<iostream>
#include<string>
#include<array>
using namespace std;

int main()
{
int data, sum = 0;
cout << "请输入一个数字(输入0终止): ";
while (cin >> data && data) //实现while的重复输入,并用data测试0
{
cout << "到目前为止累计和为: " << (sum += data) << endl;
cout << "请输入一个数字(输入0终止): ";
}
return 0;
}5-4#include<iostream>
#include<string>
#include<array>
using namespace std;

int main()
{
double d = 100, c = 100;
int count = 0;
while (c <= d)
{
count++;
d += 10;
c*1.05;
}
cout << count << endl;
return 0;
}5-5#include<iostream>
#include<string>
#include<array>
using namespace std;

int main()
{
string str1 = "请输入第", str2 = "月的销售量: ";
int month = 0, sell[12], sum = 0;
for (; month < 12; month++){
cout << str1 << month +1<< str2;
//cout << month << endl;
cin >> sell[month];
sum += sell[month];
}
cout << "总销售额: " << sum << endl;
return 0;
}5-6#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "请输入第" , str2 = "年第" , str3 = "月的销售量: ";
int year = 1 , month = 1 , sell[4][13] , sum[4] = {0};
for(; year <= 3 ; year ++){
for(month = 1; month <= 12 ; month ++){
cout << str1 << year << str2 << month << str3;
cin >> sell[year][month];
sum[year] += sell[year][month];
}
cout << "第" << year << "年的销售量是: " << sum[year] << endl;
}
cout << "总销售额: " << sum[1] + sum[2] + sum[3] << endl;
return 0;
} 5-7#include<iostream>
#include<string>
#include<array>
using namespace std;

struct car
{
string name;
int year;
};

int main()
{
cout << "How many cars do you wish to catalog? ";
int num;
cin >> num;
cin.get();
car *c = new car[num];
for (int i = 0; i <= num; i++)
{
cout << "Car #" << i << ":" << endl << "Please enter the make: ";
getline(cin, c[i - 1].name);
cout << "Please enter the year made: ";
getline(cin, c[i - 1].name);
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < num; i++)
cout << c[i].year << " " << c[i].name << endl;
return 0;
}5-8#include <iostream>
#include <cstring>
using namespace std;
int main()
{
cout << "Enter words (to stop, type the word done):" << endl;
char ch[20];
int count = 0;
while (cin >> ch && strcmp(ch, "done"))
count++;
cout << count << endl;
return 0;
}5-9#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter words (to stop, type the word done):" << endl;
string str;
int count = 0;
while(cin >> str && str != "done")
count ++;
cout << count << endl;
return 0;
}
5-10#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "enter number of rows";
int rows;
cin >> rows;
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >i; j--)
{
cout << ",";
}
for (int k = 0; k < i; k++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: