繁体   English   中英

在C porgran中实现构造函数

[英]Implementing structs an functions in a C porgran

作为大学的一项工作,我不得不用C语言编写一台老虎机。虽然可以工作,但是我必须对其进行更改以实现结构和功能。 任何人有任何想法如何更改我的代码来满足此要求? 我有一个工作的功能,但我无法弄清楚其余的。 需要为中途看到的列部分实现以下结构:

 col1 = rand() % 3 + 1;
 col2 = rand() % 3 + 1;
 col3 = rand() % 3 +1; 
  #include <stdio.h>
  #include <stdlib.h>
  #include <math.h>
  #include <time.h>

  float Calculate( float credits, float bet, int col1, int col2, int col3) 
{

    if( (col1 == col2) && (col2 == col3) )
    {
        printf("\n \n WINNER!!");
        credits = credits + (bet*2) ;
    }

    else if( (col1==col2) || (col1==col3) || (col2==col3) )
        {
            printf ("\n\n Hard luck, you nearly got it. ") ;
            credits = credits- (bet*0.5) ;
        }

    else
    {
        printf("\n \n Sorry you lost.");
        credits = credits - bet ;
    }

    return credits ;    
} 




 int main ()
  {
 setbuf(stdin, NULL);
 setbuf(stdout, NULL);

   float credits=10, bet;
   int col1, col2, col3 ;
   char ans, dummy ;

printf ("\n\n *********** Welcome To My Slot Machine. ************\n\n") ;

    while (credits >= 2 )
 {
printf("\n You have %f credits", credits) ;

printf(" \n\n Please enter the amount you wish to bet: ");
scanf (" %f", &bet ) ; 

    if ( bet > credits)
    {
        printf ("\nYou can only bet what you have.") ;
        continue ;
    }

    if ( bet < 2)
    {
        printf("\nYou must bet at least 2 tokens!");
        continue ;
    }

    srand(time(NULL));

    col1 = rand() % 3 + 1;
    col2 = rand() % 3 + 1;
    col3 = rand() % 3 +1;

    switch (col1) {
        case 1:
            printf("\n\n |Apple| \t") ;
            break;

        case 2:
            printf ("\n\n |Orange| \t ");
            break;

        case 3 :
            printf ("\n\n |Banana| \t ") ;
            break ;

    }

    switch (col2) {
        case 1:
            printf(" |Apple| \t") ;
            break;

        case 2:
            printf (" |Orange| \t ");
            break;

        case 3 :
            printf (" |Banana| \t ") ;
            break ;

    }

    switch (col3) {
        case 1:
            printf(" |Apple| \n \t") ;
            break;

        case 2:
            printf (" |Orange| \n \t ");
            break;

        case 3 :
            printf (" |Banana| \n\t ") ;
            break ;

    }


credits = Calculate(credits, bet, col1, col2, col3 ) ;

  if (credits < 2 )
{
printf(" \n\n\n Sorry You Do not have enough to play. Thank you for playing! \n\n\n") ;
    return 0 ;
}

 else if ( credits>= 2 )
{
    printf ("\n\n\n Would you like to play again? y/n : ", credits) ;
    fflush(stdin) ;
    scanf("%c", &ans) ;


        if ( ans == 'y' )
            {
                continue ;
            }

        else if (ans == 'n' )
            {
printf ("\n\n\n Thank you for playing! You walk away  with %f credits", credits ) ;
                return 0 ;
            }
}
  }


    return 0 ;
 }

像这样:

struct SlotMachine {
    int col1;
    int col2;
    int col3;
    float credits;
    float bet;
};

void Calculate(struct SlotMachine* sm) 
{

    if( (sm->col1 == sm->col2) && (sm->col2 == sm->col3) )
    {
        printf("\n \n WINNER!!");
        sm->credits = sm->credits + (sm->bet*2) ;
    }

    else if( (sm->col1==sm->col2) || (sm->col1==sm->col3) || (sm->col2==sm->col3) )
        {
            printf ("\n\n Hard luck, you nearly got it. ") ;
            sm->credits = sm->credits- (sm->bet*0.5) ;
        }

    else
    {
        printf("\n \n Sorry you lost.");
        sm->credits = sm->credits - sm->bet ;
    }
} 

您会这​​样称呼:

//initialize the members of the struct:
struct SlotMachine sm = { .col1 = 0, .col2 = 0, .col3 = 0, .credits = 0.0, .bet = 0.0};

//assign your values to the members of `sm`
//...

//perform the calculation:
Calculate(&sm);

//sm has been updated, now use the values in it as required:

//e.g.: look at the value of credits:
printf("Credits: %f\n", sm.credits);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM