-
-
Save Honzaik/f566af1e70ce89a74c7ef13497dd24f8 to your computer and use it in GitHub Desktop.
c.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
typedef struct strom { | |
char val[20]; | |
struct strom *L; | |
struct strom *P; | |
} strom; | |
void initStrom(strom **s) { | |
*s = malloc(sizeof(strom)); | |
(*s)->L = NULL; | |
(*s)->P = NULL; | |
strcpy((*s)->val, "\0"); | |
} | |
int pridej(strom **s, char *str) { | |
if (strlen(str) == 0) return 0; | |
if ((*s)->val[0] >= '0' && (*s)->val[0] <= '9') return 0; //nesnazim se pridavat syna listu | |
if ((*s)->L != NULL) { | |
if (pridej(&(*s)->L, str)) { | |
return 1; | |
} | |
if ((*s)->P != NULL) { | |
return pridej(&(*s)->P, str); | |
} | |
else { | |
initStrom(&(*s)->P); | |
strcpy((*s)->P->val, str); | |
return 1; | |
} | |
} | |
else { | |
initStrom(&(*s)->L); | |
strcpy((*s)->L->val, str); | |
return 1; | |
} | |
} | |
void vypisInfix(strom **s) { | |
if (*s != NULL) { | |
if ((*s)->L != NULL) printf("("); | |
vypisInfix(&(*s)->L); | |
if ((*s)->val[0] >= '0' && (*s)->val[0] <= '9') { | |
printf("%s", (*s)->val); | |
} | |
else { | |
printf(" %c ", (*s)->val[0]); | |
} | |
vypisInfix(&(*s)->P); | |
if ((*s)->L != NULL) printf(")"); | |
} | |
} | |
int jePrvni = 1; | |
void vypisPostfix(strom **s) { | |
if (*s != NULL) { | |
vypisPostfix(&(*s)->L); | |
vypisPostfix(&(*s)->P); | |
if (jePrvni) { | |
printf("%s", (*s)->val); | |
jePrvni = 0; | |
} | |
else { | |
printf(" %s", (*s)->val); | |
} | |
} | |
} | |
int main() { | |
strom *s = NULL; | |
int i = 0; | |
char buffer[20]; | |
char c; | |
while ((c = getchar()) != ' ' && c != EOF) { | |
buffer[i] = c; | |
i++; | |
} | |
buffer[i] = 0; | |
if (i > 0) { // neco se nacetlo, nebyl prazdny vstup | |
initStrom(&s); | |
strcpy(s->val, buffer); | |
} | |
i = 0; | |
while ((c = getchar()) != EOF) { | |
if (c == ' ') { | |
buffer[i] = 0; | |
pridej(&s, buffer); //je to jen jeden char | |
i = 0; | |
} | |
else { | |
buffer[i] = c; | |
i++; | |
} | |
} | |
//zbytek v bufferu | |
buffer[i] = 0; | |
pridej(&s, buffer); | |
vypisPostfix(&s); | |
printf("\n"); | |
vypisInfix(&s); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment