Heh, modern C++, if written
correctly, looks strange because it is clear and concise. Almost everything is What Is Written On The Can. The parts that most trip noobs up is idiomatic stuff, like that stream insertion operator to print a card. Even so, just looking at the (one line) body should give a pretty decent hint about its function.
Here is the same thing I posted before, but in C. You will notice that, except for having to implement a random number generator and a shuffle algorithm, it is almost identical in structure to the C++ stuff.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// A card is a number mapped to suit and rank
// suit = value / 13
// rank = value % 13
typedef unsigned char Card;
// C++ defined this for us: in C we have to define it ourselves
int min( int a, int b ) { return (a < b) ? a : b; }
// Here is a bunch of convenient stuff to turn a card into a string
const char* Suit_Name( Card card )
{
const char* ns[] = { "Spades", "Diamonds", "Clubs", "Hearts" };
return ns[ min( card, (Card)52 ) / 13 ];
}
const char* Rank_Name( Card card )
{
const char* ns[] = { "Ace", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
return ns[ min( card, (Card)52 ) % 13 ];
}
// ... and print one to a stream
FILE* print_card( FILE* f, Card card )
{
fprintf( f, "%s of %s", Rank_Name( card ), Suit_Name( card ) );
return f;
}
// Again, C++ provides handily for you when dealing with random
// numbers and shuffling algorithms. In C we must roll our own every time.
// (This is the Knuth-Fisher-Yates shuffle algorithm for our cards)
void swap( Card* a, Card* b ) { Card c = *a; *a = *b; *b = c; }
int random_less_than( int n )
{
int x, N = RAND_MAX / n * n;
do x = rand(); while (x >= N);
return x % n;
}
void shuffle( Card* cards, int n )
{
if (n) while (--n) swap( cards + n, cards + random_less_than( n ) );
}
int main()
{
// Our list of cards
Card cards[ 52 ];
unsigned ncards = 0;
// Add a full deck to our list
while (ncards < 52) cards[ncards] = ncards, ncards++;
// Randomize the list
srand( time( 0 ) );
shuffle( cards, ncards );
// (Heh, changed our mind: only want SEVEN random cards)
ncards = 7;
// After that, simply grab cards until there are none left:
while (ncards)
{
fprintf( print_card( stdout, cards[--ncards] ), "\n" );
}
}
|
Clearly-written C and C++ share some characteristics; the only real changes are idiomatic constructs.
It is worth your time to learn modern C++ and not spend time trying to learn something else as a supposed prerequisite. You don’t need to learn C or Java or anything else to learn good C++. Modern languages like to make expressiveness and syntax work together. /me end soapbox rant
I have a question though for you, regarding the following code |
I actually know a few people from Romania, and from what they describe, higher education there is either hit or miss. You seem to be on the “miss” end of it right now. I feel very sorry you have to put up with the confusion it causes.
That function is poorly named and, admittedly, a little difficult to read, at least in my opinion. It is a
selection sort. There are two loops:
• the outer loop goes from element
i=0 to (
n-1)
every element <
i is sorted
every element ≥
i is not sorted (or
unsorted)
• the inner loop searches all the unsorted elements for the
smallest value
Once the smallest unsorted element is found, we swap it with whatever was at
i (but only if the smallest is not already at
i).
The element at
i is now sorted, so we increment
i (restoring the conditions listed above) and continue with the next iteration of the outer loop.
Pretty please do not take this as a sign of disrespect. |
Heh, I know I have a fiery reputation, but you have done absolutely nothing to show disrespect. What you have demonstrated is an interest in learning and awareness of what is going on around you and ongoing explorations beyond the original topic of this thread. All those are good points. :O)
Hope this helps.
@
jonnin
Use the Extended Mind.
Translate says that shimbare is Romanian for what we would call swap or exchange. ;-)
[edit] Fixed an embarrassing typo (I used “their” instead of “there” <shame>).