A c ++ Licensing Program

Source: Internet
Author: User
A c ++ Licensing Program-general Linux technology-Linux programming and kernel information. For details, see the following. Enter "bd-h" for help.

The following is the source code (for the code for processing command line parameters, refer to vim ):

# Include
# Include
# Include
# Include
Using namespace std;

# Include
# Include
# Include

Static char program_name [] = "bd ";
# Define THE_VERSION "0.1"

Namespace info {
Enum {WEST = 0, NORTH = 1, EAST = 2, SOUTH = 3 ,};
};
Static char * deno [] = {"Spade", "Heart", "Diamond", "Cotton ",};
Static char card_symbol [] = "23456789 TJQKA ";

// P-pointer to argument
// Idx-index in argument
// Default value
Static int
Get_number_arg (char * p, int def)
{
If (isdigit (* p ))
{
Def = atoi (p );
}
Return def;
}

Static void
Print_help ()
{
Cout <"Usage:" <cout <"Bridge Dealer" <cout <"Example:" <cout <"-p number of players, value ranges from 1 to 4 "<cout <" \ "" <cout <"-v, -- version print version information and exit" <cout <"-h, -- help display this help and exit "<cout <exit (0 );
}

Static void
Print_version ()
{
Cout <# define BS_UNKNOWN_OPTION 0
"Too policarguments ",
# Define BS_TOO_MANY_ARGS 1
"Argument missing after ",
# Define BS_ARG_MISSING 2
"Garbage after option ",
# Define BS_GARBAGE 3
"Too commands extra commands ",
# Define BS_EXTRA_CMD 4
"Invalid argument ",
# Define BS_INVALID_ARG 5
};
// Error message routines --------------- end

Int
Main (int argc, char ** argv)
{
Int argv_idx = 1;/* active option letter is argv [0] [argv_idx] */
Int players = 1;
Int number_of_deals = 1;
Bool want_argument = false;

While (argc> 0)
{
If (argv [0] [0] = '-')
{
Char c = argv [0] [argv_idx ++];
Switch (c)
{
Case 0:
// Do nothing
Argv_idx =-1;
Break;
Case '-': // "--" don't take any more options
If (strcmp (argv [0] + argv_idx, "version") = 0 ){
Print_version ();
}
Else if (strcmp (argv [0] + argv_idx, "help") = 0 ){
Print_help ();
}
Argv_idx =-1;
Break;
Case 'H ':
Print_help ();
Break;
Case 'V ':
Print_version ();
Break;
Case 'p': // 1, 2, 3, 4 players, default 1
If (argv [0] [argv_idx]) {// "-p {tag }"
Players = get_number_arg (argv [0] + argv_idx, 1 );
Argv_idx =-1;
}
Else {// "-p {tag }"
Want_argument = true;
}
Break;
Case 'N': // number of deals, default 1
If (argv [0] [argv_idx]) {// "-n {tag }"
Number_of_deals = get_number_arg (argv [0] + argv_idx, 1 );
If (number_of_deals <= 0) number_of_deals = 1;
Argv_idx =-1;
}
Else {// "-n {tag }"
Want_argument = true;
}
Break;
Default:
Break;
}


// Handle options with argument
If (want_argument)
{
If (argv [0] [argv_idx]) {
Cerr <exit (1 );
}

-- Argc;
If (argc <1 ){
Cerr <exit (1 );
}
++ Argv;
Argv_idx =-1;

Switch (c)
{
Case 'p ':
Players = get_number_arg (argv [0], 1 );
Argv_idx =-1;
Break;
Case 'N ':
Number_of_deals = get_number_arg (argv [0], 1 );
If (number_of_deals <= 0) number_of_deals = 1;
Argv_idx =-1;
Break;
Default:
; // Impossible
}
}

}
Else {
Argv_idx =-1; // do nothing
}
// If there are no more letters after the current "-", go to next
// Argument. argv_idx is set to-1 when the current argument is to be
// Skipped.
If (argv_idx <= 0 argv [0] [argv_idx] = 0)
{
-- Argc;
++ Argv;
Argv_idx = 1;
}
}
// Init
Srand (time (0 ));

// Original card
// Value: spade, 0-12; heart, 13-25; diamond 26-38; cotton 39-51;
// Array index: west, 0-12; north, 13-25; east 26-38; south 39-51;
Const int dim = 52;
Int cards [dim];

For (int I = 0; I {
Cards = I;
}
Vector h (dim );
H. assign (cards, cards + dim );

// Redeal
Bool need_id = number_of_deals> 1;
For (int k = 0; k {
Random_shuffle (h. begin (), h. end ());
Sort (h. begin () + info: WEST * 13, h. begin () + info: WEST * 13 + 13 );
Sort (h. begin () + info: NORTH * 13, h. begin () + info: NORTH * 13 + 13 );
Sort (h. begin () + info: EAST * 13, h. begin () + info: EAST * 13 + 13 );
Sort (h. begin () + info: SOUTH * 13, h. begin () + info: SOUTH * 13 + 13 );

// Dump
If (need_id ){
Cout <"ID:" <}
Switch (players)
{
Case 1:
Dump_one_hand (h. begin () + info: SOUTH * 13 );
Break;
Case 2:
Dump_two_hands (h. begin () + info: EAST * 13, h. begin () + info: WEST * 13, 26 );
Break;
Case 3:
Dump_one_hand (h. begin () + info: NORTH * 13, 26 );
Dump_one_hand (h. begin () + info: EAST * 13 );
Dump_one_hand (h. begin () + info: SOUTH * 13, 26 );
Break;
Case 4:
Dump_one_hand (h. begin () + info: NORTH * 13, 26 );
Dump_two_hands (h. begin () + info: EAST * 13, h. begin () + info: WEST * 13, 26 );
Dump_one_hand (h. begin () + info: SOUTH * 13, 26 );
Break;
Default:
; // Impossible
}
Cout <}
Return 0;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.