c++ primer plus 第五章 课后题答案

时间:2021-08-03 05:35:35

c++ primer plus 第五章 课后题答案

#include <iostream>
using namespace std; int main()
{
int num_1,num_2,sum=; cout << "Please enter two number: ";
cin >> num_1;
cin >> num_2; if (num_1 > num_2)
{
int a;
a = num_1;
num_1 = num_2;
num_2 = a;
} for (int i = num_1; i <= num_2; i++)
{
sum += i;
}
cout << "The sum of num_1-num_2 is " << sum << endl; system("pause");
}

c++ primer plus 第五章 课后题答案  

#include <iostream>
#include <array>
using namespace std;
const int num=; int main()
{
array<long double, num+> jiecheng;
jiecheng[] = jiecheng[] = ;
cout << << "! = " << jiecheng[] << endl;
cout << << "! = " << jiecheng[] << endl;
for (int i = ; i <= num; ++i)
{
jiecheng[i] = i * jiecheng[i - ];
cout << i << "! = " << jiecheng[i]<<endl;
} system("pause");
}

c++ primer plus 第五章 课后题答案

#include <iostream>
#include <array>
using namespace std; int main()
{
int a,b=;
cout << "Please enter a number other than zero: ";
do
{
cin >> a;
if (a == )
cout << "Stop summing!\n";
else
{
b = b + a;
cout << "The sum of the numbers already entered is: " << b << endl;
} } while (a != );
system("pause");
}

c++ primer plus 第五章 课后题答案

c++ primer plus 第五章 课后题答案

#include <iostream>
using namespace std;
const float lixi1 = 0.1;
const float lixi2 = 0.05; int main()
{
float Daphne_i = , Cleo_i = , Daphne = Daphne_i, Cleo = Cleo_i;
int year = ; while (Daphne >= Cleo)
{
Daphne = Daphne + Daphne_i * lixi1;
Cleo = Cleo + Cleo * lixi2;
year += ;
// cout << Daphne << "," << Cleo<<"--"<< year<<endl;
}
cout << "In " << year << "th years, Cleo's funds exceed Daphne.\nAt this time, the funds of Cleo are " << Cleo << ", and the funds of Daphne are " << Daphne << ".\n";
system("pause");
}

c++ primer plus 第五章 课后题答案

#include <iostream>
using namespace std;
const int month = ; int main()
{
int num[month];
int sum = ;
const char* months[month] = { "January","February","March","April","May","June","July","August","September","October","November","December" }; cout << "Please enter the monthly sales volume:\n";
for (int i = ; i < month; ++i)
{
cout << months[i] << " : ";
cin >> num[i];
sum += num[i];
} cout << "The total sales this year is " << sum << ".\n"; system("pause");
}

c++ primer plus 第五章 课后题答案

#include <iostream>
using namespace std;
const int year = ;
const int month = ; int main()
{
int num[year][month];
int sum[] = { ,,, };
const char* months[month] = { "January","February","March","April","May","June","July","August","September","October","November","December" }; for (int j = ; j < year; ++j)
{
cout << "Please enter the monthly sales volume of " << j + << "th year:\n";
for (int i = ; i < month; ++i)
{
cout << months[i] << " : ";
cin >> num[j][i];
sum[j] += num[j][i];
}
cout << "The total sales of " << j+ << "th year is "<< sum[j] << ".\n";
sum[year] = sum[year] + sum[j];
} cout << "The total sales of " << year << " years are "<<sum[year] << ".\n"; system("pause");
}

c++ primer plus 第五章 课后题答案

#include <iostream>
#include <string>
using namespace std; struct car
{
string make;
int year;
}; int main()
{
int num; cout << "How many cars do you wish to catalog?";
cin >> num;
car *all_car = new car[num]; for (int i = ; i < num; ++i)
{
cout << "Car #" << i+ << endl;
cout << "Please enter the make : ";
cin.ignore();
getline(cin, all_car[i].make);
cout << "Please enter the year made : ";
cin >> all_car[i].year;
} cout << "Here is your collection:\n";
for (int i = ; i < num; ++i)
{
cout << all_car[i].year << " " << all_car[i].make << endl;
}

delete [] all_car;
system("pause");
}

c++ primer plus 第五章 课后题答案

#include <iostream>
#include <cstring>
using namespace std;
const int num_words = ; int main()
{
char words[];
int s = ;
bool flag = true; cout << "Enter words (to stop, tpye the word done) :\n";
for (int i = ; i < num_words; ++i)
{
cin >> words;
if (flag && !strcmp(words, "done"))
flag = false;
if (flag && strcmp(words, "done"))
s += ; } cout << "Your entered a total of " << s << " words.\n"; system("pause");
}

c++ primer plus 第五章 课后题答案

#include <iostream>
#include <string>
using namespace std;
const int num_words = ; int main()
{
const string sstop_word = "done";
string str;
int s = ;
bool flag = true; cout << "Enter words (to stop, tpye the word done) :\n";
for (int i = ; i < num_words; ++i)
{
cin >> str;
if (flag && str== sstop_word)
flag = false;
if (flag && !(str == sstop_word))
s += ; } cout << "Your entered a total of " << s << " words.\n"; system("pause");
}

c++ primer plus 第五章 课后题答案

#include <iostream>
using namespace std; int main()
{
int num;
cout << "Enter number of rows: ";
cin >> num; for (int i = ; i < num; ++i)
{
for (int j = ; j < (num - i); ++j)
cout << ".";
for (int j = ; j < i; ++j)
cout << "*";
cout << endl;
} system("pause");
}