[关闭]
@songpfei 2016-03-28T20:25:25.000000Z 字数 3390 阅读 1730

医院门诊管理系统

OJ_算法


华为OJ题:

  1. // huawei_test.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include <string>
  4. #include <iostream>
  5. #include<queue>
  6. using namespace std;
  7. typedef struct tagPatient
  8. {
  9. string id;
  10. bool type;
  11. int cash;
  12. int security;
  13. int treat_state;
  14. }Patient;
  15. Patient g_patient[6];
  16. queue<int> g_patient_queue;
  17. static vector<string> split(string str, const string &pattern)
  18. {
  19. string::size_type pos;
  20. vector<string> result;
  21. string str_temp;
  22. str += pattern;
  23. string::size_type size = str.size();
  24. for (string::size_type i = 0; i < size; i++)
  25. {
  26. pos = str.find(pattern, i);
  27. if (pos < size)
  28. {
  29. str_temp = str.substr(i, pos - i);
  30. result.push_back(str_temp);
  31. i = pos + pattern.size() - 1;
  32. }
  33. }
  34. return result;
  35. }
  36. static int GetPatientID(string partID)
  37. {
  38. int pat_id=0;
  39. for (int i = 0; i < 6; i++)
  40. {
  41. if (g_patient[i].id == partID)
  42. {
  43. pat_id = i;
  44. break;
  45. }
  46. }
  47. return pat_id;
  48. }
  49. static bool PayFee(int part_id, int fee_account)
  50. {
  51. //缴挂号费或处方费
  52. if ((g_patient[part_id].type == 1) && (g_patient[part_id].security >= fee_account))
  53. {
  54. g_patient[part_id].security -= fee_account;
  55. }
  56. else
  57. {
  58. if (g_patient[part_id].cash >= fee_account)
  59. g_patient[part_id].cash -= fee_account;
  60. else
  61. return false;
  62. }
  63. return true;
  64. }
  65. static int GetPatientQueueSerialNumber(int part_id)
  66. {
  67. queue<int> patient_queue_temp(g_patient_queue);
  68. int serial_num = 1;
  69. while (!patient_queue_temp.empty())
  70. {
  71. if (patient_queue_temp.front() == part_id)
  72. break;
  73. serial_num++;
  74. patient_queue_temp.pop();
  75. }
  76. return serial_num;
  77. }
  78. void initialize()
  79. {
  80. g_patient[0] = { "pat01", 0, 100, 0, 0 };
  81. g_patient[1] = { "pat02", 1, 100, 100, 0 };
  82. g_patient[2] = { "pat03", 0, 100, 0, 0 };
  83. g_patient[3] = { "pat04", 1, 100, 50, 0 };
  84. g_patient[4] = { "pat05", 1, 10, 10, 0 };
  85. g_patient[5] = { "pat06", 1, 20, 10, 0 };
  86. while (g_patient_queue.size() > 0)
  87. {
  88. g_patient_queue.pop();
  89. }
  90. cout << "E000" << endl;
  91. }
  92. void PatientRegister(string partID)
  93. {
  94. if (g_patient_queue.size() >= 4)
  95. {
  96. cout << "E003" << endl;
  97. return;
  98. }
  99. int part_id = GetPatientID(partID);
  100. if (g_patient[part_id].treat_state != 0)
  101. {
  102. cout << "E002" << endl;
  103. return;
  104. }
  105. if (!PayFee(part_id, 10))
  106. return;
  107. g_patient_queue.push(part_id);
  108. g_patient[part_id].treat_state = 1;
  109. cout << "E001" << endl;
  110. }
  111. void PatientDiagnose()
  112. {
  113. if (g_patient_queue.empty())
  114. {
  115. cout << "E006" << endl;
  116. return;
  117. }
  118. int part_id = g_patient_queue.front();
  119. g_patient[part_id].treat_state = 2;
  120. g_patient_queue.pop();
  121. cout << "E005" << endl;
  122. }
  123. void PatientPay(string partID)
  124. {
  125. int part_id = GetPatientID(partID);
  126. if (g_patient[part_id].treat_state != 2)
  127. {
  128. cout << "E014" << endl;
  129. return;
  130. }
  131. if (!PayFee(part_id, 50))
  132. cout << "E008" << endl;
  133. else
  134. cout << "E007" << endl;
  135. g_patient[part_id].treat_state = 0;
  136. }
  137. void Query(string query_cmd)
  138. {
  139. if (query_cmd[0] == '0')
  140. {
  141. if (g_patient_queue.empty())
  142. {
  143. cout << "E013:dct 0" << endl;
  144. return;
  145. }
  146. cout << "E013:dct";
  147. queue<int> patient_queue_temp(g_patient_queue);
  148. while (!patient_queue_temp.empty())
  149. {
  150. cout << " " << g_patient[patient_queue_temp.front()].id;
  151. patient_queue_temp.pop();
  152. }
  153. cout << endl;
  154. }
  155. else
  156. {
  157. int part_id = GetPatientID(query_cmd.substr(2));
  158. switch (g_patient[part_id].treat_state)
  159. {
  160. case 0:
  161. cout << "E012:" << g_patient[part_id].id << " 0 ";
  162. break;
  163. case 1:
  164. cout << "E012:" << g_patient[part_id].id << " 1 " << GetPatientQueueSerialNumber(part_id) << ",";
  165. break;
  166. case 2:
  167. cout << "E012:" << g_patient[part_id].id << " 2 ";
  168. break;
  169. default:
  170. break;
  171. }
  172. cout << g_patient[part_id].security << "," << g_patient[part_id].cash << endl;
  173. }
  174. }
  175. int main()
  176. {
  177. string command_in;
  178. vector<string> sub_cmd;
  179. getline(cin, command_in);
  180. sub_cmd = split(command_in, ",");
  181. for (vector<string>::size_type i = 0; i < sub_cmd.size(); i++)
  182. {
  183. vector<string> split_cmd = split(sub_cmd[i], "_");
  184. if (split_cmd[0] == "i")
  185. initialize();
  186. else if (split_cmd[0] == "reg")
  187. {
  188. PatientRegister(split_cmd[1]);
  189. }
  190. else if (split_cmd[0] == "diag")
  191. {
  192. PatientDiagnose();
  193. }
  194. else if (split_cmd[0] == "pay")
  195. {
  196. PatientPay(split_cmd[1]);
  197. }
  198. else if (split_cmd[0] == "qu")
  199. {
  200. Query(split_cmd[1]);
  201. }
  202. else
  203. break;
  204. }
  205. return 0;
  206. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注