Advertisement
Guest User

Untitled

a guest
May 24th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define INSERT "insert"
  6. #define DELETE "delete"
  7. #define FIND "find"
  8. #define MIN "min"
  9. #define MAX "max"
  10. #define INORDER "inorder"
  11. #define BLACK 'B'
  12. #define RED 'R'
  13.  
  14. int N = 0;
  15. FILE* file;
  16.  
  17. //zwraca 0 - sukces 1 - blad
  18. int openFile(char* fileName);
  19. //zwraca 0 - sukces 1 - plik nie zostal zainicjalizowany
  20. int closeFile();
  21. //zwraca odczytana komende
  22. char* readCommand();
  23. //zwraca argument przy komendzie
  24. int readNumber();
  25. int invokeCommand();
  26. //zwraca 0 - sukces 1 - blad
  27. int insertValue(int value);
  28. //zwraca 0 - sukces 1 - blad
  29. int deleteValue(int value);
  30. //zwraca 0 - sukces 1 - blad
  31. int findValue(int value);
  32. void maxValue();
  33. void minValue();
  34. void inOrder();
  35.  
  36. int main(int argc, char **argv)
  37. {
  38. int i = 0;
  39. char* command;
  40.  
  41. if(argc != 2)
  42. {
  43. printf("Nieprawidłowa ilosc argumentow\n");
  44. return 0;
  45. }
  46.  
  47. if(openFile(argv[1]) == 1)
  48. {
  49. printf("Proba otwarcia pliku zakonczona niepowodzeniem\n");
  50. return 0;
  51. }
  52.  
  53. N = readNumber();
  54. for ( i = 0; i < N; i++ )
  55. {
  56. command = readCommand();
  57. if(invokeCommand(command) == 1)
  58. {
  59. printf("Plik wejsciowy zawiera nieprawidowe dane\n");
  60. return 0;
  61. }
  62.  
  63. }
  64.  
  65. if(closeFile() == 1)
  66. {
  67. printf("Niepowodzenie podczas zamykania pliku\n");
  68. }
  69.  
  70.  
  71. return 0;
  72. }
  73.  
  74. int openFile(char* fileName)
  75. {
  76. if ((file = fopen(fileName, "r")) == NULL)
  77. {
  78. printf ("Nie mogę otworzyć pliku. \n");
  79. return 1;
  80. }
  81.  
  82. return 0;
  83. }
  84.  
  85. int closeFile()
  86. {
  87. if(file != NULL)
  88. {
  89. if(fclose(file) == EOF)
  90. {
  91. return 1;
  92. }
  93.  
  94. return 0;
  95. }
  96. else
  97. {
  98. return 1;
  99. }
  100. }
  101.  
  102. char* readCommand()
  103. {
  104. char command[20];
  105. char* resultCommand;
  106. fscanf(file, "%s", command);
  107. if((resultCommand = (char*)malloc(sizeof(char) + sizeof(char) * strlen(command))) == NULL)
  108. {
  109. printf("Nie udalo sie zarezerwowac miejsca, wcisnij ENTER");
  110. getchar();
  111. return NULL;
  112. }
  113. else
  114. {
  115. strcpy( resultCommand, command );
  116. return resultCommand;
  117. }
  118.  
  119. return NULL;
  120. }
  121.  
  122. int readNumber()
  123. {
  124. int argument = 0;
  125. if(fscanf(file, "%d", &argument) == EOF)
  126. {
  127. printf("Blad odczytu argumentu. Dalsze dzialanie programu moze przyniesc nieoczekiwany efekt\n");
  128. }
  129. return argument;
  130. }
  131.  
  132. int invokeCommand(char* command)
  133. {
  134. int argument;
  135.  
  136. if(strcmp(command, INSERT) == 0)
  137. {
  138. argument = readNumber();
  139. insertValue(argument);
  140. return 0;
  141. }
  142.  
  143. if(strcmp(command, DELETE) == 0)
  144. {
  145. argument = readNumber();
  146. deleteValue(argument);
  147. return 0;
  148. }
  149.  
  150. if(strcmp(command, FIND) == 0)
  151. {
  152. argument = readNumber();
  153. findValue(argument);
  154. return 0;
  155. }
  156.  
  157. if(strcmp(command, MIN) == 0)
  158. {
  159. minValue();
  160. return 0;
  161. }
  162.  
  163. if(strcmp(command, MAX) == 0)
  164. {
  165. maxValue();
  166. return 0;
  167. }
  168.  
  169. if(strcmp(command, INORDER) == 0)
  170. {
  171. inOrder();
  172. return 0;
  173. }
  174.  
  175. return 1;
  176. }
  177.  
  178. int insertValue(int value)
  179. {
  180. printf("insertValue %d \n", value);
  181. return 0;
  182. }
  183.  
  184. int deleteValue(int value)
  185. {
  186. printf("deleteValue %d \n", value);
  187. return 0;
  188. }
  189.  
  190. int findValue(int value)
  191. {
  192. printf("findValue %d \n", value);
  193. return 0;
  194. }
  195.  
  196. void maxValue()
  197. {
  198. printf("maxValue\n");
  199. }
  200.  
  201. void minValue()
  202. {
  203. printf("minValue\n");
  204. }
  205.  
  206. void inOrder()
  207. {
  208. printf("inOrder\n");
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement