Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. typedef struct Donor {
  6. char name[21];
  7. char bloodType[3];
  8. char availability[12];
  9. } Donor;
  10.  
  11. Donor donors[100];
  12. int donorCount = 0;
  13.  
  14. char dataPath[] = "data.txt";
  15.  
  16. int searchAvailable(const char * bloodType) {
  17. int i;
  18. for (i = 0; i < donorCount; ++i) {
  19. if (!strcmpi(donors[i].availability, "Available") && !strcmpi(bloodType, donors[i].bloodType)) return i;
  20. }
  21. return -1;
  22. }
  23.  
  24. int countBloodType(const char * bloodType) {
  25. int count = 0;
  26. int i;
  27. for (i = 0; i < donorCount; ++i) {
  28. if (!strcmpi(donors[i].availability, "Available") && !strcmpi(bloodType, donors[i].bloodType)) ++count;
  29. }
  30. return count;
  31. }
  32.  
  33. void swapData(Donor * A, Donor * B) {
  34. Donor tmp = *A;
  35. *A = *B;
  36. *B = tmp;
  37. }
  38.  
  39. void sortData() {
  40. int i, j;
  41. for (i = 0; i < donorCount; ++i) {
  42. for (j = 0; j < donorCount - i - 1; ++j) {
  43. if (strcmpi(donors[j].name, donors[j + 1].name) > 0) swapData(&donors[j], &donors[j + 1]);
  44. }
  45. }
  46. }
  47.  
  48. void printData() {
  49. int i;
  50. printf("|%-3s|%-20s|%-10s|%-13s|\n", "No.", "Donor Name", "Blood Type", "Availability");
  51. for (i = 0; i < donorCount; ++i) {
  52. printf("|%-3d|%-20s|%-10s|%-13s|\n", i + 1, donors[i].name, donors[i].bloodType, donors[i].availability);
  53. }
  54. }
  55.  
  56. void readData() {
  57. FILE * file;
  58. file = fopen(dataPath, "r");
  59. if (file != NULL) {
  60. char tmpName[21];
  61. char tmpBloodType[3];
  62. char tmpAvailability[12];
  63. while (fscanf(file, "%20[^#]#%2[^#]#%12[^\n]\n", tmpName, tmpBloodType, tmpAvailability) != EOF) {
  64. strcpy(donors[donorCount].name, tmpName);
  65. strcpy(donors[donorCount].bloodType, tmpBloodType);
  66. strcpy(donors[donorCount].availability, tmpAvailability);
  67. ++donorCount;
  68. }
  69. fclose(file);
  70. } else {
  71. printf("Error on opening file\n");
  72. }
  73. }
  74.  
  75. void writeData() {
  76. FILE * file;
  77. file = fopen(dataPath, "w");
  78.  
  79. int i;
  80. for (i = 0; i < donorCount; ++i) {
  81. fprintf(file, "%s#%s#%s\n", donors[i].name, donors[i].bloodType, donors[i].availability);
  82. }
  83.  
  84. fclose(file);
  85. }
  86.  
  87. void clearScreen() {
  88. int i;
  89. for (i = 0; i < 25; ++i) printf("\n");
  90. }
  91.  
  92. void printMenu() {
  93. printf("+----------------------+\n");
  94. printf("|Blood Bank Application|\n");
  95. printf("+----------------------+\n\n");
  96. printf("1. View Data (Sorted by Donor Name)\n");
  97. printf("2. Insert New Data\n");
  98. printf("3. Request Blood Transfusion\n");
  99. printf("4. Save Data and Exit Application\n\n");
  100. }
  101.  
  102. int inputBloodType(char * input) {
  103. char tmpInput[3];
  104.  
  105. scanf("%3s%*c", tmpInput);
  106. if (!strcmp(tmpInput, "A") || !strcmp(tmpInput, "B") || !strcmp(tmpInput, "O") || !strcmp(tmpInput, "AB")) {
  107. strcpy(input, tmpInput);
  108. return 0;
  109. }
  110.  
  111. return 1;
  112. }
  113.  
  114. int inputstrl(int lowerBound, int upperBound, char * input) {
  115. char tmpInput[101];
  116.  
  117. scanf("%100[^\n]%*c", tmpInput);
  118. if (strlen(tmpInput) >= lowerBound && strlen(tmpInput) <= upperBound) {
  119. strcpy(input, tmpInput);
  120. return 0;
  121. }
  122. return 1;
  123. }
  124.  
  125. int alphabetOnly(char * str) {
  126. int length = strlen(str);
  127. int i;
  128. printf("in: %s\n", str);
  129. for (i = 0; i < length; ++i) {
  130. char c = toupper(str[i]);
  131. if ((c < 'A' || c > 'Z') && c != ' ') return 0;
  132. }
  133. return 1;
  134. }
  135.  
  136. int inputi(int lowerBound, int upperBound, int * input) {
  137. int tmpInput;
  138. if (scanf("%d%*c", &tmpInput) == 0) scanf("%*s");
  139. else if (tmpInput >= lowerBound && tmpInput <= upperBound) {
  140. *input = tmpInput;
  141. return 0;
  142. }
  143. return 1;
  144. }
  145.  
  146. int main() {
  147. readData();
  148. do {
  149. clearScreen();
  150. printMenu();
  151.  
  152. int choosenIndex;
  153.  
  154. do {
  155. printf("Input your choice: ");
  156. } while (inputi(1, 4, &choosenIndex));
  157.  
  158. if (choosenIndex == 1) {
  159. sortData();
  160. clearScreen();
  161. printData();
  162. printf("Number of available blood transfusion:\n");
  163. printf("- Blood type A : %d\n", countBloodType("A"));
  164. printf("- Blood type B : %d\n", countBloodType("B"));
  165. printf("- Blood type AB: %d\n", countBloodType("AB"));
  166. printf("- Blood type O : %d\n", countBloodType("O"));
  167. printf("\nPress enter to continue...");
  168. } else if (choosenIndex == 2) {
  169. clearScreen();
  170.  
  171. char donorName[101];
  172. char bloodType[3];
  173. do {
  174. printf("Donor Name [4-20 characters (alphabet only)]: ");
  175. } while (inputstrl(4, 20, donorName) || !alphabetOnly(donorName));
  176. do {
  177. printf("Donor Blood Type ( A | B | O | AB ): ");
  178. } while (inputBloodType(bloodType));
  179.  
  180. strcpy(donors[donorCount].name, donorName);
  181. strcpy(donors[donorCount].bloodType, bloodType);
  182. strcpy(donors[donorCount].availability, "Available");
  183. ++donorCount;
  184. } else if (choosenIndex == 3) {
  185. char bloodType[3];
  186. sortData();
  187. clearScreen();
  188. printData();
  189. do {
  190. printf("Blood Type (A | B | O | AB ): ");
  191. } while (inputBloodType(bloodType));
  192.  
  193. int availableIndex;
  194. if ((availableIndex = searchAvailable(bloodType)) >= 0) {
  195. printf("Blood transfusion available found\n\n");
  196. printf("Donor Name : %s\n", donors[availableIndex].name);
  197. printf("Blood Type : %s\n", donors[availableIndex].bloodType);
  198. printf("Availability : %s\n", donors[availableIndex].availability);
  199. printf("Requesting...\n");
  200. strcpy(donors[availableIndex].availability, "Unavailable");
  201. } else {
  202. printf("Sorry, no blablabla\n");
  203. }
  204.  
  205. printf("Press enter to continue...");
  206.  
  207. } else {
  208. break;
  209. }
  210. getchar();
  211. } while (1);
  212. writeData();
  213. return 0;
  214. }
  215.  
  216. /* data.txt
  217. Brandon Holmes#AB#Unavailable
  218. Eric West#O#Available
  219. Graciela Hudson#B#Unavailable
  220. James Hubbard#B#Available
  221. Olivia Kendrick#O#Unavailable
  222. Rosa Baynes#AB#Available
  223. Test data#O#Available
  224. William Duke#A#Unavailable
  225. Yorick Mori#O#Available
  226.  
  227. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement