2019-01-30

2267 단어 2019C렉토피아2019

HW7

#include <iostream>
using namespace std;

struct Savings {
	int w500;
	int w100;
	int w50;
	int w10;
};

void init(Savings &s);
void input(int &unit, int &cnt);
void save(Savings &s, int unit, int cnt);
int total(Savings &s);
void my_flush();

int main() {
	struct Savings s;
	int unit, cnt, sum;
	while (1) {
		init(s);
		while (1) {
			input(unit, cnt);
			if (unit == -1) break;
			save(s, unit, cnt);
		}
		cout << "총 저금액 : " << total(s) << endl << endl;
	}
	return 0;
}

void init(Savings &s) {
	s.w500 = 0;
	s.w100 = 0;
	s.w50 = 0;
	s.w10 = 0;
}

void input(int &unit, int &cnt) {
	while (1) {
		cout << "동전의 금액 : ";
		cin >> unit;
		if (cin.fail()) {
			my_flush();
			unit = -1;
			return;
		}
		switch (unit) {
		case 10:;
		case 50:;
		case 100:;
		case 500: {
			cout << "동전의 개수 : ";
			cin >> cnt;
			return;

		}
		default: break;
		}
	}

}

void save(Savings &s, int unit, int cnt) {
	switch (unit) {
	case 10: s.w10 += cnt; break;
	case 50: s.w50 += cnt; break;
	case 100: s.w100 += cnt; break;
	case 500: s.w500 += cnt; break;
	}
}

int total(Savings &s) {
	int sum = 0;
	sum += 10 * s.w10;
	sum += 50 * s.w50;
	sum += 100 * s.w100;
	sum += 500 * s.w500;
	return sum;
}

void my_flush() {
	cin.clear();
	while (cin.get() != '\n');
}

HW8

#include <iostream>
using namespace std;

void swap_ptr(const char* &, const char* &);

int main() {
	const char *ap = "apple";
	const char *bp = "banana";

	cout << "바꾸기 전의 문자열 : " << ap << " " << bp << endl;
	swap_ptr(ap, bp);
	cout << "바꾼 후의 문자열 : " << ap << " " << bp << endl;

	return 0;
}

void swap_ptr(const char* &ap, const char* &bp) {
	char *tp;
	//tp = new char[len + 1];
	//strcpy(tp,)
	tp = (char*)ap;
	ap = (char*)bp;
	bp = tp;
}

좋은 웹페이지 즐겨찾기