mam stiahnutý program , keď zadám vstup( postfixový vyraz )tak spadne, niekedy vypíše náhodný výsledok. Je tam chyba alebo dávam zlý vstup ?
Ak dávam zlý vstup napíšte mi prosím vás váš vstup .
Fórum › C / C++
POSTFIX,chyba v programe

Trošku jsem to upravil, aby to vůbec šlo přeložit bez warningů. (Alespoň při použití gcc na Linuxu.)
Celý program by to chtělo trošku přepsat tak, aby se některé části dělaly v nějaké funkci. Jenom kvůli přehlednosti. Ostatní změny jsem okomentoval přímo ve zdrojáku.
// Vzhledem k tomu, ze main je deklarovano jako main()
// a ne main(void), jedna se o C++
#include <stdio.h> // V C++ lepe #include <cstdio>
// Nasledujici headery chybi
#include <cstring>
#include <cctype>
#include <cstdlib>
// Toto neni k nicemu potreba (a navic je to nestandardni)
//#include <conio.h>
// K cemu globalni promenne, kdyz je tam stejne jenom jedna funkce?
double Op[20]; /* zasobnik pre operandy */
int sp; /* pre zasobnik */
int main()
{
char riadok[100];
char s[10];
double Op1,Op2;
char c;
int i,j,k,Bodka;
sp = 0; /* inicializacia zasobnika */
for (i=1;i<100;i++)
riadok[i]=' ';
for (j=1;j<10;j++)
s[j]=0;
printf("Zadajte vyraz v postfixovej notacii:\n");
j=strlen(fgets(riadok, sizeof(riadok), stdin)); // fgets je bezpecnejsi
// j=strlen(gets(riadok));
riadok[j]='='; /* zarazka */
i=0;
zac: while(riadok[i]==' ')
i++;
c=riadok[i];
if(c!='.' && !isdigit(c))
{ /* operator */
if ((c!='=') && (sp<2))
{
printf("nespravny vyraz\n");
return 1; // Tady chybela navratova hodnota
}
if (c!='=')
{
Op2=Op[--sp]; Op1=Op[--sp];
}
switch (c)
{
case '+':
Op[sp++]=Op1+Op2;
break;
case'-':
Op[sp++]=Op1-Op2;
break;
case'/':
if (Op2!=0)
Op[sp++]=Op1/Op2;
else
{
printf("delenie nulou\n");
return 2; // Tady chybela navratova hodnota
}
break;
case'*':
Op[sp++]=Op1*Op2;
break;
case'=':
Op1=Op[--sp];
if (sp!=0) printf("Vyraz nebol spravny\n");
printf("Vysledok=%f \n",Op1);
getchar();
return 3; // Tady chybela navratova hodnota
default:
printf("Neznamy prikaz\n");
return 4; // Tady chybela navratova hodnota
}
}
else
{ /* operand */
k=0;Bodka=0;
while(riadok[i]==' ')
i++;
while((isdigit(riadok[i])) || (riadok[i]=='.'))
{
if (riadok[i]=='.')
{
if (Bodka==0)
{
Bodka=1;
}
else
{
printf("Dve des.bodky v jednom cisle\n");
return 5; // Tady chybela navratova hodnota
}
}
s[k]=riadok[i];
if (k < 9)
k++;
else
{
printf ("Dlhy operand!\n");
return 6; // Tady chybela navratova hodnota
}
i++;
}
if (sp < 19)
{
Op[sp++]=atof(s);
for (j=1;j<10;j++)
s[j]=0; Bodka=0;
goto zac;
}
else
{
printf("Plny zasobnik\n");
return 7; // Tady chybela navratova hodnota
}
}
i++;
goto zac; // To goto je zcela zbytecne, lze nahradit cyklem
return 0;
}
Pokud tomu zadám např.
2 3 + 4 * =
tak to správně vypíše 20.
Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 42 hostů
Podobná vlákna
Postfix/smtpd warning — založil DIPcom
Prepis výrazu z infixu do postfix-u — založil Kent
Rada v programe c++ — založil koral000
Rada v programe c++ — založil koral000
Otáčanie v programe — založil letsthrowq
Moderátoři diskuze