- 註冊時間
- 2006-12-30
- 最後登錄
- 2024-3-4
- 主題
- 查看
- 積分
- 19
- 閱讀權限
- 20
- 文章
- 22
- 相冊
- 0
- 日誌
- 0
狀態︰
離線
|
第一個 程式 void 用法
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
enum Status { CONTINUE, WON, LOST };
int rollDice( void );
int main()
{
int sum;
int myPoint;
enum Status game;
srand( time( NULL ) );
sum = rollDice();
switch( sum ) {
case 7:
case 11:
game = WON;
break;
case 2:
case 3:
case 12:
game = LOST;
break;
default:
game = CONTINUE;
myPoint = sum;
printf( "Point is %d\n", myPoint );
break;
}
while ( game == CONTINUE ) {
sum = rollDice();
if ( sum == myPoint ) {
game = WON;
}
else {
if ( sum == 7 ) {
game = LOST;
}
}
}
if ( game == WON ) {
printf( "Player wins\n" );
}
else {
printf( "Player loses\n" );
}
return 0;
}
int rollDice( void )
{
int die1;
int die2;
int workSum;
die1 = 1 + ( rand() % 6 );
die2 = 1 + ( rand() % 6 );
workSum = die1 + die2;
printf( "Player rolled %d + %d = %d\n", die1, die2, workSum );
return workSum;
}
第二個程式 void 用法
/* Fig. 5.12: fig05_12.c
A scoping example */
#include <stdio.h>
void useLocal( void ); /* function prototype */
void useStaticLocal( void ); /* function prototype */
void useGlobal( void ); /* function prototype */
int x = 1; /* global variable */
/* function main begins program execution */
int main()
{
int x = 5; /* local variable to main */
printf("local x in outer scope of main is %d\n", x );
{ /* start new scope */
int x = 7; /* local variable to new scope */
printf( "local x in inner scope of main is %d\n", x );
} /* end new scope */
printf( "local x in outer scope of main is %d\n", x );
useLocal(); /* useLocal has automatic local x */
useStaticLocal(); /* useStaticLocal has static local x */
useGlobal(); /* useGlobal uses global x */
useLocal(); /* useLocal reinitializes automatic local x */
useStaticLocal(); /* static local x retains its prior value */
useGlobal(); /* global x also retains its value */
printf( "\nlocal x in main is %d\n", x );
return 0; /* indicates successful termination */
} /* end main */
/* useLocal reinitializes local variable x during each call */
void useLocal( void )
{
int x = 25; /* initialized each time useLocal is called */
printf( "\nlocal x in useLocal is %d after entering useLocal\n", x );
x++;
printf( "local x in useLocal is %d before exiting useLocal\n", x );
} /* end function useLocal */
/* useStaticLocal initializes static local variable x only the first time
the function is called; value of x is saved between calls to this
function */
void useStaticLocal( void )
{
/* initialized only first time useStaticLocal is called */
static int x = 50;
printf( "\nlocal static x is %d on entering useStaticLocal\n", x );
x++;
printf( "local static x is %d on exiting useStaticLocal\n", x );
} /* end function useStaticLocal */
/* function useGlobal modifies global variable x during each call */
void useGlobal( void )
{
printf( "\nglobal x is %d on entering useGlobal\n", x );
x *= 10;
printf( "global x is %d on exiting useGlobal\n", x );
} /* end function useGlobal */
兩者的Void 用法 有何不同 因為是新手 爬了很多文 還是不太懂 拜託救救新手 |
-
總評分: 威望 + 2
查看全部評分
|