Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include "nekiheader.h"
  4.  
  5. int main(){
  6. char ulaz[MAXLINE+1];
  7. char i,j,k,c;
  8. char operatori[MAXLINE / 2];
  9. float operandi[MAXLINE / 2];
  10. int brChar, brOperatora;
  11. int operandPos, operatorPos, rb;
  12. char errorF;
  13.  
  14. while(1){
  15. operandPos=0;
  16. operatorPos=0;
  17. errorF=0;
  18.  
  19. brChar = newreadline(ulaz,MAXLINE);
  20. printf("%s",ulaz);
  21.  
  22.  
  23. if(brChar){ // broj karaktera mora biti > 0
  24. if(ulaz[0] <'0' || ulaz[0] > '9'){
  25.  
  26. continue;
  27. }
  28. for(i=0; i<brChar; i++){
  29. if((ulaz[i] < '0' || ulaz[i] > '9')
  30. && ulaz[i] != '+' && ulaz[i] != '-' && ulaz[i] != '*' && ulaz[i] != '/'){
  31.  
  32. continue;
  33. }
  34. }
  35. brOperatora=0;
  36. for(i=0; i<brChar; i++){
  37. if(ulaz[i] == '+' || ulaz[i] == '-' || ulaz[i] == '*' || ulaz[i] == '/') brOperatora++;
  38. }
  39. if(brOperatora==0){
  40.  
  41. continue;
  42. }
  43.  
  44. if(ulaz[brChar-1] =='+' || ulaz[brChar-1] == '-' || ulaz[brChar-1] == '*' || ulaz[brChar-1] == '/'){
  45.  
  46. continue;
  47. }
  48.  
  49. for(i=0; i<brChar-1; i++){
  50. if((ulaz[i] == '+' || ulaz[i] == '-' || ulaz[i] == '*' || ulaz[i] == '/') &&
  51. (ulaz[i+1] == '+' || ulaz[i+1] == '-' || ulaz[i+1] == '*' || ulaz[i+1] == '/')){
  52.  
  53. break;
  54. }
  55. }
  56. if(i<brChar-1) continue;
  57.  
  58. }
  59. else continue;
  60.  
  61. i=0;
  62. rb=brOperatora;
  63. while(rb){
  64. sscanf(ulaz, "%f", &operandi[operandPos]);
  65. operandPos++;
  66.  
  67. while(i<brChar){
  68. if(ulaz[i] == '+' || ulaz[i] == '-' || ulaz[i] == '*' || ulaz[i] == '/' ){
  69. operatori[operatorPos] = ulaz[i];
  70. operatorPos++;
  71.  
  72. k=0;
  73. for(j=i+1; j<brChar; j++,k++){
  74. ulaz[k] = ulaz[j];
  75. }
  76. ulaz[k] = 0;
  77. brChar -= i+1;
  78. i=0;
  79. break;
  80. }
  81. i++;
  82. }
  83. rb--;
  84. }
  85. sscanf(ulaz, "%f", &operandi[operandPos]);
  86.  
  87.  
  88. while(brOperatora){
  89. for(i=0; i< brOperatora; i++){
  90. if(operatori[i] == '*' || operatori[i] == '/') break;
  91. }
  92. if(i<brOperatora){
  93. if(operatori[i] == '*')
  94. operandi[i] *= operandi[i+1];
  95. else
  96. if(operandi[i+1]==0){
  97. printf("Error - dijeljenje s 0!\n");
  98. errorF = 1;
  99. break;
  100. }
  101. else operandi[i] /= operandi[i+1];
  102.  
  103. for(j=i+1; j<brOperatora+1; j++){
  104. operandi[j] = operandi[j+1];
  105. }
  106.  
  107. for(j=i; j<brOperatora-1; j++){
  108. operatori[j] = operatori[j+1];
  109. }
  110. brOperatora--;
  111. }
  112. else{
  113. for(i=0; i< brOperatora; i++){
  114. if(operatori[i] == '+' || operatori[i] == '-') break;
  115. }
  116. if(i<brOperatora){
  117. if(operatori[i] == '+')
  118. operandi[i] += operandi[i+1];
  119. else
  120. operandi[i] -= operandi[i+1];
  121.  
  122. for(j=i+1; j<brOperatora+1; j++){
  123. operandi[j] = operandi[j+1];
  124. }
  125.  
  126. for(j=i; j<brOperatora-1; j++){
  127. operatori[j] = operatori[j+1];
  128. }
  129. brOperatora--;
  130. }
  131. }
  132. }
  133. if(!errorF)
  134. printf("=%f\n",operandi[0]);
  135. }
  136. return 0;
  137. }
  138.  
  139.  
  140. char newreadline(char *buf, int max){
  141. int c;
  142. char *start, *end;
  143.  
  144. start = buf;
  145. end = buf + max;
  146. while((c = getchar()) != '\n')
  147. if(buf < end) *buf++ = c;
  148.  
  149. *buf = 0;
  150. return buf - start;
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158. nekiheder.h
  159.  
  160. #ifndef KALK_H
  161. #define KALK_H
  162.  
  163. #define MAXLINE 200
  164.  
  165. char newreadline(char *buf, int max);
  166.  
  167. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement