C++ Primer Plus (第6版)编程练习 代码-----第八章

本文深入探讨了C++编程语言的基本概念、结构和高级特性,通过实例展示了如何使用C++进行高效的数据处理和算法实现。

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

1.太笨了,真的没有看懂这个练习题



8.2


#include "stdafx.h"
#include<iostream>
#include <string>
#include<cctype>
using namespace std;
struct CandyBar
{
	char *name;
	float weight;
	int cal;

};
void display(CandyBar &cb,char *name="Millennium Munch",float weight=2.85,int cal=350);
int _tmain(int argc, _TCHAR* argv[])
{
	
	CandyBar bar,bar1;
	display(bar,"sugarsugar", 20.3,800);
	display(bar1);
	
	system("pause");
	return 0;
}
void display(CandyBar &cb,char *name,float weight,int cal)
{
	cb.name=name;
	cb.weight=weight;
	cb.cal=cal;
	cout<<"name:"<<cb.name<<endl;
	cout<<"weight:"<<cb.weight<<endl;
	cout<<"calories:"<<cb.cal<<endl;
}


</pre><pre name="code" class="cpp">//8.3
void change(string &str);
int _tmain(int argc, _TCHAR* argv[])
{
	string in;
	cout<<"Enter a string (q to quit): ";
	getline(cin,in);
	while(in!="q\0")
	{
		change(in);
	   cout<<in<<endl<<"Next string(q to quit): ";
	   getline(cin,in);
	}
	cout<<"Bye.";
	system("pause");
	return 0;
}
void change(string &str)
{
	int n=str.length();
	for(int i=0;i<n;i++)
	{
		str[i]=toupper(str[i]);
	}
	
}


8.4

struct stringy{
    char * str;
	int ct;
};
void set(stringy &s,char ch[]);
void show(const stringy &s ,int n=1);
void show(const char ch[] ,int n=1);
int _tmain(int argc, _TCHAR* argv[])
{
	stringy beany;
	char testing[]= "Reality isn't what is used to be.";
	set(beany,testing);


	show(beany);
	show(beany,2);
	testing[0]='D';
	testing[1]='u';
	show(testing);
	show(testing,3);
	show("Done!");
	system("pause");
	return 0;
}

void set(stringy &s,char ch[])
{
	s.str=ch;
	s.ct=strlen(s.str);
}
void show(const stringy &s ,int n)
{
	for(int i=0;i<n;i++)
	{
		cout<<s.str<<endl;
		cout<<s.ct<<endl<<endl;
	
	}

}
void show(const char ch[] ,int n)
{
   for(int i=0;i<n;i++)
	{
		
		cout<<ch<<endl<<endl;
	
	}

}



8.5


template<typename T>
void max5(T s[]);

int _tmain(int argc, _TCHAR* argv[])
{
	int t1[5]={1,2,4,5,3};
	double t2[5]={2.01,1.0567,3.05678,4.02345,5.12340};
	max5(t1);
	max5(t2);
	system("pause");
	return 0;
}
template<typename T>
void max5(T s[])
{
	
	T a=s[0];
	for(int i=0;i<5;i++)
	{
		cout<<i+1<<": "<<s[i]<<endl;
		
		if(a<s[i])
		 a=s[i];
		
	}
	cout<<"The max is :"<<a<<endl<<endl<<endl;
 
}



8.6  

template<typename T>
void maxn(T s[],int n);

template<>void maxn<char*>(char *st[],int n); 
int _tmain(int argc, _TCHAR* argv[])
{
	int t1[6]={1,2,4,5,3,0};
	double t2[4]={2.01,1.0567,3.05678,4.02345};
	maxn(t1,6);
	maxn(t2,4);
	char *s1[5]={"abc","defgh","kl","qwer578ddahg","applepie"};
	maxn(s1,5);
	system("pause");
	return 0;
}
template<typename T>
void maxn(T s[],int n)
{
	
	T a=s[0];
	for(int i=0;i<n;i++)
	{
		cout<<i+1<<": "<<s[i]<<endl;
		
		if(a<s[i])
		 a=s[i];
		
	}
	cout<<"The max is :"<<a<<endl<<endl<<endl;
 
}
template<>void maxn<char*>(char *st[],int n)
{
    int a=strlen(st[0]);
	int max=0;
	
	for(int i=0;i<n;i++)
	{
		cout<<i+1<<": "<<st[i]<<endl;
		int b=strlen(st[i]);
		if(a<b)
		{  a=b;
		  max=i;
		}
		
	}
	cout<<"The max is :"<<st[max]<<" has "<<a<<" character."<<endl<<endl<<endl;

}

8.7

//例子中的函数没有改动
template<typename T>
void ShowArray(T arr[],int n);


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

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


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

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

};
int main()
{
	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];

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

	cout<<"Listing Mr.E's count of things:\n";

	ShowArray(things,6);
	SumArray(things,6);
	cout<<"Listing Mr.E's debts:\n";
	ShowArray(pd,3);
	SumArray(pd,3);
	system("pause");
	return 0;
	
}


template <typename T >
void ShowArray(T arr[],int n)
{
	cout<<"template A \n";

	for(int i=0;i<n;i++)
	  cout<<arr[i]<<" ";
	 cout<<endl;
}

template <typename T >
void ShowArray(T *arr[],int n)
{
  cout<<"template B \n";

	for(int i=0;i<n;i++)
	  cout<<*arr[i]<<" ";
	 cout<<endl;
}

template <typename T >
void SumArray(T arr[],int n)
{
	cout<<"The sum of things is :";
    int sum=0;
	for(int i=0;i<n;i++)
	  sum=sum+arr[i];
	 cout<<sum<<endl;
}

template <typename T >
void SumArray(T *arr[],int n)
{
  cout<<"The sum of debt is :";
    double sum=0.0;
	for(int i=0;i<n;i++)
	  sum=sum+*arr[i];
	 cout<<sum<<endl;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值