~cpp //8_1 #include <iostream> using namespace std; void Print(char *in,int i=0); int main() { char *ex="캬캬캬 출력한다.~"; Print(ex); Print(ex,0); Print(ex,10); char *ex2="2번재 출력"; Print(ex2,100); return 0; } void Print(char *in,int i) { static int count=0; if(i==0) cout<<in<<"\t"<<count<<"\n"; else { int tempcount=count; while(tempcount>0) { cout<<in<<"\t"<<count<<"\n"; tempcount--; } } count++; } 2번 //Programming8_2.cpp #include <iostream> using namespace std; struct CandyBar { char *Brand; double Weight; int Calory; }; void StructFunction(CandyBar &in, char *brand="Millennium Munch", double weight=2.85,int calory=350); int main() { CandyBar ex1,ex2; StructFunction(ex1,"kakakaka",4.67,400); StructFunction(ex2); return 0; } void StructFunction(CandyBar &in,char *brand,double weight, int calory) { in.Brand = brand; in.Calory = calory; in.Weight = weight; cout<<in.Brand<<'\t'<<in.Weight<<'\t'<<in.Calory<<'\n'; } 3번 //Programming8_3 #include <iostream> using namespace std; #include <cstring> struct stringy { char *str; int ct; }; void set(stringy &in, char copied[]); void show(const stringy in, int iteration=1); void show(const char in[], int iteration=1); int main() { 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!"); return 0; } void set(stringy &in, char copied[]) { int ct=strlen(copied); in.str = new char [ct-3]; ct=strlen(in.str); strncpy(in.str,copied,ct); } void show(const stringy in, int iteration) { while(iteration>0) { cout<<in.str<<"\n"; iteration--; } } void show(const char in[], int iteration) { while(iteration>0) { cout<<in<<"\n"; iteration--; } } 4번 //header.h const int Len = 40; struct golf { char fullname[Len]; int handicap; }; int setgolf(golf &g); void setgolf(golf &g, const char *name,int hc); void handicap(golf &g, int hc); void showgolf(const golf &g); //golf.cpp #include "header.h" #include <iostream> #include <cstring> using namespace std; int setgolf(golf &g) { char name[30]; int handicap; cout<<"Input the name: "; cin >>name; strcpy(g.fullname,name); cout<<"Input the handicap : "; cin>>handicap; g.handicap = handicap; if(name==NULL) return 0; else return 1; } void setgolf(golf &g, const char *name, int hc) { strcpy(g.fullname,name); g.handicap=hc; } void handicap(golf &g, int hc) { g.handicap=hc; } void showgolf(const golf &g) { cout<<g.fullname<<"\t"<<g.handicap<<'\n'; } int main() { golf ex1,ex2; setgolf(ex1); setgolf(ex2,"namsang",2); handicap(ex2,1000); showgolf(ex1); showgolf(ex2); return 0; } 5번 #include <iostream> using namespace std; template <class Any> Any max(Any in[5]); void main() { int ex[5]; double ex2[5]; for(int i=0;i<5;i++) { ex[i]=i*10; ex2[i]=(0.1)*i; } int re1 = max(ex); double re2 = max(ex2); cout<<re1<<"\t"<<re2<<"\n"; } template <class Any> Any max(Any in[5]) { Any max = in[0]; for(int i=0;i<5;i++) { if(in[i]>=max) max=in[i]; } return max; } 6번 문제 #include <iostream> using namespace std; #include <cstring> template <class Any> template <> char* max(char* in[], int i); template <class Any> Any max(Any in[], int i); void main() { int ex[5]; double ex2[5]; for(int i=0;i<5;i++) { ex[i]=i*10; ex2[i]=(0.1)*i; } char *input[4]; input[0]="asddff\n"; input[1]="asddfffasdfadfadfdf4$$$$$$$f\n"; input[2]="asd\n"; input[3]="aasdfadsf3\n"; char *re; int re1 = max(ex,5); double re2 = max(ex2,5); re = max(input,4); cout<<re1<<"\t"<<re2<<"\n"; cout<<re; } template <class Any> Any max(Any in[], int i) { Any max = in[0]; for(int j=0;j<i;j++) { if(in[j]>=max) max=in[j]; } return max; } char* max(char* in[], int i) { int count=0; int maxCount=0; char *max = in[0]; for(int j=0;j<i;j++) { count=strlen(in[j]); if(count>strlen(max)) max=in[j]; } return max; }