Advertisement
dmilicev

simple calculator v2.c

Sep 26th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. // Simple calculator v2
  2.  
  3. #include<stdio.h>
  4.  
  5. int main()
  6. {
  7.     char c, str[20], snum1[20],snum2[20];
  8.     int i,snum2length=0,num1,num2;
  9.  
  10.     printf("\n CALCULATOR (for positive integers only)\n\n");
  11.     printf("\n Enter first number, operator (+,-,*,/) and second number: ");
  12.     scanf("%s",str);
  13.  
  14.     i=0;
  15.     while(isdigit(str[i]) && str[i]!='\0'){
  16.         snum1[i]=str[i];
  17.         i++;
  18.     }
  19.     snum1[i]='\0';
  20.  
  21.     num1=atoi(snum1);
  22.  
  23.     c=str[i];
  24.  
  25.     i++;
  26.     while(isdigit(str[i]) && str[i]!='\0'){
  27.         snum2[snum2length]=str[i];
  28.         i++;
  29.         snum2length++;
  30.     }
  31.  
  32.     snum2[snum2length]='\0';
  33.  
  34.     num2=atoi(snum2);
  35.  
  36.  
  37.     printf("\n %d %c %d = ",num1,c,num2);
  38.  
  39.     switch(c)
  40.     {
  41.         case '+':
  42.             printf("%d \n",num1+num2);
  43.             break;
  44.  
  45.         case '-':
  46.             printf("%d \n",num1-num2);
  47.             break;
  48.  
  49.         case '*':
  50.             printf("%d \n",num1*num2);
  51.             break;
  52.  
  53.         case '/':
  54.             printf("%f \n",(float)num1/num2);
  55.             break;
  56.         default:
  57.             printf("Error! Invalid Operator.");
  58.     }
  59.  
  60.     printf("\n\n");
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement