Advertisement
Guest User

C - pametni znak kalk

a guest
Jul 16th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #ifndef DEBUG
  6. #define DEBUG(...) printf(__VA_ARGS__)
  7. #endif
  8.  
  9. // Napisati funkciju koja cisti string od nepotrebnih znakova
  10. void clean_string(char a[], char b[], char c[]) {
  11. int i, posmak = 0;
  12.  
  13. for(i = 0; i < strlen(a); i++) {
  14. a[i - posmak] = tolower(a[i]);
  15. if(!isalpha(a[i])) {
  16. posmak++;
  17. }
  18. }
  19. a[i - posmak] = '\0';
  20. posmak = 0;
  21.  
  22. for(i = 0; i < strlen(b); i++) {
  23. b[i - posmak] = tolower(b[i]);
  24. if(!isalpha(b[i])) {
  25. posmak++;
  26. }
  27. }
  28. b[i - posmak] = '\0';
  29. posmak = 0;
  30.  
  31. for(i = 0; i < strlen(c); i++) {
  32. c[i - posmak] = tolower(c[i]);
  33. if(!isalpha(c[i])) {
  34. posmak++;
  35. }
  36. }
  37. c[i - posmak] = '\0';
  38. }
  39.  
  40. char get_operator(char o[]) {
  41. if (!strcmp("plus", o)) {
  42. return '+';
  43. }
  44.  
  45. if (!strcmp("minus", o)) {
  46. return '-';
  47. }
  48.  
  49. if (!strcmp("puta", o)) {
  50. return '*';
  51. }
  52.  
  53. if (!strcmp("podijeljeno", o)) {
  54. return '/';
  55. }
  56.  
  57. if (!strcmp("modulo", o)) {
  58. return '%';
  59. }
  60.  
  61. return ' ';
  62. }
  63.  
  64. // Napisati funkciju koja iz striga odredi vrijednost operanda
  65. char get_operand(char n[]) {
  66. char broj[10][256] = {"nula", "jedan", "dva", "tri", "cetiri", "pet", "sest", "sedam", "osam", "devet"};
  67.  
  68. for(int i = 0; i < 10; i++) {
  69. if(!strcmp(n, broj[i])) {
  70. return i;
  71. }
  72. }
  73. return 0;
  74. }
  75.  
  76. int calculate_operation(char a[], char b[], char o[]) {
  77. switch(get_operator(o)) {
  78. case '+':
  79. return get_operand(a) + get_operand(b);
  80. case '-':
  81. return get_operand(a) - get_operand(b);
  82. case '*':
  83. return get_operand(a) * get_operand(b);
  84. case '/':
  85. return get_operand(a) / get_operand(b);
  86. case '%':
  87. return get_operand(a) % get_operand(b);
  88. }
  89.  
  90. return -1;
  91. }
  92.  
  93. int main() {
  94. int n, i;
  95. char a[64], b[64], o[64];
  96.  
  97. scanf("%d", &n);
  98.  
  99. // Deklaracija i otvaranje datoteke
  100. FILE *fp;
  101. fp = fopen("input.dat", "r");
  102.  
  103. // Procitati sadrzaj datoteke i ispisati rezultat operacije
  104. for(i = 0; i < n; i++) {
  105. while (fscanf(fp, "%s %s %s\n", a, o, b) > 0) {
  106. clean_string(a, b, o);
  107. printf("%d ", calculate_operation(a, b, o));
  108. }
  109. }
  110.  
  111. // Zatvoriti datoteku
  112. fclose(fp);
  113.  
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement