我正在制作一个随机模拟游戏节目的程序。该计划选择三个门中的一个,以便随机隐藏奖品。电脑选择一扇门,看看它是否赢了。如果我改变我的选择而不是改变它,那么循环超过10000次以查看我赢了多少次。
我得到了一堆语法
#include <path-spec>
#include <stdio.h>
#include <time.h>
void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed);
void randomize_door(Door* pJackpot);
void pick_door(Door* pPick);
Status check(Door* pPick, Door* pJackpot);
enum status {WIN=1,LOSE};
enum door { FIRST=1, SECOND, THIRD };
typedef enum door Door;
typedef enum status Status;
int main(int argc, char* argv[]){
int i = 0;
srand(time);
Status result;
Status* pResult = &result ;
Door jackpot, pick, last_pick=NULL;
Door* pJackpot = &jackpot, * pPick=&pick, *pLast_pick;
int win_unchanged = 0, win_changed=0;
int* pWin_unchanged = &win_unchanged, *pWin_changed=&win_changed;
while (i < 10000){
last_pick = NULL;
randomize_door(pJackpot);
pick_door(pPick);
result = check(pPick, pJackpot);
count(result, pLast_pick, pPick, pWin_unchanged, pWin_changed);
i++;
}
printf("Wins when changed choice: %d , wins when choice is unchanged: %d", win_changed, win_unchanged);
return 0;
}
void randomize_door(Door* pJackpot){
*pJackpot = rand() % 3 + 1;
}
void pick_door(Door* pPick){
*pPick = rand() % 3 + 1;
}
Status check(Door* pPick, Door* pJackpot){
if (*pPick == *pJackpot){
return WIN;
}
else{
return LOSE;
}
}
void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed) {
if (*pLast_pick == *pPick){
if (result == WIN){
*pWin_unchanged++;
}
}
else{
if (result == WIN){
*pWin_changed++;
}
}
*pLast_pick = *pPick;
}
以下是错误以及发生的错误。其中大部分都被遗忘}
或;
,这在函数标题中没有任何意义。
1>------ Build started: Project: Program4.1, Configuration: Debug Win32 ------
1> daily4.c
daily4.c(4): error C2146: syntax error : missing ')' before identifier 'result'
daily4.c(4): error C2061: syntax error : identifier 'result'
daily4.c(4): error C2059: syntax error : ';'
daily4.c(4): error C2059: syntax error : ','
daily4.c(4): error C2059: syntax error : ')'
daily4.c(6): error C2143: syntax error : missing ')' before '*'
daily4.c(6): error C2143: syntax error : missing '{' before '*'
daily4.c(6): error C2059: syntax error : ')'
daily4.c(8): error C2143: syntax error : missing ')' before '*'
daily4.c(8): error C2143: syntax error : missing '{' before '*'
daily4.c(8): error C2059: syntax error : ')'
daily4.c(10): error C2061: syntax error : identifier 'check'
daily4.c(10): error C2059: syntax error : ';'
daily4.c(10): error C2143: syntax error : missing ')' before '*'
daily4.c(10): error C2143: syntax error : missing '{' before '*'
daily4.c(10): error C2143: syntax error : missing ';' before '*'
daily4.c(10): error C2059: syntax error : ')'
daily4.c(16): error C2370: 'Door' : redefinition; different storage class
daily4.c(10) : see declaration of 'Door'
daily4.c(21): warning C4013: 'srand' undefined; assuming extern returning int
daily4.c(24): error C2146: syntax error : missing ';' before identifier 'jackpot'
daily4.c(24): error C2065: 'jackpot' : undeclared identifier
daily4.c(24): error C2065: 'pick' : undeclared identifier
daily4.c(24): error C2065: 'last_pick' : undeclared identifier
daily4.c(24): warning C4047: '=' : 'int' differs in levels of indirection from 'void *'
daily4.c(25): error C2297: '*' : illegal, right operand has type 'int *'
daily4.c(25): error C2065: 'jackpot' : undeclared identifier
daily4.c(25): error C2065: 'pick' : undeclared identifier
daily4.c(25): warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
daily4.c(25): error C2065: 'pLast_pick' : undeclared identifier
daily4.c(25): error C2100: illegal indirection
daily4.c(30): error C2065: 'last_pick' : undeclared identifier
daily4.c(30): warning C4047: '=' : 'int' differs in levels of indirection from 'void *'
daily4.c(31): warning C4013: 'randomize_door' undefined; assuming extern returning int
daily4.c(32): warning C4013: 'pick_door' undefined; assuming extern returning int
daily4.c(33): warning C4013: 'check' undefined; assuming extern returning int
daily4.c(34): warning C4013: 'count' undefined; assuming extern returning int
daily4.c(34): error C2065: 'pLast_pick' : undeclared identifier
daily4.c(43): error C2143: syntax error : missing ')' before '*'
daily4.c(43): error C2143: syntax error : missing '{' before '*'
daily4.c(43): error C2059: syntax error : ')'
daily4.c(43): error C2054: expected '(' to follow 'pJackpot'
daily4.c(48): error C2143: syntax error : missing ')' before '*'
daily4.c(48): error C2143: syntax error : missing '{' before '*'
daily4.c(48): error C2059: syntax error : ')'
daily4.c(48): error C2054: expected '(' to follow 'pPick'
daily4.c(53): error C2143: syntax error : missing ')' before '*'
daily4.c(53): error C2143: syntax error : missing '{' before '*'
daily4.c(53): error C2143: syntax error : missing ';' before '*'
daily4.c(53): error C2059: syntax error : ')'
daily4.c(53): error C2054: expected '(' to follow 'pJackpot'
daily4.c(62): error C2143: syntax error : missing ')' before '*'
daily4.c(62): error C2081: 'Door' : name in formal parameter list illegal
daily4.c(62): error C2143: syntax error : missing '{' before '*'
daily4.c(62): error C2143: syntax error : missing ';' before '*'
daily4.c(62): error C2059: syntax error : 'type'
daily4.c(62): error C2059: syntax error : ')'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
答案 0 :(得分:2)
要修复的示例
#include <stdio.h>
#include <time.h>
#include <stdlib.h> //for srand
//enum and typedef defined prior to use
enum status {WIN=1,LOSE};
enum door { NONE, FIRST=1, SECOND, THIRD };//Define NONE instead of NULL
typedef enum door Door;
typedef enum status Status;
void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed);
void randomize_door(Door* pJackpot);
void pick_door(Door* pPick);
Status check(Door* pPick, Door* pJackpot);
int main(int argc, char* argv[]){
int i = 0;
Status result;
Status* pResult = &result ;
Door jackpot, pick, last_pick=NONE;
Door* pJackpot = &jackpot, * pPick=&pick, *pLast_pick;
int win_unchanged = 0, win_changed=0;
int* pWin_unchanged = &win_unchanged, *pWin_changed=&win_changed;
srand(time(NULL));//If you use the equivalent of the C89 compiler can not be mixed the declaration statement and execution statements.
...