Code has a slight problem, the accuracy may be lost. The code does not contain parentheses like operators.
c ++ Code:
#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 ; // number AA [J + . 1 ] = B; BB [J] = A; } for ( int K = 0 ; K <m + . 1 ; K ++ ) CPAA [K] = AA [K]; // multiplication and division treatment 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 ; } the else // result is not negative output expression statement is executed { 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 << "<<. "Description: The result is an integer direct input integer, decimal after the emergence of two decimalsendl; the while ( . 1 ) { COUT << " Please enter the number of the subject (not more than 100) number of operators and operators (not more than 3): " ; CIN >> >> n- m; IF (n-<= 100 && n-> = . 1 && m> = . 1 && m <= . 3 ) BREAK ; the else COUT << " does not meet the input requirements, please re-enter! " << endl; } operation_four (n-, m); return 0 ; }
operation result:
Java code:
Package com.lb.test; Import java.util.Scanner; public class Operator_four { public static void main (String [] args) { int n-, m; System.out.println ( "Description: integer result is directly input integer, "decimals appear to two decimal places ;) the while ( to true ) { System.out.println ( ": Please enter the number of the subject (not more than 100 operators and operands (no more than 3)) " ); Scanner SC = new new Scanner (the System.in); n- = sc.nextInt (); m = sc.nextInt (); IF(n-<= 100 n-&&> = m. 1 &&> = m. 1 && <=. 3 ) BREAK ; the else System.out.println ( "do not meet the input requirements, please re-enter!" ); } four2 F2 = new 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 + "%"); } }
operation result:
The code is still not perfect, the latter needed to be improved