Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <math.h>
- #define ARG_NUM argc-1
- #define PI_NUMBER 3.14
- #define E_NUMBER 2.71
- int main(int argc, char *argv[]) {
- int i, j;
- int k = 0;
- double result;
- double num[ARG_NUM];
- for (i = 1; i < argc; i++) {
- for (j = 0; j < strlen(argv[i]); j++) {
- if(!isdigit(argv[i][j])) {
- if (argv[i][j] == '+') {
- result = num[k-2] + num[k-1]; //stack
- num[k-2] = result;
- k = k - 1;
- break;
- }
- if (argv[i][j] == '-') {
- result = num[k-2] - num[k-1];
- num[k-2] = result;
- k = k - 1;
- break;
- }
- if (argv[i][j] == 'x') {
- result = num[k-2] * num[k-1];
- num[k-2] = result;
- k = k - 1;
- break;
- }
- if (argv[i][j] == '/') {
- result = num[k-2] / num[k-1];
- num[k-2] = result;
- k = k - 1;
- break;
- }
- if (argv[i][j] == 's' && argv[i][j+1] == 'i' && argv[i][j+2] == 'n') {
- num[k-1] = sin(num[k-1]);
- break;
- }
- if (argv[i][j] == 'c' && argv[i][j+1] == 'o' && argv[i][j+2] == 's') {
- num[k-1] = cos(num[k-1]);
- break;
- }
- if (argv[i][j] == 'p' && argv[i][j+1] == 'i') {
- num[k-1] = PI_NUMBER;
- break;
- }
- if (argv[i][j] == 'e') {
- num[k-1] = E_NUMBER;
- break;
- }
- }
- }
- if(j == strlen(argv[i])) { //if parameter is a number and his length is equal to j, add it to the array num
- num[k++] = atoi(argv[i]);
- }
- }
- for (i = 0; i < k; i++) { //writing a result
- printf("%f\t", num[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement