Author Topic: C++ garbage  (Read 3729 times)

0 Members and 1 Guest are viewing this topic.

Offline DoomBringer316

  • Honorable
  • SpongeBob
  • ******
  • Posts: 1,413
    • View Profile
    • http://Meh, Removed.
C++ garbage
« on: December 20, 2006, 09:42:55 am »
#include<iostream>

#define NULL 0

using namespace std;

template<typename T>class Vector
{
    T* Data;

    void Swap(int pos1, int pos2)
    {
        if ( pos1 < Size && pos2 < Size )
        {
            T temp = Data[pos1];
            Data[pos1] = Data[pos2];
            Data[pos2] = temp;
        }
        else
            throw "ERROR::VECTOR::SWAP invalid index";
    }
public:
    int Size;

    Vector()
    {
        Size = 0;
        Data = NULL;
    }

    ~Vector()
    {
        delete Data;
        Data = NULL;
        Size = 0;
    }

    T operator [] (int index)
    {
        if ( index < Size )
            return Data[index];
        else
            throw "ERROR::VECTOR::[] invalid index";
    }

    void Insert(T data)
    {
        T* NewData = new T[Size + 1];
        for ( int i = 0; i < Size; i++ )
        {
            NewData = Data;
        }
        NewData[Size] = data;

        Data = NewData;
        Size++;
    }
    //sorts descending for UpDown = 0 and ascending otherwise
    void Sort(int DownUp = 0)
    {
        if ( DownUp == 0 )
        {
            for ( int i = 0; i < Size; i++ )
            {
                for ( int j = i + 1; j < Size; j++ )
                {
                    if ( Data > Data[j] )
                    {
                        Swap(i,j);
                    }
                }
            }
        }
        else
        {
            for ( int i = 0; i < Size; i++ )
            {
                for ( int j = i + 1; j < Size; j++ )
                {
                    if ( Data < Data[j] )
                    {
                        Swap(i,j);
                    }
                }
            }
        }
    }
};

void main()
{
    Vector<int> MyVector;
    int temp;
    try
    {
        for ( int i = 0; i < 10; i++ )
        {
            cout << "Enter number " << i << ": ";
            cin >> temp;
            MyVector.Insert(temp);
        }
        MyVector.Sort();
        cout << endl;
        for ( int i = 0; i < MyVector.Size; i++ )
        {
            cout << MyVector << endl;
        }
    }
    catch( char* ex )
    {
        cout << ex << endl;
    }
}

Offline DoomBringer316

  • Honorable
  • SpongeBob
  • ******
  • Posts: 1,413
    • View Profile
    • http://Meh, Removed.
Re: C++ garbage
« Reply #1 on: December 20, 2006, 09:43:42 am »
#include<iostream>
#include<string>

using namespace std;

template<typename T> class Complex
{   
public:
    Complex(T real, T imaginary)
    {
        Real = real;
        Imaginary = imaginary;
    };

    ~Complex()
    {

    };

    friend ostream& operator << (ostream& os, const Complex& object)
    {
        os << object.Real << " + " << object.Imaginary << "i";
        return os;
    };

    Complex operator + (Complex& op2)
    {
        return Complex(Real + op2.Real, Imaginary + op2.Imaginary);
    };

    Complex operator - (Complex& op2)
    {
        return Complex(Real - op2.Real, Imaginary - op2.Imaginary);
    };

    T Real, Imaginary;
};

void main()
{
    Complex<double> MyComplex(10.0, 20.0);
    Complex<double> MyComplex2(10.0, 20.0);
    cout << MyComplex - MyComplex2 << endl;
}

Offline DoomBringer316

  • Honorable
  • SpongeBob
  • ******
  • Posts: 1,413
    • View Profile
    • http://Meh, Removed.
Re: C++ garbage
« Reply #2 on: December 20, 2006, 09:44:07 am »
#include<iostream>
#include<string>

using namespace std;

class PhoneNumber
{   
    bool Parse()
    {
        string numbers = "1234567890x";
        string temp = "";
        for ( int i = 0; i < Full.length(); i++ )
        {
            for ( int j = 0; j < numbers.length(); j++ )
            {
                if ( Full == numbers[j] )
                {
                    temp += Full;
                    j = -1;
                    i++;
                    if ( i == Full.length() )
                        break;
                }
            }
        }
        Full = temp;
        int xLoc = Full.find('x');
        int acLoc = 0;
        switch ( xLoc )
        {
        case 10:
            acLoc = 3;
            break;
        case 7:
            acLoc = 0;
            break;
        case -1:
            xLoc = Full.length();
            acLoc = 3;
            if ( Full.length() == 7 )
                 acLoc = 0;
            else if ( Full.length() == 10 )
                 acLoc = 3;
            else
            {
                cout << "too long" << endl;
                return false;
            }
            break;
        default:
            cout << "something else" << endl;
            return false;
        }

        for ( int i = 0; i < Full.length(); i++ )
        {
            if ( i < acLoc )
                AreaCode += Full;
            else if ( i < xLoc )
            {
                Number += Full;
                if ( Number.length() == 3 )
                    Number += '-';
            }
            else if ( i != xLoc )
                Extension += Full;
        }

        return true;
    }

public:
    string Full, AreaCode, Number, Extension;
 
    PhoneNumber()
    {
        Full = "";
        AreaCode = "";
        Number = "";
        Extension = "";
    }

    PhoneNumber(string buffer)
    {
        Full = buffer;
        AreaCode = "";
        Number = "";
        Extension = "";
    }

    bool IsValid()
    {
        return Parse();
    }
};

void main()
{
    PhoneNumber MyNumber;

    string input;

    cin >> input;

    MyNumber.Full = input;
    cout << MyNumber.IsValid() << endl;
    cout << "(" << MyNumber.AreaCode << ")" << " " << MyNumber.Number << "x" << MyNumber.Extension << endl;
    cout << MyNumber.Full << endl;
}

Offline VulturEMaN

  • Global Moderator
  • SpongeBob
  • *****
  • Posts: 3,853
  • Gender: Male
  • Dengaku Man xD
    • View Profile
Re: C++ garbage
« Reply #3 on: December 20, 2006, 10:45:08 am »
you never fail to amaze :P

it looks pretty from my java and vb background, and i understand a good bit of it, but ur silly :P

Offline IZ

  • Administrator
  • SpongeBob
  • *****
  • Posts: 7,289
  • Gender: Male
    • View Profile
    • http://www.spongebobcrazy.com
Re: C++ garbage
« Reply #4 on: December 20, 2006, 03:03:10 pm »
No wonder you got a C+! You can't even read it! :)

Chrono

  • Guest
Re: C++ garbage
« Reply #5 on: December 20, 2006, 06:07:00 pm »
I think you just gave my computer AIDS

Offline VulturEMaN

  • Global Moderator
  • SpongeBob
  • *****
  • Posts: 3,853
  • Gender: Male
  • Dengaku Man xD
    • View Profile
Re: C++ garbage
« Reply #6 on: December 20, 2006, 06:39:16 pm »
NOO WHY IS THIS NOT IN TEH BOX!?!?!?!?!

Offline Rocko

  • SpongeBob
  • *****
  • Posts: 1,955
  • Initiative comes to thems that wait.
    • View Profile
Re: C++ garbage
« Reply #7 on: December 20, 2006, 06:48:38 pm »
What is this gibberish?

In order to make it through the world one needs some insanity.

Offline VulturEMaN

  • Global Moderator
  • SpongeBob
  • *****
  • Posts: 3,853
  • Gender: Male
  • Dengaku Man xD
    • View Profile
Re: C++ garbage
« Reply #8 on: December 20, 2006, 07:41:53 pm »
NOO IT NEEDS TO BE IN TEH BOX!!!