C语言程序设计现代方法第二版,第10章课后编程习题

2023-05-16

第一题
这题10.2中的pop函数没有用,在我的电脑里(vs2017)当把pop赋值给字符串时总是报错,尝试了各种办法都不行,提示是:char(*)()和char类型不兼容。求大神帮呀

#include <stdio.h>
#include<string>
#define STACK_SIZE 100
int top = 0;
char contents[STACK_SIZE];
void make_empty(void)
{
	top = 0;
}
bool is_empty(void)
{
	return top == 0;
}
bool is_full(void)
{
	return top == STACK_SIZE;
}
void push(char i)
{
	if (!is_full())
		contents[top++] = i;
}
char pop(void)
{
	if (!is_empty())
		return contents[top--];
}

int main(void)
{
	char ch,temp;
	int n = 0;
	printf("Enter parenteses and/or brance:");
	while ((ch = getchar()) != '\n')
	{
		if (ch == '[' || ch == '{')
			push(ch);
		else if (ch == ']' || ch == '}')
		{
			temp = contents[--top];
			if (ch == ']'&& temp != '[')
			{
				n = 1;
				printf("ch:%c   temp:%c\n", ch, temp);
			}
			else if (ch == '}'&& temp != '{')
				n = 1;
		}
	}
	if (n == 1)
		printf("错误\n");
	else
		printf("正确\n");

system("pause");
return 0;
	
}

第二题

/*********************************************************
 * From C PROGRAMMING: A MODERN APPROACH, Second Edition *
 * By K. N. King                                         *
 * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
 * All rights reserved.                                  *
 * This program may be freely distributed for class use, *
 * provided that this copyright notice is retained.      *
 *********************************************************/

 /* poker.c (Chapter 10, page 233) */
 /* Classifies a poker hand */

#include <stdbool.h>   /* C99 only */
#include <stdio.h>
#include <stdlib.h>

#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 5

/* external variables */

bool straight, flush, four, three;
int pairs;   /* can be 0, 1, or 2 */

/* prototypes */
void read_cards(int a[NUM_RANKS], int b[NUM_SUITS]);
void analyze_hand(int a[NUM_RANKS], int b[NUM_SUITS]);
void print_result(void);

/**********************************************************
 * main: Calls read_cards, analyze_hand, and print_result *
 *       repeatedly.                                      *
 **********************************************************/
int main(void)
{
	int rank, suit;
	int num_in_rank[NUM_RANKS];
	int num_in_suit[NUM_SUITS];
	for (rank = 0; rank < NUM_RANKS; rank++) 
		num_in_rank[rank] = 0;
	for (suit = 0; suit < NUM_SUITS; suit++)
		num_in_suit[suit] = 0;

	for (;;) {
		read_cards(num_in_rank, num_in_suit);
		analyze_hand(num_in_rank, num_in_suit);
		print_result();
	}
}

/**********************************************************
 * read_cards: Reads the cards into the external          *
 *             variables num_in_rank and num_in_suit;     *
 *             checks for bad cards and duplicate cards.  *
 **********************************************************/
void read_cards(int a[NUM_RANKS],int b[NUM_SUITS])
{
	bool card_exists[NUM_RANKS][NUM_SUITS];
	char ch, rank_ch, suit_ch;
	int rank, suit;
	bool bad_card;
	int cards_read = 0;

	for (rank = 0; rank < NUM_RANKS; rank++) {
		for (suit = 0; suit < NUM_SUITS; suit++)
			card_exists[rank][suit] = false;
	}
	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("Duplicate card; ignored.\n");
		else {
			a[rank]++;
			b[suit]++;
			card_exists[rank][suit] = true;
			cards_read++;
		}
	}
}

/**********************************************************
 * analyze_hand: Determines whether the hand contains a   *
 *               straight, a flush, four-of-a-kind,       *
 *               and/or three-of-a-kind; determines the   *
 *               number of pairs; stores the results into *
 *               the external variables straight, flush,  *
 *               four, three, and pairs.                  *
 **********************************************************/
void analyze_hand(int a[NUM_RANKS], int b[NUM_SUITS])
{
	int num_consec = 0;
	int rank, suit;

	straight = false;
	flush = false;
	four = false;
	three = false;
	pairs = 0;

	/* check for flush */
	for (suit = 0; suit < NUM_SUITS; suit++)
		if (b[suit] == NUM_CARDS)
			flush = true;

	/* check for straight */
	rank = 0;	
	while (a[rank] == 0) rank++;
	for (; rank < NUM_RANKS && a[rank] > 0; rank++)
		num_consec++;
	if (num_consec == NUM_CARDS) {
		straight = true;
		return;
	}

	/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
	for (rank = 0; rank < NUM_RANKS; rank++) {
		if (a[rank] == 4) four = true;
		if (a[rank] == 3) three = true;
		if (a[rank] == 2) pairs++;
	}
}

/**********************************************************
 * print_result: Prints the classification of the hand,   *
 *               based on the values of the external      *
 *               variables straight, flush, four, three,  *
 *               and pairs.                               *
 **********************************************************/
void print_result(void)
{
	if (straight && flush) printf("Straight flush");
	else if (four)         printf("Four of a kind");
	else if (three &&
		pairs == 1)   printf("Full house");
	else if (flush)        printf("Flush");
	else if (straight)     printf("Straight");
	else if (three)        printf("Three of a kind");
	else if (pairs == 2)   printf("Two pairs");
	else if (pairs == 1)   printf("Pair");
	else                   printf("High card");

	printf("\n\n");
}

第三题

/*********************************************************
 * From C PROGRAMMING: A MODERN APPROACH, Second Edition *
 * By K. N. King                                         *
 * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
 * All rights reserved.                                  *
 * This program may be freely distributed for class use, *
 * provided that this copyright notice is retained.      *
 *********************************************************/

 /* poker.c (Chapter 10, page 233) */
 /* Classifies a poker hand */

#include <stdbool.h>   /* C99 only */
#include <stdio.h>
#include <stdlib.h>

#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 5

/* external variables */
int hand[NUM_RANKS][NUM_SUITS];
bool straight, flush, four, three;
int pairs;   /* can be 0, 1, or 2 */

/* prototypes */
void read_cards(void);
void analyze_hand(void);
void print_result(void);

/**********************************************************
 * main: Calls read_cards, analyze_hand, and print_result *
 *       repeatedly.                                      *
 **********************************************************/
int main(void)
{
	for (;;) {
		read_cards();
		analyze_hand();
		print_result();
	}
}

/**********************************************************
 * read_cards: Reads the cards into the external          *
 *             variables num_in_rank and num_in_suit;     *
 *             checks for bad cards and duplicate cards.  *
 **********************************************************/
void read_cards(void)
{
	bool card_exists[NUM_RANKS][NUM_SUITS];
	char ch, rank_ch, suit_ch;
	int rank, suit;
	bool bad_card;
	int cards_read = 0;

	for (rank = 0; rank < NUM_RANKS; rank++) {
		for (suit = 0; suit < NUM_SUITS; suit++)
			hand[rank][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 (hand[rank][suit]  != 0)
			printf("Duplicate card; ignored.\n");
		else {
			hand[rank][suit]++;
			cards_read++;
		}
	}
}

/**********************************************************
 * analyze_hand: Determines whether the hand contains a   *
 *               straight, a flush, four-of-a-kind,       *
 *               and/or three-of-a-kind; determines the   *
 *               number of pairs; stores the results into *
 *               the external variables straight, flush,  *
 *               four, three, and pairs.                  *
 **********************************************************/
void analyze_hand(void)
{
	int num_consec = 0;
	int rank, suit;

	straight = false;
	flush = false;
	four = false;
	three = false;
	pairs = 0;

	/* check for flush */
	for (suit = 0; suit < NUM_SUITS; suit++)
		if (hand[suit][1] == NUM_CARDS)
			flush = true;

	/* check for straight */
	rank = 0;
	while (hand[rank][0] == 0) rank++;
	for (; rank < NUM_RANKS && hand[rank][0] > 0; rank++)
		num_consec++;
	if (num_consec == NUM_CARDS) {
		straight = true;
		return;
	}

	/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
	for (rank = 0; rank < NUM_RANKS; rank++) {
		if (hand[rank][0] == 4) four = true;
		if (hand[rank][0] == 3) three = true;
		if (hand[rank][0] == 2) pairs++;
	}
}

/**********************************************************
 * print_result: Prints the classification of the hand,   *
 *               based on the values of the external      *
 *               variables straight, flush, four, three,  *
 *               and pairs.                               *
 **********************************************************/
void print_result(void)
{
	if (straight && flush) printf("Straight flush");
	else if (four)         printf("Four of a kind");
	else if (three && pairs == 1)   printf("Full house");
	else if (flush)        printf("Flush");
	else if (straight)     printf("Straight");
	else if (three)        printf("Three of a kind");
	else if (pairs == 2)   printf("Two pairs");
	else if (pairs == 1)   printf("Pair");
	else                   printf("High card");

	printf("\n\n");
}

第四题第五题做到一起了

/*********************************************************
 * From C PROGRAMMING: A MODERN APPROACH, Second Edition *
 * By K. N. King                                         *
 * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
 * All rights reserved.                                  *
 * This program may be freely distributed for class use, *
 * provided that this copyright notice is retained.      *
 *********************************************************/

 /* poker.c (Chapter 10, page 233) */
 /* Classifies a poker hand */

#include <stdbool.h>   /* C99 only */
#include <stdio.h>
#include <stdlib.h>

#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 5

/* external variables */
int num_in_rank[NUM_RANKS];
int num_in_suit[NUM_SUITS];
bool straight, flush, four, three, big_straight, small_straight;
int pairs;   /* can be 0, 1, or 2 */

/* prototypes */
void read_cards(void);
void analyze_hand(void);
void print_result(void);

/**********************************************************
 * main: Calls read_cards, analyze_hand, and print_result *
 *       repeatedly.                                      *
 **********************************************************/
int main(void)
{
	for (;;) {
		read_cards();
		analyze_hand();
		print_result();
	}
}

/**********************************************************
 * read_cards: Reads the cards into the external          *
 *             variables num_in_rank and num_in_suit;     *
 *             checks for bad cards and duplicate cards.  *
 **********************************************************/
void read_cards(void)
{
	bool card_exists[NUM_RANKS][NUM_SUITS];
	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("Duplicate card; ignored.\n");
		else {
			num_in_rank[rank]++;
			num_in_suit[suit]++;
			card_exists[rank][suit] = true;
			cards_read++;
		}
	}
}

/**********************************************************
 * analyze_hand: Determines whether the hand contains a   *
 *               straight, a flush, four-of-a-kind,       *
 *               and/or three-of-a-kind; determines the   *
 *               number of pairs; stores the results into *
 *               the external variables straight, flush,  *
 *               four, three, and pairs.                  *
 **********************************************************/
void analyze_hand(void)
{
	int num_consec = 0;
	int rank, suit;

	straight = false;
	flush = false;
	four = false;
	three = false;
	pairs = 0;
	big_straight = false;
	small_straight = false;

	/* check for flush */
	for (suit = 0; suit < NUM_SUITS; suit++)
		if (num_in_suit[suit] == NUM_CARDS)
			flush = true;

	/* check for straight */
	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 )
	{
		if (num_in_rank[8] == 1 && num_in_rank[12] == 1)
		{
			big_straight = true;
			return;
		}
		else if (num_in_rank[0] == 1 && num_in_rank[12] == 1)
		{
			small_straight = true;
			return;
		}
		else
		{
			straight = true;
			return;
		}
	}
	
	/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
	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++;
	}
}

/**********************************************************
 * print_result: Prints the classification of the hand,   *
 *               based on the values of the external      *
 *               variables straight, flush, four, three,  *
 *               and pairs.                               *
 **********************************************************/
void print_result(void)
{
	if (straight && flush) printf("Straight flush");
	if (big_straight && flush) printf("Big straight flush");
	if (small_straight && flush) printf("small straight flush");
	else if (four)         printf("Four of a kind");
	else if (three && pairs == 1) printf("Full house");
	else if (flush)        printf("Flush");
	else if (straight)     printf("Straight");
	else if (three)        printf("Three of a kind");
	else if (pairs == 2)   printf("Two pairs");
	else if (pairs == 1)   printf("Pair");
	else                   printf("High card");

	printf("\n\n");
}

第六题

在这里插入代码片
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C语言程序设计现代方法第二版,第10章课后编程习题 的相关文章

  • 兵器软件通用测试开发方法-ETest_DEV

  • 兵器软件通用测试开发环境-ETest_DEV

  • WPS添加下划线,文字尾部不显示下划线问题解决(一个So stupid问题)

    记录一个傻瓜操作 嗯 更想删WPS了 一 问题如下 首先如图 选择wps中的下划线操作 理想中他应该是这样的 选中的内容应该在下划线中间 是吧 默认正常操作就应该这样 实际上它出来的效果是这样 文字后面选中的下划线消失了 软件自身的设置就没
  • 【记录】MPU6050原理快速入门(附手册)

    目录 MPU6050 MPU6050主要参数 MPU6050通信接口 MPU6050电路 向MPU6050的一个寄存器写一个字节的数据 从MPU6050的一个寄存器读一个字节的数据 MPU6050 MPU6050是一个运动处理传感器 xff
  • 【STM32】HAL库三步实现串口重定向(代码复制可用)

    目录 第一步 xff1a 添加标准输入输出头文件 第二步 xff1a 重写fputc 函数 第三步 xff1a 重写fgetc 函数 代码汇总 xff08 直接复制使用 xff09 需要直接来复制 在PC上进行C语言程序开发时 xff0c
  • c# Post请求实例

    server span class token comment 服务器 span span class token keyword using span span class token namespace System span span
  • STM32 串口通讯及实现

    目录 一 串口通讯概述1 广义的串口2 狭义的串口3 串口数据定义4 串口通讯应用 二 STM32串口工程标准库实现1 串口的初始化2 串口数据发送 3 串口的数据接收 一 串口通讯概述 1 广义的串口 广义的串口是针对并口来说的 串口是指
  • STM32串口接受和发送数据的程序(USART/UART)

    本实验中 STM32通过串口1和串口2上位机对话 xff0c STM32通过串口1在收到上位机发过来的字符串 以回车换行结束 后 xff0c 通过串口2原原本本的返回给上位机 串口 xff08 USART UART xff09 通信配置详解
  • CMakeLists.txt中第三方库编写思考

    编写ROS时经常需要自己构建第三方库或者引用别人的第三方库 xff0c 对于第三方库的调用主要有以下两种方式 xff1a 1 引用现成的第三方库 xff1a find package PCL 1 7 REQUIRED xff1a 添加依赖
  • vscode运行卡顿解决方案

    卡顿原因 主要是rg exe扩展程序占用CPU过高 xff0c 那么只需要禁用它即可 解决方案 打开 vs code xff0c 文件 gt 首选项 gt 设置 gt 搜索 search followSymlinks 取消勾选即可
  • 优象光流模块助力无人机之使用效果分享

    优象光流模块助力无人机之使用效果分享 我是一名无人机爱好者 xff0c 一直以来对无人机就有一种慕名的喜好 xff0c 只要有时间就会与队友们在实验室研究探讨 当然 xff0c 刚开始玩无人机悬停时会遇到种种问题 xff0c 例如飞机一开始
  • 如何使用光流芯片U30实现四轴无人机悬停

    如何使用光流芯片U30实现四轴无人机悬停 在没有GPS的环境下 xff0c 比如室内环境 xff0c 四轴无人机在水平方向会不断漂移 如何让无人机实现稳定的自主悬停呢 xff1f 光流芯片可以感知无人机在水平方向的运动信息 xff08 速度
  • (CMake) 库的生成和链接

    文章目录 前言前置准备当前项目的库静态库动态库 外部项目的库静态库动态库 库的总结总code函数add subdirectory 添加源文件目录add library 指定的源文件生成库target link libraries 为目标链接
  • vscode配置C++编译环境(windows环境下)

    vscode配置C 43 43 编译环境 xff08 windows环境下 xff09 记录下自己在vscode中配置C 43 43 编译环境的过程 xff0c 仅供参考 一 VSCODE MinGW编译器 cMake跨平台编译工具下载 1
  • STL标准库详解

    STL标准库 主要由容器 迭代器 算法组成 STL主要头文件 lt algorithm gt lt deque gt lt functional gt lt iterator gt lt vector gt lt list gt lt ma
  • Mask R-CNN详解(图文并茂)

    Mask R CNN Mask R CNN是一个实例分割 xff08 Instance segmentation xff09 算法 xff0c 主要是在目标检测的基础上再进行分割 Mask R CNN算法主要是Faster R CNN 43
  • python-roslaunch : 依赖: python-roslib 但是它将不会被安装

    在配置环境中将python配置删除类 xff0c 导致ROS系统的好多依赖都没了 安装配置ROS时遇到问题 xff1a 1 先按ROS WIKI上进行安装 xff0c 之后进行测试看是否安装上 2 测试代码 xff1a 第一个终端 xff1
  • pycharm函数调用关系可视化(Graphviz + pycallgraph画图)

    文章目录 介绍Graphviz 安装pycallgraph安装实践 介绍 一个 python project 中往往包含很多 py 文件 python文件中又会包含很多函数 xff0c 函数之间相互传参和调用 如果遇到代码行数很多的情况 x
  • Linux下的UDP通信

    socket 函数 函数说明 xff1a 建立新的socket通信 头文件 xff1a include lt sys socket h gt include lt sys types h gt 函数定义 xff1a int socket i
  • error: array type has incomplete element type ‘int[]‘

    项目场景 xff1a 数组作为函数的形参 问题描述 xff1a error array type has incomplete element type 39 int 39 原因分析 xff1a 多维数组做为函数参数时 xff0c 只可以省

随机推荐