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

C++ Primer Plus第五章课后编程答案

2017-08-29 16:33 686 查看
1.

#include <iostream>
#include <string>
#include <cstring>
#include <array>

int main()
{
using namespace std;
int l, m, sum=0;
cout << "small: ";
cin >> l;
cout << "big: ";
cin >> m;
for (; l <= m; l++)
{
sum += l;
}
cout << "sum: " << sum;
cin.get();
cin.get();
return 0;
}



2.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 101;
int main()
{
using namespace std;
array<long double, ArSize> arr;
arr[1] = arr[0] = 1LL;
for (int i=2; i < ArSize; i++)
arr[i] = i*arr[i - 1];
for (int i = 0; i < ArSize; i++)
cout << i << "! = " << arr[i] << endl;
cin.get();
return 0;
}



3.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 101;
int main()
{
using namespace std;
int n, sum=0;
cout << "Enter a number: ";
do
{
cin >> n;
sum += n;
cout << "Sum: " << sum << endl;
cout << "Enter a number: ";
} while (n != 0);
cin.get();
return 0;
}



4.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 101;
int main()
{
using namespace std;
double Daphne = 100, Cleo = 100;
int i;
for (i = 0; Cleo <= Daphne; i++)
{
Cleo += Cleo*0.05;
Daphne += 100 * 0.1;
}
cout << "Daphne: " << Daphne << endl;
cout << "Cleo: " << Cleo << endl;
cout << "year: " << i << endl;
cin.get();
return 0;
}



5.
#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 12;
int main()
{
using namespace std;
const char * month[ArSize] =
{
"January: ",
"February: ",
"March: ",
"Apirl: ",
"May: ",
"June: ",
"July: ",
"August: ",
"September: ",
"October: ",
"November: ",
"December: "
};
int sell[ArSize],sum=0;
int i;
for (i = 0; i<ArSize ; i++)
{
cout << month[i];
cin >> sell[i];
sum += sell[i];
}
cout << "Sum: " << sum << endl;
cin.get();
cin.get();
return 0;
}



6.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 12;
const int Years = 3;
const int Months = 12;
int main()
{
using namespace std;
int sell[Years][Months]=
{
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 },
{ 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 },
{ 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
};
const char * month[ArSize] =
{
"January: ",
"February: ",
"March: ",
"Apirl: ",
"May: ",
"June: ",
"July: ",
"August: ",
"September: ",
"October: ",
"November: ",
"December: "
};
int sum=0;
int i;
for (i = 0; i<Months ; i++)
{
cout << month[i];
for (int j = 0; j < Years; j++)
{
cout << sell[j][i] << "\t";
sum += sell[j][i];
}
cout << endl;
}
cout << "Sum: " << sum << endl;
cin.get();
cin.get();
return 0;
}



7.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 12;
struct car
{
std::string make;
unsigned int year;
};
int main()
{
using namespace std;
int sum=0;
int i,n;
cout << "How many cars do you wish to catalog? ";
cin >> n;
cin.get();
car* pz = new car
;
for (int j = 0; j < n; j++)
{
cout << "Car #" << j << ":\nPlease enter the make: ";
getline(cin,pz[j].make);
cout << "Please enter the year made: ";
cin >> pz[j].year;
cin.get();
}
cout << "Here is your collection:\n";
cout << pz[0].year << " " << pz[0].make << endl;
cout << pz[1].year << " " << pz[1].make << endl;
cin.get();
cin.get();
return 0;
}




8.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 20;

int main()
{
using namespace std;
char name[ArSize];
int i=0;
cout << "Enter words (to stop, type the word done) :"<< endl;
cin >> name;
while (strcmp(name, "done") != 0)
{
i++;
cin >> name;
}
cout << "You enter a total of " << i << " words.";
cin.getline(name,ArSize);
// cin.get();
cin.get();
return 0;
}


9.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 20;

int main()
{
using namespace std;
string name;
int i=0;
cout << "Enter words (to stop, type the word done) :"<< endl;
cin >> name;
while (name!="done")
{
i++;
cin >> name;
}
cout << "You enter a total of " << i << " words.";
getline(cin,name);
// cin.get();
cin.get();
return 0;
}


10.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 20;

int main()
{
using namespace std;
int n;
cout << "Enter rows :"<< endl;
cin >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-i-1; j++)
{
cout << ".";
}
for (int j=n-i-1; j < n; j++)
{
cout << "*";
}
cout << endl;
}
cin.get();
cin.get();
return 0;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: