设为首页 加入收藏

TOP

C++ Primer Plus第8章题目题解(一)
2018-03-02 06:56:56 】 浏览:285
Tags:Primer Plus 题目 题解

C++ Primer Plus第8章题目题解

#include 
  
     

void PrintStr(char *str, int flag = 0);

int main()
{
	char str[] = "fuck fuck fuck";
	std::cout << "#1 " << std::endl;
	PrintStr(str);
	std::cout << "#2 " << std::endl;
	PrintStr(str, 1996);
	std::cout << "#3 " << std::endl;
	PrintStr(str, 1996);
	std::cout << "#4 " << std::endl;
	PrintStr(str);
	return 0;
}

void PrintStr(char *str, int flag)
{
	static int count = 0;                 //无连接性静态变量只初始化一次且始终存在
	++count;
	if (!flag)
		std::cout << str << std::endl;
	else
	for (int i = 0; i < count; ++i)
		std::cout << str << std::endl;
}
  

8-2

#include 
  
     
#include 
   
     struct CandyBar { char name[30]; double weight; int heat; }; void setCandyBar(CandyBar &c, const char *s = "Millennium Munch", const double w = 2.85, const int h = 350); void disp(const CandyBar &c); int main() { CandyBar c; setCandyBar(c); disp(c); char name[30]; double weight; int heat; std::cin.getline(name, 30); std::cin >> weight >> heat; setCandyBar(c, name, weight, heat); disp(c); return 0; } void setCandyBar(CandyBar &c, const char *s, const double w, const int h) { strcpy(c.name, s); c.weight = w; c.heat = h; } void disp(const CandyBar &c) { std::cout << c.name << std::endl << c.weight << std::endl << c.heat << std::endl; }
   
  

8-3

#include 
  
     
#include 
   
     void changeUpper(std::string &s); int main() { std::string s; while(getline(std::cin, s) && s[0] != 'q') { changeUpper(s); std::cout << s << std::endl; } return 0; } void changeUpper(std::string &s) { for(int i = 0; i < s.size(); ++i) s[i] = toupper(s[i]); } 
   
  

8-4

#include 
  
     
#include 
   
     struct stringy{ char *str; int ct; }; void set(stringy &s, const char *str); void show(const char *str, const int count = 1); void show(const stringy &s, const int count = 1); int main() { stringy beany; char testing[] = "Reality isn't what it used to be."; set(beany, testing); show(beany); show(beany, 2); testing[0] = 'D'; testing[1] = 'u'; show(testing); show(testing, 3); show("Done!"); return 0; } void set(stringy &s, const char *str) { int len = strlen(str); s.ct = len; s.str = new char(len + 1); //要留一个字符存放'\0' 所以分配len + 1空间 strcpy(s.str, str); } void show(const char *str, const int count) { for(int i = 0 ; i < count ; ++i) std::cout << str << std::endl; } void show(const stringy &s, const int count) { for(int i = 0 ;i < count ; ++i) std::cout << s.str << std::endl; } 
   
  

8-5

#include 
  
     
  
template 
   
     T max5(T *a); int main() { int a[5] = {2, 3, 1, 5, 4}; double b[5] = {1.1, 3.3, 5.5, 4.4, 2.2}; std::cout << max5(a) << std::endl; std::cout << max5(b) << std::endl; return 0; } template 
    
      T max5(T *a) { T max = -1; for(int i = 0 ;i < 5 ; ++i) max = max < a[i] ? a[i] : max; return max; } 
    
   
  

8-6

#include 
  
     
#include 
   
     template 
    
      T maxn(T *a, int len); template <> char *maxn(char *a[], int len); int main() { int a[6] = {1, 2, 3, 4, 5, 6}; double b[4] = {1.1, 2.2, 3.3, 4.4}; char *c[5] = {"fuck", "fuckfuck", "fuckfuckfuck", "fuckfuckfuckfuck", "fuckfuckfuckfuckfuck"}; std::cout << maxn(a, 6) << std::endl; std::cout << maxn(b, 4) << std::endl; std::cout &
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++ 输入、输出运算符重载 下一篇C++中的类和对象的封装及函数实例..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目