Author Topic: [C/C++] Joe vs Ergot: Code wars!  (Read 24176 times)

0 Members and 3 Guests are viewing this topic.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
[C/C++] Joe vs Ergot: Code wars!
« on: September 27, 2005, 07:26:53 pm »
Yeah, me and Ergot are having a little friendly competition on outcoding eachothers projects. We're both novices to C++, so we're pretty equal. Right now, we've both finished exponents in our calculator projects. Anyhow, I submit the next project in my side of the war, the ..dun dun dun.. BINARY CONVERTER!

joe@JoeMomma:~/dev/cpp/helloworld $ g++ -Wno-deprecated binconv.cpp -o binconv && ./binconv
Enter binary digits:
1
0
0
0
0
0
0
1
10000001b = 129


EDIT -
If anyone is interested in learning C++, but doesn't have very much knoledge of the language, feel free to write some small projects. I don't know about the rest of you, but being in a competitive environment (like this) sort of drives me to outpreform the others, and in this case learn C++ faster. So yeah, join up if you feel like it. And Newby, before you write an ubercool Battle.net bot and say you win: don't. =p
« Last Edit: September 27, 2005, 11:05:54 pm by Joe[e2] »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [C++] Joe vs Ergot: Code wars!
« Reply #1 on: September 27, 2005, 07:41:51 pm »
I think Ergot will win.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [C++] Joe vs Ergot: Code wars!
« Reply #2 on: September 27, 2005, 08:40:15 pm »
Thanks. He hasn't written a binary converter yet, so eh? I'm ahead unless he has another project up his sleves.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Ergot

  • 吴立峰 ^_^ !
  • x86
  • Hero Member
  • *****
  • Posts: 3724
  • I steal bandwidth. p_o
    • View Profile
Re: [C++] Joe vs Ergot: Code wars!
« Reply #3 on: September 27, 2005, 08:47:36 pm »
No... First I do everything in C. Second, my main goal is just to take your code and make it better ^_~
Mynd, I wouldn't bet on myself... my attempt at learning VB was something that should be shot, burned, buried, and then nuked ten times.
You can ask Joe how poor one of my projects turned out ;P
« Last Edit: September 27, 2005, 08:49:30 pm by Ergot »
Who gives a damn? I fuck sheep all the time.
And yes, male both ends.  There are a couple lesbians that need a two-ended dildo...My router just refuses to wear a strap-on.
(05:55:03) JoE ThE oDD: omfg good job i got a boner thinkin bout them chinese bitches
(17:54:15) Sidoh: I love cosmetology

Offline Eric

  • Full Member
  • ***
  • Posts: 304
  • I'm new here!
    • View Profile
Re: [C++] Joe vs Ergot: Code wars!
« Reply #4 on: September 27, 2005, 09:19:53 pm »
If Ergot even contemplates coding correctly, he'll have you beaten by a long shot in every language you've ever programmed in, Joe.

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [C++] Joe vs Ergot: Code wars!
« Reply #5 on: September 27, 2005, 09:25:52 pm »
Out of curiosity Joe, when does your converter know to stop taking input?
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Ergot

  • 吴立峰 ^_^ !
  • x86
  • Hero Member
  • *****
  • Posts: 3724
  • I steal bandwidth. p_o
    • View Profile
Re: [C++] Joe vs Ergot: Code wars!
« Reply #6 on: September 27, 2005, 09:44:53 pm »
Wow you guys must really hate Joe, because honestly... he was much better at VB than I will ever be at it ;P
Who gives a damn? I fuck sheep all the time.
And yes, male both ends.  There are a couple lesbians that need a two-ended dildo...My router just refuses to wear a strap-on.
(05:55:03) JoE ThE oDD: omfg good job i got a boner thinkin bout them chinese bitches
(17:54:15) Sidoh: I love cosmetology

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [C++] Joe vs Ergot: Code wars!
« Reply #7 on: September 27, 2005, 09:51:41 pm »
Out of curiosity Joe, when does your converter know to stop taking input?
Looks like he limited it to 8 bytes.

Offline Ergot

  • 吴立峰 ^_^ !
  • x86
  • Hero Member
  • *****
  • Posts: 3724
  • I steal bandwidth. p_o
    • View Profile
Re: [C++] Joe vs Ergot: Code wars!
« Reply #8 on: September 27, 2005, 10:16:22 pm »
I'm guessing he just used cin 8 times.
Who gives a damn? I fuck sheep all the time.
And yes, male both ends.  There are a couple lesbians that need a two-ended dildo...My router just refuses to wear a strap-on.
(05:55:03) JoE ThE oDD: omfg good job i got a boner thinkin bout them chinese bitches
(17:54:15) Sidoh: I love cosmetology

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [C++] Joe vs Ergot: Code wars!
« Reply #9 on: September 27, 2005, 10:53:11 pm »
Yeah, I defined a b c d e f g and h as integers and cin'd them. Its a sloppy++ way of doing it, but it works.

After that, I go through them backwards, perform an if statment on them (if = 1) and increase int accum accordingly, then I print them it to the screen.


I optimized the living daylights out of it, and its now about twice as short.

joe@JoeMomma:~/dev/cpp/helloworld $ g++ -Wno-deprecated binconv.cpp -o binconv && ./binconv
Enter binary digits:
1
1
1
1
1
1
1
1
Result: 255


// C++ binary converter
// Author: Joe[e2]
//
// TODO:
// Input the binary values as a string instead of integers, so that it can be done on one line.

#include <iostream.h>
#include <stdio.h>

int main() {
  int i;
  int accum = 0;

  printf("Enter binary digits:\n");
  cin >> i; if(i = 1) { accum = accum + 128; }
  cin >> i; if(i = 1) { accum = accum +  64; }
  cin >> i; if(i = 1) { accum = accum +  32; }
  cin >> i; if(i = 1) { accum = accum +  16; }
  cin >> i; if(i = 1) { accum = accum +   8; }
  cin >> i; if(i = 1) { accum = accum +   4; }
  cin >> i; if(i = 1) { accum = accum +   2; }
  cin >> i; if(i = 1) { accum = accum +   1; }

  cout << "Result: " << accum << "\n";
  return 0;
}


EDIT -
Oops I did it again!
joe@JoeMomma:~/dev/cpp/helloworld $ g++ -Wno-deprecated binconv2.cpp -o binconv2 && ./binconv2
Enter binary digits:
10010010
10010010b = 146
joe@JoeMomma:~/dev/cpp/helloworld $ g++ -Wno-deprecated binconv2.cpp -o binconv2 && ./binconv2
Enter binary digits:
11111111
11111111b = 255


// C++ binary converter
// Author: Joe[e2]

#include <iostream.h>
#include <stdio.h>

int main() {
  char binary[8];
  int accum = 0;
  int add = 128;

  printf("Enter binary digits:\n");
  cin >> binary;
  for(int i = 0; i < 8; i++) {
    if(binary == 0x31) { accum = accum + add; }
    add = add / 2;
  }

  cout << binary << "b = " << accum << "\n";
  return 0;
}


EDIT -
Hm, in the first code I posted, I suppose it would always return 255, due to the fact I'm testing if 1 can be asigned to i, instead of if i == 1. Oh well, its fixed in the second one.
« Last Edit: September 27, 2005, 11:29:11 pm by Joe[e2] »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [C/C++] Joe vs Ergot: Code wars!
« Reply #10 on: September 27, 2005, 11:50:24 pm »
Now, a decimal->binary converter! Yay!

joe@JoeMomma:~/dev/cpp/helloworld $ g++ -Wno-deprecated binconv-rev.cpp -o binconv-rev && ./binconv-rev
Enter decimal number:
99
99 = 01100011¼ùÿ¿¶÷@b
joe@JoeMomma:~/dev/cpp/helloworld $ g++ -Wno-deprecated binconv.cpp -o binconv && ./binconv
Enter binary digits:
01100011
01100011b = 99


// C++ decimal->binary converter
// Author: Joe[e2]

#include <iostream.h>
#include <stdio.h>

int main() {
  char binary[8];
  int input;
  int accum = 0;
  int sub = 128;

  printf("Enter decimal number:\n");
  cin >> input; accum = input;
  for(int i = 0; i < 8; i++) {
    if(accum >= sub) {
      accum = accum - sub;
      binary = 0x31;
    }else{
      binary = 0x30;
    }
    sub = sub / 2;
  }

  cout << input << " = " << binary << "b\n";
  return 0;
}
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Ergot

  • 吴立峰 ^_^ !
  • x86
  • Hero Member
  • *****
  • Posts: 3724
  • I steal bandwidth. p_o
    • View Profile
Re: [C/C++] Joe vs Ergot: Code wars!
« Reply #11 on: September 28, 2005, 12:36:13 am »
Ahh I never thought of it like that (dividing by two :()... I gave this like half an hours worth of thought... then I had to fix the sink :(
There was bugs I wanted work out (Like making sure numbers other than 0's and 1's wouldn't mess up the program) but meh... I'll do it later... maybe.
Proof that Joe > Me ;D
My crappy code:
Code: [Select]
#include <stdio.h>

int a;

int bintodec(a)
{
        int dec;
        dec = 0;
        while (a != 0)
        {
                if (a > 11111111)
                        {
                        a = a - a;
                        dec = 256;
                        }
                else if (a >= 10000000)
                        {
                        dec = dec + 128;
                        a = a - 10000000;
                        }
                else if (a >= 1000000)
                        {
                        dec = dec + 64;
                        a = a - 1000000;
                        }
                else if (a >= 100000)
                        {
                        dec = dec + 32;
                        a = a - 100000;
                        }
                else if (a >= 10000)
                        {
                        dec = dec + 16;
                        a = a - 10000;
                        }
                else if (a >= 1000)
                        {
                        dec = dec + 8;
                        a = a - 1000;
                        }
                else if (a >= 100)
                        {
                        dec = dec + 4;
                        a = a -100;
                        }
                else if (a >= 10)
                        {
                        dec = dec + 2;
                        a = a - 10;
                        }
                else if (a >= 1)
                        {
                        dec = dec + 1;
                        a = a - 1;
                        }
                }
        return dec;
}

int main()
{
        int bin, result;
        printf("Enter an 8-bit binary number: \n");
        scanf("%d",&bin);
        result = bintodec(bin);
        if (result >= 256)
                printf("8 bit binary numbers only.\n");
        else
        printf("%d in decimal is %d.\n",bin, result);
        return 0;
}

« Last Edit: September 28, 2005, 12:42:22 am by Ergot »
Who gives a damn? I fuck sheep all the time.
And yes, male both ends.  There are a couple lesbians that need a two-ended dildo...My router just refuses to wear a strap-on.
(05:55:03) JoE ThE oDD: omfg good job i got a boner thinkin bout them chinese bitches
(17:54:15) Sidoh: I love cosmetology

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [C/C++] Joe vs Ergot: Code wars!
« Reply #12 on: September 28, 2005, 07:41:30 am »
You're going to want to check if(num >= 2) instead, because your method can return false negatives.

EDIT -
Proof of concept, anything except the first bit is a 2. =p

Anyhow, my shellscript skills rock.

joe@JoeMomma:~/dev/cpp/helloworld $ ./build && ls bin
Building files..
binconv.cpp
binconv-rev.cpp
Math.cpp
DivTable.cpp
functest.cpp
MultTable.cpp
MultTable2.cpp
Finished!
binconv  binconv-rev  DivTable  functest  Math  MultTable  MultTable2


echo Building files..
echo binconv.cpp
g++ -Wno-deprecated binconv.cpp -o bin/binconv
echo binconv-rev.cpp
g++ -Wno-deprecated binconv-rev.cpp -o bin/binconv-rev
echo Math.cpp
g++ -Wno-deprecated Math.cpp -o bin/Math
echo DivTable.cpp
g++ -Wno-deprecated DivTable.cpp -o bin/DivTable
echo functest.cpp
g++ -Wno-deprecated functest.cpp -o bin/functest
echo MultTable.cpp
g++ -Wno-deprecated MultTable.cpp -o bin/MultTable
echo MultTable2.cpp
g++ -Wno-deprecated MultTable2.cpp -o bin/MultTable2
echo Finished!

# -rw-r--r--    1 joe      joe           379 2005-09-27 23:01 binconv.cpp
# -rw-r--r--    1 joe      joe           471 2005-09-28 06:59 binconv-rev.cpp
# -rw-r--r--    1 joe      joe           912 2005-09-26 18:54 calculator.cpp
# -rw-r--r--    1 joe      joe           585 2005-09-25 21:31 DivTable.cpp
# -rw-r--r--    1 joe      joe           226 2005-09-27 22:16 functest.cpp
# -rw-r--r--    1 joe      joe           226 2005-09-24 18:50 Math.cpp
# -rw-r--r--    1 joe      joe           584 2005-09-25 00:38 MultTable2.cpp
# -rw-r--r--    1 joe      joe           698 2005-09-24 20:51 MultTable.cpp
« Last Edit: September 28, 2005, 07:59:19 am by Joe[e2] »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [C/C++] Joe vs Ergot: Code wars!
« Reply #13 on: September 28, 2005, 12:43:16 pm »
This was my solution:
Code: [Select]
#include <iostream>

int main(void);
typedef unsigned long int DWORD;
typedef unsigned char BYTE;
using namespace std;

int main()
{
    DWORD numBits, curBit, inBit;
    DWORD val;
char empty;
    numBits = 0;
    curBit = 0;
    val = 0;
    do
    {
         cout << "Enter the number of bits (1 to 32) that you will be entering, from MSB to LSB." << endl;
         cin >> numBits;
    } while (numBits < 1 || numBits > 32);

    cout << "Accepting " << numBits << " bits:" << endl;
    for (curBit = numBits; curBit > 0; curBit--)
    {
        inBit = 2;

        while (inBit > 1)
        {
            cin >> inBit;

            if (inBit < 2)
break;
            cout << "Error: invalid bit (specify 0 or 1)." << endl;
        }

        if (inBit)
            val |= (inBit << (curBit - 1));
    }

    cout << "You entered: " << val << "." << endl;

cin >> empty;

return 0;
}

Enter the number of bits (1 to 32) that you will be entering, from MSB to LSB.
20
Accepting 20 bits:
0
1
1
0
0
1
1
1
1
0
0
0
1
0
1
1
0
1
0
0
You entered: 424116.


This was the decimal to binary converter:
Code: [Select]
#include <iostream>

int main(void);
typedef unsigned long int DWORD;
using namespace std;

int main()
{
    DWORD val;
char empty;
int i;
cout << "Enter a value from 0 to 4294967295:" << endl;
cin >> val;

cout << "You entered (in binary): ";
for (i = 31; i >= 0; i--)
{
if (val & (1 << i))
cout << "1";
else
cout << "0";

if (i % 8 == 0)
cout << " ";
}
cout << "b" << endl;

cin >> empty;
}

Enter a value from 0 to 4294967295:
884376993
You entered (in binary): 00110100 10110110 10000101 10100001 b


Both compile with Visual C++ 2003.

Joe, some comments (Ergot....  you need more help than comments ;)):

1.) Don't #include <iostream.h>, #include <iostream>.  That's standard C++.
2.) Why are you using ASCII literals (0x30, 0x31) as characters?  '0' and '1' work fine.
3.) Why are you using printf() in a C++ program?  Use cout.  (Unless you're formatting strings).
4.) accum = accum - sub; == accum -= sub;
5.) You shouldn't put programming statements on the same line as I/O (IMO), for example: cin >> accum; if (accum) dosomething(); -- I think that's ugly.
6.) You should have come up with a better way to do what you did in the first program.  What if your project manager wanted you to convert 32-bit numbers?  Can you imagine typing out all those literals?  Or even macros? [edit]Hrm, your third go was better, but you still could have done better with it.  Why not just cin to an int and test for 1 or 0?  (Note that my program tested for illegal values also -- what would yours do if I put in a 2 or something?)[/edit]

And DON'T make the standard I'm-Joe-I'm-Lazy argument.  Part of what likely discourages you from completing your projects is that you get frustrated when you have to go back and correct things.  Be un-lazy and do it right the first time and you don't need to spend time being consistent (like upgrading your code to take a 32-bit number).

Ergot, I wasn't trying to pick on you.  It seems like you just need to experience better what the computer is capable of doing, because it looks like you're taking the long way to get where you're going, but only because you don't know any better.
« Last Edit: September 28, 2005, 01:04:21 pm by MyndFyre[x86] »
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [C/C++] Joe vs Ergot: Code wars!
« Reply #14 on: September 28, 2005, 03:43:31 pm »
2.) Why are you using ASCII literals (0x30, 0x31) as characters?  '0' and '1' work fine.

Yes, for God sakes, anybody who does that should be shot dead. 

When I mark assignments, I see people do it all the time.  I'm not really sure whether they think they're being clever, or if they're just as dumb as a post.  Either way, it's a terribly annoying thing to do for _anybody_ reading the code.  Even somebody like me who knows the common ascii code will be slowed down.