如何在其他函数内部编写函数? [英] How to write function inside other function ?

查看:64
本文介绍了如何在其他函数内部编写函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究这个二十一点游戏..现在这个代码没有完成,但是我面临着在另一个函数中调用函数的问题,例如如何使用参数corectly。

例如:在chickcoputercards功能我知道我打电话给chickcomputercards的方式不正确..怎么做这个写方式?



我的代码是:

游戏基本上是,计算机有两张随机卡和一张特殊卡,如果两张卡+特殊卡< 21计算机获得额外的卡,直到总金额> 21 .. 

然后玩家获得两张随机卡,他可以选择 if 他可以选择如果他想要额外的卡,最后总和 接近 21 但不大于 21 获胜!

更多规则,但现在我首先要解决这些问题..





我的尝试:



  #include   <   stdio.h  >  
#include < stdlib.h >

#define MIN_BET 1
#define INITIAL_MONEY 1000
/ * 将#defines和typedef放在这里* /

int rand_range( int low, int high) // RAND_MAX假定为32767
{
static unsigned int next = 1 ;
next = next * 1103515245 + 12345 ;
return (( unsigned int )(next / 65536)% 32768 )%(高 - 低+ 1 )+ low;
}

void srand_range( unsigned int seed)
{
for int i = 0 ;( unsigned int )i< seed; i ++){
rand_range(i,i + 1);
}
}

int bett( int initialMoney)
{
int riskedMoney;
printf( 你现在有%d假美元。你想赌多少钱?\ n ,initialMoney);
scanf( %d,& riskedMoney);
while ((riskedMoney> initialMoney)&&(riskedMoney< min_bet mode = hold /> {
printf( 您的赌注必须至少为MIN_BET且最多为%d,请再试一次:\ n,initialMoney);
scanf( %d,& riskedMoney);
}
return (riskedMoney) ;
}

int 选择计算机卡( int first_card, int second_card)
{
int sum = 0 ;
first_card = rand_range( 1 13 );
second_card = rand_range( 1 , 13 );
printf( 计算机得到%d和%y。,first_card,second_card) ;
sum = first_card + second_card;
return (sum);
}

int chickcomputercards( int sum, int special_card, int bet)
{
int extra_card;
sum = choosecomputercards(first_card,second_card);
while ((sum + special_card)< 21)
{
extra_card = rand_range( 1 13 );
if ((sum + special_card + extra_card)< = 21
{
printf( 额外卡:%d。,extra_card);
sum + = extra_card;
}
}
如果((sum + special_card)> 21)
{
printf( 计算机超过21 \ n);
printf( 你赢了%d假美元!\ n,下注) ;
}
return (sum);
}

int chooseplayercard( int first_card, int second_card)
{
first_card = rand_range( 1 13 );
second_card = rand_range( 1 13 );
printf( 你得到%d和%d,first_card,second_card);
return (first_card + second_card);
}

int chickplayersum( int sum, int special_card, int bet)
{
int extra_card;
int money;
bet = bett(money)
sum = chosenplayercards;
char player_answer;
while (sum< 21)
{
printf( 当前总和是%d。你想要另一张卡吗?(Y / N)\ n,sum);
scanf( %c& player_answer);
while (player_answer == ' Y'
{
extra_card = rand_range( 1 13 ) ;
printf( 额外卡:%d。\ n,extra_card);
sum = sum + extra_card;
}
如果(player_answer =!' Y'
return (sum);
}
如果(总和> 21)
printf( 计算机赢了这一轮......你输掉了假美元!\ n,下注)
}

chickwinner ()
{
achickcomputercards( int sum, int special_card, int bet)}

int main()
{
int money = INITIAL_MONEY;
int seed = 0 ;
int bet = 0 ;
int special_card;
int first_card,second_card;
int computer_cards;
int total_computer_cards;
int player_cards,total_player_cards;
printf( 欢迎来到CS介绍二十一点!\ n);
printf( 首先输入随机种子:\ n);
scanf( %d,& seed);
srand_range(种子);
while (money> = 1
{
bet = BETT(钱);
computer_cards = choosecomputercards(first_card,second_card);
special_card = find_special_card;
total_computer_cards = chickcomputercards(computer_cards,special_card,bet);
player_cards = chosenplayercard(first_card,second_card);
total_player_cards = chickplayersum(player_cards,special_card);

return 0 ;
}

解决方案

写函数内幕其他函数是一件事另一个函数里面的调用函数是完全的,完全不同。



C不支持拧功能内部;这被称为内部函数,并受到其他(更强大的;-))语言的支持。



关于调用另一个函数内的调用函数 ,情况正好相反:你在C中执行的所有调用C代码都是调用内部另一个函数。此外,你编写的每个函数,如果被调用,被称为调用内部另一个函数,不包括入口点函数(main),但是甚至这个函数在一些运行时环境后面调用,你不直接在开发应用程序时使用修改。



这样,你的问题显然被简化为一个更普遍的问题,如何调用函数?通过学习C和一般编程的基础知识可以回答这个问题。特别是,请参阅:

C函数 [ ^ ],

C中的值函数调用 [ ^ ],

C中的引用函数调用 [ ^ ]。



如果您有更多与您的代码相关的特定问题,您必须提出其他问题,以及...更具体的代码。但在此之前...如果您有一些编译错误,请收集准确而全面的错误信息;如果您有运行时错误,请执行相同的操作,但最好使用C ++(您可以将所有代码放在C ++项目中)并观察异常,然后提供全面的异常信息。在所有情况下,在您收到错误或抛出异常的行中添加一些注释,并在您对问题的描述中引用这些注释。



-SA

i am working on this blackjack game.. now this code is not completed, but i am facing problem to call function inside another function ,for example how to use parametrs corectly.
for example: in chickcoputercards function i know the way i called chickcomputercards in incorrect .. how to do this the write way ?

my code is:

the game is basicly, the computer gets two random cards , and a special card, if the two cards+special card <21 the computer gets extra cards untill the total sum is>21..

then the player gets two random cards , and he can chose if he can chose if he wants extra cards , at the end who has total sum that is closer to 21 but not bigger than 21 wins !

there is more rules , but now i am trieng to solve these problems first.. 



What I have tried:

#include <stdio.h>
#include <stdlib.h>

#define MIN_BET 1
#define INITIAL_MONEY 1000
/* put your #defines and typedefs here*/

int rand_range (int low, int high) // RAND_MAX assumed to be 32767
{
    static unsigned int next = 1;
    next = next * 1103515245 + 12345;
    return ((unsigned int)(next/65536) % 32768) % (high - low + 1) + low;
}

void srand_range (unsigned int seed)
{
    for (int i = 0; (unsigned int)i < seed; i++) {
        rand_range(i,i+1);
    }
}

int bett(int initialMoney)
  {
      int riskedMoney;
      printf("You currently have %d fake dollars. How much do you want to bet?\n",initialMoney );
      scanf("%d",&riskedMoney);
      while((riskedMoney>initialMoney)&&(riskedMoney<min_bet mode="hold" />      {
         printf("Your bet must be at least MIN_BET and at most %d, try again:\n",initialMoney );
         scanf("%d",&riskedMoney);
      }
      return(riskedMoney);
  }

int chosecomputercards(int first_card,int second_card)
{
    int sum=0;
    first_card=rand_range(1,13);
    second_card=rand_range(1,13);
    printf("The computer got %d and %y.",first_card,second_card);
    sum=first_card+second_card;
    return(sum);
}

int chickcomputercards(int sum,int special_card,int bet)
{
    int extra_card;
    sum=chosecomputercards(first_card,second_card);
    while((sum+special_card)<21)
    {
        extra_card=rand_range(1,13);
        if((sum+special_card+extra_card)<=21)
        {
           printf("Extra card:%d.",extra_card);
           sum+=extra_card;
        }
    }
    if((sum+special_card)>21)
    {
        printf("The computer got more than 21\n");
        printf("You have won %d fake dollars!\n",bet);
    }
    return(sum);
}

int choseplayercard(int first_card,int second_card)
{
    first_card=rand_range(1,13);
   second_card=rand_range(1,13);
    printf("You got %d and %d",first_card,second_card);
    return(first_card+second_card);
}

int chickplayersum(int sum,int special_card,int bet)
{
    int extra_card;
    int money;
    bet=bett(money)
    sum=choseplayercards;
    char player_answer;
    while(sum<21)
    {
    printf("The current sum is %d. Do you want another card? (Y / N)\n",sum);
    scanf("%c"&player_answer);
    while(player_answer=='Y')
        {
           extra_card=rand_range(1,13);
           printf("Extra card:%d.\n",extra_card);
           sum=sum+extra_card;
        }
    if(player_answer=!'Y')
    return(sum);
    }
    if(sum>21)
    printf("The computer has won this round… you lose &d fake dollars!\n",bet)
}

chickwinner()
{
achickcomputercards(int sum,int special_card,int bet)}

int main()
{
  int money=INITIAL_MONEY;
  int seed=0;
  int bet=0;
  int special_card;
  int first_card,second_card;
  int computer_cards;
  int total_computer_cards;
  int player_cards,total_player_cards;
  printf("Welcome to CS intro Blackjack!\n");
  printf("Start by entering a random seed:\n");
  scanf("%d",&seed);
  srand_range(seed);
  while(money>=1)
  {
  bet=bett(money);
  computer_cards=chosecomputercards(first_card,second_card);
  special_card=find_special_card;
  total_computer_cards=chickcomputercards(computer_cards,special_card,bet);
  player_cards=choseplayercard(first_card,second_card);
  total_player_cards=chickplayersum(player_cards,special_card);

  return 0;
}

解决方案

"To write function insider other function" is one thing "call function inside another function" is totally, totally different.

C does not support wring "function inside function"; this is called "inner function" and is supported by number of other (more powerful ;-)) languages.

As to the call "call function inside another function", the situation is just the opposite: all calls you do in you C code is the "calls insider another function". Moreover, every function you write, if called, is called "calls insider another function", excluding the entry-point function ("main"), but even this function is called behind the scene by some runtime environment which you don't directly use of modify when you develop an application.

This way, your question is clearly reduced to a more general question, "how to call a function?" This question can be answered by learning the basics of C and general programming. In particular, please see:
C Functions[^],
Function call by Value in C[^],
Function call by reference in C[^].

If you have more specific problems related to your code, you have to ask other questions, well… more specific to your code. But before you do… if you have some compilation errors, collect precise and comprehensive error information; if you have runtime errors, do the same, but it would be better to use C++ (you can put all your code in C++ project) and observe the exception and then provide comprehensive exception information. In all cases, add some comments to the lines where you got the error or exception is thrown and refer to those comments in your description of the problem.

—SA


这篇关于如何在其他函数内部编写函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆