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

C++.Primer.Plus第五版第五章编程练习答案

2015-07-16 14:47 369 查看
// 1

#include <iostream>

using namespace std;

int main()

{

cout << "This progress need tow int numbers:\n";

int int1,int2;

cout << "Please input the little number: \n";

cin >> int1;

cout << "Please input the big number: ";

cin >> int2;

int total = 0;

for(int comp = int1;comp <= int2; comp++)

total += comp;

cout << "total = " << total << endl;

return 0;

}

// 2

#include <iostream>

using namespace std;

int main()

{

int num,num_total= 0;

cin >> num;

while(num != 0)

{

num_total+= num;

cout << "num_total = " << num_total << endl;

cin >> num;

}

cout << "num_total = " << num_total << endl;

/*while(cin >> i && i) sum+= i,cout << sum<< endl;*/

return 0;

}

// 3

#include <iostream>

using namespace std;

int main()

{

int Daphne = 100,Cleo = 100,year = 0;

while(Cleo <= Daphne)

{

Daphne += 100 * 10 / 100;

Cleo += Cleo * 5 / 100;

year ++;

}

cout << year << "years latter :\n";

cout << "Daphne = " << Daphne << endl;

cout << "Cleo = " << Cleo << endl;

return 0;

}

// 4

#include <iostream>

#define MONTH_TOTAL 12

using namespace std;

int main()

{

const char * month[MONTH_TOTAL] = {

"January",

"February",

"March",

"April",

"Mary",

"June",

"July",

"August",

"September",

"October",

"November",

"December"

};

int salo[MONTH_TOTAL] = {},sum = 0;

for(int i = 0; i < MONTH_TOTAL; i++)

{

cout << month[i] << ":\t\t\t ";

cin >> salo[i];

sum += salo[i];

}

cout << "total = " << sum << endl;

return 0;

}

// 5

#include <iostream>

#define MONTH_TOTAL 12

#define YEAR_TOTAL  3

using namespace std;

int main()

{

const char * month[MONTH_TOTAL] = {

"January",

"February",

"March",

"April",

"Mary",

"June",

"July",

"August",

"September",

"October",

"November",

"December"

};

int salo[YEAR_TOTAL][MONTH_TOTAL] = {},sum = 0,year = 0,i = 0;

while(year < YEAR_TOTAL)

{

if(i == MONTH_TOTAL)

{

year ++;

i = 0;

if(year == YEAR_TOTAL)

break;

}

cout << year << "\t" <<  month[i] << ":\t\t\t ";

cin >> salo[year][i];

sum += salo[year][i++];

}

cout << "total = " << sum << endl;

return 0;

}

// 6 注意cin.get()所在位置及作用      当输入多个字符串时!

#include <iostream>

#include <string>

using namespace std;

struct car

{

//string maker;

char maker[100];

int year;

};

int main()

{

cout << "How many cars do you wish to catalog: ";

int car_number;

//cin.get();

cin >> car_number;

car * car_info = new car [car_number];

for(int i = 0; i < car_number; i++)

{

cout << "Car\t#" << i + 1 << ":\n" ;

cout << "Please enter the maker: ";

//cin >> car_info[i].maker;

cin.get();

cin.getline(car_info[i].maker,100);

//cout << "maker:" << car_info[i].maker << endl;

cout << "Please enter the year made: ";

cin >> car_info[i].year;

}

cout << "Here is your collection:\n";

for(int i = 0; i < car_number; i++)

cout << car_info[i].year << "\t" << car_info[i].maker << endl;

delete [] car_info;

return 0;

}

// 7

#include <iostream>

#include <cstring>

using namespace std;

int main()

{

cout << "Enter words (to stop, type the word done):\n";

int count = 0;

while(1)

{

char string[100];

cin >> string;

if(strcmp(string,"done") == 0)

break;

else

count++;

}

cout << "You entered a total of " << count << " words.\n";

return 0;

}

// 8

#include <iostream>

#include <string>

using namespace std;

int main()

{

cout << "Enter words (to stop, type the word done):\n";

int count;

while(1)

{

string str;

cin >> str;

if(str == "done")

break;

else

count++;

}

cout << "You entered a total of " << count << " words.\n";

return 0;

}

// 9

#include <iostream>

using namespace std;

int main()

{

int line;

cout << "Please input a number: ";

cin >> line;

for(int i = 0; i < line; i++)

{

for(int dian = 0; dian < line - i - 1; dian++)

cout << ".";

for(int xing = 0;xing <= i; xing++)

cout << "*";

cout << endl;

}

return 0;

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