C语言程序设计现代方法第二版,第10章课后编程习题
第一题
这题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");elseprintf("正确\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");
}
第六题
在这里插入代码片
C语言程序设计现代方法第二版,第10章课后编程习题相关推荐
- c语言程序设计现代方法第二版 第10章程序设计题3题,自己编写的一个程序
main.c文件 #include <stdio.h> //#include "lib.h" if there is a statement like this ,th ...
- C语言程序设计-现代方法 第二版 第6.1小节 显示平方表
第6.1小节 显示平方表 //This is a comment //Author:King //Time:2020/12/5 //Reference:C Programming:A Modern A ...
- C语言程序设计-现代方法 第二版 第3.2.3小节 分数相加
第3.2.3小节 分数相加.举例说明scanf函数的模式匹配能力 ,本例程实现两个分数相加 //This is a comment //Author:King //Time:2020/12/4 //R ...
- C语言程序设计-现代方法 第二版 2.1小节 显示双关语
2.1小节代码 显示双关语. //This is a comment //Author:King //Time:2020/12/4 //Reference:C Programming:A Modern ...
- C语言程序设计-现代方法 第二版 第9.1 小节代码 显示双关语改进版
第9.1 小节代码 显示双关语改进版 //This is a comment //Author:King //Time:2020/12/6 //Reference:C Programming:A Mo ...
- 数据结构(C语言)第二版 第五章课后答案
数据结构(C语言)第二版 第五章课后答案 1~5 A D D C A 6~10 C C B D C 11~15 B C A C A 1.选择题 (1)把一棵树转换为二叉树后,这棵二叉树的形态是(A) ...
- 数据结构(C语言)第二版 第六章课后答案
数据结构(C语言)第二版 第六章课后答案 1~5 C B B B C 6~10 B A B A A 11~15 D C C (D,D) B 1.选择题 (1)在一个图中,所有顶点的度数之和等于图的边数 ...
- C语言程序设计(第三版)何钦铭著 习题5-3
C语言程序设计(第三版)何钦铭著 习题5-3 习题一览表 1. C语言程序设计(第三版)何钦铭著 习题2-1 2.C语言程序设计(第三版)何钦铭著 习题2-2 3.C语言程序设计(第三版)何钦铭著 习 ...
- 数据结构(C语言)第二版 第四章课后答案
数据结构(C语言)第二版 第四章课后答案 1~5 B B C A B 6~10 B B C B B 11~15 A B D (C,B) C 1.选择题 (1)串是一种特殊的线性表,其特殊性体现在(B) ...
最新文章
- 【抠图中的注意力机制】HAttMatting---让抠图变得如此简单!
- 心急如焚!程序员拥有 2.2 亿美元巨款,却想不起密码
- oracle和arcgis优势,Oracle spatial 使用的一些感受
- Linux下MySQL数据库主从同步配置
- MFC中CString.format用法
- 不该被遗忘的nodeName、nodeValue和nodeType!
- OSS网页上传和断点续传(OSS配置篇)
- linux下swftools 的配置
- VS2005中解决方案管理器中看不到解决方案节点的解决办法
- Junit5集成到Maven工程
- java程序运行过程数据丢失怎么办_java运行过程中OutOfMemoryError是什么原因?怎么解决...
- Failed to connect to bitbucket.org port 443: Operation timed out
- java并发编程实践-this溢出2
- GIS二次开发平台比较之我想
- 儿子于靖洋15个月照片
- 解决 “ImportError: attempted relative import with no known parent package“ 问题
- 浅谈海外工程项目投标策划
- Android禁止EditText弹出输入法
- linux sed 目录递归,shell递归遍历目录下的所有文件并统一改名的方法-文件更名...
- OpenMP 教程(一) 深入剖析 OpenMP reduction 子句