📄 c++b.txt
字号:
Choose slot of deck randomly
Place card number in chosen slot of deck
"Find card numberin deck array and printface and suit of card" 可以展开成:
For each slot of the deck array
If slot contains card number
Print the face and suit of the card
合在一起得到第三步完善结果为:
Initialize the suit array
Initialize the face array
Initialize the deck array
For each of the 52 cards
Choose slot of deck randomly
While slot of deck has been previously chosen
Choose slot of deck randomly
Place card number in chosen slot of deck
For each of the 52 cards
For each slot of deck array
If slot contains desired card number
Print the face and suit of the card
这样就完成了完善过程。注意,如果将洗牌与发牌算法组合成每张牌在放到牌堆上时进行发牌,则这个程序能更加有效。我们选择分别编程这些操作,因为通常是先洗后发,而不是边洗边发。
图5.24显示了洗牌与发牌程序,图5.25显示了示例执行结果。注意函数deal中使用的输出格式:
cout << setw( 5 ) << setiosflags( ios::right )
<< wFace[ column ] << "of"
<< setw( 8 ) << setiosflags( ios::left )
<< wSuit[ row ]
<< (card % 2 ==0? '\n': '\t');
上述输出语句使牌的面值在5个字符的域中右对齐输出,而花色在8个字符的域中左对齐输出。输出打印成两列格式。如果输出的牌在第一列,则在后面输出一个制表符,移到第二列,否则输出换行符。
1 // Fig. 5.24: fig05_24.cpp
2 // Card shuffling and dealing program
3 #include <iostream.h>
4 #include <iomanip.h>
5 #include <stdlib.h>
6 #include <time.h>
7
8 void shuffle( iht [][ 13 ] );
9 void deal( const int [][ 13 ], const char *[], const char *[] );
10
11 int main()
12 {
13 const char * suit[ 4 ] =
14 { "Hearts", "Diamonds", "Clubs", "Spades" };
15 const char * face[ 13 ] =
16 { "Ace", "Deuce", "Three", "Four",
17 "Five", "Six", "Seven", "Eight",
18 "Nine", "Ten", "Jack", "Queen", "King" };
19 int deck[ 4 ][ 13 ] = { 0 } ;
20
21 srand( time( 0 ) );
22
23 shuffle( deck );
24 deal( deck, face, suit ):
25
26 return 0;
27 }
28
29 void shuffle( int wDeck[ ][ 13 ] )
30 {
31 int row, column;
32
33 for(int card = 1; card <= 52; card++ ) {
34 do{
35 row = rand() % 4;
36 column = rand() % 13;
37 } while( wDeck[ row ][ column ] != 0 );
38
39 wDeck[ row ][ columo ] = card;
40 }
41 }
42
43 void deal( const int wDeck[ ][ 13 ], const char * wFace[ ] ,
44 const char *wSuit[] )
45 {
46 for (int card = 1; card <= 52; card++ )
47
48 for ( int row = 0; row <= 3; row++ )
49
50 for ( int column = 0; column <= 12; column++ )
51
52 if ( wDeck[ row][ column ] = card )
53 cout << setw(8) << setiosflags( ios::right )
54 << wFace[ column ] <<" of"
55 << setw( 8 ) << setiosflags( ios::left )
56 << wSuit[ row ]
57 << ( card % 2 == 0 ? ,'\n' : '\t' );
58 }
输出结果:
Six of Clubs Seven of Diamonds
Ace of Spades Ace of Diamonds
Ace of Hearts Queen of Diamonds
Queen of Clubs Seven of Hearts
Ten of Hearts Deuce of Clubs
Ten of Spades Three of Spades
Ten of Diamonds Four of Spades
Four of Diamonds Ten of Clubs
Six of Diamonds six of Spades
Eight of Hearts Three of Diamonds
Nine of Hearts Three of Hearts
Deuce of Spades six of Hearts
Five of Clubs Eight of Clubs
Deuce of Diamonds Eight of Spades
Five of Spades King of Clubs
King of Diamonds Jack of Spades
Deuce of Hearts Queen of Hearts
Ace of Clubs King of Spades
Three of Clubs King of Hearts
Nine of Clubs Nine of Spades
Four of Hearts Queen of Spades
Eight of Diamonds Nine of Diamonds
Jack of Diamonds Seven of Clubs
Five of Hearts Five of Diamonds
Four of Clubs Jack of Hearts
Jack of Clubs Seven of Spades
图5.25 洗牌与发牌程序的执行结果
发牌算法中有个缺点,一旦找到匹配之后,即使第一次就找到,两个内层的for结构仍然继续搜索deck中的其余元素。练习中要纠正这个缺点。
5.11 函数指针
函数指针包含函数在内存中的地址。第4章介绍了数组名实际上是数组中第一个元素的内存地址。同样,函数名实际上是执行函数任务的代码在内存中的开始地址。函数指针可以传人函数、从函数返回、存放在数组中和赋给其他的函数指针。
要演示如何使用函数指针,我们修改图5.15的冒泡排序程序,变成图5.26的程序。新程序包括main和函数bubble、swap、ascending和descending。函数bubbleSort接收ascending或descending函数的函数指针参数以及一个整型数组和数组长度。程序提示用户选择按升序或降序排序。如果用户输入1,则向函数bubble传递ascending函数的指针,使数组按升序排列。如果用户输入2,则向函数bubble传递descending函数的指针,使数组按降序排列。图5.27显示了示例的执行结果。
1 // Fig. 5.26: fig0526.cpp
2 // Multipurpose sorting program using function pointers
3 #include <iostream.h>
4 #include <iomanip.h>
5
6 void bubble( int [], const int, int (*)( int, int ) );
7 iht ascending( int, int );
8 int descending( int, int );
9
10 int main()
11 {
12 const int arraySize = 10;
13 int order,
14 counter,
15 a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
16
17 cout << "Enter 1 to sort in ascending order,\n"
18 << "Enter 2 to sort descending order: ";
19 cin >> order;
20 cout << "\nData items in original order\n";
21
22 for ( counter = 0; counter < arraySize; counter++ )
23 cout << setw( 4 ) << a[ counter ];
24
25 if ( order == 1 ) {
26 bubble( a, arraySize, ascending );
27 cout << "\nData items in ascending order\n";
29 else {
30 bubble( a, arraySize, descending );
31 cout << "\nData items in descending order\n";
32 }
33
34 for ( counter = 0; coun er < arraySize; counter++ )
35 cout << setw( 4 )<< a[ counter ]
36
37 cout << endl;
38 return 0;
39 }
40
41 void bubble( int work[ ], const int size,
42 int (*compare)( int, int) )
43 {
44 void swap( int *, int* );
45
46 for ( int pass = 1; pass < size; pass++ )
47
48 for ( int count = 0; count < size - 1; count++ )
49
50 if( (*compare)( work[ count ], work[count + 1 ] ) )
51 swap( &work[ count ], &work[ count + 1 ] );
52 }
53
54 void swap( int *element1Ptr, int *element2Ptr )
55 {
56 int temp;
57
58 temp = *element1Ptr;
59 *element1Ptr = *element2Ptr;
60 *element2Ptr = temp;
65 return b < a; // swap if b is less than a
66 }
67
68 int descending( int a, int b )
69 {
70 return b > a; // swap if b is greater than a
71 }
图5.26 使用函数指针的多用途排序程序
输出结果:
Enter 1 to sort in ascending order,
Enter 2 to sort in descending order:1
Data items in original order
2 6 4 8 1O 12 89 68 45 37
Data items in ascending order
2 4 6 8 1O 12 37 45 68 89
Enter 1 to sort in ascendinq order,
En(e[ 2 to sort in descending order: 1
Data items in Oriqinal order
2 6 4 8 10 12 89 68 45 37
Dats items in ascendinQ order
89 68 45 37 12 10 9 6 4 2
图5.27 使用函数指针的多用途排序程序的执行结果
注意这里只包括类型,程序员可以加上名称.但参数名只用于程序中的说明,编译器将其忽略。
if语句中调用传人bubble的函数,如下所示:
if(( *compare )( work[ count ], work[ count + 1 ] ))
就像复引用变量指针可以访问变量值一样,复引用函数指针可以执行这个函数。
也可以不复引用指针而调用函数,如下所示:
if ( compare( work[ count ],work[ count + 1 ] ) )
直接用指针作为函数名。我们更愿意使用第一种通过指针调用函数的方法,因为它显式说明compare是函数指针,通过复引用指针而调用这个函数。第二种通过指针调用函数的方法使compare好像是个实际函数。程序用户可能搞糊涂,想看看compare函数的定义却怎么也找不到。
函数指针的一个用法是建立菜单驱动系统,提示用户从菜单选择一个选项(例如从1到5)。每个选项由不同函数提供服务,每个函数的指针存放在函数指针数组中。用
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -