2012-07-22 25 views
-5

我想生成一個隨機的線性/數學方程。 我有兩個輸入,最終結果和操作符的數量。例如:如何生成一個隨機的線性方程

result = 84 
noOfOperators = 3 

所以我期望方程爲15 + (20 * 4) - 11 = 84

+5

如有特殊原因既'C++'和'動作,3'標記您的問題嗎?你沒有提到任何特定於這些語言的東西。 – jweyrich 2012-07-22 08:05:53

+4

'0 *(任意複雜的表達式)+ nOfOperators的結果> 1. nOfOperstors = 1時的'0+結果'。 – juanchopanza 2012-07-22 08:09:41

+0

您有最終結果嗎?什麼最終結果? – Mikhail 2012-07-22 08:16:44

回答

1

甚至不知道這是否是一個等式,因爲沒有未知:-)

無論哪種方式,有很多的變化。我想到的一種算法是這樣的:

  • 開始最終的結果
  • 選擇「最後一次操作」
  • 取決於操作,產生兩個合適的參數。可能是這樣的:
    • 乘法,生成素因子和隨機放置在兩個參數之一的每個素因子。如果只有一個素數因子(除非你喜歡1 * n項)
    • 用於除法,加法和減法,你可能只需要生成一個隨機的「第二個參數」
    • 編輯:for「no操作「(見下一步),只生成一個與輸入相同的參數。這僅僅是爲了便於遞歸,因爲你可以對遞歸以及
  • 每個參數做同樣的,也讓「不操作」

還是需要在合適的條件下決定停止。編輯:你的'運營商的數量'可以提供,不要指望「沒有操作」,你會沒事的

這會給你一個表達結果樹,也可以堅持整數領域(這是你通常想要什麼,如果這對像創建小學數學題)

1
  1. 輸入結果和n(運營商)
  2. 生成N + 1個隨機DATAS和n個隨機運營商。
  3. 根據隨機數據計算答案。
  4. Fo r每個數據d,如果d之前的運算符不是'*'或 '/',則讓d = d * result/answer。
  5. 打印數據和操作符。

測試代碼如下:

#include <iostream> 
#include <stdlib.h> 
#include <stack> 
#include <time.h> 

using namespace std; 

#define MAX_RAN 100 

char OpDisplay(int i) 
{ 
    switch(i) 
    { 
     case 0: return '+'; 
     case 1: return '-'; 
     case 2: return '*'; 
     case 3: return '/'; 
     default: break; 
    } 
    return '+'; 
} 

double CalOp(double a, double b, int Op) 
{ 
    switch(Op) 
    { 
     case 0: return (a+b); 
     case 1: return (a-b); 
     case 2: return (a*b); 
     case 3: return (a/b); 
    } 
    return 0; 
} 

int main(int argc, char* argv[]) 
{ 
    srand((unsigned)time(NULL)); 

    int result; 
    int numOp = 1; 
    cout<<"Enter the Result:"; 
    cin>>result; 
    cout<<"Enter the number of operators:"; 
    cin>>numOp; 

    double* pData = new double[numOp+1]; 
    int* pOp = new int[numOp]; 

    for(int i = 0; i < numOp; i++) 
    { 
     //Generate Num 
     pData[i] = rand()% MAX_RAN; 
     //Generate Op 
     pOp[i] = rand()%4; 
    } 
    pData[numOp] = rand()% MAX_RAN; 

    stack<double> sD; 
    stack<int> sOp; 
    sD.push(pData[0]); 

    //caluculate * and/first 
    for(int i = 0; i < numOp; i++) 
    { 
     if(pOp[i]>1) // * or/
     { 
      double re = sD.top(); 
      re = CalOp(re, pData[i+1],pOp[i]); 
      sD.pop(); 
      sD.push(re); 
     } 
     else 
     { 
      sOp.push(pOp[i]); 
      sD.push(pData[i+1]); 
     } 
    } 

    stack<double> sD_R; 
    stack<int> sOp_R; 
    //reverse the stack 
    while(!sD.empty()) 
    { 
     sD_R.push(sD.top()); 
     sD.pop(); 
    } 
    while(!sOp.empty()) 
    { 
     sOp_R.push(sOp.top()); 
     sOp.pop(); 
    } 

    //calculate + and - 
    double answer = sD_R.top(); 
    sD_R.pop(); 
    while(!sOp_R.empty()) 
    { 
     answer = CalOp(answer, sD_R.top(), sOp_R.top()); 
     sD_R.pop(); 
     sOp_R.pop(); 
    } 

    //resign the data according to the answer and the result we entered before. 
     for(int i = 0; i < numOp; i++) 
    { 
     cout<<"("<<pData[i]<<")"<<OpDisplay(pOp[i]); 
    } 
    cout<<"("<<pData[numOp]<<")"<<" = "<<answer<<endl; 

    pData[0] = pData[0]*result/answer; 
    for(int i = 1; i < numOp+1; i++) 
    { 
     if(3 != pOp[i-1] && 2 != pOp[i-1]) 
     { 
      pData[i] = pData[i]*result/answer; 
     } 

    } 

    for(int i = 0; i < numOp; i++) 
    { 
     cout<<"("<<pData[i]<<")"<<OpDisplay(pOp[i]); 
    } 
    cout<<"("<<pData[numOp]<<")"<<" = "<<result<<endl; 

    delete[] pData; 
    delete[] pOp; 
}