我试图从Dietels做一个问题,C怎么编程 - 第7版“书。它说我应该交两张五张牌,看看哪个更好。我甚至没有把它拿到手上比较,我有一个很大的问题。 在我将给你的代码中,一对,三,扑克,同花顺和直的信号都不好。它们应该变为2,如果有2对卡,那只是信号“对”和1,如果程序找到一对,一个三种,一种四种,直的或齐平的。相反,它总是给我一个大于1的数字,即使我将变量设置为等于0,1或2.(参见我的代码中的第一个开关函数。) 输出很好,它告诉我手中的东西是正确的,但增量和方程式陈述不是,它给了我垃圾。 你能告诉我我做错了什么吗?是{if while}部分功能还是......?
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)]
[String] $InputData,
[ValidateSet('Mode1', 'Mode2', 'Mode3')]
[string]$Mode = 'Mode1'
)
$StringArray = $InputData -split ','
$count = $StringArray.Count
Write-Verbose ("String array count ($count): $StringArray")
这是我的主要内容:
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
void deal( unsigned int wDeck[][ FACES ], const char *wFace[],const char *wSuit[], char hand[6][150] ){
size_t row;
size_t column;
size_t card;
for ( card = 0; card <= 5; ++card ) {
for ( row = 0; row < SUITS; ++row ) {
for ( column = 0; column < FACES; ++column ) {
if ( wDeck[ row ][ column ] == card ) {
strcat(hand[card],wFace[column]);
strcat(hand[card]," ");
strcat(hand[card],wSuit[row]);
}
}
}
}
}
void shuffle( unsigned int wDeck[][ FACES ] ) {
size_t row;
size_t column;
size_t card;
for ( card = 1; card <= CARDS; ++card ) {
do {
row = rand() % SUITS;
column = rand() % FACES;
} while( wDeck[ row ][ column ] != 0 );
wDeck[ row ][ column ] = card;
}
}
int fixNshow (char hand[6][150], char handFIXED[5][150]){
char faces[5][20];
char suits[5][20];
char *token;
for(int i=0; i<5; i++){
strcpy(handFIXED[i],hand[i+1]);
}
for(int i=0; i<5; i++){
printf("%s\n",handFIXED[i]);
}
for(int i=0; i<5; i++){
token=strtok(handFIXED[i]," ");
if(token!=NULL){
strcpy(faces[i],token);
token=strtok(NULL," ");
if(token!=NULL){
strcpy(suits[i],token);
}
}
}
printf("\n");
//Evaluating the hand.
int pair=0;
int three=0;
int poker=0;
int flush=0;
int straight=0;
int no=0;
int tokentwo=0;
int pass=2;
int j=1;
int g=1;
char faceatm[5][20],suitatm[20];
int hardness[4];
int swap=0;
int temp=0;
int totalpoints=0;
int highcard=0;
//Pairs, tripples, pokers, two pairs
for(int i=0; i<5; i++){
j=i+1;
g=i;
no=0;
strcpy(faceatm[i],faces[i]);
if(i>0){ //THIS
do{ //WHOLE
pass=strcmp(faceatm[i],faceatm[g-1]);//PART RESOLVES
g--; //DUPLICATE
if (pass==0){ //APPEARANCE
break; //OF
} //MATCHES
}while(g>0); //EXAMPLE
} //JACK
if (pass==0){ //JACK JACK
continue; //TEN, QUEEN
} //SIGNALS THAT ARE EQUAL TO 1 ARE: TRIPPLE,PAIR. THIS PREVENTS IT.
if(j<5){
do{
tokentwo=strcmp(faceatm[i],faces[j]);
if(tokentwo==0){
no++;
printf("ITERATION NUMBER %d, NO=%d\n",i,no);
}
j++;
}while(j<5);
}
switch(no){
case 1: printf("Pair of %ss\n",faceatm[i]); pair++; break;
case 2: printf("Three of %ss\n",faceatm[i]); three++; break;
case 3: printf("Poker of %ss\n",faceatm[i]); poker++; break;
}
}
//Flush catcher
strcpy(suitatm,suits[0]);
do{
tokentwo=strcmp(suitatm,suits[j]);
if(tokentwo==0){
no++;
}
j++;
}while(j<5);
if(no==4){
printf("FLUSH OF %s",suitatm);
flush++;
}
//Straight catcher
for(int i=0; i<5; i++){
if(strcmp("Ace",faces[i])==0){
hardness[i]=1;
}
else if(strcmp("Deuce",faces[i])==0){
hardness[i]=2;
}
else if(strcmp("Three",faces[i])==0){
hardness[i]=3;
}
else if(strcmp("Four",faces[i])==0){
hardness[i]=4;
}
else if(strcmp("Five",faces[i])==0){
hardness[i]=5;
}
else if(strcmp("Six",faces[i])==0){
hardness[i]=6;
}
else if(strcmp("Seven",faces[i])==0){
hardness[i]=7;
}
else if(strcmp("Eight",faces[i])==0){
hardness[i]=8;
}
else if(strcmp("Nine",faces[i])==0){
hardness[i]=9;
}
else if(strcmp("Ten",faces[i])==0){
hardness[i]=10;
}
else if(strcmp("Jack",faces[i])==0){
hardness[i]=11;
}
else if(strcmp("Queen",faces[i])==0){
hardness[i]=12;
}
else if(strcmp("King",faces[i])==0){
hardness[i]=13;
}
}
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
if(hardness[i]>hardness[i+1]){
swap=hardness[i];
hardness[i]=hardness[i+1];
hardness[i+1]=swap;
}
}
}
if(hardness[0]==hardness[1]-1==hardness[2]-2==hardness[3]-3==hardness[4]-4){
straight++;
}
//End of hand evaluation
//Points
highcard=hardness[4];
if(pair==0 && three==0 && poker==0 && straight==0 && flush==0){totalpoints=highcard;}
else if(pair==1 && three==0 && poker==0 && straight==0 && flush==0){totalpoints=14;}
else if(pair==2 && three==0 && poker==0 && straight==0 && flush==0){totalpoints=15;}
else if(pair==0 && three==1 && poker==0 && straight==0 && flush==0){totalpoints=16;}
else if(pair==0 && three==0 && poker==0 && straight==1 && flush==0){totalpoints=17;}
else if(pair==0 && three==0 && poker==0 && straight==0 && flush==1){totalpoints=18;}
else if(pair==1 && three==1 && poker==0 && straight==0 && flush==0){totalpoints=19;}
else if(pair==0 && three==0 && poker==1 && straight==0 && flush==0){totalpoints=20;}
else if(pair==0 && three==0 && poker==0 && straight==1 && flush==1){
if(hardness[0]==1 && hardness[1]==10 && hardness[2]==11 && hardness[3]==12 && hardness[4]==13){
totalpoints=22;
}
else {
totalpoints=21;
}
}
return totalpoints;
}
#endif
所以,当我运行游戏时,我的输出就是这个:
#include "functions.h"
int main(void) {
const char *suit[ SUITS ] ={ "Hearts", "Diamonds", "Clubs", "Spades" };
const char *face[ FACES ] ={ "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
unsigned int deck[ SUITS ][ FACES ] = { 0 };
char hand[6][150]={""};
char handFIXED[5][150]={""};
int handpoints;
srand( time( NULL ) );
shuffle( deck );
deal(deck,face,suit,hand);
handpoints=fixNshow(hand,handFIXED);
printf("HAND STRONGNESS: %d",handpoints);
}
它给了我一个很好的信息,我手里拿着什么,但这种手的强度绝不好。这意味着我的对信号是1,它不应该是。
我做错了什么?