第一题
这题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 &