1. 학생관리 프로그램


#include <iostream>
#include <string.h>
using namespace std;
#define NAME_SIZE 32
#define ID_SIZE 8
#define MAX_STUDENT 64

enum{
    MENU_INSERT=1,
    MENU_DELETE,
    MENU_FIND,
    MENU_PRINT
};

struct Student{
    char sName[NAME_SIZE];
    // +1 for '\0'
    char sID[ID_SIZE+1];
    int sKor;
    int sEng;
    int sMath;
    int sTotal;
    float sAvg;
};

void ErrorHandling(string msg);
int studentInsert(Student *student,int& sCount);
int studentDelete(Student *student,int& sCount);
int studentFind(Student *student,int& sCount);
int studentPrint(Student *student,int& sCount);

int main(){
    system("clear");
    int selectMenu;
    int studentCount = 0;
    Student sArray[MAX_STUDENT] = {};
    while(1){
        cout << endl;
        cout << "Manage Student Prg" << endl;
        cout << "1. Insert" << endl;
        cout << "2. Delete" << endl;
        cout << "3. Find" << endl;
        cout << "4. Print" << endl;
        cout << "Select Menu : ";
        cin >> selectMenu;
        system("clear");
        if(cin.fail()){
            cin.clear();
            cin.ignore(1024,'\n');
        }
        
        switch(selectMenu){
            case MENU_INSERT:
                if(studentInsert(sArray, studentCount) == -1){
                    ErrorHandling("Cannot Insert");
                    continue;
                }
                break;
            case MENU_DELETE:
                if(studentDelete(sArray, studentCount) == -1){
                    ErrorHandling("Cannot Delete");
                    continue;
                }
                break;
            case MENU_FIND:
                if(studentFind(sArray, studentCount) == -1){
                    ErrorHandling("Cannot Find");
                    continue;
                }
                break;
            case MENU_PRINT:
                if(studentPrint(sArray, studentCount) == -1){
                    ErrorHandling("Cannot Print");
                    continue;
                }
                break;
            default: {
                ErrorHandling("Input 1~4");
                continue;
            }
        }
    }
    return 0;
}

int studentInsert(Student *student, int& sCount){
    if(sCount == MAX_STUDENT-1)
        return -1;

    cout << "input name :";
    cin >> student[sCount].sName;
    cout << "input ID :";
    cin >> student[sCount].sID;
    cout << "input Kor score : ";
    cin >> student[sCount].sKor;
    cout << "input Eng score : ";
    cin >> student[sCount].sEng;
    cout << "input Math score : ";
    cin >> student[sCount].sMath;
    student[sCount].sTotal = student[sCount].sKor + student[sCount].sEng
                            + student[sCount].sMath;
    student[sCount].sAvg = (student[sCount].sTotal)/3.f;
    ++sCount;
    system("clear");
    return 0;
}

int studentDelete(Student *student, int& sCount){
    if(sCount == 0)
        return -1;

    int deleteIndex=0;
    cout << "input delete index :";
    cin >> deleteIndex;
    
    for(int i=deleteIndex; i<sCount-1; i++){
        student[i] = student[i+1];
    }
    --sCount;
    return 0;  
}

int studentFind(Student *student, int& sCount){
    if(sCount == 0)
        return -1;
    char findName[NAME_SIZE];
    bool findout = true;
    cout << "input name to find : " << endl;
    cin >> findName;
    
    for(int i=0; i<sCount; i++){
        if(!strcmp(findName, student[i].sName)){
            cout << "Name\t" << student[i].sName << endl;
            cout << "ID\t" << student[i].sID << endl;
            cout << "KOR\t" << student[i].sKor << endl;
            cout << "ENG\t" << student[i].sEng << endl;
            cout << "MATH\t" << student[i].sMath << endl;
            cout << "Total\t" << student[i].sTotal << endl;
            cout << "Average\t" << student[i].sAvg << endl;
            findout = false;
        }
    }
    if(findout = true)
        cout << "cannot find" <<endl;
    return 0;
}

int studentPrint(Student *student, int& sCount){
    if(sCount == 0)
        return -1;

    cout << "============= student list =============" << endl;
    for(int i=0; i<sCount; i++){
        cout << "Index\t" << i << endl;
        cout << "Name\t" << student[i].sName << endl;
        cout << "ID\t" << student[i].sID << endl;
        cout << "KOR\t" << student[i].sKor << endl;
        cout << "ENG\t" << student[i].sEng << endl;
        cout << "MATH\t" << student[i].sMath << endl;
        cout << "Total\t" << student[i].sTotal << endl;
        cout << "Average\t" << student[i].sAvg << endl;
        cout << "========================================" << endl;
    }
    return 0;    
}

void ErrorHandling(string msg){
    system("clear");
    cout << msg <<endl;
}

2. 도서관 책 관리 프로그램


#include <iostream>
#include <string.h>
using namespace std;
#define BOOK_NAME_SIZE 64
#define BOOK_NUMBER 8
#define MAX_BOOK_COUNT 64

enum {
    MENU_REGISTER=1,
    MENU_LEND,
    MENU_RETURN,
    MENU_LIST
};

struct Library{
    char bName[BOOK_NAME_SIZE];
    char bNumber[BOOK_NUMBER+1];
    int bPrice;
    bool isLend;
};

void ErrorHandling(string msg);
int bRegister(Library *b, int& bookCnt);
int bLend(Library *b, int& bookCnt);
int bReturn(Library *b, int& bookCnt);
int bList(Library *b, int& bookCnt);

int main(){
    int bookCount=0;
    int selectMenu;
    Library myBook[MAX_BOOK_COUNT] = {};
    while(1){

        cout << endl;
        cout << "Library System" << endl;
        cout << "1. Register Book" << endl;
        cout << "2. Lend Book" << endl;
        cout << "3. Return Book" << endl;
        cout << "4. Book List" << endl;
        cout << "select menu : ";
        cin >> selectMenu;
        
        if(cin.fail()){
            cin.clear();
            cin.ignore(1024,'\n');
        }
        
        switch(selectMenu){
            case MENU_REGISTER:
                if(bRegister(myBook, bookCount) == -1){
                    ErrorHandling("Cannot Register");
                    continue;
                }
                break;
            case MENU_LEND:
                if(bLend(myBook, bookCount) == -1){
                    ErrorHandling("Cannot Lend");
                    continue;
                }
                break;
            case MENU_RETURN:
                if(bReturn(myBook, bookCount) == -1){
                    ErrorHandling("Cannot Return");
                    continue;
                }
                break;
            case MENU_LIST:
                if(bList(myBook, bookCount) == -1){
                    ErrorHandling("Cannot Print List");
                    continue;
                }
                break;
            default:
                continue;
        }
    }
    return 0;
}

int bRegister(Library *b, int& bookCnt){
    if(bookCnt == MAX_BOOK_COUNT)
        return -1;
    system("clear");
    cin.ignore(1024,'\n');
    cout << "Book name : ";
    cin.getline(b[bookCnt].bName, BOOK_NAME_SIZE);
    cout << "Lending Price : ";
    cin >> b[bookCnt].bPrice;
    cout << "Book Number : ";
    cin >> b[bookCnt].bNumber;
    b[bookCnt].isLend = false;
    
    ++bookCnt;
    system("clear");
    return 0;
}

int bLend(Library *b, int& bookCnt){
    if(bookCnt == 0)
        return -1;
        
    system("clear");
    cin.ignore(1024,'\n');
    char bookNameToLend[BOOK_NAME_SIZE];
    cout << "input Book name to lend : ";
    cin.getline(bookNameToLend, BOOK_NAME_SIZE);
    for(int i=0; i<bookCnt; i++){
        if(!strcmp(bookNameToLend, b[i].bName)){
            b[i].isLend = true;
            cout << b[i].bName << " lended" << endl;
        }
    }
    return 0;
}

int bReturn(Library *b, int& bookCnt){
    if(bookCnt == 0)
        return -1;
    system("clear");
    cin.ignore(1024,'\n');
    char bookNameToReturn[BOOK_NAME_SIZE];
    cout << "input Book name to return : ";
    cin.getline(bookNameToReturn, BOOK_NAME_SIZE);
    for(int i=0; i<bookCnt; i++){
        if(!strcmp(bookNameToReturn, b[i].bName)) {
            b[i].isLend = false;
            cout << b[i].bName << " returned" << endl;
        }
    }
    return 0;
}

int bList(Library *b, int& bookCnt){
    if(bookCnt == 0)
        return -1;
    system("clear");
    cout << "============ book list =============" << endl;
    for(int i=0; i<bookCnt; i++){
        cout << "Book Name\t" << b[i].bName << endl;
        cout << "Lending Price\t" << b[i].bPrice << endl;
        cout << "Book Number\t" << b[i].bNumber << endl;
        if(b[i].isLend)
            cout << "on Lending" << endl;
        else
            cout << "on Library" << endl;
        cout << "===================================" << endl;
    }
    return 0;
}

void ErrorHandling(string msg){
    system("clear");
    cout << msg <<endl;
}


'Computer > C와 C++' 카테고리의 다른 글

7월 10일  (0) 2018.07.11
7월 3일  (0) 2018.07.03
7월 2일 C/C++  (0) 2018.07.02

+ Recent posts