四则运算源程序
Posted 鹄立丹香
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算源程序相关的知识,希望对你有一定的参考价值。
//四则运算程序,支持真分数运算
//2016,03,04
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
void zuidagongyue(int chushu)
{
int beichushu = 100;
int temp;
int first = beichushu%chushu;
while (!first == 0)
{
temp = chushu;
chushu = beichushu%chushu;
beichushu = temp;
first = beichushu%chushu;
}
}
void main()
{
srand((int)time(NULL));
int A[30];
int B[30];
for (int i = 0; i < 30; i++)
{
A[i] = 1+rand() % 99;
cout << A[i]<<" " ;
B[i] = 1+rand() % 99;
cout << B[i]<<" " ;
cout << endl;
}
for (int i = 0; i < 30; i++)
{
if (A[i] > B[i])
{
int suanfu = rand() % 4;
switch (suanfu)
{
case 0:
cout << A[i] << " + " << B[i] << "=" << endl; break;
case 1:
cout << A[i] << " - " << B[i] << "=" << endl; break;
case 2:
cout << A[i] << " x " << B[i] << "=" << endl; break;
case 3:
cout << A[i] << " ÷ " << B[i] << "=" << endl; break;
}
}
if (A[i] < B[i])
{
int copyA = A[i];
int copyB = B[i];
zuidagongyue(A[i]);
zuidagongyue(B[i]);
int suanfu = rand() % 4;
switch (suanfu)
{
case 0:
cout << copyA / A[i] << "/" << 100 / A[i] << " + " << copyB / B[i] << "/" << 100 / B[i] << "=" << endl; break;
case 1:
cout << copyA / A[i] << "/" << 100 / A[i] << " - " << copyB / B[i] << "/" << 100 / B[i] << "=" << endl; break;
case 2:
cout << copyA / A[i] << "/" << 100 / A[i] << " x " << copyB / B[i] << "/" << 100 / B[i] << "=" << endl; break;
case 3:
cout << copyA / A[i] << "/" << 100 / A[i] << " ÷ " << copyB / B[i] << "/" << 100 / B[i] << "=" << endl; break;
}
}
}
}
以上是关于四则运算源程序的主要内容,如果未能解决你的问题,请参考以下文章