~cpp
//458page 1
#include<iostream.h>
int count=0;//함 호 횟
void say(const char *);
void say(char *, int);
void main()
{
int input;//하 횟 호하 해
char string[20]="hahahaha\n";
char *p=string;
cout<<"함 호 합?";
cin>>input;
for(int i=0;i<input;i++)
say(p);
say(p,count);
}
void say(const char *str)
{
cout<<str;
count++;
}
void say(char * str, int n)
{
cout<<" "<<n<<" 함 호 하.\n";
while(n!=0){
cout<<str;
n--;
}
}
=========================================================================
//458page 2
#include<iostream.h>
struct CandyBar{
char *name;
double wei;
int cal;
}candy;
char temp1[20];
double temp2;
int temp3;
CandyBar input(CandyBar &, char *company="Millenium Munch", double weight=2.85, int
calorie=350);
void show(CandyBar);
void main()
{
//
candy=input(candy);
show(candy);
//
cout<<"\n회 ?";
cin>>temp1;
cout<<"\n?";
cin>>temp2;
cout<<"\n?";
cin>>temp3;
candy=input(candy, temp1, temp2, temp3);
show(candy);
}
CandyBar input(CandyBar & anycandy, char *company, double weight, int calorie)
{
CandyBar answer;
answer.name=company;
answer.wei=weight;
answer.cal=calorie;
return answer;
}
void show(CandyBar anycandy)
{
cout<<"\n표: "<<anycandy.name;
cout<<"\n: "<<anycandy.wei;
cout<<"\n: "<<anycandy.cal;
}
==================================================================
//458page 3
#include<iostream.h>
#include<string.h>
struct stringy{
char *str;// 키 포
int ct; // ('\0' )
};
//set(), show() 형 show()
void show(stringy string, int n=1);
void show(char * str, int n=1);
void set(stringy &, char *);
int main()
{
stringy beany;
char testing[]="Reality isn't what it used to be.";
set(beany, testing);//
//testing 할 할하
//beany str 하
하
//testing 하
//beany ct 한.
show(beany);
show(beany,2);
testing[0]='D';
testing[1]='u';
show(testing); //testing 한 한
show(testing, 3);//testing 한
show("Done!");
return 0;
}
void show(stringy string, int n)
{
for(int i=0;i<n;i++)
cout<<string.str<<endl;
}
void show(char * str, int n)
{
for(int i=0;i<n;i++)
cout<<str<<endl;
}
void set(stringy &string1, char *string2)
{
string1.str=string2;
string1.ct=strlen(string1.str);
}
==================================================================
//458page 4(-> 화
-> 해 )
//헤파
//golf.h
const int Len=40;
struct golf{
char fullname[Len];
int handicap;
};
//함 핸 한
// 1 하, 0 한
int setgolf(golf &g);
//함 하
//golf 핸 한
void setgolf(golf &g, const char *name, int hc);
//함 handicap 화한
void handicap(golf &g, int hc);
//함 golf 표한
void showgolf(const golf &g);
//file1
#include<iostream.h>
#include<string.h>
#include"golf.h"
int setgolf(golf &g)
{
cout<<"\n ?";
cin>>g.fullname;
cout<<"\n핸 ?";
cin>>g.handicap;
if(g.fullname=='\0')
return 0;
else
return 1;
}
void setgolf(golf &g, const char *name, int hc)
{
int n=strlen(name);
for(int i=0;i<n;i++)
g.fullname[i]=name[i];
g.handicap=hc;
}
void handicap(golf &g, int hc)
{
g.handicap=hc;
}
void showgolf(const golf & g)
{
cout<<"\nGolfer's Name: "<<g.fullname<<endl;
cout<<g.fullname<<"'s Handicap: "<<g.handicap;
}
//file2
#include<iostream.h>
#include<string.h>
#include"golf.h"
void main()
{
int n;
int handi;
char name[40];
golf golfer1;
do{
n=setgolf(golfer1);//1
if(n==1){
showgolf(golfer1);
cout<<"\n 하: ";
cin>>name;
cout<<"\n하 핸 하: ";
cin>>handi;
setgolf(golfer1, name, handi);
showgolf(golfer1);
cout<<"\n하 핸 하: ";
cin>>handi;
handicap(golfer1, handi);
showgolf(golfer1);
}
}while(1);
}
====================================================================
//458page 5
#include<iostream.h>
double array_double[5]={1.2, 13.4, 43.0, 33.2, 456.8};
int array_int[5]={10, 20, 30, 40, 43};
int count=0;
template<class T>
T max5(T array5[5]);
void main()
{
double maximum_double=max5(array_double);
int maximum_int=max5(array_int);
cout<<" 큰 "<<maximum_double<<endl;
cout<<" 큰 "<<maximum_int<<endl;
}
template<class T>
T max5(T array5[5])
{
T max;
for(int i=0;i<5;i++){
count=0;
for(int j=0;j<5;j++){
if(array5[i]>array5[j])
count++;
}
if(count==4){
max=array5[i];
break;
}
}
return max;
}
====================================================================
//458page 6
#include<iostream.h>
#include<string.h>
template<class T>
T max(T arr[], int k);
template<class T>
void max<char *>(char * arr[], int i);
int main()
{
int arr_int[6]={10, 20, 30, 40, 50, 100};
double arr_double[4]={10.2, 20.5, 40.6, 132.4};
char * arr_char_p[5];
arr_char_p[0]="Drive";
arr_char_p[1]="Fly";
arr_char_p[2]="Swim";
arr_char_p[3]="Murder";
arr_char_p[4]="Write";
int ret_int=max(arr_int, 6);
double ret_double=max(arr_double, 4);
char * ret_char_p=max(arr_char_p, 5);
cout<<ret_int<<endl;
cout<<ret_double<<endl;
cout<<ret_char_p<<endl;
return 0;
}
template<class T>
T max(T arr[], int k)
{
T temp=arr[0];
for(int i=0;i<k;i++){
if(temp<arr[i])
temp=arr[i];
}
return temp;
}
char * max<char *>(char * arr[], int k)
{
char * temp=arr[0];
for(int i=0;i<k;i++){
if(strlen(temp)<strlen(arr[i]))
temp=arr[i];
}
return temp;
}