Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <string.h>
  4.  
  5. #include <stdlib.h>
  6.  
  7. double getstr(int bgn, int nd, char *str){
  8.  
  9. char nstr[128]="";
  10.  
  11. for(int i=bgn; i<nd; i++){
  12.  
  13. nstr[i-bgn]=str[i];
  14.  
  15. }
  16.  
  17. return atof(nstr);
  18.  
  19. }
  20.  
  21. double act(double CurrentC, float NewC, char sym[1]){
  22.  
  23. if(sym[0]=='-') return CurrentC-NewC;
  24.  
  25. if(sym[0]=='+') return CurrentC+NewC;
  26.  
  27. if(sym[0]=='/') return CurrentC/NewC;
  28.  
  29. if(sym[0]=='*') return CurrentC*NewC;
  30.  
  31. return 0;
  32.  
  33. }
  34.  
  35. int main(){
  36.  
  37. char m[256];
  38.  
  39. char CurrentType[1]="+";
  40.  
  41. int CurrentTypeValue=0;
  42.  
  43. double LastValue=0.0;
  44.  
  45. scanf("%s",m);
  46.  
  47. for(int i=0; i<strlen(m); i++){
  48.  
  49. if(m[i]=='-' || m[i]=='+' || m[i]=='/' || m[i]=='*' || m[i+1]=='\0'){
  50.  
  51. if(i==0){
  52.  
  53. CurrentType[0]=m[i];
  54.  
  55. CurrentTypeValue++;
  56.  
  57. }
  58.  
  59. else if(m[i+1]=='\0'){
  60.  
  61. printf("%.2f\n", act(LastValue, getstr(CurrentTypeValue, i+1, m), CurrentType));
  62.  
  63. }
  64.  
  65. else{
  66.  
  67. LastValue=act(LastValue, getstr(CurrentTypeValue, i+1, m), CurrentType);
  68.  
  69. CurrentType[0]=m[i];
  70.  
  71. CurrentTypeValue=i+1;
  72.  
  73. }
  74.  
  75. }
  76.  
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement