~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;
}