Werte ausgeben
-
Hallo,
Ich habe ein kleines Problem, ich muss von diesem String "0 AZ 009049 001522300x" , die Werte miteinader addieren z. B 0+ az + 009049 d.h nur die Werte, also in Bit und nicht als Sting ausgeben sonder eine addierte Bit Form.
weiss jemand wie?
Der Coder hier funktioniert gib aber nur Strings wieder. ( Dev C++)
#include <vector> #include <string> #include <iostream> using namespace std; int main( ) { const char str[ ] = " 0 AZ 00-9049 0.01522300"; std::string temp( str ); //std::string::size_type pos = temp.find( ' ', 0 ); //std::string::size_type old_pos = 0; std::vector< std::string > substrings; int pos = 0; int start_pos = 0; int end_pos = 0; bool found = false; while(str[pos] != 0) { if( (str[pos] > 47 && str[pos] < 58) || (str[pos] > 64 && str[pos] < 91)){ if(found){ end_pos = pos+1; } else{ found = true; start_pos = pos; end_pos = pos+1; } } else{ if(found){ substrings.push_back(temp.substr(start_pos, end_pos - start_pos)); start_pos = pos; end_pos = pos; found = false; } } pos++; //old_pos = ++pos; //pos = temp.find( ' ', pos ); //substrings.push_back( temp.substr( old_pos, pos - old_pos ) ); } for( std::vector< std::string >::size_type i = 0, size = substrings.size( ); i < size; ++i ) { std::cout << i + 1 << ": " << substrings[ i ] << std::endl; } system("PAUSE"); return 0; }
-
Hallo
In der FAQ hier in C++ Forum kannst du im Thread Einmal String und zurück nachlesen wie mal Strings in Integer umwandelt.
Bei diesem Fall würde es sich lohnen ganz auf std::stringstream zu setzen.bis bald
akari
-
Danke, ehm könntest du mir das ev. im Code zeigen, bin nicht so bewandert was Programmieren angeht. Danke
-
Hm, der Code kommt mir irgendwie bekannt vor

kennst du schon
std::isdigit( )und co.?greetz, Swordfish
-
hehe, nein es ist nicht mehr ganz der gleiche. Habe ihn zimmlich umgeändert. ausgeklammterte war deins *G*. nein kenn ich leider gar nicht

-
Na dann, wolln wir mal...
#include <cctype> // isdigit( ), isupper( ) #include <vector> #include <string> #include <iostream> int main( ) { const char str[ ] = " 0 AZ 00-9049 0.01522300"; std::string temp( str ); std::vector< std::string > substrings; std::string::size_type pos = 0; std::string::size_type start_pos = 0; std::string::size_type end_pos = 0; // Sollten alle drei std::string::size_type sein... bool found = false; while( str[ pos ] != 0 ) { // if( ( str[ pos ] > 47 && str[ pos ] < 58 ) || ( str[ pos ] > 64 && str[ pos ] < 91 ) ) { // // leserlich: // if( ( ( '0' <= str[ pos ] ) && ( str[ pos ] <= '9' ) ) || ( ( 'A' <= str[ pos ] ) && ( str[ pos ] <= 'Z' ) ) ) // // besser: if( std::isdigit( str[ pos ] ) || std::isupper( str[ pos ] ) ) { if( found ) { end_pos = pos+1; } else { found = true; start_pos = pos; end_pos = pos+1; } } else { if( found ) { substrings.push_back( temp.substr( start_pos, end_pos - start_pos ) ); start_pos = pos; end_pos = pos; found = false; } } pos++; } for( std::vector< std::string >::size_type i = 0, size = substrings.size( ); i < size; ++i ) { std::cout << i + 1 << ": " << substrings[ i ] << std::endl; } std::cin.get( ); }Erklär das Addiren mal anschaulich!
greetz, Swordfish
-
Danke dir!! du bist ein warer Held;)
ehm aber gibt bei mir noch immer die Zahlen, in ascii aus nicht in bit *g*habe es jetzt so gemacht
#include <cctype> // isdigit( ), isupper( ) #include <vector> #include <string> #include <iostream> int main( ) { const char str[ ] = " 0 AZ 00-9049 0.01522300"; std::string temp( str ); std::vector< std::string > substrings; std::string::size_type pos = 0; std::string::size_type start_pos = 0; std::string::size_type end_pos = 0; // Sollten alle drei std::string::size_type sein... bool found = false; int sum = 0; while( str[ pos ] != 0 ) { // if( ( str[ pos ] > 47 && str[ pos ] < 58 ) || ( str[ pos ] > 64 && str[ pos ] < 91 ) ) { // // leserlich: // if( ( ( '0' <= str[ pos ] ) && ( str[ pos ] <= '9' ) ) || ( ( 'A' <= str[ pos ] ) && ( str[ pos ] <= 'Z' ) ) ) // // besser: if( std::isdigit( str[ pos ] ) || std::isupper( str[ pos ] ) ) { sum += str[pos]; if( found ) { end_pos = pos+1; } else { found = true; start_pos = pos; end_pos = pos+1; } } else { if( found ) { substrings.push_back( temp.substr( start_pos, end_pos - start_pos ) ); start_pos = pos; end_pos = pos; found = false; } } pos++; } for( std::vector< std::string >::size_type i = 0, size = substrings.size( ); i < size; ++i ) { std::cout << i + 1 << ": " << substrings[ i ] << std::endl; } std::cout << "The sum of the characters is " << sum << std::endl; std::cin.get( ); }Addiren??
Grüssle
-
Ach Gott! Dein Programm gibt z. Zt. aus:
0 AZ 00 9049 0Du sagst: "nur die Werte, also in Bit und nicht als Sting ausgeben sonder eine addierte Bit Form."
Wie sollen sie denn addiert und ausgegeben werden!?
a)
0+0+9049+0 = 9040 => 10001101011001 (das Zahlensystem von 'AZ' kenn ich nicht...)b)
'0' = 0x37 'A', 'Z' = 0x41 0x5A '0', '0' = 0x30 0x30 '9', '0', '4', '9' = 0x39 0x30 0x34 0x39 '0' = 0x30 ---------------------------------------- 0x39 0x30 0xA7 0x53 => 111001001100001010011101010011c) ??
greetz, Swordfish
PS: Was soll das eigentlich werden, wenn's fertig ist?