Update du3/program.c
This commit is contained in:
parent
618dede753
commit
a9c8b2bc84
133
du3/program.c
133
du3/program.c
@ -1,107 +1,68 @@
|
|||||||
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#define STACK_SIZE 10
|
#define LIST_SIZE 100
|
||||||
|
|
||||||
struct stack{
|
struct listok {
|
||||||
float values[STACK_SIZE];
|
char nazov[101];
|
||||||
int size;
|
float cena;
|
||||||
};
|
};
|
||||||
|
|
||||||
void push_stack(struct stack *s, float value){
|
void add_listok(struct listok *p, int *n){
|
||||||
if (s->size >= STACK_SIZE){
|
if (fgets(p[*n].nazov, 101, stdin) == NULL){
|
||||||
printf("Chyba: zasobnik je plny!\n");
|
exit(0);
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
s->values[s->size] = value;
|
|
||||||
s->size++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float pop_stack(struct stack *s){
|
p[*n].nazov[strcspn(p[*n].nazov, "\n")] = 0;
|
||||||
if (s->size <= 0){
|
|
||||||
printf("Chyba: zasobnik je prazdny!\n");
|
if (scanf("%f", &p[*n].cena) != 1){
|
||||||
exit(1);
|
exit(0);
|
||||||
}
|
|
||||||
s->size--;
|
|
||||||
return s->values[s->size];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_stack(struct stack *s){
|
getchar();
|
||||||
for (int i = 0; i < s->size; i++){
|
*n = *n + 1;
|
||||||
printf("%.2f ", s->values[i]);
|
}
|
||||||
|
|
||||||
|
int compare(struct listok *a, struct listok *b){
|
||||||
|
if (a->cena > b->cena){
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
printf("\n");
|
if (a->cena < b->cena){
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
return strcmp(a->nazov, b->nazov);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort_listok(struct listok *p, int n){
|
||||||
|
for (int i = 0; i < n - 1; i++){
|
||||||
|
for (int j = i + 1; j < n; j++){
|
||||||
|
if (compare(&p[i], &p[j]) > 0){
|
||||||
|
struct listok tmp = p[i];
|
||||||
|
p[i] = p[j];
|
||||||
|
p[j] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_listok(struct listok *p, int n){
|
||||||
|
for (int i = 0; i < n; i++){
|
||||||
|
printf("%s\n", p[i].nazov);
|
||||||
|
printf("%f\n", p[i].cena);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
struct stack s;
|
struct listok p[LIST_SIZE];
|
||||||
s.size = 0;
|
int n = 0;
|
||||||
|
|
||||||
char input[100];
|
|
||||||
|
|
||||||
while (1){
|
while (1){
|
||||||
if (scanf("%s", input) != 1){
|
add_listok(p, &n);
|
||||||
printf("Chyba: nepodarilo sa nacitat vstup.\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *endptr;
|
sort_listok(p, n);
|
||||||
float value = strtof(input, &endptr);
|
print_listok(p, n);
|
||||||
|
|
||||||
if (*endptr == '\0') {
|
|
||||||
push_stack(&s, value);
|
|
||||||
print_stack(&s);
|
|
||||||
}
|
|
||||||
else if (strcmp(input, "+") == 0){
|
|
||||||
if (s.size < 2){
|
|
||||||
printf("Chyba: malo hodnot v zasobniku!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
float b = pop_stack(&s);
|
|
||||||
float a = pop_stack(&s);
|
|
||||||
push_stack(&s, a + b);
|
|
||||||
print_stack(&s);
|
|
||||||
}
|
|
||||||
else if (strcmp(input, "-") == 0){
|
|
||||||
if (s.size < 2){
|
|
||||||
printf("Chyba: malo hodnot v zasobniku!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
float b = pop_stack(&s);
|
|
||||||
float a = pop_stack(&s);
|
|
||||||
push_stack(&s, a - b);
|
|
||||||
print_stack(&s);
|
|
||||||
}
|
|
||||||
else if (strcmp(input, "*") == 0){
|
|
||||||
if (s.size < 2){
|
|
||||||
printf("Chyba: malo hodnot v zasobniku!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
float b = pop_stack(&s);
|
|
||||||
float a = pop_stack(&s);
|
|
||||||
push_stack(&s, a * b);
|
|
||||||
print_stack(&s);
|
|
||||||
}
|
|
||||||
else if (strcmp(input, "/") == 0){
|
|
||||||
if (s.size < 2){
|
|
||||||
printf("Chyba: malo hodnot v zasobniku!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
float b = pop_stack(&s);
|
|
||||||
float a = pop_stack(&s);
|
|
||||||
if (b == 0){
|
|
||||||
printf("Chyba: delenie nulou!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
push_stack(&s, a / b);
|
|
||||||
print_stack(&s);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
printf("Chyba: neplatny vstup '%s'\n", input);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user