编写程序的过程:以自己的能力和自己会的知识,编写了几个小时的代码,基本满足了老师的要求,随机产生整数,并且随机出加减乘除,随机数存入数组中,把程序运算的结果与客户输入的数据比较判断对错,最后利用循环输出多道题。
源代码如下:
#include<stdio.h>
#include<iostream>
#include <time.h>
using namespace std;
int main(int argc, char* argv[])
{
int a[30], b[30],c[30];
int d,m,m1,n,n1;
srand((int)time(NULL));
for (int i = 0; i < 30; i++)
{
c[i] = rand() % 4;
cout << c[i] << endl;
a[i] = rand() % 100;
cout << a[i] << endl;
b[i] = rand() % 100;
cout << b[i] << endl;
d = c[i];
switch (d)
{
case 0://加法运算
cout << a[i] << "+" << b[i] << "=" ;
cin >> m;
n = a[i] + b[i];
if (m == n)
cout << "对" << endl;
else
cout << "错" << endl;
break;
case 1://减法运算
if (a[i]>b[i])
{
cout << a[i] << "-" << b[i] << "=";
n = a[i] - b[i];
}
else
{
cout << b[i] << "-" << a[i] << "=";
n = b[i] - a[i];
}
cin >> m;
if (m == n)
cout << "对" << endl;
else
cout << "错" << endl;
break;
case 2://乘法运算
cout << a[i] << "*" << b[i] << "=" ;
cin >> m;
n = a[i] * b[i];
if (m == n)
cout << "对" << endl;
else
cout << "错" << endl;
break;
case 3://除法运算
if (a[i] > b[i])
{
cout << a[i] << "/" << b[i] << "=";
n = a[i] / b[i];
n1 = a[i] % b[i];
}
else
{
cout << b[i] << "/" << a[i] << "=";
n = b[i] / a[i];
n1 = b[i] % a[i];
}
cout << "商";
cin >> m;
cout << "余数";
cin >> m1;
if (m == n&&m1 == n1)
cout << "对" << endl;
else
cout << "错" << endl;
break;
}
}
return 0;
}