Problem bei Ausgabe rechtsformatiert... PLEASE HELP
-
n abend....
und zwar hab ich folgendes problem. ich poste erstmal die ausgabe die eigentlich ercheinen soll, um mir ein paar worte zu sparen:
ZEIT DEC HEX GRAFIK 6.00 Uhr +5 5 |***** 12.00 Uhr -1 ffffffff *| 18.00 Uhr +6 6 |****** 24.00 Uhr -7 fffffff9 *******|so in etwa sollte es aussehen. also mein problem liegt nu bei der grafik. es sollen halt sterne nach links ausgegeben werden, je nachdem wieviel grad minus sind. allerdings haut ds mit meinen formatierungen nicht hin.
wäre cool. wenn mir jemand helfen könnte. ich glaub, all zu schwer ist es nicht. nur irgendwie komm ich nicht drauf.mein quelltext:
//temp1.cpp (by) Matthias Walter #include<iostream.h> #include<iomanip.h> #include<windows.h> void main() { int temp[4], zeit=6, tmp=0; const long showpos = 0x400L; for(int i=0; i<4; i++) { do { cout<<endl; cout<<" "<<"Temperatur "<<zeit<<".00 Uhr: "; cin >>temp[i]; system("cls"); zeit+=6; if(temp[i]<-20||temp[i]>20) { zeit-=6; } }while(temp[i]<-20||temp[i]>20); } zeit=6; cout<<endl; cout<<" "<<setw(12)<<"ZEIT"<<setw(18)<<"DEC HEX"<<setw(22)<<"GRAFIK"<<endl<<endl; for(i=0; i<4;i++) { cout<<" "<<setw(5)<<resetiosflags(showpos)<<dec<<zeit<<".00 Uhr"<<setw(8) <<setiosflags(showpos)<<temp[i]<<" "<<setw(8)<<hex<<temp[i]; if(temp[i]>0) { cout<<setw(20)<<char(179); for(int x=0; x<temp[i]; x++) { cout<<"*"; } } if(temp[i]<0) { tmp=temp[i]*(-1); for(int x=0; x<tmp; x++) { cout<<"*"; } cout<<char(179); } cout<<endl; zeit+=6; } cout<<endl<<endl; }
-
anbei die korrigierte Stelle, die die Temperatur-Graphik ausgibt:
if( temp[i]>=0 ) // bei temp[i] == 0 wird nur '|' ausgegeben { cout << setw(20) << char(179); for( int x=0; x<temp[i]; x++ ) { cout<<"*"; } } if( temp[i]<0 ) { cout << setw(20 + temp[i]); for( int x=temp[i]; x<0; ++x ) { cout<<"*"; } cout<<char(179); }zu empfehlen wäre eine eigene kleine Klasse - hier 'TempBar', die diese Ausgabe übernimmt. Etwa so
class TempBar { public: TempBar( int mini, int maxi ) : m_mini( mini ), m_maxi( maxi ), m_temp(0) {} TempBar& operator()( int temp ) { m_temp = temp; return *this; } friend std::ostream& operator<<( std::ostream& out, const TempBar& bar ) { out << std::setw( 2 - bar.m_mini + (bar.m_temp > 0? 0 : bar.m_temp) ); for( int i = bar.m_temp; i < 0; ++i ) out << '*'; out << '|'; for( int i = 0; i < bar.m_temp; ++i ) out << '*'; return out << std::setw( 1 + bar.m_maxi - (bar.m_temp > 0? bar.m_temp: 0 ) ) << ' '; } private: int m_mini, m_maxi; int m_temp; };.. und der Aufruf vereinfacht sich dann zu
for( int i=0; i<4; ++i) { TempBar tb( -20, 20 ); // Temperaturbereich vorgeben cout<<" "<<setw(5)<<resetiosflags(showpos)<<dec<<zeit<<".00 Uhr"<<setw(8) <<setiosflags(showpos)<<temp[i]<<" "<<setw(8)<<hex<<temp[i] << tb( temp[i] ) <<endl;; zeit+=6; }Gruß
Werner
-
jup... danke! funktioniert einwandfrei.