Guest User

Untitled

a guest
Mar 25th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define STACKSIZE 250
  7.  
  8. typedef struct{
  9.         int top;
  10.         long long int items[STACKSIZE];
  11.         }STACK;
  12.  
  13.  
  14. void push(long long int x, STACK *ps);
  15. int pop(STACK *ps);
  16. int ispositivedigit(char* input);
  17. int isnegativedigit(char* input); //function for detection negative numbers
  18. void printtop(STACK *ps); //bonus
  19.  
  20.  
  21. int main()
  22. {
  23.     char input[1024]; //array for loaded values
  24.  
  25.     int end = 1; //detects end of input
  26.     int count = 0; //variable for detection if no arguments were enetred
  27.     STACK ps = {0, {0}};
  28.     long long int number, a, b, result, printresult;
  29.  
  30.  
  31.     while (end != EOF) {
  32.  
  33.         end = scanf("%s", input);
  34.         count += 1;
  35.         if(end == EOF) {
  36.             count -= 1;
  37.             break;
  38.         }
  39.  
  40.         //if it's a positive numebr
  41.         if (ispositivedigit(input) == 1) {
  42.             number = atoll(input); //converts string to long long int
  43.             push(number, &ps); //add a number to the top of the stack
  44.         }
  45.         else {
  46.             //string is a negative number or invalid input
  47.             if (strlen(input) > 1) {
  48.                 if (isnegativedigit(input) == 1) {
  49.                     number = atoll(input);
  50.                     push(number, &ps);
  51.                 }
  52.                 else {
  53.                     fprintf(stderr, "Error: Invalid input!\n");
  54.                     exit(EXIT_FAILURE);
  55.                 }
  56.             }
  57.  
  58.  
  59.             if (strlen(input) == 1) {
  60.                 if (strcmp(input, "p") == 0) {
  61.                     printtop(&ps);
  62.                 }
  63.                 else {
  64.  
  65.                     a=pop(&ps);
  66.                     b=pop(&ps);
  67.  
  68.                     if (strcmp(input, "+") == 0) {
  69.                         result = a + b;
  70.                     }
  71.  
  72.                     else if (strcmp(input, "-") == 0)
  73.                         result = b - a;
  74.  
  75.                     else if (strcmp(input, "*") == 0)
  76.                         result = a * b;
  77.  
  78.                     else if (strcmp(input, "/") == 0) {
  79.                         if (a == 0) {
  80.                             fprintf(stderr, "Error: Division by zero!\n");
  81.                             exit(EXIT_FAILURE);
  82.                         }
  83.                         else result = b / a;
  84.                     }
  85.  
  86.                     else {
  87.                         fprintf(stderr, "Error: Invalid input!\n");
  88.                         exit(EXIT_FAILURE);
  89.                     }
  90.  
  91.                     push(result, &ps);
  92.                 }
  93.             }
  94.         }
  95.     }
  96.  
  97.     //returns error when no arguments are entered
  98.     if (count == 0) {
  99.         fprintf(stderr, "Error: No arguments!\n");
  100.         exit(EXIT_FAILURE);
  101.     }
  102.  
  103.  
  104.     //if the last character isn't "p", the program print the result clasically
  105.     if (strcmp(input, "p") != 0) {
  106.         printresult = pop(&ps);
  107.         printf("%lld\n", printresult);
  108.     }
  109.  
  110.     return 0;
  111. }
  112.  
  113.  
  114. void push(long long int x, STACK *ps)
  115. {
  116.     if(ps->top == STACKSIZE-1){
  117.         fprintf(stderr, "Error: Stack overflow!\n");
  118.         exit(EXIT_FAILURE);
  119.     } else
  120.         ps->items[++(ps->top)] = x;
  121.  
  122.     return;
  123. }
  124.  
  125.  
  126. int pop(STACK *ps)
  127. {
  128.     if(ps->top == 0) {
  129.         fprintf(stderr, "Error: Stack underflow!\n");
  130.         exit(EXIT_FAILURE);
  131.     }
  132.     else
  133.         return(ps->items[(ps->top)--]);
  134. }
  135.  
  136.  
  137. int ispositivedigit(char* input)
  138. {
  139.     //must be initialized to 0 after each cycle
  140.     int alldigits = 0;
  141.  
  142.     //cycle which determines if each character from input is a number or not
  143.     for (unsigned int i=0; i<strlen(input); i++) {
  144.         if (isdigit(input[i]) == 0)
  145.             alldigits = 1; //set alldigits to 1 if the character isn't a number
  146.             return 0;
  147.         }
  148.     return 1;
  149. }
  150.  
  151.  
  152. int isnegativedigit(char* input)
  153. {
  154.     if (strncmp(input,"-",1) == 0) {
  155.         for (unsigned int i=1; i<strlen(input); i++) {
  156.             if (isdigit(input[i]) == 0) {
  157.                 return 0; //if a character different from number appears, function returns 0
  158.             }
  159.         }
  160.     }
  161.     else
  162.         return 0; //if the firts character isn't "-", function returns 0
  163.  
  164.     return 1; //it is a negative number
  165. }
  166.  
  167.  
  168. void printtop(STACK *ps) //bonus
  169. {
  170.     //not empty stuck
  171.     if (ps->top != 0)
  172.         printf("%lld \n", ps->items[ps->top]);
  173.     //empty stack
  174.     else {
  175.         fprintf(stderr, "Error: Attempt to call for an empty stack!\n");
  176.         exit(EXIT_FAILURE);
  177.  
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment