U E D R , A S I H C RSS

Cpp Study_2002_1/과제1/Yggdrasil

영동

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

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:02
Processing time 0.0125 sec