# Shell.cpp 1/25 接收輸入部分 ``` #include <iostream> #include <string.h> using namespace std; bool CmdCompare(const char* CmdToCmp); bool StrCompare(char* context, const char* Compare); //compare maximum 20 characters void SubContxt(char* subArr, char* mainArr, short begin, char target); //從指定位置開始分割字串直到指定字元 short CheckWind(char* wind); class Gameinfo{ public: short PlayerID = 1, BWind = 1, SWind = 1, OpenAt = 1, Master = 1, Mcombo = 0; Gameinfo() {} }; char command_title[20] = {0}; char command_contxt[70] = {0}; int main() { Gameinfo currentGame; while (true) { memset(command_title, 0, 20 * sizeof(char)); //sting.h 僅用 memset memset(command_contxt, 0, 70 * sizeof(char)); char inputchar; bool is_title = true; for (short title_size = 0, contxt_size = 0; title_size < 20 && contxt_size < 70;) { inputchar = cin.get(); //輸入形式必須為 "<command> <context>[Enter]" if (!is_title) { if (inputchar == '\n') { command_contxt[contxt_size] = 0; break; } else command_contxt[contxt_size++] = inputchar; continue; } else if (is_title && inputchar == ' ') { is_title = false; command_title[title_size] = 0; continue; } else command_title[title_size++] = inputchar; } cout << command_title << ' ' << command_contxt << endl; if (CmdCompare("/start")) currentGame.PlayerID = (short)command_contxt[3] - '0'; if (CmdCompare("/initGame")) { char subtxt[6] = {0}; SubContxt(subtxt, command_contxt, 0, ' '); currentGame.BWind = CheckWind(subtxt); SubContxt(subtxt, command_contxt, 0, ' '); currentGame.SWind = CheckWind(subtxt); currentGame.OpenAt = command_contxt[0] - '0'; currentGame.Master = command_contxt[2] - '0'; currentGame.Mcombo = command_contxt[4] - '0'; } if (CmdCompare("/quit")) break; } return -1; } bool CmdCompare(const char* CmdToCmp) { bool flag = true; for (short i = 0; command_title[i] != 0; i++) if(command_title[i] != CmdToCmp[i] || i > 10) { flag = false; break; } return flag; } bool StrCompare(char* context, const char* Compare) //compare maximum 20 characters { bool flag = true; for (short i = 0; context[i] != 0; i++) if(context[i] != Compare[i] || i > 20) { flag = false; break; } return flag; } void SubContxt(char* subArr, char* mainArr, short begin, char target) { short i; for (i = begin; *(mainArr + i - 1) != target; i++) { *(subArr + i) = *(mainArr + i); } for (short j = begin; *(mainArr + j) != 0; j++) { if (*(mainArr + j + i) != 0) *(mainArr + j) = *(mainArr + j + i); else *(mainArr + j) = 0; } } short CheckWind(char* wind) { switch (*wind) { case 'E': return 1; case 'S': return 2; case 'W': return 3; case 'N': return 4; default: return -1; } } ```