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

C++ primer plus(第六版)第五章练习题

2014-10-12 19:27 459 查看
/********************************************************************************************************************************************************/

C++ primer plus(第六版)第五章 第一题

/********************************************************************************************************************************************************/
#include<iostream>
using namespace std;
int main(void)
{
int min, max;
int sum = 0;
cout << "Enter min number: ";
cin >> min;
cout << "Enter max number: ";
cin >> max;
for (int i = min; i <= max; i++)
{
sum += i;
}
cout << "Sum of " << min << " to " << max << " is " << sum << endl;

return 0;
}


/********************************************************************************************************************************************************/

C++ primer plus(第六版)第五章 第二题

/********************************************************************************************************************************************************/
#include<iostream>
#include<array>
using namespace std;
int main(void)
{
const int ArraySize = 101;
array<long double, ArraySize> factorials;
factorials[0] = 1;
for (int i = 1; i < ArraySize; i++)
factorials[i] = i * factorials[i - 1];
cout << "100! = " << factorials[100];

return 0;
}

/********************************************************************************************************************************************************/

C++ primer plus(第六版)第五章 第三题

/********************************************************************************************************************************************************/
#include<iostream>

using namespace std;

int main(void)
{
int temp;
long sum = 0;
cout << "Enter a number: ";
cin >> temp;
while (temp != 0)
{
sum += temp;
cout << "sum = " << sum << endl;
cout << "Enter a number: ";
cin >> temp;
}

return 0;
}

/********************************************************************************************************************************************************/

C++ primer plus(第六版)第五章 第四题

/********************************************************************************************************************************************************/
#include<iostream>

using namespace std;

int main(void)
{
double Daphne = 100;
double interest_Da = 10;
double Cleo = 100;
double interest_Cl = 0.05;
int count = 1;

Daphne = Daphne +interest_Da;
Cleo = Cleo * (interest_Cl + 1);

for (; Cleo < Daphne; count++)
{
Daphne = Daphne + interest_Da;
Cleo = Cleo * (interest_Cl + 1);
}
cout << "After " << count << " years : ";
cout << "Daphne has " << Daphne << " $ , " << "Cleo has " << Cleo << " $ " << endl;
return 0;

}
/********************************************************************************************************************************************************/

C++ primer plus(第六版)第五章 第五题

/********************************************************************************************************************************************************/
#include<iostream>
#include<string>

using namespace std;

int main(void)
{
string months[12] = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" };
int sales[12];
int sum = 0;
for (int i = 0; i < 12; i++)
{
cout << "Enter the sales of " << months[i] << " : ";
cin >> sales[i];
}
for (int i = 0; i < 12; i++)
{
sum += sales[i];
}
cout << "The total sales of the whole year is : " << sum << endl;

return 0;
}

/********************************************************************************************************************************************************/

C++ primer plus(第六版)第五章 第六题

/********************************************************************************************************************************************************/
#include<iostream>
#include<string>

using namespace std;

int main(void)
{
string months[12] = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" };
int sales[3][12];
int sum[3] = { 0,0,0 };
for (int i = 0; i < 3; i++)
{
cout << "The " << i + 1 << "th year: " << endl;
for (int j = 0; j < 12; j++)
{
cout << "Enter the sales of " << months[j] << ": ";
cin >> sales[i][j];
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 12; j++)
{
sum[i] += sales[i][j];
}

cout << "The sales of " << i + 1 << "th year is: " << sum[i] << endl;
}
cout << "The total sales of three years is: " << sum[0] + sum[1] + sum[2] << endl;
return 0;
}

/********************************************************************************************************************************************************/

C++ primer plus(第六版)第五章 第七题

/********************************************************************************************************************************************************/
#include<iostream>
#include<string>
using namespace std;

struct car
{
string company;
int year_product;
};

int main(void)
{
int m_car;
cout << "How many car do you have: ";
(cin >> m_car).get();

car * p_car = new car[m_car];
for (int i = 0; i < m_car; i++)
{
cout << "Car #" << i + 1 << ": " << endl;
cout << "Please enter the make: ";
getline(cin, (p_car + i)->company);
cout << "Please enter the year made: ";
(cin >> (p_car + i)->year_product).get();
}
cout << "Here is your collection: " << endl;
for (int i = 0; i < m_car; i++)
{
cout << (p_car + i)->year_product << " " << (p_car + i)->company << endl;
}

return 0;
}
/********************************************************************************************************************************************************/

C++ primer plus(第六版)第五章 第八题

/********************************************************************************************************************************************************/
#include<iostream>

using namespace std;

int main(void)
{
char temp[20] = {NULL};
char stop[20] = "done";
int count = 0;
cout << "Enter words (to stop ,type the word done): " << endl;
while (strcmp(temp, stop) != 0)
{
cin >> temp;
count++;
}
cout << "You entered a total of " << count-1 << " words" << endl;

return 0;
}
/********************************************************************************************************************************************************/

C++ primer plus(第六版)第五章 第九题

/********************************************************************************************************************************************************/
#include<iostream>
#include<string>

using namespace std;

int main(void)
{
string stop = "done";
string temp = "";
cout << "Enter words (to stop, type the word done):\n";

int count = 0;
while (temp != stop)
{
cin >> temp;
++count;
}

cout << "You entered a total of " << count - 1 << " words.";
return 0;
}

/********************************************************************************************************************************************************/

C++ primer plus(第六版)第五章 第十题

/********************************************************************************************************************************************************/
#include<iostream>

int main(void)
{
using std::cout;
using std::cin;

int rows = 0;
cout << "Enter number of rows: ";
cin >> rows;

for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < rows - i - 1; ++j)
cout << ".";
for (int k = 0; k <= i; ++k)
cout << "*";
cout << "\n";
}
}
4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: