1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > C语言程序设计现代方法-第二版-第10章练习题

C语言程序设计现代方法-第二版-第10章练习题

时间:2019-01-25 21:20:50

相关推荐

C语言程序设计现代方法-第二版-第10章练习题

1.修改10.2节的栈示例使它存储的是字符而不是整数.接下来,增加main函数,用来要求用户输入一串花括号,然后指出它们之间的嵌套是否正确:

```/*Enter parenteses and /or braces:((){}{()}}Parenteses/braces are nested properly*/#include<stdio.h>#include<stdbool.h>//bool类型需要的头文件#include<stdlib.h>//exit函数需要的头文件#define STACK_SIZE 5//数组长度我故意搞小一点,这样比较容易看到溢出的情况int top = 0;char contents[STACK_SIZE];void stack_underflow() {exit(EXIT_FAILURE);}//这个是数组长度过短void stack_overflow() {printf("Stack overflow");exit(EXIT_FAILURE);//一个函数(EXIT_FAILURE 是返回错误的值(非正常返回(1)),SUCCESSFUL是返回正常的值(0))}//这个是数组的长度过长了,所有输出数组的长度过长void make_empty(void) {top = 0;}//这个是把top重新置零bool is_empty(void) {return top == 0;}//判断数组是不是空的bool is_full(void) {return top == STACK_SIZE;}//判断数组的是不是满的void push(char i) {if (is_full())//如果在压之前数组在压之前就满了stack_overflow();elsecontents[top++] = i;}//把元素压进来char pop(void) {if (is_empty())stack_underflow();//如果在弹出去前数组就是空的elsereturn contents[--top];//把数组弹出去}//这里用--top的原因是++top回让top的值比实际存储的top大一位int main() {int a;printf("Enter parenteses and /or brances:");while ((a = getchar()) != '\n') {//常用的表达式if (a == '{' || a == '[')push(a);//这个是压数组if (a == '}') {if (pop() == '}');elsetop++;//这个是两种情况,如果不匹配,top就要加回去,因为top不管匹配不匹配,top都要自减}if (a == ']')if (pop() == ']');elsetop++;}if (a == '\n'&&is_empty())//条件判断printf("Parenteses/braces are nested properly");else printf("Parenteses/braces are not nested properly");return 0;}```

2 修改10.5节的poker.c程序,把数组num_in_rank和数组num_in_suit 移到main函数中.main函数将把这两个数组作为实际的参数传递给read_cards函数,和analyze_hand函数.

#include<stdio.h>#include<stdlib.h>#include<stdbool.h>//C99only#define NUM_RANKS 13#define NUM_SUITS 4#define NUM_CARDS 5bool straight, flush, four, three;int pairs;void read_cards(int a[],int b[]);void analy_hands(int a[],int b[]);void print_result(void);int main() {int num_in_rank[NUM_RANKS];int num_in_suit[NUM_SUITS];//改动的部分,这两个变量变为全局变量for (;;) {read_cards(num_in_rank,num_in_suit);analy_hands(num_in_rank, num_in_suit);print_result();}return 0;}void read_cards(int num_in_rank[NUM_RANKS],int num_in_suit[NUM_SUITS]) {//改动的部分,这两个做参数传递bool card_exists[NUM_RANKS][NUM_CARDS];char ch, rank_ch, suit_ch;int rank, suit;bool bad_card;int cards_read = 0;for (rank = 0; rank < NUM_RANKS; rank++) {num_in_rank[rank] = 0;for (suit = 0; suit < NUM_SUITS; suit++)card_exists[rank][suit] = false;}for (suit = 0; suit < NUM_SUITS; suit++)num_in_suit[suit] = 0;while (cards_read < NUM_CARDS) {bad_card = false;printf("Enter a card: ");rank_ch = getchar();switch (rank_ch){case '0':exit(EXIT_SUCCESS);case '2':rank = 0; break;case '3':rank = 1; break;case '4':rank = 2; break;case '5':rank = 3; break;case '6':rank = 4; break;case '7':rank = 5; break;case '8':rank = 6; break;case '9':rank = 7; break;case 't':case 'T':rank = 8; break;case 'j':case 'J':rank = 9; break;case 'q':case 'Q':rank = 10; break;case 'k':case 'K':rank = 11; break;case 'a':case 'A':rank = 12; break;default:bad_card = true;}suit_ch = getchar();switch (suit_ch) {case 'c':case 'C':suit = 0; break;case 'd':case 'D':suit = 1; break;case 'h':case 'H':suit = 2; break;case 's':case 'S':suit = 3; break;default: bad_card = true;}while ((ch = getchar()) != '\n')if (ch != ' ')bad_card = true;if (bad_card)printf("Bad card; ignored.\n");else if (card_exists[rank][suit])printf("Duplicats card;ignored .\n");else {num_in_rank[rank]++;num_in_suit[suit]++;card_exists[rank][suit] = true;cards_read++;}}}void analy_hands(int num_in_rank[NUM_RANKS], int num_in_suit[NUM_SUITS]) {//改动的部分,这两个做参数传递int num_consec = 0;int rank, suit;straight = false;flush = false;three = false;pairs = 0;for (suit = 0; suit < NUM_SUITS; suit++)if (num_in_suit[suit] == NUM_CARDS)flush = true;rank = 0;while (num_in_rank[rank] == 0)rank++;for (; rank < NUM_RANKS&&num_in_rank[rank]>0; rank++)num_consec++;if (num_consec == NUM_CARDS) {straight = true;return;}for (rank = 0; rank < NUM_RANKS; rank++) {if (num_in_rank[rank] == 4)four = true;if (num_in_rank[rank] == 3)three = true;if (num_in_rank[rank] == 2)pairs++;}}void print_result(void) {if (straight&&flush)printf("Stragight flush");else if (four)printf("Four of a kind ");else if (three&&pairs == 1)printf("Full of a kind");else if (flush)printf("Flush");else if (straight)printf("Stragight");else if (three)printf("Three of a kind");else if (pairs == 2)printf("Two pairs");else if (pairs == 1)printf("Pairs");elseprintf("High card");printf("\n\n");}

3 把数组num_in_rank,num_in_suit和card_exists从原来的函数中去掉,程序改为5*2的数组来存储牌,数组的每一行表示一张牌,例如,如果数组名为hand,则hand[0][0]存储第一个张牌的等级,hand[0][1]存储第一张牌的花色

#include<stdio.h>#include<stdlib.h>#include<stdbool.h>//C99only#define NUM_RANKS 13#define NUM_SUITS 4#define NUM_CARDS 5int cards[5][2] = {0 };bool straight, flush, four, three;int pairs;void read_cards();void analy_hands();void print_result(void);int main() {for (;;) {read_cards();analy_hands();print_result();}return 0;}void read_cards() {char rank_ch, suit_ch;int rank, suit;bool bad_card;int cards_read = 0;while (cards_read < NUM_CARDS) {bad_card = false;printf("Enter a card: ");scanf(" ");rank_ch = getchar();suit_ch = getchar();switch (rank_ch) {case '0':exit(EXIT_SUCCESS);case '2':rank = 0; break;case '3':rank = 1; break;case '4':rank = 2; break;case '5':rank = 3; break;case '6':rank = 4; break;case '7':rank = 5; break;case '8':rank = 6; break;case '9':rank = 7; break;case 't':case 'T':rank = 8; break;case 'j':case 'J':rank = 9; break;case 'q':case 'Q':rank = 10; break;case 'k':case 'K':rank = 11; break;case 'a':case 'A':rank = 12; break;default:bad_card = true;}switch (suit_ch) {case 'c':case 'C':suit = 0; break;case 'd':case 'D':suit = 1; break;case 'h':case 'H':suit = 2; break;case 's':case 'S':suit = 3; break;default: bad_card = true;}if (bad_card)printf("Bad card; ignored.\n");else {cards[cards_read][0] = rank;//给等级赋值cards[cards_read][1] = suit;//给花色赋值for (int i = 0; i < cards_read; i++) {if (cards[cards_read][1] == cards[i][1] && cards[cards_read][0] == cards[i][0]) {printf("Duplicats card;ignored .\n");cards_read--;break;}//检查有没有重复的,有重复的就不算else;}cards_read++;//循环的条件项}}}void analy_hands() {//改动的部分,这两个做参数传递int num_consec = 0;int rank = 0;straight = false;flush = false;three = false;pairs = 0;if (cards[0][1] == cards[1][1] && cards[1][1] == cards[2][1] && cards[2][1] == cards[3][1] && cards[3][1] == cards[4][1]) {flush = true;//判断同花}for (int i = 0; i < NUM_CARDS - 1; i++) {for (int j = i + 1; j < NUM_CARDS; j++)if (cards[i][0] > cards[j][0]) {int t = cards[i][0];cards[i][0] = cards[j][0];cards[j][0] = t;t = cards[i][1];cards[i][1] = cards[j][1];cards[j][1] = t;}}//这个是将牌的大小进行排序,大的放在后面,小的放在前面for (int i = 0; i < NUM_CARDS; i++) {for (int j = i + 1; j < NUM_CARDS; j++) {if (cards[i][0] == cards[j][0])rank++;}if (rank == 2) {three = true;}else if (rank == 3) {four = true;}else if (rank == 1)pairs++;else;rank = 0;}//判断牌的样式for (int i = 0; i < NUM_CARDS - 1; i++) {if (cards[i][0] + 1 == cards[i + 1][0]) {if (i == NUM_CARDS - 2)straight = true;else;}elsebreak;}}void print_result(void) {if (straight&&flush)printf("Stragight flush");else if (four)printf("Four of a kind ");else if (three&&pairs == 1)printf("Full of a kind");else if (flush)printf("Flush");else if (straight)printf("Stragight");else if (three)printf("Three of a kind");else if (pairs == 2)printf("Two pairs");else if (pairs == 1)printf("Pairs");elseprintf("High card");printf("\n\n");}

4 修改10.5节的poker.c程序,使其能识别牌的另一种类型---- 同花大顺"(同花色的A,K,Q,J和10),同花大顺的级别高于其他所有的类型

#include<stdio.h>#include<stdlib.h>#include<stdbool.h>//C99only#define NUM_RANKS 13#define NUM_SUITS 4#define NUM_CARDS 5int num_in_rank[NUM_RANKS];int num_in_suit[NUM_SUITS];bool straight, flush, four, three, flowers_dashu;int pairs;void read_cards();void analy_hands();void print_result(void);int main() {for (;;) {read_cards();analy_hands();print_result();}return 0;}void read_cards() {//改动的部分,这两个做参数传递bool card_exists[NUM_RANKS][NUM_CARDS];char ch, rank_ch, suit_ch;int rank, suit;bool bad_card;int cards_read = 0;for (rank = 0; rank < NUM_RANKS; rank++) {num_in_rank[rank] = 0;for (suit = 0; suit < NUM_SUITS; suit++)card_exists[rank][suit] = false;}for (suit = 0; suit < NUM_SUITS; suit++)num_in_suit[suit] = 0;while (cards_read < NUM_CARDS) {bad_card = false;printf("Enter a card: ");rank_ch = getchar();switch (rank_ch) {case '0':exit(EXIT_SUCCESS);case '2':rank = 0; break;case '3':rank = 1; break;case '4':rank = 2; break;case '5':rank = 3; break;case '6':rank = 4; break;case '7':rank = 5; break;case '8':rank = 6; break;case '9':rank = 7; break;case 't':case 'T':rank = 8; break;case 'j':case 'J':rank = 9; break;case 'q':case 'Q':rank = 10; break;case 'k':case 'K':rank = 11; break;case 'a':case 'A':rank = 12; break;default:bad_card = true;}suit_ch = getchar();switch (suit_ch) {case 'c':case 'C':suit = 0; break;case 'd':case 'D':suit = 1; break;case 'h':case 'H':suit = 2; break;case 's':case 'S':suit = 3; break;default: bad_card = true;}while ((ch = getchar()) != '\n')if (ch != ' ')bad_card = true;if (bad_card)printf("Bad card; ignored.\n");else if (card_exists[rank][suit])printf("Duplicats card;ignored .\n");else {num_in_rank[rank]++;num_in_suit[suit]++;card_exists[rank][suit] = true;cards_read++;}}}void analy_hands() {//改动的部分,这两个做参数传递int num_consec = 0;int rank, suit;flowers_dashu = false;straight = false;flush = false;three = false;pairs = 0;for (suit = 0; suit < NUM_SUITS; suit++)if (num_in_suit[suit] == NUM_CARDS)flush = true;rank = 0;while (num_in_rank[rank] == 0)rank++;for (; rank < NUM_RANKS&&num_in_rank[rank]>0; rank++)num_consec++;if (num_consec == NUM_CARDS) {straight = true;//这里有个return我去掉了}for (rank = 0; rank < NUM_RANKS; rank++) {if (num_in_rank[rank] == 4)four = true;if (num_in_rank[rank] == 3)three = true;if (num_in_rank[rank] == 2)pairs++;}if (flush == true&&straight== true){for (int i = 8; i < NUM_RANKS; i++)if (num_in_rank[i] == 1) {if (i == NUM_RANKS - 1)flowers_dashu = true;}elsebreak;}//Using for judge With flowers dashu}void print_result(void) {if (flowers_dashu)printf("flowers_dashu");else if(straight&&flush)printf("Stragight flush");else if (four)printf("Four of a kind ");else if (three&&pairs == 1)printf("Full of a kind");else if (flush)printf("Flush");else if (straight)printf("Stragight");else if (three)printf("Three of a kind");else if (pairs == 2)printf("Two pairs");else if (pairs == 1)printf("Pairs");elseprintf("High card");printf("\n\n");}

截图如下

5 修改10.5的poker.程序.使其允许“小a顺",(A,2,3,4,5)

程序如下

#include<stdio.h>#include<stdlib.h>#include<stdbool.h>//C99only#define NUM_RANKS 13#define NUM_SUITS 4#define NUM_CARDS 5int num_in_rank[NUM_RANKS];int num_in_suit[NUM_SUITS];bool straight, flush, four, three,small_A_flush;int pairs;void read_cards();void analy_hands();void print_result(void);int main() {for (;;) {read_cards();analy_hands();print_result();}return 0;}void read_cards() {//改动的部分,这两个做参数传递bool card_exists[NUM_RANKS][NUM_CARDS];char ch, rank_ch, suit_ch;int rank, suit;bool bad_card;int cards_read = 0;for (rank = 0; rank < NUM_RANKS; rank++) {num_in_rank[rank] = 0;for (suit = 0; suit < NUM_SUITS; suit++)card_exists[rank][suit] = false;}for (suit = 0; suit < NUM_SUITS; suit++)num_in_suit[suit] = 0;while (cards_read < NUM_CARDS) {bad_card = false;printf("Enter a card: ");rank_ch = getchar();switch (rank_ch) {case '0':exit(EXIT_SUCCESS);case '2':rank = 0; break;case '3':rank = 1; break;case '4':rank = 2; break;case '5':rank = 3; break;case '6':rank = 4; break;case '7':rank = 5; break;case '8':rank = 6; break;case '9':rank = 7; break;case 't':case 'T':rank = 8; break;case 'j':case 'J':rank = 9; break;case 'q':case 'Q':rank = 10; break;case 'k':case 'K':rank = 11; break;case 'a':case 'A':rank = 12; break;default:bad_card = true;}suit_ch = getchar();switch (suit_ch) {case 'c':case 'C':suit = 0; break;case 'd':case 'D':suit = 1; break;case 'h':case 'H':suit = 2; break;case 's':case 'S':suit = 3; break;default: bad_card = true;}while ((ch = getchar()) != '\n')if (ch != ' ')bad_card = true;if (bad_card)printf("Bad card; ignored.\n");else if (card_exists[rank][suit])printf("Duplicats card;ignored .\n");else {num_in_rank[rank]++;num_in_suit[suit]++;card_exists[rank][suit] = true;cards_read++;}}}void analy_hands() {//改动的部分,这两个做参数传递int num_consec = 0;int rank, suit;small_A_flush=false;straight = false;flush = false;three = false;pairs = 0;for (suit = 0; suit < NUM_SUITS; suit++)if (num_in_suit[suit] == NUM_CARDS)flush = true;rank = 0;while (num_in_rank[rank] == 0)rank++;for (; rank < NUM_RANKS&&num_in_rank[rank]>0; rank++)num_consec++;if (num_consec == NUM_CARDS) {straight = true;}//去掉一个return,免的程序返回for (rank = 0; rank < NUM_RANKS; rank++) {if (num_in_rank[rank] == 4)four = true;if (num_in_rank[rank] == 3)three = true;if (num_in_rank[rank] == 2)pairs++;}if (num_in_rank[12] == 1) {for (int i = 0; i < 3; i++) {if (num_in_rank[i] == 1) {if (i == 2)small_A_flush = true;}elsebreak;}}}void print_result(void) {if(small_A_flush&&flush)printf("Small_A_flush and flush ");//因为A小顺是可以和顺子的情况连在一起的,所以我个人加了上去else if(small_A_flush)printf("Small_A_flush");//这个就是a小顺else if (straight&&flush)printf("Stragight flush");else if (four)printf("Four of a kind ");else if (three&&pairs == 1)printf("Full of a kind");else if (flush)printf("Flush");else if (straight)printf("Stragight");else if (three)printf("Three of a kind");else if (pairs == 2)printf("Two pairs");else if (pairs == 1)printf("Pairs");elseprintf("High card");printf("\n\n");}

截图如下

6 有些计算器(尤其是惠普的计算器)使用逆波兰表示法(Reverse Polish Notation ,RPN),来书写数学表达式,在这一系列表示法中,运算符放在操作数后面而不是放在操作数中间,例如,在逆波兰表示法中1+2将表示为12+,而1+23将表示为12+,而1+2 * 3将表示为123+ 。逆波兰表达式可以很方便的用栈求值.算法从左向右读取运算符和操作数,并向左向右读取运算符,和操作数,并执行下列步骤.

(1) 当遇到操作数时,将其压入栈中

(2) 当遇到运算符时,从栈中,弹出它的操作数,执行运算结果并把结果压入栈中.

编写程序对逆波兰表达式求值,操作数都是个位的整数,运算符为"+,-,*,/=".遇到运算符=时,将显示栈顶值,随后清空栈值并提升用户输入新的表达式,这一过程持续进行,直到用户输入一个即不是运算符也不是操作数的字符为止

Enter an RPN expression : 1 2 3 * + =

value of expression : 7

Enter an RPN expression : 5 8 * 4 9 - / =

value of expression : -8

Enter an RPN expression : q

#include<stdio.h>#include<stdbool.h>//bool类型需要的头文件#include<stdlib.h>//exit函数需要的头文件#define STACK_SIZE 15//数组长度我故意搞小一点,这样比较容易看到溢出的情况int top = 0;int contents[STACK_SIZE];void stack_underflow() {printf("Not enough operands");exit(EXIT_FAILURE);}//这个是数组长度过短void stack_overflow() {printf("Expression is too complex");exit(EXIT_FAILURE);//一个函数(EXIT_FAILURE 是返回错误的值(非正常返回(1)),SUCCESSFUL是返回正常的值(0))}//这个是数组的长度过长了,所有输出数组的长度过长void make_empty(void) {top = 0;}//这个是把top重新置零bool is_empty(void) {return top == 0;}//判断数组是不是空的bool is_full(void) {return top == STACK_SIZE;}//判断数组的是不是满的void push(int i) {if (is_full())//如果在压之前数组在压之前就满了stack_overflow();elsecontents[top++] = i;}//把元素压进来int pop(void) {if (is_empty())stack_underflow();//如果在弹出去前数组就是空的elsereturn contents[--top];//把数组弹出去}//这里用--top的原因是++top回让top的值比实际存储的top大一位int main() {char a;printf("Enter an RPN expression :");while((scanf(" %c",&a))!='\n'){if ((a >= '0'&&a <= '9'))push(a - 48);else {if (a == '+') {int b = pop();int c = pop();contents[top++] = b + c;//这个是这题是精髓吧(个人认为),因为这几天,i++,和++i的运用//脑子清除的话,这题还是很快的}else if (a == '*') {int b = pop();int c = pop();contents[top++] = b * c;}else if (a == '/') {int b = pop();int c = pop();contents[top++] = b / c;}else if (a == '-') {int b = pop();int c = pop();contents[top++] = b - c;}else if (a == '=') {printf("Value of expression:");printf("%d\n", contents[--top]);make_empty();printf("Enter an RPN expression :");}else{printf("Game over");break;}//唯一的要弹出的情况}}return 0;}

7 编写程序,提示用户输入一个数,并显示改数,使用字符模拟七段符模拟七段显示器的效果:

Enter a number :491-9014

以下题目省略

#define MAX_DIGITS 10#include<stdio.h>char segments[7];char digits[3][4] = {'0' };int line = 0;int a[10];//定义了一个数组来存储定义的数void clear_digits_array(void);//清除数组void process_digits(int, int);//定义数组吗void print_digits_array(void);//数组数组int main(){clear_digits_array();char c;int digits, position=0;int i = 0;while ((c = getchar()) != '\n') {if (c >= '0'&&c <= '9') {//只接受0-9的数组digits = c - 48;a[i] = digits;clear_digits_array();process_digits(digits, position);print_digits_array();i++;position++;}}line++;printf("\n");for (int j = 0; j < i; j++) {clear_digits_array();process_digits(a[j], position);print_digits_array();}line++;printf("\n");for (int j = 0; j < i; j++) {clear_digits_array();process_digits(a[j], position);print_digits_array();}return 0;}void clear_digits_array(void) {for (int i = 0; i < 3; i++)for (int j = 0; j < 4; j++)digits[i][j] = ' ';}void process_digits(int digit, int position) {//给数组赋初始值if(position<=10){if (digit == 1) {digits[0][3] = '|';digits[2][3] = '|';}else if (digit == 2) {digits[0][2] ='-';digits[0][3] = '|';digits[1][2] ='-';digits[2][1] = '|';digits[2][2] ='-';}else if (digit == 3) {digits[0][2] ='-';digits[0][3] = '|';digits[1][2] ='-';digits[2][3] = '|';digits[2][2] ='-';}else if (digit == 4) {digits[0][1] = '|';digits[0][3] = '|';digits[1][2] ='-';digits[2][3] = '|';}else if (digit == 5) {digits[0][1] = '|';digits[0][2] ='-';digits[1][2] ='-';digits[2][3] = '|';digits[2][2] ='-';}else if (digit == 6) {digits[0][1] = '|';digits[1][2] ='-';digits[2][2] = '-';digits[2][3] ='|';digits[2][1] = '|';}else if (digit == 7) {digits[0][2] ='-';digits[0][3] = '|';digits[2][3] = '|';}else if (digit == 8) {digits[0][1] = '|';digits[0][2] ='-';digits[0][3] = '|';digits[1][2] ='-';digits[2][3] = '|';digits[2][2] ='-';digits[2][1] = '|';}else if (digit == 9) {digits[0][1] = '|';digits[0][2] ='-';digits[0][3] = '|';digits[1][2] ='-';digits[2][3] = '|';}else if (digit == 0) {digits[0][1] = '|';digits[0][2] ='-';digits[0][3] = '|';digits[2][2] = '-';digits[2][3] ='|';digits[2][1] = '|';}else {;}}}void print_digits_array(void) {for (int i = 0; i < 4; i++)printf("%c", digits[line][i]);//输出一行的数字,line我用了全局变量}

第10章题目写完

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。