Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #define ARG_NUM argc-1
  7. #define PI_NUMBER 3.14
  8. #define E_NUMBER 2.71
  9.  
  10. int main(int argc, char *argv[]) {
  11.     int i, j;
  12.     int k = 0;
  13.     double result;
  14.     double num[ARG_NUM];
  15.  
  16.     for (i = 1; i < argc; i++) {
  17.         for (j = 0; j < strlen(argv[i]); j++) {
  18.             if(!isdigit(argv[i][j])) {
  19.                 if (argv[i][j] == '+') {
  20.                     result = num[k-2] + num[k-1];             //stack
  21.                     num[k-2] = result;
  22.                     k = k - 1;
  23.           break;
  24.                 }
  25.                 if (argv[i][j] == '-') {
  26.                     result = num[k-2] - num[k-1];
  27.                     num[k-2] = result;
  28.                     k = k - 1;
  29.           break;
  30.                 }
  31.                 if (argv[i][j] == 'x') {
  32.                     result = num[k-2] * num[k-1];
  33.                     num[k-2] = result;
  34.                     k = k - 1;
  35.                     break;
  36.                 }
  37.                 if (argv[i][j] == '/') {
  38.                     result = num[k-2] / num[k-1];
  39.                     num[k-2] = result;
  40.                     k = k - 1;
  41.                     break;
  42.                 }
  43.                 if (argv[i][j] == 's' && argv[i][j+1] == 'i' && argv[i][j+2] == 'n') {
  44.                     num[k-1] = sin(num[k-1]);
  45.                     break;
  46.                 }
  47.                 if (argv[i][j] == 'c' && argv[i][j+1] == 'o' && argv[i][j+2] == 's') {
  48.                     num[k-1] = cos(num[k-1]);
  49.                     break;
  50.                 }
  51.                 if (argv[i][j] == 'p' && argv[i][j+1] == 'i') {
  52.                     num[k-1] = PI_NUMBER;
  53.                     break;
  54.                 }
  55.                 if (argv[i][j] == 'e') {
  56.                     num[k-1] = E_NUMBER;
  57.                     break;
  58.                 }
  59.             }
  60.         }
  61.         if(j == strlen(argv[i])) {      //if parameter is a number and his length is equal to j, add it to the array num
  62.       num[k++] = atoi(argv[i]);
  63.         }
  64.     }
  65.  
  66.     for (i = 0; i < k; i++) {         //writing a result
  67.         printf("%f\t", num[i]);
  68.     }
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement