http://forum.ubuntu.org.cn/viewtopic.php?t=99934&highlight=
>> 本文固定链接: http://www.vcgood.com/archives/2079
>> 转载请注明: flyinflash 2008年01月08日 于 C语言帝国 发表
http://forum.ubuntu.org.cn/viewtopic.php?t=99934&highlight=
>> 本文固定链接: http://www.vcgood.com/archives/2079
>> 转载请注明: flyinflash 2008年01月08日 于 C语言帝国 发表
你必须先 登录才能发表评论。
/*
Brain Juice
Power by GNU * & Lee
20080105 start
20080108 fix input bug
Study Hall,Lib.,GDCP.
Lee <flyinflash@gmail.com>
Ar Liang
al_rand.h
Lee
Brain_Juice.c
*/
/*
Easy 1~10 20
Normal 1~20 30
Normal 1~30 40
Normal 1~100 50
*/
#include <stdio.h>
#include “al_rand.h”
#include <stdlib.h>
int arithmetic_unit(int factor_a,char operator_tmp,int factor_b)
{
switch(operator_tmp)
{
case ‘+’:
return factor_a+factor_b;
break;
case ‘-’:
return factor_a-factor_b;
break;
case ‘*’:
return factor_a*factor_b;
break;
default:
return factor_a/factor_b;
}
}
int get_operator_level(char tmp_operator)
{
switch (tmp_operator)
{
case ‘+’:
return 1;
break;
case ‘-’:
return 1;
break;
case ‘*’:
return 2;
break;
default:
return 2;
}
}
int calculator(int factor_1,char operator_1,int factor_2,char operator_2,int factor_3)
{
int operator1_level;
int operator2_level;
int tmp;
operator1_level = get_operator_level(operator_1);
operator2_level = get_operator_level(operator_2);
if (operator1_level > operator2_level || operator1_level == operator2_level)
{
tmp = arithmetic_unit(factor_1,operator_1,factor_2);
tmp = arithmetic_unit(tmp,operator_2,factor_3);
return tmp;
} else {
tmp = arithmetic_unit(factor_2,operator_2,factor_3);
tmp = arithmetic_unit(factor_1,operator_1,tmp);
return tmp;
}
}
void create_exp(int exp[6],int level)
{
int i,j;
int end_;
switch(level)
{
case 1:
end_ = 10;
break;
case 2:
end_ = 30;
break;
case 3:
end_ = 60;
break;
case 4:
end_ = 100;
break;
}
// create factors
for (i=1; i<=3; i++)
{
exp[(i-1)*2+1] = rand_num(1,end_);
}
// create operators
for (i=1; i<=2; i++)
{
j = rand_num(1,4);
switch(j)
{
case 1:
exp[i*2] = 43;
break;
case 2:
exp[i*2] = 45;
break;
case 3:
exp[i*2] = 42;
break;
default:
exp[i*2] = 47;
}
}
}
void quit_game(void)
{
char tmp_key[2];
do {
printf(“\n”);
printf(“ Are you sure to quit? (y/n) “);
scanf(“%1s”,tmp_key);
printf(“\n”);
// printf(“\n * Debug: tmp_key:%s \n”,tmp_key);
if (tmp_key[0] == ‘Y’ || tmp_key[0] == ‘y’)
{
printf(“\n”);
printf(“ *** Byebye *** “);
printf(“\n”);
abort();
}
} while(tmp_key[0] != ‘N’ && tmp_key[0] != ‘n’);
}
void start_info(void)
{
printf(“\n”);
printf(“ *** Brain Juice *** \n”);
printf(“ Power by GNU * & Lee \n”);
printf(“\n”);
printf(“ Press ‘Enter’ to continue. “);
getchar();
printf(“\n”);
}
int set_level(void)
{
int level;
char tmp_key;
do
{
if (level > 4 || level < 1)
{
printf(“\n”);
printf(“ ** Press Num Key to Choice a Level ** \n”);
printf(“\n”);
printf(“ 1.Easy -> 1~10 -> 20 Times \n”);
printf(“ 2.Normal -> 1~30 -> 30 Times \n”);
printf(“ 3.Hard -> 1~60 -> 40 Times \n”);
printf(“ 4.Extra Hard -> 1~100 -> 50 Times \n”);
printf(“ ”);
scanf(“%d”,&level);
}
} while (level > 4 || level < 1);
printf(“\n”);
switch (level)
{
case 1:
printf(“ * Easy -> 1~10 -> 20 Times \n”);
break;
case 2:
printf(“ * Normal -> 1~30 -> 30 Times \n”);
break;
case 3:
printf(“ * Hard -> 1~60 -> 40 Times \n”);
break;
default:
printf(“ * Extra Hard -> 1~100 -> 50 Times \n”);
}
printf(“\n”);
printf(“ Press ‘Enter’ to continue. “);
getchar();
// fix bug
getchar();
printf(“\n”);
return level;
}
void how_to_play(void)
{
printf(“\n”);
printf(“ ** How To Play ** \n”);
printf(“\n”);
printf(“ Input two operators of ‘+’ ‘-’ ‘*’ ‘/’ to calculate: \n”);
printf(“\n”);
printf(“ eg.3/2=1,\n “);
printf(“ 2/3=0. \n”);
printf(“\n”);
printf(“ Press ‘Enter’ to continue. “);
getchar();
printf(“\n”);
}
int playing(int level,int go_next_level)
{
int tmp_result = 0;
int result = 0;
char operators[3];
int times=1;
int exp[6];
double score = 0;
while (times)
{
create_exp(exp,level);
result = calculator(exp[1],(char)exp[2],exp[3],(char)exp[4],exp[5]);
do {
printf(“\n”);
printf(“ ** %2d/%2d Times,Level %d ** \n”,times,go_next_level,level);
printf(“\n”);
printf(” %2d %2d %2d = %d \n”,exp[1],exp[3],exp[5],result);
printf(“ ^ ^\n”);
printf(“ “);
scanf(“%2s”,operators);
if (operators[0] == ‘q’ || operators[0] == ‘Q’)
{
quit_game();
}
// printf(“\n * Debug: operator[0]:%c \n”,operators[0]);
// printf(“\n * Debug: operator[1]:%c \n”,operators[1]);
} while (
(operators[0] != ‘+’ && operators[0] != ‘-’ && operators[0] != ‘*’ && operators[0] != ‘/’)
|| (operators[1] != ‘+’ && operators[1] != ‘-’ && operators[1] != ‘*’ && operators[1] != ‘/’));
tmp_result = calculator(exp[1],operators[0],exp[3],operators[1],exp[5]);
if (tmp_result != result)
{
printf(“\n Pity! \n”);
printf(” %d %c %d %c %d = %d >< %d %c %d %c %d = %d \n”,exp[1],operators[0],exp[3],operators[1],exp[5],tmp_result,exp[1],exp[2],exp[3],exp[4],exp[5],result);
} else {
score += 1;
printf(“\n Bingo! \n”);
printf(” %d %c %d %c %d = %d \n”,exp[1],exp[2],exp[3],exp[4],exp[5],result);
}
times++;
printf(“\n”);
if (times > go_next_level)
{
break;
}
}
return score;
}
int main()
{
int level;
int last_level_times = 50;
int now_level_times;
int last_score = 0;
int level_score[5];
int i;
for (i=1; i<=4; i++)
{
level_score[i] = 0;;
}
start_info();
how_to_play();
level = set_level();
now_level_times = (level+1)*10;
while (now_level_times <= last_level_times)
{
level_score[now_level_times] = playing(level,now_level_times);
printf(“\n * Debug: Level %d Score: %d \n”,level,level_score[now_level_times]);
printf(“\n Change level automatically. \n”);
level += 1;
now_level_times += 10;
}
for (i=1; i<=4; i++)
{
last_score += level_score[i];
}
printf(“\n * Debug: Last Score: %d \n”,last_score);
printf(“\n”);
printf(“\n Game Over \n”);
printf(“\n”);
return 0;
}
#include<time.h>
#include<stdlib.h>
int rand_num(int min,int max)
{
int ret;
static int change_seed=0;
srand(change_seed+time(0));
ret=rand()%(max+1-min)+min;
if(change_seed<=1000)change_seed++;else change_seed=0;
return(ret);
}
/*————
14:23 2008-1-5
This function returns an integer between min to max.
Built By Arliang.
^_^
————*/