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
#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风格字符串的运用发现不是特别熟练,这是个比较好的题目,运用了函数重载= =只不过自己写的时候有点菜,何时使用解除引用运算符什么都没弄明白,慢慢来吧~
相关文章推荐
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第五章(chapter 5) 1-5
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第五章(chapter 5) 6-10
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第六章(chapter 6) 1-4
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第六章(chapter 6) 5-9
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第九章(chapter 9) 1-4
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第七章(chapter 7) 1-5
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第七章(chapter 7) 6-10
- C++ Primer Plus第五版 第八章 编程练习答案
- C++ Primer Plus第六版 第八章 编程练习答案
- c++ primer plus(文章6版本)中国版 编程练习答案第八章
- c++ primer plus(第6版)中文版 第八章编程练习答案
- C++primer plus第六版课后编程练习答案10.8
- C++ primer plus第六版课后编程练习答案:7.2
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 6
- C++ Primer Plus第五版 第二章 编程练习答案
- C primer plus 第八章 字符输入输出与输入验证 编程练习 个人答案
- c++ primer plus(第6版)中文版 第四章编程练习答案
- C++primer plus第六版课后编程练习答案4.5
- C++primer plus第六版课后编程练习答案6.2
- C++primer plus第六版课后编程练习答案6.9