c++ Primer Plus(第六版)第五章习题,写代码之路

//c++ Primer Plus(习题5.1)
/*Input two integers*/
/*Between the calculation and plus them;*/
#include<iostream>
int main()
{
	long double result=0;
	using namespace std;
	cout << "Input two integers,i will plus the numbers between.\n"
		<< "Now,give me the frist number: \a";
	long double fristnumber;
	cin >> fristnumber;
	cout<< "There,give me the second number: \a";
	long double secondnumber;
	cin >> secondnumber;
	for (; fristnumber<=secondnumber; fristnumber++)
		result = result + fristnumber;
	cout << "Here is the result: " << result << endl;
	return 0;

} 
/*用array对象重写程序清单5.4,并计算100的阶乘*/
#include<iostream>
#include<array>
const int ArSize = 101;
int main()
{
	using namespace std;
	cout << "This prpgram acculate 100!,here is the result: \n";
	array<long double, ArSize>factorials;
	factorials[1] = factorials[0] = 1LL;
	for (int i = 2; i <ArSize; i++)
		factorials[i] = i*factorials[i - 1];
	for (int i = 0; i < ArSize; i++)
		cout << i << "!= " << factorials[i] << endl;
	return 0;
}

/*Input interger and plus them,until input 0 to end!*/
#include<iostream>
int main()
{
	using namespace std;
	double input,result=0;
	cout << "Enter integers,plus them(until input 0): \a";
	while (cin >> input&&input != 0)     //输入不正确或者0停止循环
	{
		result += input;
		cout << "Until input 0,The result: " << result << endl;
		cout << "Go on,please a interger:\a";
	}
	cout << "Error Input or you input 0,Bye!" << endl;
	return 0;

}

/*计算几年后Daphne才能超过Cleo*/
#include<iostream>
const float Danli = 0.1f;
const float Fuli = 0.05f;
const float Amount = 100.0f;
const float EPSINON = 0.001f;        //涉及到浮点值的比较,计算机只保证精度所以自己定义一个范围
int main()							//借此来判断	
{
	float Daphne=Amount, Cleo=Amount;
	float compare;
	int years=0;
	do
	{
		Daphne+= Amount*Danli;
		Cleo=Cleo*Fuli+Cleo;
		years++;
		compare = Cleo - Daphne;              //计算差值
	} while (compare<EPSINON);                //比较范围就行了,如果直接比较,程序会崩溃
	std::cout << "Cleocount :" << Cleo << std::endl;
	std::cout << "Daphnecount: " << Daphne << std::endl;
	std::cout << "After " << years << " years,Cleo's count will past over Daphne.\n";
	return 0;
}

//Input <<c++ For Fools>>sellmount each months,plus Calculate total.1
#include <iostream> 
const int MONTHS = 12;
const char*months[MONTHS] = { "January","February","March","April","May","June","July","August","September","October","November","December"}; 
int main() 
{ 
using namespace std;
int sales[MONTHS],sum = 0; 
for (int i = 0; i<MONTHS; i++) 
{
	cout << "Input in " << months[i] << "<<C++ For Fools>>salemount:";       
	cin >> sales[i];        
	sum += sales[i];
}
cout << "This year,<<C++ For Fools>>sellmount is:" << sum << endl;    
return 0; 
}

//Input <<c++ For Fools>>sellmount each months,plus Calculate total.
//total 3 years
#include <iostream> 
const int YEARS = 3;
const int MONTHS = 12;
const char*months[MONTHS] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
int main()
{
	using namespace std;
	int sales[YEARS][MONTHS], year = 0,sum=0;
	int mount_years[YEARS];
	for (int i = 0; i<YEARS; i++)
	{
		cout << "Please input " << i + 1 << " yesrs mount:\n";
		for (int j = 0; j < MONTHS; j++)
		{
			cout<< months[j] << "<<C++ For Fools>>salemount:";
			cin>>sales[i][j];
			year+= sales[i][j];
		}
		mount_years[i] = year;
		sum += mount_years[i];
	}
	for(int i=0;i<YEARS;i++)
	cout <<i+1<< " year,<<C++ For Fools>>sellmount is:" << mount_years[i]<< endl;
	cout << "Three years sellmount is " << sum << endl;
	return 0;
}
c++ Primer Plus(习题5.7)
//汽车的信息,用结构来做
#include <iostream> 
#include <string> 
using namespace std; 
struct car{ 
	string name; 
	int year; 
};
int main() 
{
	cout << "How many cars do you wish to catalog? "; 
	int num;
	cin >> num;    
	cin.get();                  //清空缓存区的换行符
	car* ps = new car[num];     //动态数组
	for (int i = 0; i<num; ++i) {
		cout << "Car #" << i + 1 << ":\n"; 
		cout << "Please enter the make: ";            
		getline(cin, ps[i].name);
		cout << "Please enter the year made: ";  
		cin >> ps[i].year;
		cin.get();					 //清空缓存区的换行符
	}
	cout << "Here is your collection:\n";     
	for (int i = 0; i<num; ++i)
		cout << ps[i].year << " " << ps[i].name << endl; 
	delete[] ps;    //删除分配的内存
	return 0;
}
c++ Primer Plus(习题5.8)
//输入句子,支出有多少单词,done结束输入,单词数目不包括done
#include<iostream>
#include <cstring>  
const int Number = 20;
int main()
{
	using namespace std;
	char word[Number];     
	int sum = 0;
	cout << "Enter words (to stop,type the word done,in ):\n";
	cin >> word;
	while (strcmp(word, "done")) 
	{
		sum++;           
		cin >> word;
	}
	cout << "You entered a total of " << sum << " words.\n";    
	return 0;
}

c++ Primer Plus(习题5.9)

//与前面类似,不过改用string类来代替数组
#include<iostream>
#include <string>  
int main()
{
	using namespace std;
	string setence;
	int sum = 0;
	cout << "Enter words (to stop,type the word done):\n";
	cin >> setence;                  //从输入流读写单词,cin遇到空白会停止读入,用getline会变成死循环
	while (setence!="done")          //getline直接干掉一行,就无法比较了
	{
		sum++;
		cin >> setence;                //检查是否读到done
		
	}
	cout << "You entered a total of " << sum << " words.\n";
	return 0;
}

c++ Primer Plus(习题5.10)

/*input a interger.out put a Graphical*/
#include<iostream>
int main()
{
	using namespace std;
	int lines;
	cout << "Input a interger to set lines: ";
	while (cin >> lines&&lines > 0)
	{
		for (int i = 0; i < lines; i++)       //控制行
		{
			for (int j = lines-i; j>1; j--)    //控制'.'的数目,每次递减,和i的关系是line-i
					cout << ".";
				for (int k = 0; k <=i;k++)         //输出*在.完成循环后
					cout << "*"; 
			cout << "\n";
		}
		cout << "Go on: ";
	}
	cout << "Bye!";
	return 0;
}

继续更新··········


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值