View difference between Paste ID: UVRYSZiK and VifrP6cF
SHOW: | | - or go back to the newest paste.
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
17+
int isnegativedigit(char* input);  // function for detection negative numbers
18-
void printtop(STACK *ps); //bonus
18+
void printtop(STACK *ps);  // bonus
19
20
21
22
int main()
23-
    char input[1024]; //array for loaded values
23+
24
    char input[1024];  // array for loaded values
25-
    int end = 1; //detects end of input
25+
26-
    int count = 0; //variable for detection if no arguments were enetred
26+
    int end = 1;  // detects end of input
27
    int count = 0;  // variable for detection if no arguments were enetred
28
    STACK ps = {0, {0}};
29
    long long int number, a, b, result, printresult;
30
31
32
    while (end != EOF) {
33
34
        // Read next token
35
        end = scanf("%s", input);
36
        count += 1;
37
        if(end == EOF) {
38
            count -= 1;
39
            break;
40-
        //if it's a positive numebr
40+
41-
        if (ispositivedigit(input) == 1) {
41+
42-
            number = atoll(input); //converts string to long long int
42+
        // If it's a numebr
43-
            push(number, &ps); //add a number to the top of the stack
43+
        if (ispositivedigit(input) || isnegativedigit(input)) {
44
            number = atoll(input);  // converts string to long long int
45
            push(number, &ps);  // add a number to the top of the stack
46-
            //string is a negative number or invalid input
46+
47-
            if (strlen(input) > 1) {
47+
        // It it's an operator
48-
                if (isnegativedigit(input) == 1) {
48+
        else if (isoperator(input)) {
49-
                    number = atoll(input);
49+
            // p
50-
                    push(number, &ps);
50+
            if (strcmp(input, "p") == 0) {
51
                printtop(&ps);
52-
                else {
52+
53-
                    fprintf(stderr, "Error: Invalid input!\n");
53+
            // +
54
            else if (strcmp(input, "+") == 0) {
55
                a=pop(&ps); b=pop(&ps);
56
                result = a + b;
57
                push(result, &ps);
58
            }
59-
            if (strlen(input) == 1) {
59+
            // -
60-
                if (strcmp(input, "p") == 0) {
60+
            else if (strcmp(input, "+") == 0) {
61-
                    printtop(&ps);
61+
                a=pop(&ps); b=pop(&ps);
62
                result = a - b;
63-
                else {
63+
                push(result, &ps);
64
            }
65-
                    a=pop(&ps);
65+
            // *
66-
                    b=pop(&ps);
66+
            else if (strcmp(input, "+") == 0) {
67
                a=pop(&ps); b=pop(&ps);
68-
                    if (strcmp(input, "+") == 0) {
68+
                result = a * b;
69-
                        result = a + b;
69+
                push(result, &ps);
70-
                    }
70+
71
            // /
72-
                    else if (strcmp(input, "-") == 0)
72+
            else if (strcmp(input, "+") == 0) {
73-
                        result = b - a;
73+
                a=pop(&ps); b=pop(&ps);
74
                
75-
                    else if (strcmp(input, "*") == 0)
75+
                if (a == 0) {
76-
                        result = a * b;
76+
                    fprintf(stderr, "Error: Division by zero!\n");
77
                    exit(EXIT_FAILURE);
78-
                    else if (strcmp(input, "/") == 0) {
78+
79-
                        if (a == 0) {
79+
                else result = b / a;
80-
                            fprintf(stderr, "Error: Division by zero!\n");
80+
81-
                            exit(EXIT_FAILURE);
81+
                push(result, &ps);
82-
                        }
82+
83-
                        else result = b / a;
83+
        } 
84-
                    }
84+
        // Detect invalid input
85
        else {
86-
                    else {
86+
            fprintf(stderr, "Error: Invalid input!\n");
87-
                        fprintf(stderr, "Error: Invalid input!\n");
87+
            exit(EXIT_FAILURE);
88-
                        exit(EXIT_FAILURE);
88+
89-
                    }
89+
90
91-
                    push(result, &ps);
91+
    // Returns error when no arguments are entered
92
    if (count == 0) {
93
        fprintf(stderr, "Error: No arguments!\n");
94
        exit(EXIT_FAILURE);
95
    }
96
97-
    //returns error when no arguments are entered
97+
98
    // If the last character isn't "p", the program print the result clasically
99
    if (strcmp(input, "p") != 0) {
100
        printresult = pop(&ps);
101
        printf("%lld\n", printresult);
102
    }
103
104-
    //if the last character isn't "p", the program print the result clasically
104+
105
}
106
107
108
109
void push(long long int x, STACK *ps)
110
{
111
    if (ps->top == STACKSIZE-1){
112
        fprintf(stderr, "Error: Stack overflow!\n");
113
        exit(EXIT_FAILURE);
114
    } else
115
        ps->items[++(ps->top)] = x;
116-
    if(ps->top == STACKSIZE-1){
116+
117
    return;
118
}
119
120
121
122
int pop(STACK *ps)
123
{
124
    if (ps->top == 0) {
125
        fprintf(stderr, "Error: Stack underflow!\n");
126
        exit(EXIT_FAILURE);
127
    }
128-
    if(ps->top == 0) {
128+
129
        return(ps->items[(ps->top)--]);
130
}
131
132
133
134
bool ispositivedigit(char* input)
135
{
136
    // cycle which determines if each character from input is a number or not
137-
int ispositivedigit(char* input)
137+
138
        if (isdigit(input[i]) == 0) {}
139-
    //must be initialized to 0 after each cycle
139+
            return false;
140-
    int alldigits = 0;
140+
141
    }
142-
    //cycle which determines if each character from input is a number or not
142+
    
143
    return true;
144-
        if (isdigit(input[i]) == 0)
144+
145-
            alldigits = 1; //set alldigits to 1 if the character isn't a number
145+
146-
            return 0;
146+
147
148-
    return 1;
148+
bool isnegativedigit(char* input)
149
{
150
    if (strlen(input) > 1 && strncmp(input, "-", 1) == 0) {
151
        for (unsigned int i = 1; i < strlen(input); i++) {
152-
int isnegativedigit(char* input)
152+
153
                return false;  // if a character different from number appears, function returns false
154-
    if (strncmp(input,"-",1) == 0) {
154+
155-
        for (unsigned int i=1; i<strlen(input); i++) {
155+
156
    }
157-
                return 0; //if a character different from number appears, function returns 0
157+
158
        return false;  // if the firts character isn't "-", function returns false
159
160
    return true;  // it is a negative number
161
}
162-
        return 0; //if the firts character isn't "-", function returns 0
162+
163
164-
    return 1; //it is a negative number
164+
165
bool isoperator(char* input)
166
{
167
    if (strlen(input) != 1) return false;  // only one-char operators are allowed
168-
void printtop(STACK *ps) //bonus
168+
    
169
    if (strncmp(input, "+", 1) == 0) return true;
170-
    //not empty stuck
170+
    if (strncmp(input, "-", 1) == 0) return true;
171
    if (strncmp(input, "*", 1) == 0) return true;
172
    if (strncmp(input, "/", 1) == 0) return true;
173-
    //empty stack
173+
    if (strncmp(input, "p", 1) == 0) return true;
174
175
    return false;
176
}
177
178
179
180
void printtop(STACK *ps)  // bonus
181
{
182
    // not empty stuck
183
    if (ps->top != 0)
184
        printf("%lld \n", ps->items[ps->top]);
185
    // empty stack
186
    else {
187
        fprintf(stderr, "Error: Attempt to call for an empty stack!\n");
188
        exit(EXIT_FAILURE);
189
    }
190
}