首页 > 代码库 > 四则运算

四则运算

// text.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;
#define size 10
char operation[4] = { +,-,*,/ };
int oper_[4], number_[10];
void IntAndInt()
{
    for (int i = 0; i < 4; i++)
        oper_[i] = rand() % 4;
    for (int j = 0; j < 5; j++)
        number_[j] = 1 + rand() % 10;
    cout << number_[0] << operation[oper_[0]] << number_[1] << operation[oper_[1]] << number_[2] << operation[oper_[2]] << number_[3] << operation[oper_[3]] << number_[4] << endl << endl;
}
void IntAndFloat()
{
    for (int i = 0; i < 4; i++)
        oper_[i] = rand() % 4;
    for (int j = 0; j < 7; j++)
        number_[j] = 1 + rand() % 10;
    cout << "(" << number_[0] << "/" << number_[1] << ")" << operation[oper_[0]] << number_[2] << operation[oper_[1]] << "(" << number_[3] << "/" << number_[4] << ")" << operation[oper_[2]] << number_[5] << operation[oper_[3]] << number_[6] << endl << endl;
}
void FloatAndFloat()
{
    for (int i = 0; i < 4; i++)
        oper_[i] = rand() % 4;
    for (int j = 0; j < 10; j++)
        number_[j] = 1 + rand() % 10;
    cout << "(" << number_[0] << "/" << number_[1] << ")" << operation[oper_[0]] << "(" << number_[2] << "/" << number_[3] << ")" << operation[oper_[1]] << "(" << number_[4] << "/" << number_[5] << ")" << operation[oper_[2]] << "(" << number_[6] << "/" << number_[7] << ")" << operation[oper_[3]] << "(" << number_[8] << "/" << number_[9] << ")" << endl << endl;
}
int main()
{
    int operation_type[size], i;
    for (i = 0; i < size; i++)
        operation_type[i] = rand() % 3;
    for (i = 0; i < size; i++)
        switch (operation_type[i])
        {
        case 0:FloatAndFloat(); break;
        case 1:IntAndFloat(); break;
        case 2:IntAndInt(); break;
        }
}

 

四则运算