Ĺadny brzuch
Napisałem taki program:
#include <iostream> #include <string> #include <fstream> using namespace std; void dwojkowo( fstream &strum, int liczba ); int main() { cout << "Podaj nazwę pliku: "; string nazwa; cin >> nazwa; int klucz; cout << endl; cout << "Podaj klucz: "; cin >> klucz; fstream pout( nazwa.c_str() ); fstream wyj( "wyj.txt" ); char c; c = pout.get(); int l; int j; while ( !pout.eof() ) { l = static_cast<int>( c ); j = l | klucz; dwojkowo( wyj, j); wyj << " "; c = pout.get(); } pout.close(); wyj.close(); return 0; } void dwojkowo( fstream &strum, int liczba) { int reszta = liczba % 2; if( liczba > 1) { dwojkowo( strum, liczba / 2); } strum << reszta; }
No i mam teraz problem :( . Nie wiem jak to odwrócić!!!!! Jeśli ktoś ma pomysł jak to odwrócić to niech pisze śmiało. Pomysłów wciąż szukam na GOOGLACH.
Użytkownik Xenox edytował ten post 12 wrzesień 2008, 07:11
Chodzi ci o coś takiego?:
#include <iostream> #include <string> #include <cmath> using namespace std; int bin_to_dec(string bin) { int dec_value = 0; for(int i = bin.length() - 1; i >= 0; --i) { if(bin[i] == '1') dec_value += pow(2, bin.length() - (i + 1)); } return dec_value; } int main() { string bin; cout << "bin: "; cin >> bin; int value = bin_to_dec(bin); cout << "value: " << value << endl; return 0; }
Dzięki neosnooze :clap: Oto właśnie chodziło.
Musiałem tylko drobnie poprawić program, ponieważ kompilator wywalał:
odt.cpp: In function ‘int bin_to_dec(std::string)’: odt.cpp:12: error: call of overloaded ‘pow(int, unsigned int)’ is ambiguous /usr/include/bits/mathcalls.h:154: note: candidates are: double pow(double, double) /usr/include/c++/4.1.3/cmath:361: note: long double std::pow(long double, int) /usr/include/c++/4.1.3/cmath:357: note: float std::pow(float, int) /usr/include/c++/4.1.3/cmath:353: note: double std::pow(double, int) /usr/include/c++/4.1.3/cmath:349: note: long double std::pow(long double, long double) /usr/include/c++/4.1.3/cmath:345: note: float std::pow(float, float)
Więc po przeróbkach funkcja wygląda następująco:
int bin_to_dec(string bin) { float dec_value = 0; for(int i = bin.length() - 1; i >= 0; --i) { if(bin[i] == '1') dec_value += pow((float)2, (int) (bin.length() - (i + 1)) ); } return (int)dec_value; }
Kwestia kompilatora, g++ 4.3.2 kompilował bez problemu ;)
zanotowane.pl doc.pisz.pl pdf.pisz.pl zsf.htw.pl
#include <iostream> #include <string> #include <fstream> using namespace std; void dwojkowo( fstream &strum, int liczba ); int main() { cout << "Podaj nazwę pliku: "; string nazwa; cin >> nazwa; int klucz; cout << endl; cout << "Podaj klucz: "; cin >> klucz; fstream pout( nazwa.c_str() ); fstream wyj( "wyj.txt" ); char c; c = pout.get(); int l; int j; while ( !pout.eof() ) { l = static_cast<int>( c ); j = l | klucz; dwojkowo( wyj, j); wyj << " "; c = pout.get(); } pout.close(); wyj.close(); return 0; } void dwojkowo( fstream &strum, int liczba) { int reszta = liczba % 2; if( liczba > 1) { dwojkowo( strum, liczba / 2); } strum << reszta; }
No i mam teraz problem :( . Nie wiem jak to odwrócić!!!!! Jeśli ktoś ma pomysł jak to odwrócić to niech pisze śmiało. Pomysłów wciąż szukam na GOOGLACH.
Użytkownik Xenox edytował ten post 12 wrzesień 2008, 07:11
Chodzi ci o coś takiego?:
#include <iostream> #include <string> #include <cmath> using namespace std; int bin_to_dec(string bin) { int dec_value = 0; for(int i = bin.length() - 1; i >= 0; --i) { if(bin[i] == '1') dec_value += pow(2, bin.length() - (i + 1)); } return dec_value; } int main() { string bin; cout << "bin: "; cin >> bin; int value = bin_to_dec(bin); cout << "value: " << value << endl; return 0; }
Dzięki neosnooze :clap: Oto właśnie chodziło.
Musiałem tylko drobnie poprawić program, ponieważ kompilator wywalał:
odt.cpp: In function ‘int bin_to_dec(std::string)’: odt.cpp:12: error: call of overloaded ‘pow(int, unsigned int)’ is ambiguous /usr/include/bits/mathcalls.h:154: note: candidates are: double pow(double, double) /usr/include/c++/4.1.3/cmath:361: note: long double std::pow(long double, int) /usr/include/c++/4.1.3/cmath:357: note: float std::pow(float, int) /usr/include/c++/4.1.3/cmath:353: note: double std::pow(double, int) /usr/include/c++/4.1.3/cmath:349: note: long double std::pow(long double, long double) /usr/include/c++/4.1.3/cmath:345: note: float std::pow(float, float)
Więc po przeróbkach funkcja wygląda następująco:
int bin_to_dec(string bin) { float dec_value = 0; for(int i = bin.length() - 1; i >= 0; --i) { if(bin[i] == '1') dec_value += pow((float)2, (int) (bin.length() - (i + 1)) ); } return (int)dec_value; }
Kwestia kompilatora, g++ 4.3.2 kompilował bez problemu ;)