C语言 :编写一个函数,通过计算机随机产生一道四则运算题

2024-11-25 20:21:25
推荐回答(1个)
回答1:

// clca4.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
using namespace std;
void fun(int n)
{
int i, x = 0, s = 0, a = 0, b = 0, sum = 0, count = 0;
srand((unsigned)time(NULL));
for (i = 0; i {
a = rand() % 10;
b = rand() % 10;
x = rand() % 4; //3不对,没有除法
printf("%d\n", a);
switch (x)
{
case 0:
printf("+\n");
s = a + b;
break;
case 1:
printf("-\n");
s = a - b;
break;
case 2:
printf("*\n");
s = a*b;
break;
case 3:
printf("/\n");
s = a / b;
break;
default:
printf("Not correct!\n");
}
printf("%d\n", b);
printf("请输入计算结果:\n");
scanf_s("%d", &sum); //scanf 没有 \n
if (sum == s)
{
count++;
printf("Right!\n");
}
else
printf("wrong!\n");
}
printf("%d道题目正确\n", count);
}

int _tmain(int argc, _TCHAR* argv[])
{
int n;
printf("请输入需要完成的题目数量:");
scanf_s("%d", &n);
fun(n);
return 0;
}

以上程序在VS2013 win32控制台测试过。。。。。。