R
raven1276
Hallo,
ich sitz nun schon seit einigen Tagen an einer Programmierenaufgabe. Es soll ein Infix-Ausdruck in einen üpostfic-ausdruck umgewandelt werden. Ich dachte, ich hätts verstanden, aber mein Programm macht jede Menge murks. Die Klammern z.B. werden ausgegeben, obwohl sie ja gelöscht werden sollten. Es wäre echt klasse, wenn sich jemand mal den Quellcode ansehen würde und mir sagen könnte, was ich alles flasch gemacht habe. Ich blicke da nicht mehr durch. Achja, wir sollten gets nehmen. Vielen Dank schonmal im vorraus.
gruß Robert
ich sitz nun schon seit einigen Tagen an einer Programmierenaufgabe. Es soll ein Infix-Ausdruck in einen üpostfic-ausdruck umgewandelt werden. Ich dachte, ich hätts verstanden, aber mein Programm macht jede Menge murks. Die Klammern z.B. werden ausgegeben, obwohl sie ja gelöscht werden sollten. Es wäre echt klasse, wenn sich jemand mal den Quellcode ansehen würde und mir sagen könnte, was ich alles flasch gemacht habe. Ich blicke da nicht mehr durch. Achja, wir sollten gets nehmen. Vielen Dank schonmal im vorraus.
Code:
#include <stdio.h>
char input[100];
char output[100];
char help[100];
int input_index=0;
int help_index=0;
int output_index=0;
int input_prio;
int help_prio;
void push(char a){
help[help_index]=a;
help_index++;
}
char pop(){
help_index--;
return help[help_index];
}
int priority(char a){
int prio;
switch(a){
case '+':prio=1;
break;
case '-':prio=1;
break;
case '*':prio=2;
break;
case '/':prio=2;
break;
case '(':prio=3;
break;
case ')':prio=4;
break;
default:prio=0;
break;
}
return prio;
}
void umformung(){
// input_prio=priority(input[input_index]);
// if(input_prio==0){
//
// output[output_index]=input[input_index];
// output_index++;
// input_index++;
// }
// else{
// push(input[input_index]);
// input_index++;
// }
while (input[input_index] !='\0')
{
if(help[help_index]=='#'){
input_prio=priority(input[input_index]);
if(input_prio==0){
output[output_index]=input[input_index];
output_index++;
input_index++;
}
else{
push(input[input_index]);
input_index++;
}
}
else{
input_prio=priority(input[input_index]);
help_prio=priority(help[help_index]);
if(input_prio==4){
while(help[help_index] !='#'){
output[output_index]=pop();
output_index++;
}
}
else{
if(input_prio==3){
push(input[input_index]);
input_index++;
}
else{
if(input_prio==0){
output[output_index]=input[input_index];
output_index++;
input_index++;
}
else{
if(input_prio>help_prio){
push(input[input_index]);
input_index++;
}
else{
output[output_index]=pop();
output_index++;
}
}
}
}
}
}
while(help[help_index] !='#'){
if(help[help_index] !='#'){
output[output_index]=pop();
output_index++;
}
}
}
main(){
printf("\n\nDieses Programm formt Ausdrücke von der Infix in die Postfix-Notation um.\n");
printf("Es können nur Konstanten,Variablen und Zahlen mit einer Stelle verwendet werden.\n");
printf("Erlaubte Operatoren sind: '+''-''*''/''('')'\n");
printf("Bitte geben sie den Ausdruck, der umgeformt werden soll ein:");
gets (input);
push('#');
umformung();
printf("umgeformte Gleichung: %s\n",output);
}
gruß Robert