//
// main.cpp
// C++PrimerPlusEight
//
// Created by amengdev on 16/3/26.
// Copyright © 2016年 amengdev. All rights reserved.
//
#include <iostream>
#include "string"
using namespace std;
/*
//习题6:模板求数组的最大值,具体化求最长的字符串地址
template <typename T>
T maxn(T arr[],int n);
template <> char* maxn(char *arr[],int n);
int main()
{
int arr1[6]={4,2,6,2,4,2};
cout<<maxn(arr1, 6)<<endl;
double arr2[4]={2.1,5.3,5.5,3.2};
cout<<maxn(arr2, 4)<<endl;
char *arr3[4]={"abc","abcd","fd","fdf"};
cout<<maxn(arr3, 4)<<endl;
return 0;
}
template <typename T>
T maxn(T arr[],int n)
{
T max=arr[0];
for(int i=1;i<n;i++)
{
if(max<arr[i])
max=arr[i];
}
return max;
}
template <> char* maxn(char *arr[],int n)
{
char *adress=arr[0];
for(int i=1;i<n;i++)
{
if(strlen(adress)<strlen(arr[i]))
adress=arr[i];
}
return adress;
}
*/
/*
//习题5:模板计算数组的最大值
template <typename T>
T max5(T arr[]);
int main()
{
int arr1[5]={1,2,5,4,3};
cout<<max5(arr1)<<endl;
double arr2[5]={1.2,2.4,6.5,2.1,3.3};
cout<<max5(arr2)<<endl;
return 0;
}
template <typename T>
T max5(T arr[])
{
T max=arr[0];
for(int i=1;i<5;i++)
{
if(max<arr[i])
max=arr[i];
}
return max;
}
*/
/*
//习题4:结构体引用
struct stringy{
char *str;
int ct;
};
void set(stringy &stry,const char *str);
void show(const stringy &stry);
void show(const stringy &stry,int num);
int main()
{
stringy beany;
char testing[]="Reality isn't what it used to be.";
set(beany,testing);
show(beany);
show(beany,2);
return 0;
}
void set(stringy &stry,const char *str)
{
stry.str=new char[20];
strcpy(stry.str, str);
stry.ct=strlen(str);
}
void show(const stringy &stry)
{
cout<<stry.str<<endl;
}
void show(const stringy &stry,int num)
{
for(int i=0;i<num;i++)
{
cout<<stry.str<<endl;
}
}
*/
/*
//习题3
//字符串引用,字符串大小写转换
void trans(string &str);
int main()
{
string str;
cout<<"Enter a string (q to quit):";
while (getline(cin,str)) {
if(str=="q")
break;
trans(str);
cout<<str<<endl;
cout<<"Next string (q to quit):";
}
cout<<"Bye"<<endl;
return 0;
}
void trans(string &str)
{
for(int i=0;i<str.length();i++)
{
str[i]=toupper(str[i]);
}
}
*/
/*
//习题2:结构体使用引用
const int nameMax=20;
struct CandyBar
{
char name[nameMax];
double weight;
int ka;
};
void input(CandyBar &C,const char *name,const double weight,const int ka);
void show(const CandyBar &C);
int main()
{
CandyBar c1;
CandyBar c2;
input(c1, "name1:", 10.5, 10);
show(c1);
return 0;
}
void input(CandyBar &C,const char *name,const double weight,const int ka)
{
strcpy(C.name, name);
C.weight=weight;
C.ka=ka;
}
void show(const CandyBar &C)
{
cout<<"name:"<<C.name<<endl;
cout<<"weight:"<<C.weight<<endl;
cout<<"ka:"<<C.ka<<endl;
}
*/