• C++ Primer Pluse_8_课后题


    #include <iostream>
    #include <string>
    #include<cstring>
    
    using namespace std;
    
    void showStr(const char * str, int & n)
    {
    	cout << str << endl;
    	
    	if (n > 0)
    	{
    		showStr(str, --n);
    	}
    }
    int test8_1()
    {
    	char str[] = "hello cat";
    	int n = 2;
    	showStr(str, n);
    	return 0;
    }
    
    int test8_2()
    {
    
    	return 0;
    }
    
    void Up(string & str)
    {
    	int i = 0;
    	while (str[i])
    	{
    		str[i] = toupper(str[i]);
    		i++;
    	}
    
    	cout << str << endl;
    
    }
    
    int test8_3()
    {
    	string str;
    
    	cout << "Enter a string (q to quit): ";
    	getline(cin, str);
    
    	while (str != "q")
    	{
    		Up(str);
    
    		cout << "Enter next string:(q to quit):";
    		getline(cin, str);
    	}
    	cout << "bye.
    ";
    	
    
    	return 0;
    }
    
    struct stringy{
    	char *str;
    	int ct;
    };
    
    void set(stringy & beany, const char *test)
    {
    	beany.ct = strlen(test);
    	beany.str = new char[beany.ct +1];
    	int i = 0;
    	for (i = 0; test[i]; i++)
    	{
    		beany.str[i] = test[i];
    	}
    	beany.str[i] = 0;
    }
    
    void show(const stringy beany, int n = 1)
    {
    	for (int i = 1; i <= n; i++)
    	{
    		cout << beany.str << endl;
    		cout << beany.ct << endl;
    	}
    }
    
    void show(const char *testing, int n = 1)
    {
    	for (int i = 1; i <= n; i++)
    	{
    		cout << testing << endl;
    	}
    }
    int test8_4()
    {
    	stringy beany;
    	char testing[] = "Hello cat.";
    
    	set(beany, testing);
    	show(beany);
    	cout << endl;
    	show(beany, 2);
    	testing[0] = 'D';
    	testing[1] = 'u';
    	show(testing);
    
    	show(testing, 3);
    	show("Done!");
    	return 0;
    }
    
    template <typename T>
    T Max5(T arr[])
    {
    	int i = 0;
    	T max = arr[0];
    	for (i = 1; i < 5; i++)
    	{
    		if (max < arr[i])
    		{
    			max = arr[i];
    		}
    	}
    
    	return max;
    
    }
    int test8_5()
    {
    	int a[5] = { 2, 3, 1, 5, 4 };
    
    	cout << Max5(a);
    
    	double b[5] = { 6, 8, 5, 9, 6 };
    
    	cout << endl << Max5(b);
    	return 0;
    }
    
    template <typename T>
    T maxn(T arr[], int n)
    {
    	int i = 0;
    	T max = arr[0];
    	for (i = 1; i < n; i++)
    	{
    		if (max < arr[i])
    		{
    			max = arr[i];
    		}
    	}
    	return max;
    }
    
    template <> char * maxn<char *>(char * arr[], int n)
    {
    	int i = 0;
    	int max = strlen(arr[0]);
    	int maxk = 0;
    
    	for (i = 1; i < n; i++)
    	{
    		if (max < strlen(arr[i]))
    		{
    			max = strlen(arr[i]);
    			maxk = i;
    		}
    			
    	}
    
    	return arr[maxk];
    }
    int test8_6()
    {
    	int a[6] = { 2, 3, 1, 5, 6, 4 };
    	double b[4] = { 9, 6, 7, 4 };
    
    	char * strarr[3] = { "Hello cat!", "boat", "miao wu" };
    
    	cout << maxn(a, 6) << endl;
    	cout << maxn(b, 4) << endl;
    	cout << maxn(strarr, 3) << endl;
    	return 0;
    }
    
    template <typename T>
    void ShowArray(T arr[], int n);
    
    template <typename T>
    void ShowArray(T * arr[], int n);
    
    struct debts
    {
    	char name[50];
    	double amount;
    };
    
    template <typename T>
    void ShowArray(T arr[], int n)
    {
    	cout << "template A
    ";
    	for (int i = 0; i < n; i++)
    	{
    		cout << arr[i] << ' ';
    	}
    	cout << endl;
    }
    
    template <typename T>
    void ShowArray(T *arr[], int n)
    {
    	cout << "using template B
    ";
    	for (int i = 0; i < n; i++)
    	{
    		cout << *arr[i] << ' ';
    	}
    
    	cout << endl;
    }
    
    template <typename T>
    T SumArray(T arr[], int n)
    {
    	T sum = 0;
    	for (int i = 0; i < n; i++)
    	{
    		sum += arr[i];
    	}
    	return sum;
    }
    
    template <typename T>
    T SumArray(T *arr[], int n)
    {
    	T sum = 0;
    	for (int i = 0; i < n;i++)
    	{
    		sum += *arr[i];
    	}
    	return sum;
    }
    int test8_7()
    {
    	int things[6] = { 1, 2, 3, 4, 5, 6 };
    
    	struct debts mr_e[3] =
    	{
    		"Cat mioa", 10.1,
    		"Dog Boat", 20.1,
    		"Fish ni", 30.1
    	};
    	double *pd[3];
    	for (int i = 0; i < 3; i++)
    	{
    		pd[i] = &mr_e[i].amount;
    	}
    
    	ShowArray(things, 6);
    
    	ShowArray(pd, 3);
    
    	cout << SumArray(things, 6) << endl;
    	cout <<SumArray(pd, 3) << endl;
    	return 0;
    }
    
    int main()
    {
    	test8_7();
    
    	system("pause");
    	return 0;
    }
    

      

  • 相关阅读:
    动图+源码,演示 Java 中常用数据结构执行过程及原理
    16 个超级实用的 Java 工具类
    图解 Java 垃圾回收机制,写得非常好!
    一些值得收藏的开源框架
    JVM 发生内存溢出的 8 种原因、及解决办法
    VC的function类说明 -- 继续
    引用文章 如何在lambda中引入递归调用
    VC中function函数解析
    folly无锁队列正确性说明
    C++ Programming Language中的Calculator源代码
  • 原文地址:https://www.cnblogs.com/my-cat/p/6129628.html
Copyright © 2020-2023  润新知