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

// 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;

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值