ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C] 키오스크 - IEC61508 위배 코드
    공부/C 2023. 8. 4. 17:23

    IEC 61508  표준 위배 코드 작성

    06/04 ~ 

     

    #include <Windows.h>
    #include <stdio.h>
    #include <string.h>
    
    // 키 입력
    #define UP 72
    #define LEFT 75
    #define RIGHT 77
    #define DOWN 80 
    #define ENTER 13
    #define MENUNUM 4
    #define OPTION(a) a?"hot":"ice"
    
    static int pageStatement = 0;
    
    // 콘솔에 위치할 메뉴의 높이, 메뉴의 개수
    //static int menuHeight = 8;
    //static int menuNumber = 2;
    
    static const char* menu[MENUNUM][2] = {
    	{"Americano", "2000"},
    	{"Cafe_Latte", "3000"},
    	{"Peach_Ice_Tea", "2500"},
    	{"Green_Tea", "1500"}
    };
    
    /* 함수 전방 선언 */
    void Init();
    void CursorHide();
    void gotoxy(int x, int y);
    void InputKey();
    void InputKey2();
    COORD GetConsoleCursorPosition();
    void PrintMainPage();
    void InitMenu();
    void clear();
    void PrintMenuPage();
    void InitItem();
    void SetMenuName(char _idx);
    void AddMenu();
    void PrintOrderList();
    //void AddMenu();
    
    
    struct Item {
    	char name[30];
    	int option;
    	int price;
    };
    
    struct Order {
    	int orderType;
    	struct Item* itemList;
    	int size;
    };
    
    static struct Order receipt;
    static struct Item item;
    
    int main()
    {
    	Init();
    	while (1)
    	{
    		if (pageStatement != 0)
    			goto page2;	//goto 문 사용
    		InputKey();
    	}
    	page2:
    	while (1)
    	{
    		if (pageStatement != 1)
    			break;
    		InputKey2();
    	}
    
    	return 0;
    }
    
    void Init()
    {
    	pageStatement = 0;
    	system("mode con cols=64 lines=24 | title test");
    	CursorHide();
    	PrintMainPage();
    }
    
    void CursorHide()
    {
    	CONSOLE_CURSOR_INFO cursorInfo = { 0, };
    	cursorInfo.dwSize = 1; //커서 굵기 (1 ~ 100)
    	cursorInfo.bVisible = FALSE; //커서 Visible TRUE(보임) FALSE(숨김)
    	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
    }
    
    void gotoxy(int x, int y)
    {
    	HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    	COORD pos;
    	pos.X = x;
    	pos.Y = y;
    	SetConsoleCursorPosition(consoleHandle, pos);
    }
    
    COORD GetConsoleCursorPosition()
    {
    	CONSOLE_SCREEN_BUFFER_INFO cs;
    	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cs);
    	return cs.dwCursorPosition;
    }
    
    void PrintMainPage()
    {
    	gotoxy(13, 2);
    	printf("======================================");
    	gotoxy(13, 3);
    	printf("==      IEC61508 Test Program       ==");
    	gotoxy(13, 4);
    	printf("======================================");
    
    	gotoxy(24, 8);
    	printf("> 1. For here\n");
    	gotoxy(26, 9);
    	printf("2. Take out\n");
    	gotoxy(24, 8);
    	//printf("Exit%d\n");
    }
    
    void InputKey()
    {
    	int key = 0;
    
    	// 현재 키 값 읽어오기
    	COORD pos = GetConsoleCursorPosition();
    
    	// 키가 눌렸다면
    	if (_kbhit() > 0)
    	{
    		key = _getch();
    
    		// 방향키 입력
    		if (key == 224)
    		{
    			//up:72, left:75, right:77, down:80 
    			key = _getch();
    			switch (key)
    			{
    			case UP:
    				if (pos.Y > 8)
    				{
    					gotoxy(24, pos.Y);
    					printf(" ");
    					gotoxy(24, pos.Y - 1);
    					printf(">");
    				}
    				break;
    			case LEFT:
    				break;
    			case RIGHT:
    				break;
    			case DOWN:
    				if (pos.Y < 9)
    				{
    					gotoxy(24, pos.Y);
    					printf(" ");
    					gotoxy(24, pos.Y + 1);
    					printf(">");
    				}
    				break;
    			default:
    				break;
    			}
    
    		}
    		//Enter
    		else if(key == 13)
    		{
    			// 먹고가기는 0, 포장은 1
    			receipt.orderType = pos.Y == 8 ? 0 : 1;
    			InitMenu();
    		}
    	}
    }
    
    //
    void InitMenu()
    {
    	pageStatement = 1;
    	clear();
    	InitItem();
    	PrintMenuPage();
    }
    
    void InitItem()
    {
    	strcpy(item.name, menu[0][0]);
    	item.option = 0;	//ICE
    	item.price = atoi(menu[0][1]);
    }
    
    // 콘솔 화면 지우기
    void clear()
    {
    	for (int i = 0; i < 64; i++)
    	{
    		gotoxy(0, i);
    		printf("                                                               ");
    	}
    }
    
    // 메뉴 출력
    void PrintMenuPage()
    {
    	gotoxy(13, 2);
    	printf("======================================");
    	gotoxy(13, 3);
    	printf("==               Menu               ==");
    	gotoxy(13, 4);
    	printf("======================================");
    	gotoxy(13, 8);
    	printf(">");
    	//printf("%d",length);
    	int k = 0;
    	for (int i = 0; i < MENUNUM-1; ++k)
    	{
    		i = k;
    		gotoxy(15, i+8);
    		printf("%d. %s : %s", i+1, menu[i][0], menu[i][1]);
    	}
    	gotoxy(15, k + 9);
    	printf("ICE / HOT");
    	gotoxy(15, k + 10);
    	printf("===");		//length : 6
    	gotoxy(13, 8);
    }
    
    void InputKey2()
    {
    	int key = 0;
    
    	// 현재 키 값 읽어오기
    	COORD pos = GetConsoleCursorPosition();
    
    	// 키가 눌렸다면
    	if (_kbhit() > 0)
    	{
    		key = _getch();
    
    		// 방향키 입력
    		if (key == 224)
    		{
    			//up:72, left:75, right:77, down:80 
    			key = _getch();
    			switch (key)
    			{
    			case UP:
    				if (pos.Y > 8)
    				{
    					gotoxy(13, pos.Y);
    					printf(" ");
    					gotoxy(13, pos.Y - 1);
    					printf(">");
    				}
    				break;
    			case LEFT:
    				//printf("%s\n", OPTION(item.option));
    				if (item.option == 1)
    				{
    					int temp = pos.Y;
    					gotoxy(21, 14);
    					printf("   ");
    					gotoxy(15, 14);
    					printf("===");
    					gotoxy(pos.X, pos.Y);
    					item.option = 0;
    				}
    				break;
    			case RIGHT:
    				if (item.option == 0)
    				{
    					int temp = pos.Y;
    					gotoxy(15, 14);
    					printf("   ");
    					gotoxy(21, 14);
    					printf("===");		//length : 6
    					gotoxy(pos.X, pos.Y);
    					item.option = 1;
    				}
    				break;
    			case DOWN:
    				if (pos.Y < 8 + MENUNUM -1)
    				{
    					gotoxy(13, pos.Y);
    					printf(" ");
    					gotoxy(13, pos.Y + 1);
    					printf(">");
    				}
    				break;
    			default:
    				break;
    			}
    
    		}
    		//Enter
    		else if (key == 13)
    		{
    			SetMenuName(pos.Y);
    			AddMenu();
    			InitMenu();
    			PrintOrderList();
    		}
    	}
    }
    
    void SetMenuName(char _idx)
    {
    	char* srcString = menu[_idx - 8][0];
    	char* name = (char*)malloc(sizeof(char) * strlen(srcString) + 1);
    	strcpy(name, srcString);
    	strcpy(item.name, srcString);
    	printf("%s \n", item.name);
    	free(name);
    }
    
    void AddMenu()
    {
    	if (receipt.size)
    	{
    		receipt.size += 1;
    		receipt.itemList = realloc(receipt.itemList, sizeof(struct Item) * receipt.size);
    		receipt.itemList[receipt.size - 1] = item;
    	}
    	else
    	{
    		//recipt 가 비어있을 경우
    		receipt.size = 1;
    		receipt.itemList = malloc(sizeof(struct Item) * receipt.size);
    		receipt.itemList[receipt.size - 1] = item;
    	}
    }
    
    void PrintOrderList()
    {
    	COORD pos = GetConsoleCursorPosition();
    	for (int i = 0; i < receipt.size; ++i)
    	{
    		gotoxy(15, 16 + i);
    		printf("%d. %s : %s", i+1, receipt.itemList[i].name, OPTION(receipt.itemList[i].option));
    	}
    	gotoxy(pos.X, pos.Y);
    }

    '공부 > C' 카테고리의 다른 글

    과제 studentmgnt  (0) 2020.12.20
    1219C Project  (0) 2020.12.19
    linkedlist  (0) 2020.12.17
Designed by Tistory.