Problem ze stosem
Ostatnio zmodyfikowano 2011-03-21 22:02
MrOsamaful Temat założony przez niniejszego użytkownika |
Problem ze stosem » 2011-03-21 21:02:46 Program nie działa tak jak powinien.
Input:
2 +4 + 3
Output:
Operatory: +
Cyfry: 34
A powinno być
Output:
Operatory: ++
Cyfry: 342
#include <iostream> #include <stack> #include <string> #include <cstdlib>
using namespace std; typedef unsigned int uint;
stack < int > numbers;
stack < char > operators; char buffer[ 256 ]; string input;
int main() { char * conv; char ch[ 1 ]; cout << "Wpisz dzialanie:" << endl; while( 1 ) { cin.getline( buffer, 256 ); input = buffer; for( uint i = 0; i < input.size(); i++ ) { begin: if( isspace( input[ i ] ) ) { i++; goto begin; } if( isalnum( input[ i ] ) ) { ch[ 0 ] = input[ i ]; conv = ch; numbers.push( atoi( conv ) ); } else operators.push( input[ i ] ); cout << input[ i ]; } cout << endl << "Operatory: "; for( uint i = 0; i < operators.size(); i++ ) { cout << operators.top(); operators.pop(); } cout << endl << "Cyfry: "; for( uint i = 0; i < numbers.size(); i++ ) { cout << numbers.top(); numbers.pop(); } cout << "\n\n\n\n\n"; } return 0; }
|
|
szyx_yankez |
» 2011-03-21 21:37:28 Spróbuj tak:
{ [...] cout << endl << "Operatory: "; for(; !operators.empty(); ) { cout << operators.top(); operators.pop(); } cout << endl << "Cyfry: "; for(; !numbers.empty(); ) { cout << numbers.top(); numbers.pop(); } cout << "\n\n\n\n\n"; } return 0; }
|
|
MrOsamaful Temat założony przez niniejszego użytkownika |
» 2011-03-21 22:02:39 Dzięki, działa świetnie, uratowałeś mnie :P |
|
« 1 » |