C++ primer 第八章课后练习题

在这里插入图片描述

#include<iostream>
#include<string>
using namespace std;
void string_display(string* ad, int num);
int main()
{
	string a = "dhsaih caishihsa saia";
	string* b = &a;
	string_display( b, 5);


}
void string_display(string* ad, int num)
{
	if (num > 0)
	{
		cout << *ad << endl;
		string_display(ad, num - 1);
	}
}

在这里插入图片描述

#include<iostream>
#include<cstring>
using namespace std;
struct candy_bar 
{
    char name[20];
    double weight;
    int hot;
};
void strcut_value(candy_bar& a, const char* name = "Miilennium Munch", double weight=2.85, int hot=350);
void display(candy_bar a);
int main()
{
    candy_bar a ;
    char name[20] = "dbhsab dh";
    char* b = name;
    strcut_value(a, b, 22, 1);
    display(a);
    candy_bar c;
    strcut_value(c);
    display(c);

}
void strcut_value(candy_bar& a, const char* name, double weight, int hot)
{
    strcpy_s(a.name, name);
    a.weight = weight;
    a.hot = hot;
}
void display(candy_bar a)
{
    cout << "Name: " << a.name << endl;
    cout << "Weight: " << a.weight << endl;
    cout << "Hot: " << a.hot << endl;
}

在这里插入图片描述

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
void to_up(string& a);
int main()
{
	string a;
	cout << "Enter a string (q to quit):";
	while (getline(cin, a) && a != "q")
	{
		to_up(a);
		cout <<a<<endl;
		cout << "Enter next string:(q to quit):";
	}

}
void to_up(string& a)
{
	for (int i = 0; i < a.size(); i++)
	{
		a[i] = toupper(a[i]);
	}
}

在这里插入图片描述

#include<iostream>
using namespace std;
#include<cstring>;
struct stringy {
	char* str;
	int ct;
};
void set(stringy& first, const char* test);
void show(const stringy& first, int num=1);
void show(const char* first, int num = 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& first,const char* test)
{
	first.str = new char[strlen(test) + 1];
	strcpy_s(first.str, strlen(test) + 1,test);
}
void show(const stringy& first, int num)
{
	cout << first.str<<endl;
	while (num > 1)
	{
		cout << first.str << endl;
		num--;
	}
}
void show(const char * first, int num)
{
	cout << first << endl;
	while (num > 1)
	{
		cout << first << endl;
		num--;
	}
}

在这里插入图片描述

#include<iostream>
using namespace std;
template <typename T>
T Max5(const T* array);

int main()
{
	int test1[5] = { 1, 3, 5, 7, 9 };
	double test2[5] = { 10.0, 20.0, 15.0, 12.0, 30.0 };

	cout << "The max of int array is: " << Max5(test1) << endl;
	cout << "The max of double array is: " << Max5(test2) << endl;
	return 0;
}
template <typename T>
T Max5(const T* array)
{
	T tempt = array[0];
	for (int i = 0; i < 5; i++)
	{
		if (array[i] > tempt)
			tempt = array[i];
	}
	return tempt;
}

在这里插入图片描述

#include <iostream>
#include <cstring>
using namespace std;

template <typename T>
T maxn(T *arr, int n);

template <>
const char *maxn(const char *str[], int n);

int main()
{
    int test1[6] = {1, 3, 5, 7, 9, 11};
    double test2[4] = {10.0, 20.0, 15.0, 12.0};
    const char *test3[5] = {"123", "12345", "123456", "abcdefg", "xio"};

    cout << "The max of int array is: " << maxn(test1, 6) << endl;
    cout << "The max of double array is: " << maxn(test2, 4) << endl;
    cout << "The max of string array is: " << maxn(test3, 5) << endl;

    return 0;
}

template <typename T>
T maxn(T *arr, int n)
{
    T temp = arr[0];

    for (int i = 0; i < n; i++)
    {
        if (temp < arr[i])
        {
            temp = arr[i];
        }
    }
    return temp;
}

template <>
const char *maxn(const char *str[], int n)
{
    const char *temp = *str;

    for (int i = 1; i < n; i++)
    {
        if (strlen(temp) < strlen(str[i]))
        {
            temp = str[i];
        }
    }
    return temp;
}

在这里插入图片描述

#include <iostream>

template <typename T>
T SumArray(T arr[], int n);

template <typename T>
T SumArray(T *arr[], int n);

struct debts
{
    char name[50];
    double amount;
};

int main()
{
    using namespace std;
    int things[6] = {13, 31, 103, 301, 310, 130};
    debts mr_E[3] =
    {
        {"Ima Wolfe", 2400.0},
        {"Ura Foxe", 1300.0},
        {"Iby Stout", 1800.0}
    };

    double *pd[3];
    for (int i = 0; i < 3; i++)
    {
        pd[i] = &mr_E[i].amount;
    }

    cout << "Listing Mr. E's total of things:" << endl;
    cout << SumArray(things, 6) << endl;
    cout << "Listing Mr. E's total of debts:" << endl;
    cout << SumArray(pd, 3) << endl;

    return 0;
}

template <typename T>
T SumArray(T arr[], int n)
{
    using namespace std;
    double sum = 0.0;
    for (int i = 0; i < n; i++)
    {
        sum += arr[i];
    }
    return sum;
}

template <typename T>
T SumArray(T *arr[], int n)
{
    using namespace std;
    double sum = 0.0;
    for (int i = 0; i < n; i++)
    {
        sum += *arr[i];
    }
    return sum;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值