Update du3/program.c
This commit is contained in:
parent
c10fc52f48
commit
3e5c6936d4
109
du3/program.c
109
du3/program.c
@ -1,68 +1,77 @@
|
|||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define LIST_SIZE 100
|
#define STACK_SIZE 10
|
||||||
|
|
||||||
struct listok {
|
struct stack {
|
||||||
char nazov[101];
|
float values[STACK_SIZE];
|
||||||
float cena;
|
int size;
|
||||||
};
|
};
|
||||||
|
|
||||||
int add_listok(struct listok *p, int *n){
|
void print_stack(struct stack *s) {
|
||||||
if (fgets(p[*n].nazov, 101, stdin) == NULL){
|
for (int i = 0; i < s->size; i++) {
|
||||||
return 0;
|
printf("%.2f ", s->values[i]);
|
||||||
}
|
}
|
||||||
|
printf("\n");
|
||||||
p[*n].nazov[strcspn(p[*n].nazov, "\n")] = 0;
|
|
||||||
|
|
||||||
if (scanf("%f", &p[*n].cena) != 1){
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
getchar();
|
|
||||||
*n = *n + 1;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int compare(struct listok *a, struct listok *b){
|
void push_stack(struct stack *s, float value) {
|
||||||
if (a->cena > b->cena){
|
if (s->size >= STACK_SIZE) {
|
||||||
return 1;
|
printf("no input\n");
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
if (a->cena < b->cena){
|
s->values[s->size] = value;
|
||||||
return -1;
|
s->size++;
|
||||||
}
|
|
||||||
return strcmp(a->nazov, b->nazov);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sort_listok(struct listok *p, int n){
|
float pop_stack(struct stack *s) {
|
||||||
for (int i = 0; i < n - 1; i++){
|
if (s->size <= 0) {
|
||||||
for (int j = i + 1; j < n; j++){
|
printf("no input\n");
|
||||||
if (compare(&p[i], &p[j]) > 0){
|
exit(0);
|
||||||
struct listok tmp = p[i];
|
}
|
||||||
p[i] = p[j];
|
s->size--;
|
||||||
p[j] = tmp;
|
return s->values[s->size];
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
struct stack s;
|
||||||
|
s.size = 0;
|
||||||
|
|
||||||
|
char input[100];
|
||||||
|
|
||||||
|
while (scanf("%s", input) == 1) {
|
||||||
|
if (strcmp(input, "+") == 0 || strcmp(input, "-") == 0 ||
|
||||||
|
strcmp(input, "*") == 0 || strcmp(input, "/") == 0) {
|
||||||
|
|
||||||
|
if (s.size < 2) {
|
||||||
|
printf("no input\n");
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float b = pop_stack(&s);
|
||||||
|
float a = pop_stack(&s);
|
||||||
|
float result;
|
||||||
|
|
||||||
|
if (strcmp(input, "+") == 0) result = a + b;
|
||||||
|
else if (strcmp(input, "-") == 0) result = a - b;
|
||||||
|
else if (strcmp(input, "*") == 0) result = a * b;
|
||||||
|
else result = a / b;
|
||||||
|
|
||||||
|
push_stack(&s, result);
|
||||||
|
print_stack(&s);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
char *endptr;
|
||||||
|
float val = strtof(input, &endptr);
|
||||||
|
if (*endptr != '\0') {
|
||||||
|
printf("no input\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
push_stack(&s, val);
|
||||||
|
print_stack(&s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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(){
|
|
||||||
struct listok p[LIST_SIZE];
|
|
||||||
int n = 0;
|
|
||||||
|
|
||||||
while (add_listok(p, &n)){
|
|
||||||
}
|
|
||||||
|
|
||||||
sort_listok(p, n);
|
|
||||||
print_listok(p, n);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user