在C程序中实现结构和函数

时间:2014-02-09 18:04:57

标签: c struct parameter-passing

作为大学的一项任务,我不得不用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 ;
 }

1 个答案:

答案 0 :(得分:1)

像这样:

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);