用C语言编写生成小学四则运算程序
使用软件——VS 2015
使用环境——C语言
早在上周我就开始使用C#语言做,由于最后一点问题而放弃,之后用C语言开始做,很顺利,但是也碰到了一些问题,但是通过了百度文库上的一些程序的借鉴和吴阿平同学的悉心指导最后还是解决了,主要是通过C语言实现能够生成100以内的加减乘除题目,并且判断使用者的答题是否正确。一开始的时候先会给使用者提供加减乘除四个选项,使用者选择后会提示是一位数还是两位数;之后使用者开始作答,之后之前的步骤一直循环,直到使用者不再使用。下面是程序代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int add(int num1, int num2)//加法
{
return num1 + num2;
}
int subtraction(int num1, int num2)//减法
{
return num1 - num2;
}
int mul(int num1, int num2)//乘法
{
return num1*num2;
}
float divis(int num1, int num2)//除法
{
return ((int)(((float)num1 / num2) * 100 + 0.5)) / 100.0;
}
int main()
{
int type = -1;
int data = -1;
int choice, num1, num2, results;
float div_result;
int right = 0;
int wrong = 0;
char ch;
printf("欢迎进入小学四则运算题,现在考验你的智商是不是真正的“小学生”了!\n");
srand((unsigned int)time(NULL));
while (1)
{
printf("请选择运算类型:1.加法 2.减法 3.乘法 4.除法(保留两位小数)\n");
scanf("%d", &choice);
printf("请选择位数:1.一位数 2.两位数\n");
scanf("%d", &data);
switch (choice)
{
case 1:
if (data == 1)
{
num1 = rand() % 10;
num2 = rand() % 10;
printf("%d + %d=__?__\n", num1, num2);
scanf("%d", &results);
if (results == add(num1, num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c", &ch);
if (ch == 'y')
break;
else if (ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand() % 100;
num2 = rand() % 100;
printf("%d + %d=__?__\n", num1, num2);
scanf("%d", &results);
if (results == add(num1, num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c", &ch);
if (ch == 'y')
break;
else if (ch == 'n')
goto end;
}
break;
case 2:
if (data == 1)
{
num1 = rand() % 10;
num2 = rand() % 10;
printf("%d - %d=__?__\n", num1, num2);
scanf("%d", &results);
if (results == subtraction(num1, num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c", &ch);
if (ch == 'y')
break;
else if (ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand() % 100;
num2 = rand() % 100;
printf("%d - %d=__?__\n", num1, num2);
scanf("%d", &results);
if (results == subtraction(num1, num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c", &ch);
if (ch == 'y')
break;
else if (ch == 'n')
goto end;
}
break;
case 3:
if (data == 1)
{
num1 = rand() % 10;
num2 = rand() % 10;
printf("%d * %d=__?__\n", num1, num2);
scanf("%d", &results);
if (results == mul(num1, num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c", &ch);
if (ch == 'y')
break;
else if (ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand() % 100;
num2 = rand() % 100;
printf("%d * %d=__?__\n", num1, num2);
scanf("%d", &results);
if (results == mul(num1, num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c", &ch);
if (ch == 'y')
break;
else if (ch == 'n')
goto end;
}
break;
case 4:
if (data == 1)
{
num1 = rand() % 10;
num2 = rand() % 10 + 1;
printf("%d / %d=__?__\n", num1, num2);
scanf("%f", &div_result);
if (div_result == divis(num1, num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c", &ch);
if (ch == 'y')
break;
else if (ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand() % 100;
num2 = rand() % 100 + 1;
printf("%d / %d=__?__\n", num1, num2);
scanf("%f", &div_result);
if (div_result == divis(num1, num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c", &ch);
if (ch == 'y')
break;
else if (ch == 'n')
goto end;
}
break;
default:
printf("输入错误!\n");
break;
}
}
end:
printf("回答正确%d题,回答错误%d题\n", right, wrong);
printf("\n===================感谢使用=====================!\n");
system("pause");
return 0;
}
实验截图: