精华内容
下载资源
问答
  • 2.程序填空(1) 下列程序的功能为:判断从键盘上输入的一个字符,并按下列要求输出。若该字符是数字 输出字符串"0-9"若该字符是大写字母 输出字符串"A-Z" 若该字符是小写字母 输出字符串"a-z"若该字符是其他字符 ...

    2.程序填空

    (1) 下列程序的功能为:判断从键盘上输入的一个字符,并按下列要求输出。

    若该字符是数字 输出字符串"0-9"

    若该字符是大写字母 输出字符串"A-Z" 若该字符是小写字母 输出字符串"a-z"

    若该字符是其他字符 输出字符串"!,@, "

    补充完善程序,以实现其功能。程序以文件名sy4_4.c保存。 #include main() { char c;

    scanf(___ _________); if('0'<=c&&c<='9')

    ___________

    else if(_________) printf("A-Z\n");

    ____ _______('a'<=c&&c<='z') printf("a-z\n");

    ____ __________________ printf("!,@, \n");

    }

    (2)下列程序的功能为:实现加、减、乘、除四则运算。补充完善程序,以实现其功能。程序以文件名sy4_5.c保存。

    #include main() {

    int a,b,d; char ch;

    4-119-jpg_6_0_______-239-0-4-239.jpg

    printf("Please input a expression:");

    scanf("%d%c%d", &a,&ch,&b ); switch(ch) {

    case '+':d=a+b; printf("%d+%d=%d\n",a,b,d);

    break;

    case '-':d=a-b; printf("%d-%d=%d\n",a,b,d);

    break; case '*':d=a*b;

    printf("%d*%d=%d\n",a,b,d); break; case '/':

    4-120-jpg_6_0_______-202-0-123-202.jpg

    展开全文
  • C语言程序设计第五版谭浩强课后答案 最下方有所有答案的总目录 1.什么是文件型指针?通过文件指针访问文件有什么好处? 答:缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。每个被使用的文件都...

    C语言程序设计第五版谭浩强课后答案

    最下方有所有答案的总目录
    1.什么是文件型指针?通过文件指针访问文件有什么好处?

    • 答:缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。每个被使用的文件都在内存中开辟一个相应的文件信息区,用来存放文件的有关信息(如文件的名字、文件状态及文件当前位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是由系统声明的,取名为FILE。

    • 通过文件指针访问文件的好处是:可以随机访问文件,有效表示数据结构,动态分配内存,方便使用字符串,有效使用数组。

    2.对文件的打开与关闭的含义是什么?为什么要打开和关闭文件?

    • 答:”打开“是指为文件建立相应的信息区(用来存放有关文件的信息)和文件缓冲区(用来暂时存放输人输出的数据)。

    • ”关闭“是指撤销文件信息区和文件缓冲区,使文件指针变量不再指向该文件,显然就无法进行对文件的读写了。

    3.从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件test中保存,输入的字符串以“!”结束。

    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void ) {
    	FILE *fp = NULL;
    	char c;
    	int i;
    	
    	if ( (fp=fopen("test", "w")) == NULL ) {
    		printf("open file test error!\n");
    		exit(EXIT_FAILURE);
    	}
    
    	while ( (c=getchar()) != EOF && c != '!' ) {
    		if ( c>='a' && c<='z' )
    			c = c-'a' + 'A';
    		fputc(c, fp);
    	}
    
    	fclose(fp);
    }
    

    结果:

    输入 : 123我的AbcABC!
    test文件的内容 : 123我的ABCABC
    

    4.有两个磁盘文件A和B,各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件C中去。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void swap(char *s, int i, int j) {
        char t = s[i];
        s[i] = s[j];
        s[j] = t;
    }
    
    void select_sort(char *str) {
        int i, j;
        int len = strlen(str);
        for (i=0; i<len; i++) {
            int min = i;
            for (j=i+1; j<len; j++) {
                if ( str[j] < str[min] )
                    min = j;
            }   
            swap(str, min, i); 
        }   
    }
    
    int main( void ) { 
        FILE *fa, *fb, *fc;
        char buf[1024] = {0};
    
        fa = fopen("A", "r");
        fb = fopen("B", "r");
        fc = fopen("C", "w");
    
        fgets(buf, 1024, fa);
        int len = strlen(buf);
        fgets(buf+len, 1024-len, fb);
        select_sort(buf);
        fputs(buf, fc);
    
        fclose(fa);
        fclose(fb);
        fclose(fc);
    }
    

    5.有5个学生,每个学生有3门课程的成绩,从键盘输人学生数据(包括学号,姓名,3门课程成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。

    #include <stdio.h>
    #include <stdlib.h>
    
    struct student {
        int num;
        char name[32];
        int score[3];
        float avg;
    };
    
    int main( viod ) { 
        int i;
        struct student stu[5];
        FILE *fp = NULL;
            
        for (i=0; i<5; i++) {
            printf("num name score1 score2 score3:\n");
            scanf("%d %s %d %d %d", &stu[i].num, &stu[i].name, 
                &stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
            stu[i].avg = (stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
        }   
    
        if ( (fp=fopen("stud", "wb")) == NULL ) { 
            printf("open file stud for write error\n");
            return 1;
        }
    
        if ( fwrite(stu, sizeof(stu), 1, fp) != 1 ) {
            printf("write error\n");
            return 1;
        }
        fclose(fp);
    }
    

    测试程序查看输入文件的容:

    1 zhagnsan 10 20 30 20.000000
    2 lisi 3 6 9 6.000000
    3 wangwu 1 2 3 2.000000
    4 zhaoliu 90 80 10 60.000000
    5 sunqi 90 1 1 30.000000
    

    6.将第5题stud文件中的学生数据,按平均分进行排序处理,将已排序的学生数据存入一个新文件stu_ sort 中。

    #include <stdio.h>
    #include <stdlib.h>
    
    struct student {
        int num;
        char name[32];
        int score[3];
        float avg;
    };
    
    void sort(struct student stu[], int len) {
        int i, j;
        struct student tmp;
        for (i=0; i<len; i++) {
            int min = i;
            for (j=i+1; j<len; j++) {
                if ( stu[j].avg > stu[min].avg )
                    min = j;
            }   
            tmp = stu[min];
            stu[min] = stu[i];
            stu[i] = tmp;
        }   
    }
    
    int main( viod ) {
        int i;
        struct student stu[5];
        FILE *fp = NULL;
        if ( (fp=fopen("stud", "rb")) == NULL ) {
            printf("open file stud for read error\n");
            return 1;
        }
    
        if ( fread(stu, sizeof(stu), 1, fp) != 1 ) {
            printf("write error\n");
            return 1;
        }
        fclose(fp);
    
        sort(stu, 5);
    
        FILE *fw = fopen("stu_sort", "wb");
        fwrite(stu, sizeof(stu), 1, fw);
        fclose(fw);
    }
    

    测试程序,查看文件内容,确实排过序:

    4 zhaoliu 90 80 10 60.000000
    5 sunqi 90 1 1 30.000000
    1 zhagnsan 10 20 30 20.000000
    2 lisi 3 6 9 6.000000
    3 wangwu 1 2 3 2.000000
    

    7.将第6题已排序的学生成绩文件进行插人处理。插人一个学生的3门课程成绩,程序先计算新插人学生的平均成绩,然后将它按成绩高低顺序插入,插入后建立一个新文件。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct student {
        int num;
        char name[32];
        int score[3];
        float avg;
    };
    
    void sort(struct student stu[], int len) {
        int i, j;
        struct student tmp;
        for (i=0; i<len; i++) {
            int min = i;
            for (j=i+1; j<len; j++) {
                if ( stu[j].avg > stu[min].avg )
                    min = j;
            }   
            if ( min != i ) { 
                tmp = stu[min];
                stu[min] = stu[i];
                stu[i] = tmp;
            }   
        }   
    }
    int main( viod ) { 
        int i;
        struct student stu[5];
        FILE *fp = NULL;
        if ( (fp=fopen("stu_sort", "rb")) == NULL ) {
            printf("open file stud for read error\n");
            return 1;
        }
    
        if ( fread(stu, sizeof(stu), 1, fp) != 1 ) {
            printf("write error\n");
            return 1;
        }
        fclose(fp);
    
        struct student new_stu[6];
        memcpy(new_stu, stu, sizeof(stu));
        printf("num name score0 score1 score2:\n");
        scanf("%d %s %d %d %d", &new_stu[5].num, &new_stu[5].name, &new_stu[5].score[0],
                &new_stu[5].score[1], &new_stu[5].score[2]);
        new_stu[5].avg = (new_stu[5].score[0]+new_stu[5].score[1]+new_stu[5].score[2])/3.0;
        sort(new_stu, 6);
    
        FILE *fw = fopen("tmp_sort", "wb");
        fwrite(new_stu, sizeof(new_stu), 1, fw);
        fclose(fw);
    }
    

    查看tmp_sort文件,确实插入和排序了:

    4 zhaoliu 90 80 10 60.000000
    5 sunqi 90 1 1 30.000000
    1 zhagnsan 10 20 30 20.000000
    8 hehe 12 3 4 6.333333
    2 lisi 3 6 9 6.000000
    3 wangwu 1 2 3 2.000000
    

    8.将第7题结果仍存人原有的stu_sort 文件而不另建立新文件。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct student {
        int num;
        char name[32];
        int score[3];
        float avg;
    };
    
    int main( viod ) { 
        int i;
        struct student stu[6];
        FILE *fp = NULL;
        if ( (fp=fopen("tmp_sort", "rb")) == NULL ) { 
            printf("open file stud for read error\n");
            return 1;
        }   
    
        if ( fread(stu, sizeof(stu), 1, fp) != 1 ) { 
            printf("write error\n");
            return 1;
        }   
        fclose(fp);
    
        FILE *fw = fopen("stu_sort", "wb");
        fwrite(stu, sizeof(stu), 1, fw);
        fclose(fw);
    }
    

    查看原本的stu_sort文件:

    4 zhaoliu 90 80 10 60.000000
    5 sunqi 90 1 1 30.000000
    1 zhagnsan 10 20 30 20.000000
    8 hehe 12 3 4 6.333333
    2 lisi 3 6 9 6.000000
    3 wangwu 1 2 3 2.000000
    

    9.有一磁盘文件employee,内存放职工的数据。每个职工的数据包括职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。今要求将职工名、工资的信息单独抽出来另建一个简明的职工工资文件。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct employee {
        int  num;      // 编号
        char name[32];
        char sex[4]; 
        int  age;
        char addr[60];
        int  salary;   
        char health[10]; // 健康状况
        char class[10];  // 文化程度
    };
    
    struct emp {
        char name[32];
        int salary;
    };
    
    int main( void ) { 
        int i;
        FILE *fp1, *fp2; 
        struct emp emp_arr[5];
        struct employee employee_arr[5];
    
        fp1=fopen("employee", "rb");
        fread(employee_arr, sizeof(employee_arr), 1, fp1);
        fclose(fp1);
    
        for (i=0; i<5; i++) {
            strcpy(emp_arr[i].name, employee_arr[i].name);
            emp_arr[i].salary = employee_arr[i].salary;
        }
    
        fp2=fopen("emp", "wb");
        fwrite(emp_arr, sizeof(emp_arr), 1, fp2);
        fclose(fp2);
    }
    

    查看emp文件的内容如下:

    abc 1800 
    def 2000 
    hehe 3000 
    haha 2800 
    ggg 2500 
    

    10.从第9题的“职工工资文件”中删去一个职工的数据,再存回原文件。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct emp {
        char name[32];
        int salary;
    };
    
    int main( void ) { 
        int i;
        FILE *fp;
        char name[32]; 
        struct emp emp_arr[5];
    
        fp=fopen("emp", "rb");
        fread(emp_arr, sizeof(emp_arr), 1, fp);
        fclose(fp);
    
        printf("name:");
        scanf("%s", &name);
        fp=fopen("emp", "wb");
        for (i=0; i<5; i++) {
            if ( strcmp(emp_arr[i].name, name) == 0 ) { 
                continue;
            }   
            fwrite(&emp_arr[i], sizeof(emp_arr[i]), 1, fp);
        }   
        fclose(fp);
    }
    

    删除ggg后的源文件内容:

    abc 1800 
    def 2000 
    hehe 3000 
    haha 2800 
    

    11.从键盘输人若干行字符(每行长度不等),输人后把它们存储到一磁盘文件中。再从该文件中读入这些数据,将其中小写字母转换成大写字母后在显示屏上输出。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main( void ) { 
        int i;
        FILE *fp = fopen("tmp.txt", "w");
        char buf[1024] = {}; 
        
        while ( fgets(buf, 1024, stdin) != NULL ) { 
            fputs(buf, fp);
            memset(buf, 0x00, sizeof(buf));
        }   
        fclose(fp);
    
        fp = fopen("tmp.txt", "r");
        while ( !feof(fp) ) { 
            memset(buf, 0x00, sizeof(buf));
            fgets(buf, 1024, fp);
            for (i=0; buf[i] != '\0'; i++) {
                if ( buf[i]>='a' && buf[i]<='z' )
                    printf("%c", buf[i]-32);
                else
                    printf("%c", buf[i]);
            }   
        }   
        fclose(fp);
    }
    

    执行结果:

    输入:
    this is maomaochong
    litao love IDFD
    1243
    输出:
    THIS IS MAOMAOCHONG
    LITAO LOVE IDFD
    1243
    

    C语言程序设计第五版谭浩强课后习题答案 第一章
    C语言程序设计第五版谭浩强课后习题答案 第二章
    C语言程序设计第五版谭浩强课后习题答案 第三章
    C语言程序设计第五版谭浩强课后习题答案 第四章
    C语言程序设计第五版谭浩强课后习题答案 第五章
    C语言程序设计第五版谭浩强课后习题答案 第六章
    C语言程序设计第五版谭浩强课后习题答案 第七章
    C语言程序设计第五版谭浩强课后习题答案 第八章
    C语言程序设计第五版谭浩强课后习题答案 第九章
    C语言程序设计第五版谭浩强课后习题答案 第十章

    展开全文
  • 1: #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) ...bool is_...
    1#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;
    	
    }
    
    
    
    2/*********************************************************
     * 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 <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");
    }
    
    
    3#include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_CARDS 5
    #define RANK 0
    #define SUIT 1
    
    /* external variables */
    int hand[NUM_CARDS][2];
    /*    0    1
        ____ ____
     0 |____|____|
     1 |____|____|
     2 |____|____|
     3 |____|____|
     4 |____|____|
        rank suit
    */
    
    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 variable *
     *             hand; checks for bad cards and duplicate   *
     *             cards.                                     *
     **********************************************************/
    void read_cards(void)
    {
      char ch, rank_ch, suit_ch;
      int i, rank, suit;
      bool bad_card, duplicate_card;
      int cards_read = 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");
          continue;
        }
    
        duplicate_card = false;
        for (i = 0; i < cards_read; i++)
          if (hand[i][RANK] == rank && hand[i][SUIT] == suit) {
            printf("Duplicate card; ignored.\n");
            duplicate_card = true;
            break;
          }
    
        if (!duplicate_card) {
          hand[cards_read][RANK] = rank;
          hand[cards_read][SUIT] = 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 rank, suit, card, pass, run;
    
      straight = true;
      flush = true;
      four = false;
      three = false;
      pairs = 0;
    
      /* sort cards by rank */
      for (pass = 1; pass < NUM_CARDS; pass++)
        for (card = 0; card < NUM_CARDS - pass; card++) {
          rank = hand[card][RANK];
          suit = hand[card][SUIT];
          if (hand[card+1][RANK] < rank) {
            hand[card][RANK] = hand[card+1][RANK];
            hand[card][SUIT] = hand[card+1][SUIT];
            hand[card+1][RANK] = rank;
            hand[card+1][SUIT] = suit;
          }
        }
    
      /* check for flush */
      suit = hand[0][SUIT];
      for (card = 1; card < NUM_CARDS; card++)
        if (hand[card][SUIT] != suit)
          flush = false;
    
      /* check for straight */
      for (card = 0; card < NUM_CARDS - 1; card++)
        if (hand[card][RANK] + 1 != hand[card+1][RANK])
          straight = false;
    
      /* check for 4-of-a-kind, 3-of-a-kind, and pairs by
         looking for "runs" of cards with identical ranks */
      card = 0;
      while (card < NUM_CARDS) {
        rank = hand[card][RANK];
        run = 0;
        do {
          run++;
          card++;
        } while (card < NUM_CARDS && hand[card][RANK] == rank);
        switch (run) {
          case 2: pairs++;      break;
          case 3: three = true; break;
          case 4: four = true;  break;
        }
      }
    }
    
    /**********************************************************
     * 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");
    }
    
    
    
    4/*********************************************************
     * 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 <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
    		{
    			straight = true;
    			return;
    		}}
    	else {
    		if ((num_consec == 4)&&(num_in_rank[12]==1))
    	{ 
    	small_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");
    }
    
    
    展开全文
  • 5: /********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 200...
    5/*********************************************************
     * 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 <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
    		{
    			straight = true;
    			return;
    		}}
    	else {
    		if ((num_consec == 4)&&(num_in_rank[12]==1))
    	{ 
    	small_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");
    }
    
    
    6#include <stdio.h>
    #include<math.h>
    int top = 0;
    #define STACK_SIZE 100	
    int contents[STACK_SIZE];
    void make_empty(void)
    {
    	top = 0;
    }
    bool is_empty(void)
    {
    	return top == 0;
    }
    bool is_full(void)
    { 
    	return top==STACK_SIZE;}
    bool push(int i){
    	if(is_full()){
    		printf("Expression is too complex");return
    	false;}
    	else 
    	{ contents[top++]=i;
    	return true;}
    }
    	int pop(void){
    		
    	if(is_empty())
    	return contents[0];
    	else return contents[top--];}//弹出contens[top--]那个数据
    
    
    	int main(void)
    	{
    	printf("输入运算:");
    	char ch;
    	int ch1;
    	int x,y,i=0,o;
    	while((ch=getchar())!='\n')
    	{
    		if(ch>='0'&&ch<='9')
    		{     ch1=ch-48;
    			o=push(ch1);
    			if(o==false)
    				return 0;
    			
    			 
    		}
    
    		else {
    			switch(ch)
    			{
    			case '+':
    				top--;
    				if(top==0){
    					printf("Not enough operands in expression");
    					i++;break;}
    				else
    				{y=pop();}
    				if(top==0){
    					x=pop();}
    				else { x=pop();
    				top++;}
    				push((x+y));
    			
    						
    			break;
    			case '-':
    				top--;
    				y=pop();
    				if(top==0){
    					x=pop();}
    				else { x=pop();
    				top++;}
    				push((x-y));
    			break;
    			case '*':top--;
    				y=pop();
    				if(top==0){
    					x=pop();}
    				else { x=pop();
    				top++;}
    				push((x*y));
    			break;
    			case '/':top--;
    				y=pop();
    				if(top==0){
    					x=pop();}
    				else { x=pop();
    				top++;}
    				push((x/y));
    					   
    			break;
    			default:
    				break;
    			}
    		}}
    	if(i==0)
    	printf("%d\n",contents[0]);
    	else 
    	return 0;
    	}
    
    7#include<stdio.h>
    #include<math.h>
    #define MAX_DIGHTS 10
    /*
         0
     1    _   4
         |_|
     2   |_|  5
         6
         */
    bool segments[10][7]= {{true,true,true,false,true,true,true}, /*0*/
                          {false,false,false,false,true,true,false},/*1*/
                          {true,false,true,true,true,false,true},/*2*/
                           {true,false,false,true,true,true,true},/*3*/
                          {false,true,false,true,true,true,false},/*4*/
                          {true,true,false,true,false,true,true},/*5*/
                          {true,true,true,true,false,true,true},/*6*/
                          {true,false,false,false,true,true,false},/*7*/
                          {true,true,true,true,true,true,true},/*8*/
                          {true,true,false,true,true,true,true}};/*9*/
    char digits[3][4*MAX_DIGHTS];
      
    void clear_digits_array(void);
    int process_digit(int digit,int position);
    void print_digits_array();
      
    int main(void)
    {
        int position=0,digit;
        char ch;
        clear_digits_array();
        printf("Enter a number:");
        do{
            ch=getchar();
            if(ch>='0'&&ch<='9'){
                digit=(int)(ch-'0');
               position=process_digit(digit,position);
            }
        }while(ch!='\n');
        print_digits_array();
        return 0;
    }
    void clear_digits_array(void)
    {
        int i,j;
        for(i=0; i<3; i++)
            for(j=0; j<4*MAX_DIGHTS; j++)
                digits[i][j]=' ';
    }
    int process_digit(int digit,int position)
    {
        int j;
        for(j=0; j<7; j++){
            if(segments[digit][j]==true){
                if(j%3==0)
                    digits[j/3][1+position*4]='_';
                if(j%3!=0)
                   digits[j%3][((j/3>0)?2:0)+position*4]='|';
            }
        }
        return ++position;
    }
    void print_digits_array(void)
    {
        int i, j;
        for (i = 0; i < 3; i++){
            for (j = 0; j <MAX_DIGHTS * 4; j++)
                putchar(digits[i][j]);
            putchar('\n');
        }
    }
    
    
    展开全文
  • 第五版谭浩强课后答案 第十章《对文件的输入输出​》习题答案1.什么是文件型指针?通过文件指针访问文件有什么好处?2.对文件的打开与关闭的含义是什么?为什么要打开和关闭文件?3.从键盘输入一个字符串,将其中的...
  • C语言程序设计(谭浩强版) 课后习题答案 从第三章 到 第十章
  • PAGE 目录 第一 C语言基础知识 1 第二 C语言程序设计的初步知识 3 第三 顺序结构程序设计 5 第四 选择结构程序设计 9 第五 循环结构程序设计 12 第六 数组 16 第七 函数 19 第八 指针 23 第九 ...
  • 计算机二级C语言习题集汇总及答案 目录 第一 C语言基础知识 第二 C语言程序设计的初步知识 第三 顺序结构程序设计 第四 选择结构程序设计 第五 循环结构程序设计 第六 数组 第七 函数 第八 指针 第...
  • C语言谭浩强版 答案

    2012-10-29 23:03:52
    这是C程序设计语言,谭浩强版,第十章到第十五章答案,很全名的,还有前面几章节的下次再传。

空空如也

空空如也

1 2
收藏数 35
精华内容 14
关键字:

c语言程序设计第十章答案

c语言 订阅