01

// golf.h

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

void setgolf(golf & g, const char * name, int hc);

int setgolf(golf & g);

void handicap(golf & g, int hc);

void showgolf(const golf & g);
// golf.cpp

#include <iostream>
#include <cstring>
#include <string>
#include "golf.h"

void setgolf(golf & g, const char * name, int hc)
{
	strcpy(g.fullname, name);
	g.handicap = hc;
}

int setgolf(golf & g)
{
	using std::cout;
	using std::cin;

	char name[50];
	cout << "이름을 입력하세요 : ";
	cin.getline(name, 49);

	if (name[0] == '\0')
	{
		cout << "입력을 종료합니다.\n";
		std::cin.clear();
		return 0;
	}

	int hc;
	cout << "핸디캡을 입력하세요 : ";
	cin >> hc;
	cin.get();
	
	strcpy(g.fullname, name);
	g.handicap = hc;

	return 1;
}

void handicap(golf & g, int hc)
{
	g.handicap = hc;
}

void showgolf(const golf & g)
{
	using std::cout;
	using std::endl;

	cout << "이름 : " << g.fullname << endl;
	cout << "핸디캡 : " << g.handicap << endl;
}
// golf_main.cpp

#include <iostream>
#include "golf.h"

int main(void)
{
	golf arr[4];
	int i;

	for (i = 0; i < 3; i++)
	{
		if (setgolf(arr[i]) == 0)
			break;
	}
	if (i != 3)
		setgolf(arr[i], "abcde", 10);

	handicap(arr[0], 100);

	for (int j = 0; j <= i; j++)
		showgolf(arr[j]);

	return 0;
}

02

#include <iostream>
#include <string>

void strcount(std::string & str);

int main()
{
	using namespace std;

	string input;
	char next;

	cout << "영문으로 한 행을 입력하십시오:\n";
	getline(cin, input);
	while (input != "")
	{
		strcount(input);
		cout << "다음 행을 입력하십시오(끝내려면 빈 행을 입력) : \n";
		getline(cin, input);
	}
	cout << "프로그램을 종료합니다.\n";
	return 0;
}

void strcount(std::string & str)
{
	using namespace std;
	static int total = 0;
	int count = 0;
	
	cout << "\"" << str << "\"에는 ";
	for (int i = 0; str[i]; i++)
	{
		if (str[i] != ' ')
			count++;
	}
	total += count;
	cout << count << "개의 문자가 있습니다.\n";
	cout << "지금까지 총 " << total << "개의 문자를 입력하셨습니다. \n";
}

03

#include <iostream>

struct chaff
{
	char dross[20];
	int slag;
};

const int N = 2;
const int BUF = 512;
char buffer[BUF];

int main()
{
	using namespace std;

	chaff *arr;

	arr = new (buffer) chaff[N];

	char temp[20];
	for (int i = 0; i < N; i++)
	{
		cout << "##" << i + 1 << "번째##\n";
		cout << "dross 입력 : ";
		cin.getline(temp, 19);
		strcpy(arr[i].dross, temp);

		cout << "slag 입력 : ";
		cin >> arr[i].slag;
		cin.get();
	}

	cout << endl;

	for (int i = 0; i < N; i++)
	{
		cout << "##" << i + 1 << "번째##\n";
		cout << "dross : " << arr[i].dross << endl;
		cout << "slag : " << arr[i].slag << endl;
	}
	return 0;
}

04

// sales.h

#ifndef SALES_H_
#define SALES_H_

namespace SALES
{
	const int QUARTERS = 4;
	struct Sales
	{
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	};

	void setSales(Sales & s, const double ar[], int n);

	void setSales(Sales & s);
	
	void showSales(const Sales & s);
}

#endif
// sales.cpp

#include <iostream>
#include "sales.h"

double cal_mean(const double ar[], int n);
double cal_max(const double ar[], int n);
double cal_min(const double ar[], int n);

void SALES::setSales(Sales & s, const double ar[], int n)
{
	int small = n < 4 ? n : 4;
	int big = n > 4 ? n : 4;

	for (int j = 0; j < big; j++)
	{
		if (j < small)
			s.sales[j] = ar[j];
		else
			s.sales[j] = 0;
	}
	s.average = cal_mean(ar, small);
	s.max = cal_max(ar, small);
	s.min = cal_min(ar, small);
}

void SALES::setSales(Sales & s)
{
	using std::cout;
	using std::cin;

	for (int i = 0; i < 4; i++)
	{
		cout << i + 1 << "분기 판매액을 입력하세요 : ";
		cin >> s.sales[i];
	}
	s.average = cal_mean(s.sales, 4);
	s.max = cal_max(s.sales, 4);
	s.min = cal_min(s.sales, 4);
}

void SALES::showSales(const Sales & s)
{
	using std::cout;

	cout << std::endl;
	for (int i = 0; i < 4; i++)
		cout << i + 1 << "분기 판매액은 " << s.sales[i] << "$ 입니다.\n";
	cout << "평균은 " << s.average << "$ 입니다.\n";
	cout << "최대값은 " << s.max << "$ 입니다.\n";
	cout << "최소값은" << s.min << "$ 입니다.\n";
}

double cal_mean(const double ar[], int n)
{
	double sum = 0;
	for (int i = 0; i < n; i++)
		sum += ar[i];
	return sum / n;
}

double cal_max(const double ar[], int n)
{
	double max = 0;
	for (int i = 0; i < n; i++)
	{
		if (max < ar[i])
			max = ar[i];
	}
	return max;
}

double cal_min(const double ar[], int n)
{
	double min = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		if (min > ar[i])
			min = ar[i];
	}
	return min;
}
// sales_main.cpp

#include <iostream>
#include "sales.h"

int main(void)
{
	SALES::Sales arr[2];

	double ar[3] = {1.1, 2.2, 3.3};
	SALES::setSales(arr[0], ar, 3);
	SALES::setSales(arr[1]);

	SALES::showSales(arr[0]);
	SALES::showSales(arr[1]);
	return 0;
}

Tags:

Categories:

Updated: