Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define STACKSIZE 250
- typedef struct{
- int top;
- long long int items[STACKSIZE];
- }STACK;
- void push(long long int x, STACK *ps);
- int pop(STACK *ps);
- int ispositivedigit(char* input);
- int isnegativedigit(char* input); // function for detection negative numbers
- void printtop(STACK *ps); // bonus
- int main()
- {
- char input[1024]; // array for loaded values
- int end = 1; // detects end of input
- int count = 0; // variable for detection if no arguments were enetred
- STACK ps = {0, {0}};
- long long int number, a, b, result, printresult;
- while (end != EOF) {
- // Read next token
- end = scanf("%s", input);
- count += 1;
- if(end == EOF) {
- count -= 1;
- break;
- }
- // If it's a numebr
- if (ispositivedigit(input) || isnegativedigit(input)) {
- number = atoll(input); // converts string to long long int
- push(number, &ps); // add a number to the top of the stack
- }
- // It it's an operator
- else if (isoperator(input)) {
- // p
- if (strcmp(input, "p") == 0) {
- printtop(&ps);
- }
- // +
- else if (strcmp(input, "+") == 0) {
- a=pop(&ps); b=pop(&ps);
- result = a + b;
- push(result, &ps);
- }
- // -
- else if (strcmp(input, "-") == 0) {
- a=pop(&ps); b=pop(&ps);
- result = a - b;
- push(result, &ps);
- }
- // *
- else if (strcmp(input, "*") == 0) {
- a=pop(&ps); b=pop(&ps);
- result = a * b;
- push(result, &ps);
- }
- // /
- else if (strcmp(input, "/") == 0) {
- a=pop(&ps); b=pop(&ps);
- if (a == 0) {
- fprintf(stderr, "Error: Division by zero!\n");
- exit(EXIT_FAILURE);
- }
- else result = b / a;
- push(result, &ps);
- }
- }
- // Detect invalid input
- else {
- fprintf(stderr, "Error: Invalid input!\n");
- exit(EXIT_FAILURE);
- }
- }
- // Returns error when no arguments are entered
- if (count == 0) {
- fprintf(stderr, "Error: No arguments!\n");
- exit(EXIT_FAILURE);
- }
- // If the last character isn't "p", the program print the result clasically
- if (strcmp(input, "p") != 0) {
- printresult = pop(&ps);
- printf("%lld\n", printresult);
- }
- return 0;
- }
- void push(long long int x, STACK *ps)
- {
- if (ps->top == STACKSIZE-1){
- fprintf(stderr, "Error: Stack overflow!\n");
- exit(EXIT_FAILURE);
- } else
- ps->items[++(ps->top)] = x;
- return;
- }
- int pop(STACK *ps)
- {
- if (ps->top == 0) {
- fprintf(stderr, "Error: Stack underflow!\n");
- exit(EXIT_FAILURE);
- }
- else
- return(ps->items[(ps->top)--]);
- }
- bool ispositivedigit(char* input)
- {
- // cycle which determines if each character from input is a number or not
- for (unsigned int i=0; i<strlen(input); i++) {
- if (isdigit(input[i]) == 0) {}
- return false;
- }
- }
- return true;
- }
- bool isnegativedigit(char* input)
- {
- if (strlen(input) > 1 && strncmp(input, "-", 1) == 0) {
- for (unsigned int i = 1; i < strlen(input); i++) {
- if (isdigit(input[i]) == 0) {
- return false; // if a character different from number appears, function returns false
- }
- }
- }
- else
- return false; // if the firts character isn't "-", function returns false
- return true; // it is a negative number
- }
- bool isoperator(char* input)
- {
- if (strlen(input) != 1) return false; // only one-char operators are allowed
- if (strncmp(input, "+", 1) == 0) return true;
- if (strncmp(input, "-", 1) == 0) return true;
- if (strncmp(input, "*", 1) == 0) return true;
- if (strncmp(input, "/", 1) == 0) return true;
- if (strncmp(input, "p", 1) == 0) return true;
- return false;
- }
- void printtop(STACK *ps) // bonus
- {
- // not empty stuck
- if (ps->top != 0)
- printf("%lld \n", ps->items[ps->top]);
- // empty stack
- else {
- fprintf(stderr, "Error: Attempt to call for an empty stack!\n");
- exit(EXIT_FAILURE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment