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风格字符串的运用发现不是特别熟练,这是个比较好的题目,运用了函数重载= =只不过自己写的时候有点菜,何时使用解除引用运算符什么都没弄明白,慢慢来吧~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值