일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- DFS
- 서브쿼리
- 플로이드-워셜
- 분할정복
- 그래프 이론
- 트리
- 누적합
- 투포인터
- 다익스트라
- DP
- 다이나믹프로그래밍
- GROUP BY
- 다시
- 그래프 탐색
- 구현
- 시뮬레이션
- 크루스칼
- 브루트포스
- 에라토스테네스의 체
- MST
- 수학
- 그리디
- 다이나믹 프로그래밍
- 자료구조
- 재귀
- 백트래킹
- join
- BFS
- 우선순위큐
- 해시
- Today
- Total
기록하고 까먹지 말기
과제11 - final (1) 본문
버스 예약 프로그램을 만드는 과제였다.
개인적으로 이런 작은 프로젝트성 과제는 예전에 수강할때 해 보아서 ptsd가 왔지만 조금은 성장했으리라 생각했기 때문에 과제가 나온 당일날 곧바로 부딫혔다.
문제를 보니까 단순 알고리즘뿐만 아니라 파일 스트림까지 활용하여 문제를 해결해야 하는 것이었다.
그래서 파일 스트림을 활용하기 위해 구글이랑 열혈 c프로그래밍을 꽤나 많이 봤던 것 같았다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void registration();
void login();
void reservation();
void back();
void quit();
// structure as a person(customer)
struct person{
char first[20];
char last[20];
char uname[20];
char pw[20];
};
int main(){
// printing main menu
int n=0;
printf("Welcome to Hanyang bus reservation!\n\n\n");
printf("1 : register!!!\n");
printf("2 : login!!!\n");
printf("3 : quit\n");
printf("Enter you choice : \n");
scanf("%d", &n);
while(1){
switch(n){
case 1:
registration();
break;
case 2:
login();
break;
case 3:
printf("Bye!");
return 0;
default: // if no matching number with menu
printf("Enter correct number!!\n\n");
main();
}
}
}
void registration(){
struct person user;
FILE *fp; // declaring file to write
fp = fopen("user.txt", "wt");
// entering data
printf("\nEnter first name : ");
scanf("%s", user.first);
printf("\nEnter last name : ");
scanf("%s", user.last);
printf("\nEnter your Username : ");
scanf("%s", user.uname);
printf("\nEnter your password : ");
scanf("%s", user.pw);
// printing entered data from user
printf("\nYou are successfully registered!!\n");
printf("Your UserId is %s and your password is %s\n", user.uname, user.pw);
printf("Now login with your username and password!!\n");
// adding new lines at the end of lines to save with new line
strcat(user.first, "\n");
strcat(user.last, "\n");
strcat(user.uname, "\n");
strcat(user.pw, "\n");
// inputs into file
if(fp != NULL){
fputs(user.first, fp);
fputs(user.last, fp);
fputs(user.uname, fp);
fputs(user.pw, fp);
}
else{
printf("File open failed!\n");
}
fclose(fp); // closing file
// goto main menu
back();
}
void login(){
struct person user;
struct person check;
FILE* fp=fopen("user.txt", "rt"); // reading file
if(fp != NULL){
// getting data from file
fgets(user.first, sizeof(user.first), fp);
fgets(user.last, sizeof(user.last), fp);
fgets(user.uname, sizeof(user.uname), fp);
fgets(user.pw, sizeof(user.pw), fp);
user.uname[strlen(user.uname)-1] = '\0'; // erasing new line for comparing
user.pw[strlen(user.pw)-1] = '\0'; // erasing new line for comparing
}
// entering id and pw to check with data in file
printf("\nUserID : ");
scanf("%s", check.uname);getchar();
printf("\nPassword : ");
scanf("%s", check.pw);getchar();
// when same with entered and original
if(strcmp(check.uname, user.uname) == 0 && strcmp(check.pw, user.pw) == 0){
printf("\nYou are successfully logged in, welcome :-)\n");
reservation(); // go to reservation menu
}
else{ // if not
printf("Yout UserID or password is incorrect, please try again :-(\n");
back(); // go back to main menu
}
}
int seat[10][10]={0, }; // total seats
void display(){ // relational operator 출력 고민
int i, j;
printf("\tSeats\n");
printf("\tWindow Aisle Aisle Window\n");
for(i=0; i<10; i++){
printf("Row %d : ", (i+1));
for(j=0; j<4; j++)
printf("%d ", seat[i][j]);
printf("\n");
}
printf("\n");
}
void reservation(){
int i, j, n;
int cnt, row, col;
// printing reservation menu
printf("\n\nReservation Menu\n");
display();
printf("\n1. reserve a seat\n");
printf("2 : cancel a seat\n");
printf("3 : Main Menu\n");
printf("Enter your choice : \n");
scanf("%d", &n); // selecting menu to reserve
if(n == 1){
printf("How many seats do you want to reserve?\n");
scanf("%d", &cnt);
display(); // displaying total seats
for(i=0; i<cnt; i++){
// entering location to reserve
printf("Which row do you want to choose? : ");
scanf("%d", &row);
printf("Which seat do you want to choose? : ");
scanf("%d", &col);
seat[row-1][col-1] = 1; // checking seats selected from user
}
printf("Reservation complete, thank you :-)\n\n");
reservation(); // goto reservation menu
}
else if(n == 2){ // to cancel
// entering location to cancel
printf("Which row do you want to cancel? : ");
scanf("%d", &row);
printf("Which seat do you want to cancel? : ");
scanf("%d", &col);
row--; col--;
if(seat[row][col] == 1){ // entered seat is already reserved
seat[row][col]--; // cancel with decreasing value
printf("Your Seat is Cancelled\n");
display();
}
else // if not
printf("Entered seat is not reserved!\n");
}
else if(n == 3){ // end of reservation
printf("Thank you for your registration !!");
quit();
}
reservation();
}
void back(){ // going back to main menu
printf("Press any key to continue ...\n\n");
main();
}
void quit(){
exit(0); // end of program
}
우선 기본적으로 등록, 로그인, 종료를 선택할 수 있는 메뉴를 만들었다.
이건 int형 변수를 통해 입력받은 후 switch를 활용하여 원하는 함수로 이동하는 방식을 사용했다.
유저를 등록하는 registration의 경우에는 앞서 만든 struct를 사용하여 person을 선언, 파일 스트림을 열어 인적사항을 저장할 텍스트 파일인 user.txt를 생성하였다. 그 이후에는 scanf를 활용하여 성, 이름, 닉네임, 비밀번호를 입력받았다.
gets를 활용할 수도 있었겠지만 개행문자를 넣음으로써 텍스트를 구분하기 위해 일부러 scanf 함수를 사용했다.
그리고 strcat를 통해 개행문자를 넣고, fputs를 통해 user.txt에 데이터를 저장했다.
로그인의 경우에는 입력한 정보와 로드한 정보가 서로 일치하는지 여부를 판별하는 함수이다.
그렇기때문에 입력할 구조체인 user, 비교 대상인 check를 선언했다.
파일 스트림을 열어 fgets를 통해 데이터를 가져왔고, strcmp를 통해 닉네임과 비밀번호를 대조했다.
그 결과 모두 동일하면 로그인 성공 메시지와 함께 reservation 함수로 이동하였고 아닌 경우 실패 메시지와 함께 다시 메인함수로 이동하는 방식으로 함수를 작성했다.
reservation은 버스의 좌석을 예약하는 함수이다.
우선 버스가 하나라는 가정으로 프로그램을 만들었기 때문에 10 by 10의 전역변수 형식으로 int형 array를 만들었다.
먼저 간단한 메뉴창이 나오고 입력한 데이터에 따라 예약, 취소, 메인메뉴, 이 세 개로 이동 가능하다.
예약하는 경우 예약할 좌석의 수를 입력받고 그에 따라 for문을 통해 좌석을 입력받는다.
예약된 좌석은 기본값인 0에서 1로 수정한다.
취소의 경우에는 예약과 반대로 원하는 좌석을 입력, 예약이 되지 않은 경우 해당 메시지를 출력, 예약이 되어 있는 경우 값을 1에서 0으로 수정 및 결과를 출력한다.
back 함수는 main 함수로 되돌아가는 함수로 더 간단하게 만들기 위해 작성하였다.
전반적으로 예약, 종료 함수의 경우에는 만드는데 큰 어려움은 없었다. 생각나는 대로 만들었더니 큰 수정 없이 만들 수 있었다.
그렇지만 파일 스트림을 이용하는 로그인에 관해서는 꽤나 시간이 많이 들었던 기억이 난다. 데이터를 가져왔는데 dump값이 왔던 경우도 있었고, 개행을 못해서 아이디 값만 가져와야 하는데 비밀번호의 일부 값까지 같이 로드한 경우도 있었다.
이 과정에서 string 라이브러리를 구글, 다른 교재 등을 통해 꽤나 공부를 했던 것 같았다.
그래도 이번 과제에서 나에게 가장 의미있는 문제가 아니었나 싶다.
'전공 > C' 카테고리의 다른 글
Final Coding Test (0) | 2021.06.22 |
---|---|
과제11 - final (2) (0) | 2021.06.22 |
과제10 - StructUnion (1 ~ 7) (0) | 2021.06.22 |
과제9 - Pointer Dynamic (6 ~ 9) (0) | 2021.06.22 |
과제9 - Pointer Dynamic (1 ~ 5) (0) | 2021.06.22 |