C++ primer plus(sixth edition) 編程練習答案(answers for programing exercises)第八章(chapter 8) 1-4

8.1

#include <iostream>
void silly(const char * s, int n = 0);
int main(void)
{ 
   using namespace std;
    char * p1 = "Why me?\n";
    silly(p1);
    for (int i = 0; i < 3; i++)
    {
        cout << i << " = i\n";
        silly(p1, i);
    }
    cout << "Done\n";
    return 0;//misad
}
void silly(const char * s, int n)
{ 
   using namespace std;
    static int uses = 0;
    int lim = ++uses;
    if (n == 0)
     lim = 1;
    for (int i = 0; i < lim; i++)
        cout << s;
}

8.1運用了static變量。。一開始沒怎麼看懂,即便現在要寫一遍也很是問題。。。唉

8.2

//use const as more as you can
#include <iostream>
#include <cstring>
using namespace std;
struct CandyBar
{
	char logo[20];
	double weight;
	int calorie;
};
void set(CandyBar &,const char* logo="Millennium Munch",const double weight=2.85,const int calorie=350);
void show(const CandyBar&);
int main()
{
	CandyBar candy;
	set(candy);
	show(candy);
	
	return 0;//dd
}

void set(CandyBar& candy,const char* logo,const double weight,const int calorie)
{
	strcpy(candy.logo,logo);
	candy.weight=weight;
	candy.calorie=calorie;
}

void show(const CandyBar& candy)
{
	cout<<candy.logo<<endl;
	cout<<candy.weight<<endl;
	cout<<candy.calorie<<endl;
	
}

8.3

//turn the lower letter to the upper one
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void tran(string& );
int main()
{
	string str;
	cout<<"Enter a string(q to quit): ";
	getline(cin,str);
	while(str!="q")
	{
        tran(str);
		cout<<str<<endl;
	    cout<<"Next string(q to quit): ";
	    getline(cin,str);
	}
	cout<<"Bye";
	
	return 0;//dd
}

void tran(string& str)
{
	int i=0;
	while(str[i])
		{
		    str[i]=toupper(str[i]);
		    i++;
		}

}

8.4
//8.4
#include <iostream>
using namespace std;
#include <cstring>   //for strlen(),strcpy()
struct stringy {
	char * str;     //points to a string
	int ct;        //length of string (not counting '\0')
}; 

void show(const char*,int n=1);
void show(const stringy&,int n=1);
void set(stringy&,const char*);

//prototypes for set(),show(),and show() go here
int main()
{
	stringy beany;
	char testing[]="Reality isn't what it used to be.";
	
	set(beany,testing);//first argument is a reference
	                      //allocates space to hold copu of testing,
	                      //sets str member of beany to point to the 
	                      //new block,copies testing to new block,
	                      //and sets ct member of beany
	show(beany);        //prints testing string once
	show(beany,2);      //prints member string twice
	testing[0]='D';
	testing[1]='u';
	show(testing);           //prints testing string once
	show(testing,3);   //prints testing string thrice
	show("Done!");
	return 0;//add
}

void show(const char pt[],int n)
{
	for(int i=0;i<n;i++)
	    cout<<pt<<endl;
}

void show(const stringy& beany,int n)
{
	for(int i=0;i<n;i++)
	{
		cout<<beany.str<<endl;
		cout<<"length of string: "<<beany.ct<<endl;
	}
} 

void set(stringy& beany ,const char pt[])
{
	beany.ct=strlen(pt);
	beany.str=new char[strlen(pt)+1];
	strcpy(beany.str,pt);
	
}
8.4中關於指針和c風格字符串的運用發現不是特別熟練,這是個比較好的題目,運用了函數重載= =只不過自己寫的時候有點菜,何時使用解除引用運算符什麼都沒弄明白,慢慢來吧~
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章