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) {
- end = scanf("%s", input);
- count += 1;
- if(end == EOF) {
- count -= 1;
- break;
- }
- //if it's a positive numebr
- if (ispositivedigit(input) == 1) {
- number = atoll(input); //converts string to long long int
- push(number, &ps); //add a number to the top of the stack
- }
- else {
- //string is a negative number or invalid input
- if (strlen(input) > 1) {
- if (isnegativedigit(input) == 1) {
- number = atoll(input);
- push(number, &ps);
- }
- else {
- fprintf(stderr, "Error: Invalid input!\n");
- exit(EXIT_FAILURE);
- }
- }
- if (strlen(input) == 1) {
- if (strcmp(input, "p") == 0) {
- printtop(&ps);
- }
- else {
- a=pop(&ps);
- b=pop(&ps);
- if (strcmp(input, "+") == 0) {
- result = a + b;
- }
- else if (strcmp(input, "-") == 0)
- result = b - a;
- else if (strcmp(input, "*") == 0)
- result = a * b;
- else if (strcmp(input, "/") == 0) {
- if (a == 0) {
- fprintf(stderr, "Error: Division by zero!\n");
- exit(EXIT_FAILURE);
- }
- else result = b / a;
- }
- else {
- fprintf(stderr, "Error: Invalid input!\n");
- exit(EXIT_FAILURE);
- }
- push(result, &ps);
- }
- }
- }
- }
- //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)--]);
- }
- int ispositivedigit(char* input)
- {
- //must be initialized to 0 after each cycle
- int alldigits = 0;
- //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)
- alldigits = 1; //set alldigits to 1 if the character isn't a number
- return 0;
- }
- return 1;
- }
- int isnegativedigit(char* input)
- {
- if (strncmp(input,"-",1) == 0) {
- for (unsigned int i=1; i<strlen(input); i++) {
- if (isdigit(input[i]) == 0) {
- return 0; //if a character different from number appears, function returns 0
- }
- }
- }
- else
- return 0; //if the firts character isn't "-", function returns 0
- return 1; //it is a negative number
- }
- 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