您的位置:首页 > 编程语言 > C语言/C++

C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第八章(chapter 8) 1-4

2014-02-22 23:49 429 查看
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风格字符串的运用发现不是特别熟练,这是个比较好的题目,运用了函数重载= =只不过自己写的时候有点菜,何时使用解除引用运算符什么都没弄明白,慢慢来吧~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐