第八章 函数探幽
第一题:编写一个函数,可以接收两个参数,其中一个为字符串地址,另一个为数字并带有默认参数,使用默认参数时,打印字符 串一次,不使用默认参数时,则打印次数为该函数被调用的次数。
#include<iostream>
#include<string>
using namespace std;
void showstr(string *str,int b=0);
static int call;
int main()
{
string str1="I am a good boy!";
showstr(&str1);
showstr(&str1);
cout<<"/Now showstr has been called twice./\n";
showstr(&str1,1);
system("pause");
return 0;
}
void showstr( string *str,int b)
{
if(b==0)
cout<<*str<<endl;
else
{
for(int i=0;i<call;i++)
cout<<*str<<endl;
}
call++;
}
第二题:定义一个包含三种元素的结构体,然后定义一个接受结构体引用,char指针,double和int作为参数,并用后三个参数对结构体赋值。还定义一个接受结构体引用的函数,并显示结构体内容。
#include<iostream>
#include<string.h>
using namespace std;
static const int sizena=40;
struct CandyBar{char name[sizena];double weight;int calorie;};
void setc(CandyBar &str,const char *na="Millennium Munch",const double wei=2.85,const int cal=350);
void show(CandyBar &str);
int main()
{
CandyBar albes;
setc(albes);
show(albes);
system("pause");
return 0;
}
void setc(CandyBar &str,const char *na,const double wei,const int cal)
{
str.calorie=cal;
str.weight=wei;
int i=0;
for(;i<sizena;i++) //字符串填充;
{
str.name[i]=na[i];
}
}
void show(CandyBar &str)
{
cout<<"Name : "<<str.name<<endl;
cout<<"Weight : "<<str.weight<<endl;
cout<<"Calorie : "<<str.calorie<<endl;
}
第三题:编写一个接受string对象引用的函数,并将结果转换为大写,循环接受输入,直到输入q结束。
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
void trasn(string &str,int a);
int main()
{
string str;
cout<<"Pleas enter a string(q to quit): ";
getline(cin,str);
while(cin)
{
if(str!="q")
{
trasn(str,str.size());
cout<<"Next string(q to quit): ";
getline(cin,str);
}
else
{
cout<<"Bye."<<endl;
break;
}
}
system("pause");
return 0;
}
void trasn(string &str,int a)
{
for(int i=0;i<a;i++)
{
cout<<char(toupper(str[i]));
}
cout<<endl;
}
第四题:按要求补完程序
#include<iostream>
#include<string>
using namespace std;
struct stringy
{
char *str;
int ct;
};
void set(stringy &a, const char b[]);
void show(stringy a,int b=1);
void show(string a,int b=1);
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be. ";
cout <<testing<<endl;
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 &a, const char b[])
{
int len = strlen(b);
char *pc = new char[len];
for (int i = 0; i < len; i++)
{
pc[i]= b[i];
}
a.str = pc;
}
void show(string a,int b)
{
if (b == 1)
cout << a<<endl;
else
{
for (int i = 0; i < b; i++)
cout << a << endl;
};
}
void show(stringy a,int b)
{
if (b == 1)
cout << a.str<<endl;
else
{
for (int i = 0; i < b; i++)
cout << a.str<<endl;
}
}
ps:通过显示的结果来看,关于指针的使用此处存在问题,待以后修改。
第五题:编写函数模板max5(),包含5个T类型数据,返回5个中的最大值,用int型和double型数据进行测试。
#include<iostream>
using namespace std;
template <typename T>
T max5(T arr[], int count=5);
int main(void)
{
int arr1[5] = { 13, 31, 103, 301, 310 };
double arr2[5] = { 13.6, 31.5, 103.26, 301.11, 310.21 };
cout << "The int test data contains: ";
for (int i = 0; i < 5; i++)
cout << arr1[i] << " ";
cout<<"\nSO the max value is " << max5(arr1) << endl;
cout << "The double test data contains: ";
for (int i = 0; i < 5; i++)
cout << arr2[i] << " ";
cout << "\nSO the max value is " << max5(arr2) << endl;
system("pause");
return 0;
}
template <typename T>
T max5(T arr[], int count)
{
T temp = arr[0];
for (int i = 1; i < count; i++)
if (temp < arr[i])
temp = arr[i];
return temp;
}
第六题:编写函数模板maxn(),包含n个T类型数据的数组参数,和一个表示数组个数参数,返回数组中的最大值。使用一个 6个型号的int 整型及4个double型的数组来测试。并编写要给具体化,针对char型数组,返回数组中最长的字符串地址。若有多个最长,则返回的第一个最长。
#include<iostream>
#include<string>
using namespace std;
template <typename T>
T maxn(T arr[], int count);
template <> char *maxn(char *arr[], int n);
int main(void)
{
int arr1[] = { 13, 31, 103, 301, 310,999 };
double arr2[] = { 13.6, 31.5, 103.26, 301.11 };
char *arr3[5]= { "What a good day !","Here you are.","I am a good boy.","Get out of here,ok?","Come on ,man~" };
cout << "The int test data contains: ";
for (int i = 0; i < sizeof(arr1)/sizeof(int); i++)
cout << arr1[i] << " ";
cout << "\nSO the max value is " << maxn(arr1,sizeof(arr1)) << endl;
cout << "The double test data contains: ";
for (int i = 0; i <sizeof(arr2) / sizeof(double); i++)
cout << arr2[i] << " ";
cout << "\nSO the max value is " << maxn(arr2, sizeof(arr2)) << endl;
cout << "SO the longest string is "<< "\'"<<maxn(arr3, 5) << "\'" <<endl;
system("pause");
return 0;
}
template <typename T>
T maxn(T arr[], int count)
{
T temp = arr[0];
for (int i = 1; i < count/sizeof(T); i++)
if (temp < arr[i])
temp = arr[i];
return temp;
}
template <> char *maxn(char *arr[], int n)
{
char *temp = arr[0];
cout << strlen(*arr);
cout << "\'"<<arr[0]<<"\' length is "<<strlen(arr[0])<<endl;
for (int i = 1; i < n; i++)
{
cout << "\'" << arr[i] << "\' length is " << strlen(arr[i]) << endl;
if (strlen(temp) < strlen(arr[i]))
temp = arr[i];
}
return temp;
}
ps:关于如何自动计算指针个数不是很清楚,所有maxn在处理字符串时,采用的时直接输入指针个数。
第七题:修改示例8.14,由原来的显示数据改成计算数组总和,并显示全部的总和。
#include<iostream>
using namespace std;
template <typename T>
void ShowArray(T arr[], int n);
template <typename T>
void ShowArray(T *arr[], int n);
struct debts
{
char name[50];
double amount;
};
int main(void)
{
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;
ShowArray(things, 6);
cout << "listing Mr.E's debts:\n";
ShowArray(pd, 3);
system("pause");
return 0;
}
template <typename T>
void ShowArray(T arr[], int n)
{
T temp = 0;
cout << "Tmplate A\n";
for (int i = 0; i < n; i++)
temp+=arr[i];
cout <<temp<< endl;
}
template <typename T>
void ShowArray(T *arr[], int n)
{
double temp=0;
cout << "Tmplate B\n";
for (int i = 0; i < n; i++)
temp+=*arr[i];
cout << temp << endl;
}
第八章磕磕碰碰的完成了,一边继续推进,一边抽个时间复习吧。
加油,努力,坚持,早日实现经济自由!