C++ Primer Plus(第6版)---编程作业完成(第八章)

#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;
//函数原型
void p8_1(void);
void p8_2(void);
void p8_3(void);
void p8_4(void);
void p8_5(void);
void p8_6(void);
void p8_7(void);
//主函数
int main()
{
	//p8_1();
	//p8_2();
	//p8_3();
	//p8_4();
	//p8_5();
	//p8_6();
	p8_7();
	return 0;
}
//函数定义
//-----------作业一----------------
void print_str(const string str,int num=1)
{
	cout<<str<<"\t"<<num<<endl;
	if(num>1)
	{
		num--;
		print_str(str,num);
	}
		
	return;
}
void p8_1(void)
{
	cout<<"Enter the string: ";
	string str;
	getline(cin,str);
	cout<<"Enter the time: ";
	int time=0;
	cin>>time;
	print_str(str,time);
	cout<<"--------------"<<endl;
	print_str(str);
	return;
}

//-------------------作业二-------------------
struct candybar
{
	char* name;
	double weight;
	int carlo;
};
void candy_fill(candybar & can,char* str,double wei,int car)
{
	can.name=str;
	can.weight=wei;
	can.carlo=car;
}
void show_candy(const candybar & candy)
{
	cout<<candy.name<<endl;
	cout<<candy.weight<<endl;
	cout<<candy.carlo<<endl;
}
void p8_2(void)
{
	candybar candy;
	char name[50];
	double weight;
	int carlo;
	cout<<"Enter the name:";
	cin.getline(name,50);
	cout<<"Enter the weight:";
	cin>>weight;
	cout<<"Enter the carlo:";
	cin>>carlo;
	candy_fill(candy,name,weight,carlo);
	show_candy(candy);
	return;
}


//-----------------作业三----------------
//toupper(),参数小写返回大写,否则返回其参数
void convert_str(string & str)
{
	for(int i=0;i<(str.length());i++)
		str[i]=toupper(str[i]);//string也可以如同数组使用
	cout<<str<<endl;
}
void p8_3(void)
{
	cout<<"Enter a string(q to quit):";
	string str;
	getline(cin,str);
	while(str!="q")
	{
		convert_str(str);
		cout<<"Next string(q to quit):";
		getline(cin,str);
	}
	return;
}

//-------------作业四---------------------
struct stringy
{
	char* str;
	int ct;
};
void show(stringy & str1,int times=1)
{
	while(times>0)
	{
		cout<<str1.str<<endl;
		times--;
	}
	return;
}
void show(const char* str2,int times=1)
{
	while(times>0)
	{
		cout<<str2<<endl;
		times--;
	}
	return;
}
void set(stringy & str3,const char* arr)
{
	int num=strlen(arr);//strlen不计算\0
	char * ps=new char[num+1];//记得delete
	/*for(int i=0;i<(num+1);i++)
		ps[i]=arr[i];*/
	strcpy(ps,arr);//两种都可以运行,此处有警告
	//此处要自己补'\0'
	str3.str=ps;
	str3.ct=strlen(arr);
}
void p8_4(void)
{
	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!");
	delete [] beany.str;
	return;
}

//----------------作业五-------------------
template<typename T>
T max5(const T arr[])
{
	T max=arr[0];
	for(int i=0;i<5;i++)
	{
		if(arr[i]>max)
			max=arr[i];
	}
	return max;
}
void p8_5(void)
{
	int arr1[5]={1,2,3,4,5};
	double arr2[5]={11.1,12.0,1.0,5.8,20.3};
	int max_int=max5(arr1);
	cout<<max_int<<endl;
	double max_double=max5(arr2);
	cout<<max_double<<endl;
	return;
}


//-----------作业六--------------
template<typename T>
T maxn(T arr[],int num)
{
	T max=arr[0];
	for(int i=0;i<num;i++)
	{
		if(arr[i]>max)
			max=arr[i];
	}
	return max;
}
//模板具体化,显式具体化
template<> char* maxn<char*>(char* arr1[],int num)
{
	//用strlen()函数
	char* ps=arr1[0];
	for(int i=0;i<num;i++)
	{
		if(strlen(arr1[i])>strlen(ps))
			ps=arr1[i];
	}
	return ps;
}
void p8_6(void)
{
	int arr1[6]={1,2,3,4,5,6};
	double arr2[4]={10.1,20.2,30.3,40.4};
	char* arr3[5]={"HELLO","hi","APP","aaa","nb"};
	int max_int=maxn(arr1,6);
	double max_double=maxn(arr2,4);
	char* max_char=maxn(arr3,5);
	cout<<max_int<<endl;
	cout<<max_double<<endl;
	cout<<max_char<<endl;
	return;
}


//--------------作业七------------------
struct debts
{
	char name[50];
	double amount;
};
template<typename T>
T sum_array(T arr[],int num)
{
	T sum=0;
	for(int i=0;i<num;i++)
		sum+=arr[i];
	return sum;
}
template<typename T>
T sum_array(T* arr[],int num)
{
	T sum=0;
	for(int i=0;i<num;i++)
		sum+=*arr[i];
	return sum;
}

void p8_7(void)
{
	int things[6]={1,2,3,4,5,6};
	struct debts my[3]=
	{{"AAA",10.0},
	{"BBB",20.0},
	{"CCC",30.0}
	};
	int sum_int=sum_array(things,6);
	cout<<sum_int<<endl;
	double* arr[3];
	for(int i=0;i<3;i++)
	{
		arr[i]=&(my[i].amount);
	}
	double sum_double=sum_array(arr,3);
	cout<<sum_double<<endl;
}

如有错误,欢迎指正! 

上一篇:Mybatis-Plus入门系列(18) -基于注解的动态数据权限实现方案


下一篇:用例与需求