dmilicev

simple calculator v1.c

Sep 26th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. // Simple calculator v1 with how it works prints and comments
  2.  
  3. #include<stdio.h>
  4.  
  5. int main()
  6. {
  7. // c is operator, snum1 and snum2 are entered strings of numbers num1 and num2
  8.     char c, str[20], snum1[20],snum2[20];
  9. // i is counter, snum2length is length of snum2, num1 and num2 are entered numbers
  10.     int i,snum2length=0,num1,num2;
  11.  
  12.     printf("\n  1. CALCULATOR \n\n");
  13.     printf("\n  2. Enter first number, operator (+,-,*,/) and second number \n\n");
  14.     scanf("%s",str);    // no need for &str because str is address of str[0]
  15.  
  16.     printf("\n  3. Entered string: %s \n", str);
  17.  
  18. // print str[i]
  19.     while(str[i]!='\0'){    // from beginning to end of str
  20.         printf("\n  4. str[%d] = %c \n",i,str[i]);
  21.         i++;    // next character of str
  22.     }
  23.  
  24.     i=0;    // reset counter
  25.  
  26. // while str[i] is digit and it is not the end of str
  27. // read snum1[i] from str[i] until str[i] is operator (not digit)
  28.     while(isdigit(str[i]) && str[i]!='\0'){ // from beginning to end of str
  29.         snum1[i]=str[i];    // copy str[i] to snum1[i]
  30.         printf("\n  5. str[%d] = %c \t snum1[%d] = %c \n",i,str[i],i,snum1[i]);
  31.         i++;    // next character of str and snum1
  32.     }
  33.     snum1[i]='\0';  // finish snum1
  34.  
  35.     num1=atoi(snum1);   // convert string snum1 to integer num1
  36.  
  37.     c=str[i];   // read operator c from str[i]
  38.  
  39.     printf("\n  6. snum1 = %s \n", snum1);
  40.  
  41.     printf("\n  7. num1 = %d \n", num1);
  42.  
  43.     printf("\n  8. i = %d \t c = %c \n",i,c);
  44.  
  45.     i++;    // next character of str, this is first character of snum2
  46.  
  47.     printf("\n  9. i = %d \n",i);
  48.  
  49. // while str[i] is digit and it is not the end of str
  50. // read snum2[i] from str[i] until the end of str
  51.     while(isdigit(str[i]) && str[i]!='\0'){ // from first character of snum2 in str, to end of str
  52.         snum2[snum2length]=str[i];  // copy str[i] to snum2[snum2length]
  53.         printf("\n 10. str[%d] = %c \t snum2[%d] = %c \n",i,str[i],snum2length,snum2[snum2length]);
  54.         i++;    // next character of str
  55.         snum2length++;  // next character of snum2, increase the lenghth of string snum2
  56.     }
  57.  
  58.     printf("\n 11. i = %d \t snum2length = %d \n",i,snum2length);
  59.  
  60.     snum2[snum2length]='\0';    // finish snum2
  61.  
  62.     i=0;    // reset counter
  63.     while(snum2[i]!='\0'){
  64.         printf("\n 12. snum2[%d] = %c \n",i,snum2[i]);
  65.         i++;    // next character of snum2
  66.     }
  67.  
  68.     printf("\n 13. snum2 = %s \n", snum2);
  69.  
  70.     num2=atoi(snum2);   // convert string snum2 to integer num2
  71.  
  72.     printf("\n 14. num2 = %d \n", num2);
  73.  
  74.  
  75.     printf("\n %d %c %d = ",num1,c,num2);
  76.  
  77.     switch(c)   // analyze operator c
  78.     {
  79.         case '+':
  80.             printf("%d \n",num1+num2);
  81.             break;
  82.  
  83.         case '-':
  84.             printf("%d \n",num1-num2);
  85.             break;
  86.  
  87.         case '*':
  88.             printf("%d \n",num1*num2);
  89.             break;
  90.  
  91.         case '/':
  92.             printf("%f \n",(float)num1/num2);
  93.             break;
  94.         default:
  95.             printf("Error! Invalid Operator.");
  96.     }
  97.  
  98.     printf("\n\n");
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment