ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

四则运算随机出题

2020-03-25 12:57:44  阅读:27  来源: 互联网

标签:aa 四则运算 bb int float ++ summ 出题 随机


 

代码有一个小问题,就是精度可能会丢失。该代码不包含括号等操作符。

c++代码:

#include<bits/stdc++.h>
using namespace std;
float aa[5],bb[4],cc[10],cpaa[5]; 

void operation_four(int n,int m)
{
    int d;
    float flag = 0,flagn;
    flagn = n;
    d = n;
    srand((unsigned)time(NULL));
    for(int i = 0;i < d;i++)
    {
        float summ = 0;
        memset(aa,0,sizeof(aa));
        memset(bb,0,sizeof(bb));
        memset(cc,0,sizeof(cc));
        memset(cpaa,0,sizeof(cpaa));
        float first = rand() % 101;
        aa[0] = first;
        for(int j = 0;j < m;j++)
        {
            float a = rand() % 4;//符号 
            float b = rand() % 101;//数字 
            aa[j + 1] = b;
            bb[j] = a;
        }
        
        for(int k = 0;k < m + 1;k++)
            cpaa[k] = aa[k];

        //乘除法处理
        for(int k = 0;k < m;k++)
            if(bb[k] == 1)
                aa[k + 1] = -aa[k + 1];
        for(int k = 0;k < m;k++)
        {
            if(bb[k] == 2 || bb[k] == 3)
            {
                if(bb[k] == 2)
                {
                    aa[k + 1] = aa[k] * aa[k + 1];
                    cc[k] = 1;
                }
                if(bb[k] == 3)
                {
                    aa[k + 1] = aa[k] / aa[k + 1];
                    cc[k] = 1;
                }
            }
        }
        
        for(int k = 0;k < m + 1;k++)
        {
            if(cc[k] == 0)
            {
                summ += aa[k];
                summ = ((float)((int)((summ + 0.005) * 100))) / 100;
            }
        }
        if(summ < 0)
        {
            d++;
            continue;
        }
        else//结果不为负就执行输出表达式语句 
        {
            for(int k = 0;k < m + 1;k++)
            {
                cout << cpaa[k];
                if(k < m)
                {
                    if(bb[k] == 0)
                        cout << "+";
                    if(bb[k] == 1)
                        cout << "-";
                    if(bb[k] == 2)
                        cout << "*";
                    if(bb[k] == 3)
                        cout << "/";
                }
            }
            cout << "=";
        }
        
        float in;
        cin >> in;
        if(in == summ)
        {
            cout << "√" << endl;
            flag++;
        }
        else
            cout << "×" << endl; 
        cout << "正确答案为:" << summ << endl;
    }
    printf("正确率为:%.2f%%",flag / flagn * 100);
}

int main()
{
    int n,m;
    cout << "说明:结果为整数直接输入整数,出现小数则保留小数后两位" << endl;
    while(1)
    {
        cout << "请输入题目数(不超过100个)和运算符个数(不超过3个):" ; 
        cin >> n >> m;
        if(n <= 100 && n >= 1 && m >= 1 && m <= 3)
            break;
        else
            cout << "不符合输入要求,请重新输入!" << endl;
    }
    operation_four(n,m);
    return 0;
}

运行结果:

 

 Java代码:

package com.lb.test;

import java.util.Scanner;

public class Operator_four {
    public static void main(String[] args){
        int n,m;
        System.out.println("说明:结果为整数直接输入整数,出现小数则保留小数点后两位");
        while(true){
            System.out.println("请输入题目数(不超过100个)和运算符数(不超过3个):");
            Scanner sc = new Scanner(System.in);
            n = sc.nextInt();
            m = sc.nextInt();
            if(n <= 100 && n >= 1 && m >= 1 && m <= 3)
                break;
            else
                System.out.println("不符合输入要求,请重新输入!");
        }
        four2 f2 = new four2();
        f2.operation_four(n,m);
    }
}
package com.lb.test;

import java.util.Random;
import java.util.Scanner;

public class four2 {
    public void operation_four(int n,int m){
        int d;
        float flag = 0,flagn;
        flagn = n;
        d = n;
        Random rand = new Random();
        for (int i = 0;i < d;i++){
            float[] aa = new float[] {0,0,0,0,0};
            float[] bb = new float[] {0,0,0,0};
            float[] cc = new float[] {0,0,0,0,0};
            float[] cpaa = new float[] {0,0,0,0,0};
            float summ = 0;
            float first = rand.nextInt(101);
            aa[0] = first;
            for(int j = 0;j < m;j++){
                float a = rand.nextInt(4);
                float b = rand.nextInt(101);
                aa[j + 1] = b;
                bb[j] = a;
            }

            for(int k = 0;k < m + 1;k++)
                cpaa[k] = aa[k];

            for(int k = 0;k < m;k++){
                if(bb[k] == 1){
                    aa[k + 1] = -aa[k + 1];
                }
            }
            for(int k = 0;k < m;k++){
                if(bb[k] == 2 || bb[k] == 3){
                    if(bb[k] == 2){
                        aa[k + 1] = aa[k] * aa[k + 1];
                        cc[k] = 1;
                    }
                    if(bb[k] == 3){
                        aa[k + 1] = aa[k] / aa[k + 1];
                        cc[k] = 1;
                    }
                }
            }

            for (int k = 0;k < m + 1;k++){
                if(cc[k] == 0){
                    summ = summ + aa[k];
                    summ = ((float)((int)((summ + 0.005) * 100))) / 100;
                }
            }
            if(summ < 0){
                d++;
                continue;
            }
            else{
                for (int k = 0;k < m + 1;k++){
                    System.out.print(cpaa[k]);
                    if(k < m){
                        if (bb[k] == 0)
                            System.out.print("+");
                        if (bb[k] == 1)
                            System.out.print("-");
                        if (bb[k] == 2)
                            System.out.print("*");
                        if (bb[k] == 3)
                            System.out.print("/");
                    }
                }
                System.out.print("=");
            }

            Scanner sc = new Scanner(System.in);
            float in;
            in = sc.nextFloat();
            if(in == summ){
                System.out.println("√");
                flag++;
            }else{
                System.out.println("×");
            }
            System.out.println("正确答案为:" + summ);
        }
        System.out.println("正确率为:" + flag / flagn * 100 + "%");
    }
}

运行结果:

 

 代码还不够完善,需待后期改进

 

标签:aa,四则运算,bb,int,float,++,summ,出题,随机
来源: https://www.cnblogs.com/biaobiao88/p/12565396.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有