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();			
	else
		contents[top++] = i;
}									//把元素压进来
char pop(void) {
   
	if (is_empty())
		stack_underflow();			//如果在弹出去前数组就是空的
	else
		return 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() == '}')
				;
			else
				top++;							//这个是两种情况,如果不匹配,top就要加回去,因为top不管匹配不匹配,top都要自减
		}
		if (a == ']')
			if (pop() == ']')
				;
			else
				top++;
	}
	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 5
 
bool 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 ");
	
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值