Schoolwork/자료구조와 C++프로그래밍

[C++] 1주차 과제1

FATKITTY 2020. 8. 5. 10:42
반응형

학과명, 학번, 이름, 생년월일을 입력받아서 학과명, 학번, 이름, 생년월일, 나이, 현재날짜를 출력하는 C++ 프로그램을 작성하라.

  • Date 클래스를 정의해서 이용하라.
  • 입출력 예:
 환영합니다! 학과명, 학번, 이름, 생년월일(yyyy/mm/dd)을 입력하세요> 소프트웨어학과, 1111222333, 홍길동, 2000/03/01
 >> 소프트웨어학과 1111222333 홍길동님 2000년3월1일생 2020년3월16일 현재 20세입니다.
 ** 프로그램을 수행할 때의 현재 날짜를 출력해야하며, 본인의 학번과 이름은 정확히 기록해야함(출석 및 과제 제출 확인용).
 생일은 임의로 작성 가능.

 

Code

// use class Date

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <ctime>
#pragma warning(disable:4996)
using namespace std;

const string currentDate()   // returns current date as string yyyy/mm/dd
{
	time_t current_time = time(NULL);  // save current time to now as type current_time
	struct tm timeinfo;
	char buf[80];
	timeinfo = *localtime(&current_time);
	strftime(buf, sizeof(buf), "%Y/%m/%d", &timeinfo);  // yyyy/mm/dd

	return buf;
}

class Date
{
public:
	void input(string whatday);  // ',' , '/'
	void output();  // 날짜출력형식
	void set(int newMonth, int newDay);  // year is not to be defined, month and day should form a possible date
	void check_match(int newMonth, int newDay);  // 예외처리 - if the month and the date doesn't match such as 2/30 or 4/31
	int getYear();  // read the private member year
	int getMonth();  // read month
	int getDay();  // read day

private:
	int year;
	int month;
	int day;
};

class Information
{
public:
	string whole_info;
	void input();
	void output();
	int Age();

private:
	string department;
	string id_number;
	string name;
	string birthday;
	int age;
	string current_date;
};

void Date::set(int newMonth, int newDay)  // data conditions
{
	if ((newMonth >= 1) && (newMonth <= 12))
		month = newMonth;
	else
	{
		cout << "생년월일을 제대로 기입했는지 확인해주세요." << endl;
		exit(1);
	}

	if ((newDay >= 1) && (newDay <= 31))
		day = newDay;
	else
	{
		cout << "생년월일을 제대로 기입했는지 확인해주세요." << endl;
		exit(1);
	}
}

void Date::check_match(int newMonth, int newDay)   // does the month and date match each other?
{
	// 1/31, 2/28, 3/31, 4/30, 5/31, 6/30, 7/30, 8/31, 9/30, 10/31, 11/30, 12/31 - 2,4,6,7,9,11
	// 예외처리 - 윤달
	if ((newMonth == 2) && (newDay > 28) || (newMonth == 4) && (newDay > 30) || (newMonth == 6) && (newDay > 30) || (newMonth == 7) && (newDay > 30)
		|| (newMonth == 9) && (newDay > 30) || (newMonth == 11) && (newDay > 30)) 
	{
		cout << "생년월일을 제대로 기입했는지 확인해주세요." << endl;
		exit(1);
	}
}

void Date::input(string whatday)  // parse string into year, month and day
{
	// slash 기준으로 string parsing 후 int로 형변환하고 각각 맞는 private 변수에 저장
	string delimiter = "/";
	size_t last = 0;
	size_t next = 0;
	string token;
	vector<string> date_token;
	while ((next = whatday.find(delimiter, last)) != string::npos)
	{
		token = whatday.substr(last, next - last);
		date_token.push_back(token);
		last = next + 1;
	}
	date_token.push_back(whatday.substr(last));

	year = stoi(date_token[0]);
	month = stoi(date_token[1]);
	day = stoi(date_token[2]);
	set(month, day);  // month, day 가 유효한 값인지 확인
	check_match(month, day);  // 입력한 날짜가 진짜 정상적인지 확인
}

void Date::output()  // ~년~월~일
{
	cout << year << "년" << month << "월" << day << "일";
}

int Date::getYear() { return year; }
int Date::getMonth() { return month; }
int Date::getDay() { return day; }

// ....
void Information::input()  // 예외처리 - 4가지 중 하나라도 입력 안 했을 때. 생년월일이 이상할 때.
{
	cout << "환영합니다! 학과명, 학번, 이름, 생년월일(yyyy/mm/dd)을 입력하세요> ";
	
	// 쉼표+공백 때문에 while 돌려서 전체 입력값 저장
	string partial_info;
	while (1) 
	{
		cin >> partial_info;
		whole_info.append(partial_info);
		if (cin.get() == '\n') break;
	}
	
	// 쉼표 기준으로 string parsing 후 각각 맞는 private 변수에 저장
	string delimiter = ",";
	size_t last = 0; 
	size_t next = 0; 
	string token;
	vector<string> info_token;
	while ((next = whole_info.find(delimiter, last)) != string::npos) 
	{ 
		token = whole_info.substr(last, next - last);
		info_token.push_back(token);
		last = next + 1; 
	} 
	info_token.push_back(whole_info.substr(last));
	
	department = info_token[0];
	id_number = info_token[1];
	name = info_token[2];
	birthday = info_token[3];
}

int Information::Age()  // 만 나이 = 생일 지났으면 (현재년도-출생년도), 생일 안 지났으면 (현재년도-출생년도-1)
{
	Date date;
	string currentdate = currentDate();
	date.input(currentdate);
	int currentyear = date.getYear();
	int currentmonth = date.getMonth();
	int currentday = date.getDay();
	date.input(birthday);
	int birthyear = date.getYear();
	int birthmonth = date.getMonth();
	int birthday = date.getDay();

	// 현재 날짜가 생월을 지난 경우
	if (currentmonth > birthmonth) age = currentyear - birthyear;
	// 현재 날짜가 생월인 경우 : 생일이거나 지남 or 생일 전
	if (currentmonth == birthmonth && currentday >= birthday) age = currentyear - birthyear;
	if (currentmonth == birthmonth && currentday < birthday) age = currentyear - birthyear - 1;
	// 현재 날짜가 생월 이전인 경우
	if (currentmonth < birthmonth) age = currentyear - birthyear - 1;

	return age;
}

void Information::output()  // department, id_number, name, birthday, age, current_date
{
	Date date;
	date.input(birthday);

	cout << ">> " << department << " " << id_number << " " << name << "님 ";
	date.output();
	cout << "생 ";

	date.input(currentDate());
	date.output();
	cout << " 현재 " << age << "세입니다." << endl;
}

int main()
{
	Information info;
	info.input();
	if (info.Age() < 0)
	{
		cout << "생년월일을 제대로 기입했는지 확인해주세요." << endl;
		exit(1);
	}
	info.output();
}

실행결과

 

점수 10/10

 

 

  ❤와 댓글은 큰 힘이 됩니다. 감사합니다 :-)  

반응형

'Schoolwork > 자료구조와 C++프로그래밍' 카테고리의 다른 글

[C++] 9주차 과제6  (0) 2020.08.05
[C++] 7주차 과제5  (0) 2020.08.05
[C++] 5주차 과제4  (0) 2020.08.05
[C++] 3주차 과제3  (0) 2020.08.05
[C++] 2주차 과제2  (0) 2020.08.05