Advertisement
dmilicev

simple calculator v3.c

Sep 27th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 KB | None | 0 0
  1. // Simple calculator v3, works with real numbers,
  2. // with how it works prints and comments
  3.  
  4. #include<stdio.h>
  5. #include <stdlib.h> // for atof() function
  6.  
  7. int main()
  8. {
  9. // c is operator, str is entered string, snum1 and snum2 are entered strings of numbers num1 and num2
  10.     char c, str[20], snum1[20], snum2[20];
  11. // i is counter for str, i1 is counter for snum1, i2 is counter for snum2
  12.     int i=0, i1=0, i2=0, sign_num1=1, sign_num2=1;
  13. // num1 and num2 are entered real numbers
  14.     float num1,num2;
  15.  
  16.     printf("\n CALCULATOR (for real numbers) \n");
  17.     printf("\n Enter first number and operator (+,-,*,/) and second number: \n");
  18.     printf("\n Example: -123+-456 \n\n");
  19.     scanf("%s",str);    // no need for &str because str is address of str[0]
  20.  
  21.     printf("\n  1. Entered string: %s \n", str);
  22.  
  23. // print str[i]
  24.     while(str[i]!='\0'){    // from beginning to end of str
  25.         printf("\n  2. str[%d] = %c \n",i,str[i]);
  26.         i++;    // next character of str
  27.     }
  28.  
  29.     i=0;    // reset counter
  30.  
  31. /*
  32.    read sign_num1 (default is +1) of first number num1
  33.    if first character of str:
  34.    -, sign_num1 is -1
  35.    +, sign_num1 is +1 (default is +1)
  36. */
  37.  
  38.     if(str[0]=='-'){
  39.         sign_num1 = -1;
  40.         i++;    // next character of str, this is first character of snum1
  41.     }
  42.     else if(str[0]=='+'){
  43.         //(sign_num1 default is +1)
  44.         i++;    // next character of str, this is first character of snum1
  45.     }
  46.  
  47.     printf("\n  3. i = %d \t sign_num1 = %d \n",i,sign_num1);
  48.  
  49. // read snum1[i] from str[i] until str[i] is operator (not digit)
  50. // while str[i] is digit and it is not the end of str
  51.     while(isdigit(str[i]) && str[i]!='\0'){ // from beginning to end of str
  52.         snum1[i1]=str[i];   // copy str[i] to snum1[i1]
  53.         printf("\n  4. str[%d] = %c \t snum1[%d] = %c \n",i,str[i],i1,snum1[i1]);
  54.         i++;    // next character of str
  55.         i1++;   // next character of snum1
  56.     }
  57.     snum1[i1]='\0'; // finish snum1
  58.     printf("\n  5. snum1 = %s \n", snum1);
  59.  
  60.     num1=sign_num1*atof(snum1); // convert string snum1 to float num1
  61.     printf("\n  6. num1 = %f \n", num1);
  62.  
  63.     c=str[i];   // read operator c from str[i]
  64.     printf("\n  7. i = %d \t operator c = %c \n",i,c);
  65.  
  66.     i++;    // next character of str, this is first character of snum2
  67.  
  68.     printf("\n  8. i = %d \t str[%d] = %c\n",i,i,str[i]);
  69.  
  70. /*
  71.    read sign_num2 (default is +1) of second number num2
  72.    if str[i]:
  73.    -, sign_num2 is -1
  74.    +, sign_num2 is +1 (default is +1)
  75. */
  76.  
  77.     if(str[i]=='-'){
  78.         sign_num2 = -1;
  79.         i++;    // next character of str, this is first character of snum2
  80.     }
  81.     else if(str[i]=='+'){
  82.         //(sign_num2 default is +1)
  83.         i++;    // next character of str, this is first character of snum2
  84.     }
  85.  
  86.     printf("\n  9. i = %d \t sign_num2 = %d \n",i,sign_num2);
  87.  
  88. // read snum2[i] from str[i] until the end of str
  89. // while str[i] is digit and it is not the end of str
  90.     while(isdigit(str[i]) && str[i]!='\0'){ // from first character of snum2 in str, to end of str
  91.         snum2[i2]=str[i];   // copy str[i] to snum2[i2]
  92.         printf("\n 10. str[%d] = %c \t snum2[%d] = %c \n",i,str[i],i2,snum2[i2]);
  93.         i++;    // next character of str
  94.         i2++;   // next character of snum2
  95.     }
  96.  
  97.     printf("\n 11. i = %d \t i2 = %d \n",i,i2);
  98.  
  99.     snum2[i2]='\0'; // finish snum2
  100.  
  101.     i2=0;   // reset counter
  102.     while(snum2[i2]!='\0'){
  103.         printf("\n 12. snum2[%d] = %c \n",i2,snum2[i2]);
  104.         i2++;   // next character of snum2
  105.     }
  106.  
  107.     printf("\n 13. snum2 = %s \n", snum2);
  108.  
  109.     num2=sign_num2*atof(snum2); // convert string snum2 to float num2
  110.  
  111.     printf("\n 14. num2 = %f \n", num2);
  112.  
  113.  
  114.     printf("\n (%f) %c (%f) = ",num1,c,num2);
  115.  
  116.     switch(c)   // analyze operator c
  117.     {
  118.         case '+':
  119.             printf("(%f) \n",num1+num2);
  120.             break;
  121.  
  122.         case '-':
  123.             printf("(%f) \n",num1-num2);
  124.             break;
  125.  
  126.         case '*':
  127.             printf("(%f) \n",num1*num2);
  128.             break;
  129.  
  130.         case '/':
  131.             printf("(%f) \n",num1/num2);
  132.             break;
  133.         default:
  134.             printf("Error! Invalid Operator.");
  135.     }
  136.  
  137.     printf("\n\n");
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement