1. 程式人生 > >C++PRIMER PLUS第六版課後程式設計答案 5.6-510

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();


}