栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

#include
#include
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
#include
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
#include
#include
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 <
	for (int i = 0; i < a.size(); i++)
	{
		a[i] = toupper(a[i]);
	}
}

#include
using namespace std;
#include;
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< 1)
	{
		cout << first.str << endl;
		num--;
	}
}
void show(const char * first, int num)
{
	cout << first << endl;
	while (num > 1)
	{
		cout << first << endl;
		num--;
	}
}

#include
using namespace std;
template 
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 
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 
#include 
using namespace std;

template 
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 
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 

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

template 
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 
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 
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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1030601.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号