这个作业属于哪个班级 | C语言--网络2011/2012 |
---|---|
这个作业的地址 | C博客作业03--函数 |
这个作业的目标 | 学习循环结构内容,包括for循环、while循环、循环嵌套 |
姓名 | 杨振鹏 |
1.本章学习总结
1.1 函数定义,调用,声明
函数类型 函数名(参数)
e.g.int giao(int emmm);
1.2 全局,局部变量,静态局部变量
- 全局变量:在所有函数外定义的变量,它的作用域默认是整个程序。
- 局部变量:在函数内定义的变量,它的作用域只在该函数中。
- 静态局部变量:该变量会在一次调用函数时,继承上一次调用函数后的值
e.g. static int giao;
1.3 c数据类型及注意点
- 整型:short<int<long<long long
- 浮点型:float<double
- 字符型:char
- 浮点误差处理:后+0.00001
1.4 c运算符
1.综合作业
2.1 模块流程图
-
一年级
-
二年级
-
三年级
2.2 函数功能及全局变量
2.2.1 全局变量
无
2.2.2 菜单相关函数
void OperateMenu();//游戏操作界面
void GradeOneMenu();//一年级菜单
void GradeTwoMenu();//二年级菜单
void GradeThreeMenu();//三年级菜单
void GradeOneMenu()//一年级
{
while (1)
{
system("cls");
printf("*********************一年级小朋友好!********************************
");
printf("小兔崽子,你想做多少题呀?输入你要做的题数啊!
");
printf("你要是怕了就输入n退回主菜单。
");
printf("***************************************************************
");
printf("输入题数(怕了输n):
");
char ch;
int TitleNumber, i, temp;
int Right = 0;
int Number1, Number2, symbol, RightAnswer, YourAnswer;
double accuracy;
srand(time(0));
scanf("
%c", &ch);
if (ch == 'n')
{
Sleep(500);
OperateMenu();
return;
}
else//选择答题
{
TitleNumber = ch - '0';
for (i = 0; i < TitleNumber; i++)
{
Number1 = RandNumber(10);
Number2 = RandNumber(10);
symbol = RandSymbol(1);
if (symbol == '-')
{
if (Number1 < Number2)
{
temp = Number1; Number1 = Number2; Number2 = temp;
}
}
printf("%d %c %d = ?
", Number1, symbol, Number2);
printf("来吧宝贝输入你的答案:
");
scanf("%d", &YourAnswer);
switch (symbol)
{
case '+':RightAnswer = Number1 + Number2; break;
case '-':RightAnswer = Number1 - Number2; break;
case '*':RightAnswer = Number1 * Number2; break;
case '/':RightAnswer = Number1 / Number2; break;
}
if (YourAnswer == RightAnswer) {
Right++; printf("强啊!
");
}
else { printf("菜鸡,正确答案:%d
", RightAnswer); }
printf("觉得太菜不想做了就输n,不然就输y继续
");
scanf("
%c", &ch);
if (ch == 'n')
{
Sleep(500);
GradeOneMenu();
}
}
accuracy = (double(Right) / TitleNumber) * 100;
if (accuracy < 60)printf("小老弟你不行啊,正确率:%.0f%", accuracy);
else printf("可以啊小老弟,正确率:%.0f%c", accuracy, '%');
printf("还要再来吗?
");
printf("请输入y/n:");
scanf("
%c", &ch);
if (ch == 'n')
{
system("cls");
Sleep(500);
OperateMenu();
return;
}
else
{
system("cls");
Sleep(500);
GradeOneMenu();
return;
}
}
}
}
void GradeTwoMenu()//二年级
{
while (1)
{
system("cls");
printf("*********************二年级小朋友好!********************************
");
printf("小兔崽子,你想做多少题呀?输入你要做的题数啊!
");
printf("你要是怕了就输入n退回主菜单。
");
printf("***************************************************************
");
printf("输入题数(怕了输n):
");
char ch;
int TitleNumber, i, temp;
int Right = 0;
int Number1, Number2, symbol, RightAnswer, YourAnswer;
double accuracy;
srand(time(0));
scanf("
%c", &ch);
if (ch == 'n')
{
Sleep(500);
OperateMenu();
return;
}
else//选择答题
{
TitleNumber = ch - '0';
for (i = 0; i < TitleNumber; i++)
{
Number1 = RandNumber(20);
Number2 = RandNumber(20);
symbol = RandSymbol(2);
if (symbol == '-')
{
if (Number1 < Number2)
{
temp = Number1; Number1 = Number2; Number2 = temp;
}
}
if (symbol == '/')
{
Number2= RandNumber(5);
Number1 = Number2 * (rand()%10+1);
}
if (symbol == '*')
{
Number1 /= 2;
Number2 /= 2;
}
printf("%d %c %d = ?
", Number1, symbol, Number2);
printf("来吧宝贝输入你的答案:
");
scanf("%d", &YourAnswer);
switch (symbol)
{
case '+':RightAnswer = Number1 + Number2; break;
case '-':RightAnswer = Number1 - Number2; break;
case '*':RightAnswer = Number1 * Number2; break;
case '/':RightAnswer = Number1 / Number2; break;
}
if (YourAnswer == RightAnswer) {
Right++; printf("强啊!
");
}
else { printf("菜鸡,正确答案:%d
", RightAnswer); }
printf("觉得太菜不想做了就输n,不然就输y继续
");
scanf("
%c", &ch);
if (ch == 'n')
{
Sleep(500);
GradeTwoMenu();
}
}
accuracy = (double(Right) / TitleNumber) * 100;
if (accuracy < 60)printf("小老弟你不行啊,正确率:%.0f%", accuracy);
else printf("可以啊小老弟,正确率:%.0f%c", accuracy, '%');
printf("还要再来吗?
");
printf("请输入y/n:");
scanf("
%c", &ch);
if (ch == 'n')
{
system("cls");
Sleep(500);
OperateMenu();
return;
}
else
{
system("cls");
Sleep(500);
GradeTwoMenu();
return;
}
}
}
}
void GradeThreeMenu()//二年级
{
while (1)
{
system("cls");
printf("*********************二年级小朋友好!********************************
");
printf("小兔崽子,你想做多少题呀?输入你要做的题数啊!
");
printf("你要是怕了就输入n退回主菜单。
");
printf("***************************************************************
");
printf("输入题数(怕了输n):
");
char ch;
int TitleNumber, i, temp;
int Right = 0;
int Number1, Number2, symbol, RightAnswer, YourAnswer;
double accuracy;
srand(time(0));
scanf("
%c", &ch);
if (ch == 'n')
{
Sleep(500);
OperateMenu();
return;
}
else//选择答题
{
TitleNumber = ch - '0';
for (i = 0; i < TitleNumber; i++)
{
Number1 = RandNumber(50);
Number2 = RandNumber(50);
symbol = RandSymbol(2);
if (symbol == '/')
{
Number2 = RandNumber(20);
Number1 = Number2 * (rand() % 10 + 1);
}
if (symbol == '*')
{
Number1 /= 5;
Number2 /= 5;
}
printf("%d %c %d = ?
", Number1, symbol, Number2);
printf("来吧宝贝输入你的答案:
");
scanf("%d", &YourAnswer);
switch (symbol)
{
case '+':RightAnswer = Number1 + Number2; break;
case '-':RightAnswer = Number1 - Number2; break;
case '*':RightAnswer = Number1 * Number2; break;
case '/':RightAnswer = Number1 / Number2; break;
}
if (YourAnswer == RightAnswer) {
Right++; printf("强啊!
");
}
else { printf("菜鸡,正确答案:%d
", RightAnswer); }
printf("觉得太菜不想做了就输n,不然就输y继续
");
scanf("
%c", &ch);
if (ch == 'n')
{
Sleep(500);
GradeThreeMenu();
}
}
accuracy = (double(Right) / TitleNumber) * 100;
if (accuracy < 60)printf("小老弟你不行啊,正确率:%.0f%", accuracy);
else printf("可以啊小老弟,正确率:%.0f%c", accuracy, '%');
printf("还要再来吗?
");
printf("请输入y/n:");
scanf("
%c", &ch);
if (ch == 'n')>
{
system("cls");
Sleep(500);
OperateMenu();
return;
}
else
{
system("cls");
Sleep(500);
GradeThreeMenu();
return;
}
}
}
}
2.2.3 出题相关函数
int RandNumber(int limit);//生成随机数
char RandSymbol(int hard);//生成随机符号
- 随机数要输入一个参数决定生成的范围(参数为5,则范围为1~5)
- 随机符号也要输入一个参数(1为‘+’和‘-’,2为‘+’,‘-’,‘*’和‘/’)
int RandNumber(int limit)
{
int number;
number = rand() % (limit - 1) + 1;
return number;
}
char RandSymbol(int hard)
{
int n;
if (hard == 1)n = rand() % 2;
if (hard == 2)n = rand() % 4;
switch (n)
{
case 0:return '+';
case 1:return '-';
case 2:return '*';
case 3:return '/';
}
}
2.3 运行结果
- 测试数据为一年级,题数为3题,每次都输入y来继续做题
2.4 总结
适当时机清屏会让界面很舒服