C++ Primer Plus第六版编程题(第8章)

本文提供了C++ Primer Plus第六版第八章的编程题解答,包括字符串操作、结构体使用、模板函数及最大值查找等关键编程任务的实现,通过实例演示了C++中不同类型和结构的处理技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++ Primer Plus第六版编程题(第8章)

题目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

程序

8.1

#include <iostream>
using namespace std;
void print(const char * str,int n=0);
int count=1;
	
int main()
{
	char p[]="**";
	print(p);
	print(p,3);
	print(p,8);
	print(p);
	return 0;
}

void print(const char * str,int n)
{
	if(n==0)
	{
		cout<<str<<endl;
	}	
	else
	{
		for(int i=0;i<count;i++)
		{
			cout<<str;
		}	
		cout<<endl;
	}
	count++;
}

8.2

#include <iostream>
#include <cstring>
using namespace std;
const int SIZE=40;

struct CandyBar
{
	char name[SIZE];
	double weight;
	int calories;
};

void set(CandyBar &candy,char *name="Millennium Munch",double weight=2.85,int calories=350);
void show(const CandyBar &candy);
	
int main()
{
	CandyBar candy1,candy2;
	char name[SIZE];
	double weight;
	int calories;
	set(candy1);
	show(candy1);
	
	cout<<"Name: ";
	cin.getline(name,SIZE);
	cout<<"Weight: ";
	cin>>weight;
	cout<<"Calories: ";
	cin>>calories;
	set(candy2,name,weight,calories);
	show(candy2);
	return 0;
}

void set(CandyBar &candy,char *name,double weight,int calories)
{
	strcpy(candy.name,name);
	candy.weight=weight;
	candy.calories=calories;
}
void show(const CandyBar &candy)
{
	cout<<"Name: "<<candy.name<<endl;
	cout<<"Weight: "<<candy.weight<<endl;
	cout<<"Calories: "<<candy.calories<<endl;
}

8.3

#include <iostream>
#include <string>
using namespace std;
void toup(string &letter);
	
int main()
{
	string letter;
	cout<<"Enter a string(q to quit): ";
	getline(cin,letter);
	while(letter!="q")
	{
		toup(letter);
		cout<<"Next string(q to quit): ";
		getline(cin,letter);
	}
	cout<<"Bye."<<endl;
	return 0;
}

void toup(string &letter)
{
	for(int i=0;i<letter.size();i++)
	{
		letter[i]=toupper(letter[i]);
	}
	cout<<letter<<endl;
}

8.4

#include <iostream>
using namespace std;
#include <cstring>
struct stringy
{
	char * str;
	int ct;
};
void set(stringy & beany,char *str);
void show(const stringy & beany,int n=1);
void show(const char *str,int n=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 & beany,char *str)
{
	beany.ct=strlen(str);
	beany.str=new char[beany.ct+1];
	strcpy(beany.str,str);
}
void show(const stringy & beany,int n)
{
	for(int i=0;i<n;i++)
	{
		cout<<beany.str<<endl;
	}
	cout<<endl;
}
void show(const char *str,int n)
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<strlen(str);j++)
		{
			cout<<str[j];
		}
		cout<<endl;
	}
	cout<<endl;
}

8.5

#include <iostream>
using namespace std;
template <class T>
T max5(T num[]);
	
int main()
{
	int num1[5]={4,2,3,4,5};
	cout<<max5(num1)<<endl;
	double num2[5]={3.2,4.5,5.3,6.4,7.3};
	cout<<max5(num2)<<endl;
	return 0;
}

template <class T>
T max5(T num[])
{
	const int n=5;
	T max=num[0];
	for(int i=1;i<n;i++)
	{
//		if(max<num[i])
//		{
//			max=num[i];
//		}
		max=max>num[i]? max: num[i];
	}
	return max;
}

8.6

#include <iostream>
#include <cstring>
using namespace std;
template <class T>
T maxn(T num[],int n);

template <> char *maxn(char *str[],int n);
	
int main()
{
	int num1[6]={4,2,3,4,5,9};
	cout<<maxn(num1,6)<<endl;
	double num2[4]={4.5,5.3,6.4,7.3};
	cout<<maxn(num2,4)<<endl;
	char * str[5]={"dog","duck","box","computer","egg"};
	cout<<maxn(str,5)<<endl;
	return 0;
}

template <class T>
T maxn(T num[],int n)
{
	T max=num[0];
	for(int i=1;i<n;i++)
	{
//		if(max<num[i])
//		{
//			max=num[i];
//		}
		max=max>num[i]? max: num[i];
	}
	return max;
}
template <> char *maxn(char *str[],int n)
{
	char *max=str[0];
	int l=strlen(str[0]);
	for(int i=1;i<n;i++)
	{
		if(l<strlen(str[i]))
		{
			max=str[i];
			l=strlen(str[i]);
		}
	}
	return max;
}

8.7

#include <iostream>

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

template <typename T>            // template B
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};
    struct debts mr_E[3] =
    {
        {"Ima Wolfe", 2400.0},
        {"Ura Foxe", 1300.0},
        {"Iby Stout", 1800.0}
    };
    double * pd[3]; 

// set pointers to the amount members of the structures in mr_E
    for (int i = 0; i < 3; i++)
        pd[i] = &mr_E[i].amount;
    
    cout << "Listing Mr. E's counts of things: "<<SumArray(things,6)<<endl;
    
    cout << "Listing Mr. E's debts: "<<SumArray(pd,3)<<endl;
    // cin.get();
    return 0;
}

template <typename T>
T SumArray(T arr[], int n)
{
    using namespace std;
    cout << "template A\n";
    T sum=arr[0];
    for (int i = 1; i < n; i++)
    	sum+=arr[i];
    return sum;
    
}

template <typename T>
T SumArray(T * arr[], int n)
{
    using namespace std;
    cout << "template B\n";
    T sum=* arr[0];
    for (int i = 1; i < n; i++)
        sum+=*arr[i];
    return sum;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值