마스터 파일 내용 보여주기
//////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <algorithm>
/*
Microsoft visual studio 2015 에서 실행했습니다~~
*/
#pragma warning ( disable : 4996 )
using namespace std;
// 과목 데이터 타입 선언
typedef struct course_type {
char dept[10]; //부서
char course_name[20];//과목 이름
char prof_id[7]; //교수 id
int credit; //학점
}COURSE;
// 학생 레코드 구조 선언
typedef struct student {
int id; //학번
char name[20]; //이름
char address[50]; //주소
int no_of_courses; //과목의 갯수
course_type course[2]; // 한 학생이 최대 2강좌 수강 가능
}STUDENT;
typedef struct transaction {
STUDENT student;
char mode; // C: 갱신, D: 삭제, I: 추가
}TRANSACTION;
int print_menu(); //메뉴 출력
void init_file(); //초기 마스터 파일 읽기, 예제 트랜잭션 생성
void run_trans(); //트랜잭션 실행
void trans_insert(STUDENT data); //트랜잭션 삽입 기능
void trans_delete(STUDENT data); //트랜잭션 삭제 기능
void trans_correct(STUDENT data); //트랜잭션 수정 기능
void make_trans(char mode, STUDENT data); //트랜잭션 파일 생성
void write_file(); //마스터 파일에 저장 쓰기
void report(); //마스터 파일 출력
void write_trans(); //생성할 트랜잭션 데이터 입력
void remove_trans(); //기존 생성한 트랜잭션 파일들 삭제
bool id_compare(STUDENT a, STUDENT b); //정렬을 위한 비교 함수
vector<STUDENT> students;
int trans_cnt = 0;
void main() {
int menu = 0;
init_file();
while (1) {
menu = print_menu();
switch (menu)
{
case 1 :
report();
break;
case 2 :
write_trans();
break;
case 3:
run_trans();
break;
case 4:
remove_trans();
break;
default:
cout << "종료합니다...." << endl;
return;
break;
}
}
}
//메뉴 출력
int print_menu() {
int num = 0;
while (1) {
cout << "/////////////////////////////////////////////////////////////////" << endl;
cout << "-------------------------------------------------------------------" << endl;
cout << "| 1. 마스터 파일 보기 2. 새로운 트랜잭션 생성 3.트랜잭션 실행|" << endl;
cout << "| 4.기존 트랜잭션 삭제 5.종료 |" << endl;
cout << "-------------------------------------------------------------------" << endl;
cout << "/////////////////////////////////////////////////////////////////" << endl;
cout << "메뉴를 입력해 주세요 : ";
cin >> num;
if (num == 1 || num == 2 || num == 3 || num == 4 || num == 5) {
break;
}
else {
cout << "다시 입력해주세요." << endl;
}
}
return num;
}
//마스터파일 내용 출력
void report() {
int i = 0;
for (i = 0; i < students.size(); i++) {
cout << i + 1 << "번째 학생//////////////////" << endl;
cout << "학번: " << students[i].id << endl;
cout << "이름: " << students[i].name << endl;
cout << "주소: " << students[i].address << endl;
cout << "수강 강좌 수: " << students[i].no_of_courses << endl;
cout << "--------------------------------------" << endl;
if (students[i].no_of_courses >= 1) {
cout << "1번과목 종류: " << students[i].course[0].dept << endl;
cout << "1번과목 이름: " << students[i].course[0].course_name << endl;
cout << "1번과목 교수번호: " << students[i].course[0].prof_id << endl;
cout << "1번과목 학점: " << students[i].course[0].credit << endl;
}
if (students[i].no_of_courses == 2) {
cout << "--------------------------------------" << endl;
cout << "2번과목 종류: " << students[i].course[1].dept << endl;
cout << "2번과목 이름: " << students[i].course[1].course_name << endl;
cout << "2번과목 교수번호: " << students[i].course[1].prof_id << endl;
cout << "2번과목 학점: " << students[i].course[1].credit << endl;
}
cout << "/////////////////////////////////////////\n\n" << endl;
}
}
//파일 준비, 생성
void init_file() {
int i = 0, j=0;
ifstream file;
file.open("student_list.txt");
if (!file.is_open()) {
cout << "파일 열기 실패!" << endl;
}
//마스터 파일 읽기
while (!file.eof()) {
STUDENT tmp_data;
memset(&tmp_data, 0, sizeof(tmp_data));
file >> tmp_data.id;
file >> tmp_data.name;
file >> tmp_data.address;
file >> tmp_data.no_of_courses;
if (tmp_data.no_of_courses >= 1) {
file >> tmp_data.course[0].dept;
file >> tmp_data.course[0].course_name;
file >> tmp_data.course[0].prof_id;
file >> tmp_data.course[0].credit;
}
if (tmp_data.no_of_courses == 2) {
file >> tmp_data.course[1].dept;
file >> tmp_data.course[1].course_name;
file >> tmp_data.course[1].prof_id;
file >> tmp_data.course[1].credit;
}
students.push_back(tmp_data);
i++;
}
file.close();
////////////////////////트랜잭션 작성 (임시 6개 생성)
STUDENT temp_data_1;
memset(&temp_data_1, 0, sizeof(temp_data_1));
temp_data_1.id = 35;
strcpy(temp_data_1.name, "김영희");
strcpy(temp_data_1.address, "전라남도_전라시_중앙동");
temp_data_1.no_of_courses = 1;
strcpy(temp_data_1.course[0].dept, "교양");
strcpy(temp_data_1.course[0].course_name, "성문화와심리");
strcpy(temp_data_1.course[0].prof_id, "7777");
temp_data_1.course[0].credit = 2;
make_trans('I', temp_data_1);
make_trans('C', temp_data_1);
make_trans('D', temp_data_1);
/////////////////////////////////////
STUDENT temp_data_2;
memset(&temp_data_2, 0, sizeof(temp_data_2));
temp_data_2.id = 20;
strcpy(temp_data_2.name, "장수왕");
strcpy(temp_data_2.address, "경상남도_고성시_고룡동");
temp_data_2.no_of_courses = 2;
strcpy(temp_data_2.course[0].dept, "교양");
strcpy(temp_data_2.course[0].course_name, "성문화와심리");
strcpy(temp_data_2.course[0].prof_id, "7777");
temp_data_2.course[0].credit = 2;
strcpy(temp_data_2.course[1].dept, "전공");
strcpy(temp_data_2.course[1].course_name, "경영학개론");
strcpy(temp_data_2.course[1].prof_id, "121212");
temp_data_2.course[1].credit = 3;
make_trans('I', temp_data_2);
make_trans('C', temp_data_2);
make_trans('D', temp_data_2);
}
void remove_trans() {
char file_name[20];
//기존 트랜잭션파일 삭제
for (int i = 0; i < trans_cnt; i++) {
memset(file_name, 0, sizeof(file_name));
itoa(i + 1, file_name, 10);
strcat(file_name, "_trans.txt");
remove(file_name);
}
trans_cnt = 0;
}
//트랜잭션 데이터 입력.
void write_trans() {
TRANSACTION trs;
memset(&trs, 0, sizeof(trs));
cout << "//////////////////////////////////////////" << endl;
cout << "트랜잭션을 추가합니다..." << endl;
cout << "모드를 입력해주세요 (I : 삽입, C : 수정, D : 삭제) = ";
cin >> trs.mode;
cout << "학번을 입력하세요 = ";
cin >> trs.student.id;
fflush(stdin);
if (trs.mode == 'D') {
make_trans(trs.mode, trs.student);
return;
}
cout << "이름을 입력하세요 = ";
cin >> trs.student.name;
cout << "주소를 입력하세요(띄어쓰기는 _언더바로 해주세요) = ";
cin >> trs.student.address;
cout << "수강 갯수를 입력하세요(최대 2개) = ";
cin >> trs.student.no_of_courses;
if (trs.student.no_of_courses >= 1) {
cout << "과목1의 종류를 입력하세요 = ";
cin >> trs.student.course[0].dept;
cout << "과목1의 이름을 입력하세요 = ";
cin >> trs.student.course[0].course_name;
cout << "과목1의 교수id를 입력하세요 = ";
cin >> trs.student.course[0].prof_id;
cout << "과목1의 학점을 입력하세요 = ";
cin >> trs.student.course[0].credit;
}
if (trs.student.no_of_courses == 2) {
cout << "과목2의 종류를 입력하세요 = ";
cin >> trs.student.course[1].dept;
cout << "과목2의 이름을 입력하세요 = ";
cin >> trs.student.course[1].course_name;
cout << "과목2의 교수id를 입력하세요 = ";
cin >> trs.student.course[1].prof_id;
cout << "과목2의 학점을 입력하세요 = ";
cin >> trs.student.course[1].credit;
}
make_trans(trs.mode, trs.student);
}
//트랜잭션 파일 생성
void make_trans(char mode, STUDENT data) {
char file_name[20];
ofstream outfile;
//파일이름 설정
itoa(trans_cnt+1, file_name, 10);
strcat(file_name, "_trans.txt");
//파일 생성
outfile.open(file_name, ios::trunc);
outfile << mode << '\t';
outfile << data.id << '\t';
outfile << data.name << '\t';
outfile << data.address << '\t';
outfile << data.no_of_courses;
if (data.no_of_courses >= 1) {
outfile << '\t' << data.course[0].dept << '\t';
outfile << data.course[0].course_name << '\t';
outfile << data.course[0].prof_id << '\t';
outfile << data.course[0].credit;
}
if (data.no_of_courses == 2) {
outfile << '\t' << data.course[1].dept << '\t';
outfile << data.course[1].course_name << '\t';
outfile << data.course[1].prof_id << '\t';
outfile << data.course[1].credit;
}
outfile.close();
trans_cnt++;
}
//트랜잭션 실행
void run_trans() {
int i = 0;
char file_name[20];
while (i < trans_cnt) {
ifstream file;
memset(file_name, 0, sizeof(file_name));
itoa(i + 1, file_name, 10);
strcat(file_name, "_trans.txt");
file.open(file_name);
if (!file.is_open()) {
cout << "트랜잭션 파일 열기 실패!" << endl;
}
cout << "트랜잭션 파일 : " << file_name;
while (!file.eof()) {
TRANSACTION tmp_data;
file >> tmp_data.mode;
file >> tmp_data.student.id;
file >> tmp_data.student.name;
file >> tmp_data.student.address;
file >> tmp_data.student.no_of_courses;
if (tmp_data.student.no_of_courses >= 1) {
file >> tmp_data.student.course[0].dept;
file >> tmp_data.student.course[0].course_name;
file >> tmp_data.student.course[0].prof_id;
file >> tmp_data.student.course[0].credit;
}
if (tmp_data.student.no_of_courses == 2) {
file >> tmp_data.student.course[1].dept;
file >> tmp_data.student.course[1].course_name;
file >> tmp_data.student.course[1].prof_id;
file >> tmp_data.student.course[1].credit;
}
if (tmp_data.mode == 'I') {
trans_insert(tmp_data.student);
}
else if (tmp_data.mode == 'D') {
trans_delete(tmp_data.student);
}
else if (tmp_data.mode == 'C') {
trans_correct(tmp_data.student);
}
}
file.close();
i++;
}
sort(students.begin(), students.end(), id_compare);
write_file();
}
//트랜잭션 추가
void trans_insert(STUDENT data) {
int i = 0;
int cnt = 0;
for (i = 0; i < students.size(); i++) {
if (students[i].id == data.id) {
cnt++;
}
}
if (cnt == 0) {
students.push_back(data);
cout << "[INSERT SUCCESS]" << endl;
} else {
cout << "[INSERT FAIL] 이미존재하는 학생입니다." << endl;
}
}
//트랜잭션 삭제
void trans_delete(STUDENT data) {
int i = 0;
int flag = 0;
for (i = 0; i < students.size(); i++) {
if (students[i].id == data.id) {
students.erase(students.begin() + i);
flag = 1;
break;
}
}
if (flag == 1) {
cout << "[DELETE SUCCESS]" << endl;
} else {
cout << "[DELETE FAIL] 존재하지 않는 학생입니다." << endl;
}
}
//트랜잭션 수정
void trans_correct(STUDENT data) {
int i = 0;
int flag = 0;
for (i = 0; i < students.size(); i++) {
if (students[i].id == data.id) {
students.erase(students.begin() + i);
students.push_back(data);
flag = 1;
break;
}
}
if (flag == 1) {
cout << "[CORRECT SUCCESS]" << endl;
}
else {
cout << "[CORRECT FAIL] 존재하지 않는 학생입니다." << endl;
}
}
//마스터파일에 저장
void write_file() {
ofstream outfile;
int i;
outfile.open("student_list.txt", ios::trunc);
for (i = 0; i < students.size(); i++) {
outfile << students[i].id << '\t';
outfile << students[i].name << '\t';
outfile << students[i].address << '\t';
outfile << students[i].no_of_courses;
if (students[i].no_of_courses >= 1) {
outfile << '\t' << students[i].course[0].dept << '\t';
outfile << students[i].course[0].course_name << '\t';
outfile << students[i].course[0].prof_id << '\t';
outfile << students[i].course[0].credit;
}
if (students[i].no_of_courses == 2) {
outfile << '\t' << students[i].course[1].dept << '\t';
outfile << students[i].course[1].course_name << '\t';
outfile << students[i].course[1].prof_id << '\t';
outfile << students[i].course[1].credit;
}
if (i != students.size() - 1) {
outfile << '\n';
}
}
outfile.close();
cout << "마스터 파일에 저장 완료!" << endl;
}
//정렬을 위한 비교 함수
bool id_compare(STUDENT a, STUDENT b)
{
return a.id < b.id;
}
'공부 > C & C++' 카테고리의 다른 글
(과제) 해쉬 테이블 구현 (0) | 2016.06.23 |
---|---|
(과제) 대체 선택 알고리즘 (0) | 2016.06.23 |
for문 안 int 선언 (2) | 2016.04.16 |
(함수, C++) 입력 버퍼 비우기 [cin.ignore()] (0) | 2016.04.16 |
(함수) 메모리 값 바꾸기, 초기화 [memset] (0) | 2016.04.14 |