Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. int CalcAll()
  5. {
  6. int number1,number2;
  7. char op,ch;
  8. char str[100];
  9.  
  10. FILE *f;
  11. f=fopen("Exp.txt","w");
  12. if(f==NULL)
  13. {
  14. printf("Failed open");
  15. return 0;
  16. }
  17. fputs("12+4=",f);
  18. fputs("15+7",f);
  19. fclose(f);
  20.  
  21. f=fopen("Exp.txt","r");
  22. if(f==NULL)
  23. {
  24. printf("Falied open");
  25. return 0;
  26.  
  27. }
  28. fgets(str,100,f);
  29.  
  30. while(!feof(f))
  31. {
  32. printf("%f",CalcExp(str));
  33. fgets(str,100,f);
  34. }
  35.  
  36. fclose(f);
  37.  
  38.  
  39. }
  40.  
  41.  
  42.  
  43.  
  44. float CalcExp(char* str)
  45. {
  46. int counter=0,i,Sum=0,Sum2=0;
  47. char P;
  48. float answer;
  49.  
  50. for(i=0;str[i]!='=';i++)
  51. {
  52. if('0'<=str[i]&&str[i]<='9')
  53. {
  54. if(counter==2)
  55. {
  56. Sum=0;
  57. counter=3;
  58. }
  59.  
  60. Sum=Sum*10;
  61. Sum=Sum+str[i]-'0';
  62.  
  63. }
  64. if(str[i]==' ')
  65. {
  66. Sum2=Sum;
  67. counter++;
  68. }
  69. if(str[i]=='*'||str[i]=='+'||str[i]=='-'||str[i]=='/')
  70. P=str[i];
  71.  
  72. }
  73.  
  74. switch(P)
  75. {
  76. case '+':
  77. answer=Sum2+Sum;
  78. break;
  79.  
  80. case '-':
  81. answer=Sum2-Sum;
  82. break;
  83.  
  84. case '*':
  85. answer=Sum2*Sum;
  86. break;
  87.  
  88. case '/':
  89. answer=(float)Sum2/Sum;
  90. break;
  91. }
  92.  
  93. return answer;
  94. }
  95.  
  96. void main()
  97. {
  98. int answer;
  99.  
  100. answer=CalcAll();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement