Merge branch 'main' of git.kemt.fei.tuke.sk:ak643du/usaa24

This commit is contained in:
Anton 2024-10-13 11:26:46 +02:00
commit 0039fa7500

View File

@ -2,54 +2,59 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <ctype.h>
#define LINE_SIZE 100
#define STACK_SIZE 10 #define STACK_SIZE 10
// Stack structure
struct stack { struct stack {
float values[STACK_SIZE]; float values[STACK_SIZE];
int size; int size;
}; };
void print_stack(struct stack* stack) { // Function to print the stack
for (int i = 0; i < stack->size; i++) { void print_stack(struct stack* s) {
printf("%.2f ", stack->values[i]); for (int i = 0; i < s->size; i++) {
printf("%.2f", s->values[i]);
if (i < s->size - 1) {
printf(" "); // Print space between values
}
} }
printf("\n"); printf("\n");
} }
void push_stack(struct stack* stack, float value) { // Function to push a value onto the stack
if (stack->size >= STACK_SIZE) { void push_stack(struct stack* s, float value) {
if (s->size >= STACK_SIZE) {
printf("Chyba: zasobnik je plny\n"); printf("Chyba: zasobnik je plny\n");
exit(1); exit(1);
} }
stack->values[stack->size] = value; s->values[s->size++] = value;
stack->size++;
} }
float pop_stack(struct stack* stack) { // Function to pop a value from the stack
if (stack->size == 0) { float pop_stack(struct stack* s) {
if (s->size == 0) {
printf("Chyba: zasobnik je prazdny\n"); printf("Chyba: zasobnik je prazdny\n");
exit(1); exit(1);
} }
stack->size--; return s->values[--s->size];
return stack->values[stack->size];
} }
// Check if input is an operator
int is_operator(char* input) { int is_operator(char* input) {
return (strcmp(input, "+") == 0 || strcmp(input, "-") == 0 || return (strcmp(input, "+") == 0 || strcmp(input, "-") == 0 ||
strcmp(input, "*") == 0 || strcmp(input, "/") == 0); strcmp(input, "*") == 0 || strcmp(input, "/") == 0);
} }
void perform_operation(struct stack* stack, char* operator) { // Perform the operation on the top of the stack
if (stack->size < 2) { void perform_operation(struct stack* s, char* operator) {
if (s->size < 2) {
printf("Chyba: nedostatok operandov\n"); printf("Chyba: nedostatok operandov\n");
exit(1); exit(1);
} }
float b = pop_stack(stack); float b = pop_stack(s);
float a = pop_stack(stack); float a = pop_stack(s);
float result; float result;
if (strcmp(operator, "+") == 0) { if (strcmp(operator, "+") == 0) {
@ -69,20 +74,20 @@ void perform_operation(struct stack* stack, char* operator) {
exit(1); exit(1);
} }
push_stack(stack, result); push_stack(s, result);
} }
int main() { int main() {
char input[LINE_SIZE]; char input[100];
float value;
struct stack mystack; struct stack mystack;
memset(&mystack, 0, sizeof(struct stack)); mystack.size = 0;
while (fgets(input, LINE_SIZE, stdin) != NULL) { // Reading input from stdin until EOF or an error occurs
// Remove trailing newline character while (fgets(input, sizeof(input), stdin) != NULL) {
input[strlen(input) - 1] = '\0'; input[strlen(input) - 1] = '\0'; // Remove newline character
// Try to parse input as a number // Try to parse input as a number
float value;
if (sscanf(input, "%f", &value) == 1) { if (sscanf(input, "%f", &value) == 1) {
push_stack(&mystack, value); push_stack(&mystack, value);
} }
@ -96,9 +101,10 @@ int main() {
return 1; return 1;
} }
// Print stack after each input // Print stack after each valid input
print_stack(&mystack); print_stack(&mystack);
} }
printf("no input\n");
return 0; return 0;
} }