View difference between Paste ID: JFzdin6h and A6upR5xW
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
18
void printtop(STACK *ps);  // bonus
19
20
21
22
int main()
23
{
24
    char input[1024];  // array for loaded values
25
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
        }
41
42
        // If it's a numebr
43-
        if (ispositivedigit(input) || isnegativedigit(input)) {
43+
        if (ispositivedigit(input) == 1 || isnegativedigit(input) == 1) {
44
            number = atoll(input);  // converts string to long long int
45
            push(number, &ps);  // add a number to the top of the stack
46
        }
47
        // It it's an operator
48-
        else if (isoperator(input)) {
48+
        else if (isoperator(input) == 1) {
49
            // p
50
            if (strcmp(input, "p") == 0) {
51
                printtop(&ps);
52
            }
53
            // +
54
            else if (strcmp(input, "+") == 0) {
55
                a=pop(&ps); b=pop(&ps);
56
                result = a + b;
57
                push(result, &ps);
58
            }
59
            // -
60
            else if (strcmp(input, "-") == 0) {
61
                a=pop(&ps); b=pop(&ps);
62
                result = a - b;
63
                push(result, &ps);
64
            }
65
            // *
66
            else if (strcmp(input, "*") == 0) {
67
                a=pop(&ps); b=pop(&ps);
68
                result = a * b;
69
                push(result, &ps);
70
            }
71
            // /
72
            else if (strcmp(input, "/") == 0) {
73
                a=pop(&ps); b=pop(&ps);
74
                
75
                if (a == 0) {
76
                    fprintf(stderr, "Error: Division by zero!\n");
77
                    exit(EXIT_FAILURE);
78
                }
79
                else result = b / a;
80
81
                push(result, &ps);
82
            }
83
        } 
84
        // Detect invalid input
85
        else {
86
            fprintf(stderr, "Error: Invalid input!\n");
87
            exit(EXIT_FAILURE);
88
        }
89
    }
90
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
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
    return 0;
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
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
    else
129
        return(ps->items[(ps->top)--]);
130
}
131
132
133
134-
bool ispositivedigit(char* input)
134+
int ispositivedigit(char* input)
135
{
136
    // cycle which determines if each character from input is a number or not
137
    for (unsigned int i=0; i<strlen(input); i++) {
138
        if (isdigit(input[i]) == 0) {}
139-
            return false;
139+
            return 0;
140
        }
141
    }
142
    
143-
    return true;
143+
    return 1;
144
}
145
146
147
148-
bool isnegativedigit(char* input)
148+
int isnegativedigit(char* input)
149
{
150
    if (strlen(input) > 1 && strncmp(input, "-", 1) == 0) {
151
        for (unsigned int i = 1; i < strlen(input); i++) {
152
            if (isdigit(input[i]) == 0) {
153-
                return false;  // if a character different from number appears, function returns false
153+
                return 0;  // if a character different from number appears, function returns 0
154
            }
155
        }
156
    }
157
    else
158-
        return false;  // if the firts character isn't "-", function returns false
158+
        return 0;  // if the firts character isn't "-", function returns 0
159
160-
    return true;  // it is a negative number
160+
    return 1;  // it is a negative number
161
}
162
163
164
165-
bool isoperator(char* input)
165+
int isoperator(char* input)
166
{
167-
    if (strlen(input) != 1) return false;  // only one-char operators are allowed
167+
    if (strlen(input) != 1) return 0;  // only one-char operators are allowed
168
    
169-
    if (strncmp(input, "+", 1) == 0) return true;
169+
    if (strncmp(input, "+", 1) == 0) return 1;
170-
    if (strncmp(input, "-", 1) == 0) return true;
170+
    if (strncmp(input, "-", 1) == 0) return 1;
171-
    if (strncmp(input, "*", 1) == 0) return true;
171+
    if (strncmp(input, "*", 1) == 0) return 1;
172-
    if (strncmp(input, "/", 1) == 0) return true;
172+
    if (strncmp(input, "/", 1) == 0) return 1;
173-
    if (strncmp(input, "p", 1) == 0) return true;
173+
    if (strncmp(input, "p", 1) == 0) return 1;
174
175-
    return false;
175+
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
}