C++ primer plus 第五章课后习题

#include <iostream>
#include <array>
#include <string>
#include <cstring>
using namespace std;
void add_integer()//5.1
{
int integer1=0, integer2=0,sum=0;
cout << "Input the smaller integer: ";
cin >> integer1;
cout << "Input the lager integer: ";
cin >> integer2;
for (int i = integer1; i <= integer2; i++)
{
sum = i + sum;
}
cout << "Then get the sum: " << sum << endl;
}
void formore()//5.2
{
const int ArSize = 101;
array<long double, ArSize>factorials;
factorials[0] = factorials[1] = 1;
for (int i = 2; i < ArSize; i++)
factorials[i] = i * factorials[i - 1];
cout << "100!= " << factorials[100] << endl;
}
void input_sum()//5.3
{
int input_number=1,input_sum=0;
while (input_number != 0)
{
cout << "Input the number: ";
cin >> input_number;
input_sum = input_number + input_sum;
if(input_number != 0)
cout << "So far, you get the sum: " << input_sum<<endl;
else cout << "The end!"<<endl;
}
}
void Cleo_and_Daphne()//5.4
{
int Daphne_money = 100, Cleo_money = 100,years=0;
double Daphne_accrual = 0.1, Cleo_accrual = 0.05;
do 
{
Daphne_money = 100 * 0.1 + Daphne_money;
Cleo_money = Cleo_money * 0.05 + Cleo_money;
years++;
} while (Cleo_money < Daphne_money);
cout << years << " years later, Cleo is richer;" << endl;
cout << "Cleo's money: " << Cleo_money << endl;
cout << "Daphne's money: " << Daphne_money << endl;
}
void Book_sell1()//5.5
{
const char * month[12] = { "January","February","March","April","May","June",
"July","August","September","October","November","December" };
int sales_amount[12] = { 0 },sales_sum=0;
for (int i = 0; i < 12; i++)
{
cout << month[i] << ": ";
cin >> sales_amount[i];
sales_sum += sales_amount[i];
}
cout << "This year, our sales are " << sales_sum<<endl;
}
void Book_sell2()//5.6
{
const char * month[12] = { "January","February","March","April","May","June",
"July","August","September","October","November","December" };
int sales_amount[3][12] = { 0 }, sales_sum[3] = { 0 };
const char years[3][20] = { "First_year","Second_year","Third_year" };
for (int j = 0; j < 3; j++)
{
cout << "The " <<years[j]<<" 's sale: ";
for (int i = 0; i < 12; i++)
{
cout << month[i] << ": ";
cin >> sales_amount[j][i];
sales_sum[j] += sales_amount[j][i];
}
}
cout << "The first year, our sales are " << sales_sum[0] << endl;
cout << "The second year, our sales are " << sales_sum[1] << endl;
cout << "The third year, our sales are " << sales_sum[2] << endl;
cout << "Sum: " << sales_sum[0] + sales_sum[1] + sales_sum[2] << endl;
}
void car_collection()//5.7
{
 struct car
 {
string producer;
int produce_year;
 };
 int car_number;
 cout << "How many cars do you wish to catalog? ";
 cin >> car_number;
 car *car_brand = new car[car_number];
 for (int i = 0; i < car_number; i++)
 {
cout << "Car #" << i + 1 << endl;
cout << "Please enter the make: ";
cin.get();//这里由于上一次出现的数字输入会把换行符留在队列,所以需要cin.get()去除换行符
getline(cin,car_brand[i].producer);
cout << "Please enter the year made: ";
cin >> car_brand[i].produce_year;
 }
 cout << "Here is your collection: " << endl;
 for (int i = 0; i < car_number; i++)
 {
cout << car_brand[i].produce_year << " " << car_brand[i].producer<<endl;
 }

void calculate1()//5.8
{
int words_number = 0;
const int Arsize = 20;
char words[Arsize];
cout << "Enter words(to stop,type the word done): " << endl;
while (strcmp(words,"done"))
{
cin >> words;//cin以区分\0来确定是否输入结束,故每个单词之间的空格
//可以用来终止一次输入,然后执行下次输入,这样一次输入到words中
//但遇到输入换行符 则会先执行一遍程序 然后继续输入新的字符,可以用cout<<words来验证一下想法
words_number++;
}
cout << "You entered a total of " << words_number-1 << " words" << endl;
}
void calculate2()//5.9
{
int words_number = 0;
string words;
cout << "Enter words(to stop,type the word done): " << endl;
while (words!="done")
{
cin >> words;
words_number++;
}
cout << "You entered a total of " << words_number - 1 << " words" << endl;
}
void print_star()//5.10
{
int star_number=0,i=0,j=0,k=0,full_stop_number=0;
cout << "Enter number of rows: ";
cin >> star_number;
for (i = star_number; i > 0; i--)
{
full_stop_number = 0;
for(j=i;j>1;j--)
{
cout << ".";
full_stop_number++;
}
for (k = star_number-full_stop_number; k > 0; k--)
cout << "*";
cout << endl;
}
}
void main()
{
print_star();
system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值