自己看书时解答的C++ Primer plus 第五章习题答案
5.1
#include <iostream>int main()
{
using namespace std;
int n, m, x, y,i;
int t = 0;
cout << "Pls enter a number A: ";
cin >> n;
cout << "Pls enter another number B: ";
cin >> m;
for (y = n; n > m;)
{
y = m;
m = n;
n = y;
};
cout << "Now,I will show all number between A and B(include A and B),and Sum them"<<endl;
for (x = n; x <= m; ++x)
{
cout << x << "\t";
t = t + x;
};
cout << endl <<"all number sum is: "<< t << endl;
system("pause");
return 0;
}
5.2
#include <iostream>
#include<array>
int main()
{
using namespace std;
array <long double, 101>jiecheng;
jiecheng[0] =jiecheng[1]=1;
for (int n = 1; n <= 100; n++)
jiecheng[n] = jiecheng[n - 1] * n;
for (int n = 0; n <= 100; n++)
cout << n << "!= " << jiecheng[n] << endl;
system("pause");
return 0;
}
5.3
#include <iostream>
int main()
{
using namespace std;
int num;
int x = 0;
int i = 0;
cout << "Pls enter a number: ";
cin >> num;
while (num != 0)
{
x = x + num;
cout <<"now summary all number,and the value is: "<< x<<endl;
++i;
cout << "Pls enter a number: ";
cin >> num;
};
system("pause");
return 0;
}
5.4
#include <iostream>
int main()
{
using namespace std;
double daphne[100];
double cleo[100];
daphne[0] = 100;
cleo[0]=100;
int n;
for ( n = 0; cleo[n] <= daphne[n]; n++)
{
daphne[n + 1] = 10 * n + 110;
cleo[n + 1] = cleo[n] * 1.05;
cout << n << endl;
cout <<"daphne "<< daphne[n] << endl;;
cout <<"cleo "<< cleo[n]<<endl;
};
cout << n << " years pasted and ";
cout << "now daphne have $" << daphne[n] << endl;
cout << " now cleo have $" << cleo[n] << endl;
system("pause");
return 0;
}
5.5
#include <iostream>
int main()
{
using namespace std;
char*month[12] =
{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Spe",
"Oct",
"Nov",
"Dec"
};
int count[12];
int years=0;
int m;
for ( m = 0; m < 12; m++)
{
cout << "Pls enter the sells count of " << month[m] << endl;
cin >> count[m];
years = years + count[m];
};
cout << " the sells count of all years is : " << years << endl;
system("pause");
return 0;
}
5.6
int main()
{
using namespace std;
char*month[12] =
{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Spe",
"Oct",
"Nov",
"Dec"
};
char*year[3] =
{
"1999",
"2000",
"2001"
};
int quantity[3][12];
int sells = 0;
int y = 0;
for (y = 0; y < 3; y++)
{
int m = 0;
for (m = 0; m < 12; m++)
{
cout << "Pls enter the sells quantity of " << month[m] << " " << year[y] << endl;
cin >> quantity[y][m];
sells = sells + quantity[y][m];
}
};
cout << " the sells quantity of 3 years is : " << sells << endl;
system("pause");
return 0;
}
5.7
#include <iostream>
#include<string>
int main()
{
using namespace std;
struct car
{
string name;
int year;
};
int n;
cout << "how many cars do you want to catalog?" << endl;
cin >> n;
car *cars = new car[n];
for (int m = 0; m < n; m++)
{
cout << "car #"<<m+1<<":" << endl;
cout << "pls enter the make:";
cin.get();
getline(cin, cars[m].name);
cout << "pls enter the year made:";
cin >> cars[m].year;
};
cout << "here is your colletction:" << endl;
for (int i = 0; i < n; i++)
cout << cars[i].year << " " << cars[i].name << endl;
delete[]cars;
system("pause");
return 0;
}
5.8
#include<cstring>
int main()
{
using namespace std;
char words[100][100];
int n;
cin >> words[0];
for ( n = 0; strcmp(words[n], "done"); n++)
{
cin >> words[n + 1];
}
for (int i=0; i < n ; i++)
{
cout << words[i]<<" ";
};
cout<<endl<< "you entered " << n << " words";
system("pause");
return 0;
}
5.9
#include<string>
int main()
{
using namespace std;
string words[100];
int n;
cin >> words[0];
for ( n = 0; words[n]!="done"; n++)
{
cin >> words[n + 1];
}
for (int i=0; i < n ; i++)
{
cout << words[i]<<" ";
};
cout<<endl<< "you entered " << n << " words";
system("pause");
return 0;
}
#include <iostream>
int main()
{
using namespace std;
int row;
cout << "enter numbers of rows: ";
cin >> row;
for (int i = 1; i <= row; i++)
{
int l=1;
for (l = 1; l <= row - i; l++)
cout << ". ";
for (int l = row-i+1; l <=row ; l++)
cout << "* ";
cout << endl;
};
system("pause");
return 0;
}