Seite 2 von 2

Verfasst: 01.01.2006 19:54
von Nik
Das sollte in keinsterweise eine Anfang für einen Spam War werden, ich habe mich gefragt wie groß der Unterschied ist, habe dieses Programm geschrieben und mich gewundert also habe ich es gepostet was ist daran nun biotte verwerflich? Ich kann dir auch den Code schicken den ich in den letzten 3 Tagen geschrieben habe wenn du glaubst ich würde spammen oderso... Außerdem hoffte ich eben ein wenig auf Hilfe oder Kommentare was ich falsch gemacht habe, das dabei auch unkonstruktives gespamme bei raus kommt da kann ich doch nichts dafür....

Verfasst: 01.01.2006 20:50
von MARTIN
@Spirit
Ich habe mich leider hier mächtig vertan.
Str() und Val() verwechselt, egal.
Vielleicht braucht jemand auch Val() in c++.

Ansonsten man kann , wenn man grade kein bock oder zeit hat eigene algorythmen zu entwickeln, auch sprintf(...) benutzen oder aus STL streamstring:

Code: Alles auswählen

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
	stringstream steam;

	stream <<1458554;
	string a;
	a =  stream.str();
	cout << a << endl;
	return 0;
}
Vielleicht gibt es noch einen Weg.

Verfasst: 02.01.2006 15:35
von Leo
Für alle möglichen Umwandlungen von Zahl zu String und String zu Zahl gibt es diese Funktion, die aber auch mit stringstreams arbeitet:

Code: Alles auswählen

template<typename T, typename S> inline T lexical_cast(const S& source)
{
	std::stringstream converter;
	T result;

	if(!(converter << source && converter >> result && (converter >> std::ws).eof()))
		throw std::logic_error("lexical_cast failed");

	return result;
}

Code: Alles auswählen

try
{
    std::string foo("1234");
    unsigned int bar = lexical_cast<unsigned int>(foo);
} 
catch(const std::exception& exp)
{
    std::cout << exp.what() << std::endl;
}


Verfasst: 02.01.2006 16:32
von Nik
thx

Verfasst: 04.01.2006 22:12
von Danilo
Leo hat geschrieben:Für alle möglichen Umwandlungen von Zahl zu String und
String zu Zahl gibt es diese Funktion, die aber auch mit
stringstreams arbeitet:
Für BASIC-Coder, die Str() und Hex() gewohnt sind, hier
noch bissl was zum rumprobieren und rumspielen:

Code: Alles auswählen

/*
 *
 * Str(), Hex(), Oct(), Val()
 *
 */
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdlib.h>
#include <string>

using namespace std;



template<class T>
std::string Str(T x) {
   std::ostringstream stream;
   stream << x;
   return stream.str();
}

template<class T>
std::string Str(T x, int length, char fill=' ', bool left=false) {
   std::ostringstream stream;

   if(left) stream << setfill(fill) << setw(length) << std::left << x;
   else     stream << setfill(fill) << setw(length) << x;

   return stream.str();
}

template<class T>
std::string Hex(T x) {
   std::ostringstream stream;
   stream << std::hex << std::uppercase << x;
   return stream.str();
}

template<class T>
std::string Hex(T x, int length, char fill=' ', bool left=false) {
   std::ostringstream stream;

   if(left) stream << setfill(fill)  << setw(length) << std::hex
                   << std::uppercase << std::left    << x;
   else     stream << setfill(fill)  << setw(length) << std::hex
                   << std::uppercase << x;

   return stream.str();
}

template<class T>
std::string Oct(T x) {
   std::ostringstream stream;
   stream << std::oct << x;
   return stream.str();
}

template<class T>
std::string Oct(T x, int length, char fill=' ', bool left=false) {
   std::ostringstream stream;

   if(left) stream << setfill(fill) << setw(length) << std::oct << std::left << x;
   else     stream << setfill(fill) << setw(length) << std::oct << x;

   return stream.str();
}

template<class T>
T Val(std::string s) {
   T a;
   std::stringstream stream;
   stream << s;
   stream >> a;
   return a;
}

std::string Chr(unsigned char x) {
   std::ostringstream stream;
   stream << x;
   return stream.str();
}



int main(int argc, char *argv[])
{
  int                a = 12;
  float              b = 12.34f;
  unsigned char      c = 65;
  long               d = -1234567;
  unsigned long      e = 0xFFFFFFFF;
  long               f = 0xFFFFFFFF;
//  long long          g = 0x7FFFFFFFFFFFFFFF;
//  unsigned long long h = 0xFFFFFFFFFFFFFFFF;

  cout << "Dezimal: " << Str(a,10) << " Hex: " << Hex(a,8) << endl
       << "Dezimal: " << Str(b,10) << " Hex: " << Hex(b,8) << endl
       << "Dezimal: " << Str(c,10) << " Hex: " << Hex(c,8) << endl
       << "Dezimal: " << Str(d,10) << " Hex: " << Hex(d,8) << endl
       << "Dezimal: " << Str(e,10) << " Hex: " << Hex(e,8) << endl
       << "Dezimal: " << Str(f,10) << " Hex: " << Hex(f,8) << endl;
       //<< "Dezimal: " << Str(g,20) << " Hex: " << Hex(g,8) << endl
       //<< "Dezimal: " << Str(h,20) << " Hex: " << Hex(h,8) << endl;

  cout << endl;

  // string von datentyp erstellen
  string s = Str(a) + " -- " + Str(b);

  cout << "string: " << s << endl << endl;
  
  a=255;
  cout << Oct(a) << endl;
  
  int x = Val<int>("-123");
  cout << x << endl;

  float y = Val<float>("123.456");
  cout << y << endl;
  
  for(long i=0; i<256; i++) cout << Chr(i);

  system("PAUSE");	
  return 0;
}

Code: Alles auswählen

/*
 * ToString() und ToHex() Tests als Template Klasse
 *
 * Ausgabe:
 *
 *  Dezimal:                   10  Hex:                A
 *  Dezimal:                12.34  Hex:            12.34
 *  Dezimal:                    A  Hex:                A
 *  Dezimal:             -1234567  Hex:         FFED2979
 *  Dezimal:           4294967295  Hex:         FFFFFFFF
 *  Dezimal:                   -1  Hex:         FFFFFFFF
 *  Dezimal:  9223372036854775807  Hex: 7FFFFFFFFFFFFFFF
 *  Dezimal: 18446744073709551615  Hex: FFFFFFFFFFFFFFFF
 *  
 *  string: 10 -- 12.34
 *
 */
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdlib.h>
#include <string>

using namespace std;



template<class T>
class var {
  public:
    var()      { value=0; }
    var(T a)   { value=a; }
/*
    friend ostream &operator<<(ostream &stream, T &o) {
       return stream << o.value;
    }

    friend istream &operator>>(istream &stream, T &o) {
       return stream >> o.value;
    }
*/
    std::string ToString() {
       std::ostringstream stream;
       stream << this->value;
       return stream.str();
    }

    std::string ToString(int length, char fill=' ', bool left=false) {
       std::ostringstream stream;

       if(left) stream << setfill(fill) << setw(length) << std::left << value;
       else     stream << setfill(fill) << setw(length) << value;

       return(stream.str());
    }

    std::string ToHex() {
       std::ostringstream stream;
       stream << hex << uppercase << value;
       return stream.str();
    }

    std::string ToHex(int length, char fill=' ', bool left=false) {
       std::ostringstream stream;

       if(left) stream << setfill(fill)  << setw(length) << std::hex
                       << std::uppercase << std::left    << value;
       else     stream << setfill(fill)  << setw(length) << std::hex
                       << std::uppercase << value;

       return(stream.str());
    }

    operator std::string() {
       return this->ToString();
    }

    operator T() { return value; }
    
    T operator++()      { value++; return *this; }
    T operator++(int a) { value++; return *this; }
    T operator--()      { value--; return *this; }
    T operator--(int a) { value--; return *this; }

    T operator+=(int a) { value += a; return *this; }
    T operator-=(int a) { value -= a; return *this; }

  private:
    T value;
};



template<class T>
void Ausgabe(T x) {
  cout << "Dezimal: "
       << x.ToString(20)
       << "  Hex: "
       << x.ToHex(16)
       << endl;
}



int main(int argc, char *argv[])
{

  var<int>                a = 12;
  var<float>              b = 12.34f;
  var<char>               c = 65;
  var<long>               d = -1234567;
  var<unsigned long>      e = 0xFFFFFFFF;
  var<long>               f = 0xFFFFFFFF;
//  var<long long>          g = 0x7FFFFFFFFFFFFFFF;
//  var<unsigned long long> h = 0xFFFFFFFFFFFFFFFF;

  a++; // teste operatoren ++
  ++a; //                

  a--; // teste operatoren --
  --a; //                

  a += 1; // 12 + 1 = 13
  a -= 3; // 13 - 3 = 10


  Ausgabe(a);
  Ausgabe(b);
  Ausgabe(c);
  Ausgabe(d);
  Ausgabe(e);
  Ausgabe(f);
//  Ausgabe(g);
//  Ausgabe(h);

  cout << endl;

  // string von datentyp erstellen
  string s = a.ToString() + " -- " + b.ToString();

  cout << "string: " << s << endl << endl;

  system("PAUSE");	
  return 0;
}

Code: Alles auswählen

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

class Integer {
  public:
    Integer()      { value=0; }
    Integer(int a) { value=a; }
    
    friend ostream &operator<<(ostream &stream, Integer &o) {
       return stream << o.value;
    }

    friend istream &operator>>(istream &stream, Integer &o) {
       return stream >> o.value;
    }

    operator string() {
       std::stringstream stream;
       stream << value;
       return stream.str();
    }
    
    operator int() { return value; }
    
    Integer operator++()      { value++; return *this; }
    Integer operator++(int a) { value++; return *this; }
    Integer operator--()      { value--; return *this; }
    Integer operator--(int a) { value--; return *this; }

    Integer operator+=(int a) { value += a; return *this; }
    Integer operator-=(int a) { value -= a; return *this; }

  private:
    int value;
};


int main(int argc, char *argv[])
{
  Integer a = 12;
  
  a++; // teste operatoren ++
  ++a; //                

  a--; // teste operatoren --
  --a; //                

  a += 1; // 12 + 1 = 13
  a -= 3; // 13 - 3 = 10
  
  cout << a << endl
       << "Bitte geben sie eine Integer-Zahl ein:  ";
  
  cin >> a;
  
  cout << a << endl;
  
  string x = a;
  
  cout << "Inhalt von String 'x':  " << x << endl;
  
  
  system("PAUSE");	
  return 0;
}
Wie gesagt, nur zum rumspielen für Umsteiger gedacht... ;)

Verfasst: 11.01.2006 08:52
von BrunoF
hehe da sehe ich sofort warum ich als Gelegenheitsprogrammierer immer wieder auf BASIC zurückgekommen bin.

Verfasst: 11.01.2006 10:52
von Kiffi
> hehe da sehe ich sofort warum ich als Gelegenheitsprogrammierer immer
> wieder auf BASIC zurückgekommen bin.

LOL! Mir geht's genauso :-)

Grüße ... Kiffi