c语言程序设计现代方法第二版 第10章程序设计题3题,自己编写的一个程序
生活随笔
收集整理的這篇文章主要介紹了
c语言程序设计现代方法第二版 第10章程序设计题3题,自己编写的一个程序
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
main.c文件
#include <stdio.h> //#include "lib.h" if there is a statement like this ,then there will be a wrong compile statement //about :multiple defination of .....because of separate compile... #include "lib.h" int main() {for(;;){read();analyze();print();}return 0; }function.c文件
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include "lib.h" #define MAX_RANK 13 #define MAX_SUIT 4 #define MAX_NUMBER 5 bool straight =false; bool flush =false; bool four =false; bool three =false; unsigned pairs = 0; unsigned hand[MAX_NUMBER][2]; void read(void) {char card_stored[MAX_RANK][MAX_SUIT] = {false};char ch;unsigned number = 0;while(number < 5){printf("Enter a card:");ch = getchar();unsigned rank;unsigned suit;unsigned first;unsigned second;bool bad_card = false;switch(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; }//switchch = getchar();switch(ch){case 'h':case 'H': suit = 0;break;case 's':case 'S': suit = 1;break;case 'd':case 'D': suit = 2;break;case 'c':case 'C': suit = 3;break;default:bad_card = true;}while((ch = getchar()) != '\n')if(ch != ' ') bad_card = true;if(bad_card == true)printf("Bad card,ignored.\n");else if(card_stored[rank][suit]){printf("card exists.ignored.\n");continue;}else{hand[number][0] = rank;hand[number][1] = suit;card_stored[rank][suit] = true;number ++;}//else }//while }//readvoid analyze(void) {straight = false;four = false;three = false;pairs = 0;flush = 0;bool flush_label = true;//straightint i;int array_copy[MAX_RANK] = {0};for(i = 0; i != MAX_NUMBER ;++i)array_copy[hand[i][0]] ++;for(i = 0; i != MAX_RANK && array_copy[i] == 0; ++i){}int index_of_no_zero = i;int cnt_of_sequence = 0;for(i = index_of_no_zero; i!= MAX_RANK && array_copy[i] != 0;++i){cnt_of_sequence ++;}if(cnt_of_sequence == 5)straight == true; //fourfor(i = 0; i != MAX_RANK ;++i ){if(array_copy[i] == 4)four = true;else if(array_copy[i] == 3)three = true;else if(array_copy[i] == 2)pairs ++;else ;} //flushfor(i = 0; i != MAX_NUMBER - 1;++i){if(hand[i][1] != hand[i + 1][1]){flush_label = false;break;}}flush = flush_label; }void print(void) {if(straight && flush) printf("Straight flush\n");else if(four) printf("Four\n");else if(three && pairs == 1)printf("Full house\n");else if(flush) printf("Flush\n");else if(straight) printf("Straight\n");else if(three) printf("Three\n");else if(pairs == 2) printf("Two pairs\n");else if(pairs == 1) printf("Pairs\n");else printf("High card\n");printf("\n"); }lib.h文件:
#ifndef INCLUDE_H_LIB #define INCLUDE_H_LIB#include <stdio.h> void read(void); void analyze(void); void print(void);#endif分離式編譯注意問題:
(1)關(guān)于函數(shù)的全局變量不能定義在自定義庫lib.h中,因?yàn)槿缦略?#xff0c;
假設(shè)定義在庫lib.h中,那么分離式編譯function.c?和main.c都各自需要一個(gè)lib.h,但是這樣編譯時(shí)候就會(huì)產(chǎn)生每個(gè)函數(shù)重復(fù)定義錯(cuò)誤。所以,全局變量必須此時(shí)放在function.c中
(2)function.c程序如果循環(huán)體內(nèi)有三個(gè)getchar(),那么如果中途需要continue,那么必須放在三個(gè)getchar()之后。使用循環(huán)必須注意循環(huán)的”核心“.因?yàn)間etchar() 不會(huì)放過任何一個(gè)字符,例如"\n",所以每次的結(jié)束循環(huán)動(dòng)作必須放在三個(gè)getchar()之后,否則"\n"會(huì)被不正確的地方讀入。
總結(jié)
以上是生活随笔為你收集整理的c语言程序设计现代方法第二版 第10章程序设计题3题,自己编写的一个程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu18下安装微信
- 下一篇: ubuntu18.04 安装qt5.12