U E D R , A S I H C RSS

Cpp Study_2002_1/과제1/Cherry Boy

대근


~cpp 

====================================================================

// 1번 프로그래밍 연습...


#include <iostream>
using namespace std;
void print(char *,int n=0);

int main()
{
	char exam[40];
	int choice;
	do
	{
		cout << "문자열(20자내)을 입력하세요...\n";
		cin.getline(exam,40);
		cout << "숫자를 입력하세요.. _\b";
		cin >> choice;
		cin.get();
		print(exam,choice);
	}while(choice==0);
	return 0;
}

void print(char *exam,int n)
{
	static int count=0;
	if(n==0)
		cout << exam << endl;
	else
	{
		cout << "당신은 출력 함수를 " << count << " 번 사용하셨습니다.\n";
		for(int i=0;i<count;i++)
			cout << exam << endl;
	}
	count++;
}

/*
0을 입력받고 싶지 않은데 그게 어렵네요... 
0을 따로 계속 입력해야합니다..
*/

========================================================================

// 2번 프로그래밍 연습...


#include <iostream>
using namespace std;
struct candybar
{
	char name[30];
	double weight;
	int cal;
}candy;

void print(candybar &, char * name="millenium Munch",double weight=2.85,int cal=350);

int main()
{
	print(candy);
	return 0;
}

void print(candybar &candy,char * name,double weight,int cal)
{
	for(int i=0;i<30;i++)
		candy.name[i]=name[i];
	candy.weight=weight;
	candy.cal=cal;
	cout << "캔디바의 이름\t:\t" << candy.name <<endl;
	cout << "캔디바의 무게\t:\t" << candy.weight << endl;
	cout << "캔디바의 칼로리\t:\t" << candy.cal << endl;
}

===================================================================


// 3번 연습문제..


#include <iostream>
using namespace std;
#include <cstring>
struct stringy
{
	char * str;
	int ct;
};

void show(char *, int n=1);
void show(stringy, int n=1);
void set(stringy &,char *);

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 show(char *string,int n)
{
	for(int i=0;i<n;i++)
		cout << string << endl;
}

void show(stringy beany,int n)
{
	for(int i=0;i<n;i++)
		cout << beany.str << endl;
}

void set(stringy &beany,char *string)
{
	char* temp=new char[strlen(string)];
	temp=string;
	beany.str=temp;	
	beany.ct=strlen(beany.str);
}

=============================================================

//4번임다..

// 다음은 헤더 파일이다.

// golf.h  --  pe8-3.cpp 를 위해

#include <iostream>
#include <cstring>
using namespace std;

const int Len=40;
struct golf
{
	char fullname[Len];
	int handicap;
}g1,g2;

//함수는 사용자에게 이름과 핸디켑을 요구한다
//이름이 입력되면 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);

int main()
{
	int k;
	do
	{
		k=setgolf(g1);
		setgolf(g2, "DKNY", 100);
		handicap(g2,77);
		showgolf(g1);
		cout<<endl;
		showgolf(g2);
	}while(k!=0);
	return 0;
}

int setgolf(golf & g)
{
	cout << "What's U'r name?\n";
	cin >> g.fullname;
	cout << "Handicap?\n";
	cin >> g.handicap;
	if(g.fullname==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 << "Name    \t:\t" << g.fullname << endl;
	cout << "HandyCap\t:\t" << g.handicap << endl;
}


=============================================================



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