C++PRIMER PLUS第六版课后编程答案 5.6-510

5.6
#include <iostream>
#include <string>
void main56()
{
	using std::cout;
	using std::cin;
	using std::string;

	string m[12]={"1","2","3","4","5","6","7","8","9","10","11","12"};
	const string *s=m;
	int arr[3][15];
	int sum=0;
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<12;j++,s++)
		{
			cout<<"Please enter the "<<i+1<<" years "<<*s<<" moth sales:";
			cin>>arr[i][j];
			sum+=arr[i][j];
			cout<<"Now sum is "<<sum<<"\n";
		}
		s=m;//重新令s指向m的开头
	}
	cout<<"All sum is "<<sum<<", THE END\n";
	cin.get();
}


5.7

#include <iostream>
#include <string>
using namespace std;
struct car{
	string name;
	int year;

};
void get(car *);
void show(const car const *);

void main57()
{
	cout<<"How many cars do your wish to catalog?";
	int num;
	cin>>num;
	car *c=new car[num];
	for(int i=0;i<num;i++,c++)
	{
		cout<<"Car #"<<i+1<<":"<<endl;
		get(c);
		show(c);
	
	}
	cin.get();
}
void get(car *c)
{
	cin.get();
	cout<<"Please enter the make:";
	string name;
	getline(cin,name);
	cout<<"\nplease enter the years of make:";
	int y;
	cin>>y;
	c->name=name;
	c->year=y;
	
}

void show(const car const *c)
{
	cout<<"/nHere is your collection: ";
	cout<<c->year<<"  "<<c->name<<endl;
}


5.8有点BUG,详看5.9,我懒得改了

#include <iostream>
#include <cstring>
using namespace std;
void main58()
{
	
	char test[20];
	int count=0;
	char ch;
	int i=0;
	cout<<"Enter words (to stop,type the word done):";
	//cin.get();
	while(strcmp(test,"done")!=0)
	{
		//cout<<"is in"<<endl;
		//cin.get(ch)>>test[i];
		cin.get(ch);
		if(ch==' ')
		{
			test[i]='\0';
			count++;
			//cout<<"i=0"<<test<<"\ncount="<<count;
			i=0;
			
		}
		else
		{	
			test[i]=ch;
			test[i+1]='\0';
			//cout<<"i++"<<test<<endl;
			i++;
		}
	}
	cout<<"You entered a total of "<<count<<" words";
	cin.get();




}


5.9(有错误,看下一道正确的)

#include <iostream>
#include <string> //cstring 没有定义string类型的符号运算符,例如==,!=


//要注意输入是这种情况  doneff ajgk done,这时候,要注意doneff的判断 增加flag量
using namespace std;
void main59()
{
	
	string test="";
	string t="done";
	//if(test==t) 

	char ch;
	int count=0;
	int flag=1;
	cout<<"Enter words (to stop, type the word done):"<<endl;
	//cin.get();
	while(test!=t)
	{
		//cout<<"Test2="<<test<<"  Count="<<count<<endl;
		//cout<<"here"<<endl;
		flag=1;
		while(flag==1)
		{
		cin.get(ch);
		if(ch=='\n')	//回车键的表示
			break;
		else if(ch!=' ')
		{
			
			test=test+ch;
			
		}
		else
		{
			test="";
			count++;
			flag=0;
		}
		//cout<<"test1="<<test<<" count="<<count<<endl;
		//cout<<"in in"<<endl;
		}
	}
	cout<<"You enter a total of "<<count<<" words";
	cin.get();





}

5.9(正确版)

#include <iostream>
#include <cstring>
//#include <cctype>
using namespace std;
int main()
{
	string test;
	cout<<"Enter words (to stop ,type the word done)"<<endl;
	int counts=0;
	do{
		cin>>test;
		if(strcmp(test.c_str(),"done")==0)
			break;
		else
			counts++;
	}while(true);
	cout<<"You entered a total of "<<counts<<" words"<<endl;



}
运行截图:


感谢网友的指出! --------2014.10.8


5.10

#include <iostream>

void main510()
{
	using namespace std;
	int row;
	cout<<"Enter number of rows:";
	cin>>row;
	for(int i=0;i<row;i++)
	{
		for(int j=0;j<row;j++)
		{
			if(j<row-i-1)
				cout<<".";
			else
				cout<<"*";
		}
		cout<<endl;
	}
	cin.get();


}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值