2016-01-06 103 views
-3

我在看了一個小時的C++教程後,寫了這個愚蠢的小程序。這是垃圾,但它產生了一些非常奇怪的錯誤,一些軟件工程師無法解決,所以現在我們很感興趣。關於初學者程序的難題

它應該吐出二項式和三項式,然後給出三項式的導數。美元符號可以幫助我將副本複製到LaTeX中。

編輯:在輸出中的確切的錯誤是它的發佈: 「inear方程你需要-12x + 8 $?」的 代替 「微分=」

感謝您的評論。

對於那些指出爲什麼輸出如此奇怪的人來說,這是一個很好的選擇。

輸出:

How many sets of 3 linear equations do you need? 
2 
How many sets of 3 quadratics do you need? 
2 
12x-3 
-3x+12 
-5x-9 
15x-5 
-5x+15 
-4x-2 
$8x^2-15x-6$ 
inear equations do you need?16x-15$ 
$-15x^2-6x+8$ 
inear equations do you need?-30x-6$ 
$-6x^28x-15$ 
inear equations do you need?-12x+8$ 
$2x^2-13x-2$ 
inear equations do you need?4x-13$ 
$-13x^2-2x+2$ 
inear equations do you need?-26x-2$ 
$-2x^22x-13$ 
inear equations do you need?-4x+2$ 
$11x^2-3x-13$ 
inear equations do you need?22x-3$ 
$-3x^2-13x+11$ 
inear equations do you need?-6x-13$ 
$-13x^211x-3$ 
inear equations do you need?-26x+11$ 
Press any key to continue . . . 

驗證碼:

#include <iostream> 
#include <vector> 
#include <string> 
#include <fstream> 
/*Simple Function maker*/ 
using namespace std; 



void polynomial(string numberOfPolys) 
{ 
    /*AX^2+BX+C format*/ 
    for (int i = 0; i <= (std::stoi(numberOfPolys, 0)); i++) { 
     int a = (rand() % 15) + 1; 
     int b = -1 * ((rand() % 15) + 1); 
     int c = -1 * ((rand() % 15) + 1); 
     int d = -1 * ((rand() % 15) + 1); 
     string problemWriter = '$' + to_string(a) + 'x' + '^' + '2' + to_string(b) + 'x' + to_string(c) + '$'; 
     string problemWriter2 = '$' + to_string(b) + 'x' + '^' + '2' + to_string(c) + 'x' + '+' + to_string(a) + '$'; 
     string problemWriter3 = '$' + to_string(c) + 'x' + '^' + '2' + to_string(a) + 'x' + to_string(b) + '$'; 

     string answerWriter = "Derivative= " + '$' + to_string(a * 2) + 'x' + to_string(b) + '$'; 
     string answerWriter2 = "Derivative= " + '$' + to_string(b * 2) + 'x' + to_string(c) + '$'; 
     string answerWriter3 = "Derivative= " + '$' + to_string(c * 2) + 'x' + '+' + to_string(a) + '$'; 

     cout << problemWriter << endl; 
     cout << answerWriter << endl; 

     cout << problemWriter2 << endl; 
     cout << answerWriter2 << endl; 

     cout << problemWriter3 << endl; 
     cout << answerWriter3 << endl; 
    } 

} 


int main() 
{ /*MX+B format*/ 


    string input; 
    string polys; 

    cout << "How many sets of 3 linear equations do you need?" << endl; 
    getline(cin, input); 
    cout << "How many sets of 3 quadratics do you need?" << endl; 
    getline(cin, polys); 



    for (int i = 0; i < (std::stoi(input, 0)); i++) { 
     int f = (rand() % 15) + 1; 
     int g = -1 * ((rand() % 15) + 1); 
     int h = -1 * ((rand() % 15) + 1); 
     int k = -1 * (rand() % 15) + 1; 
     string problemLWriter = to_string(f) + 'x' + to_string(g); 
     string problemLWriter2 = to_string(g) + 'x' + '+' + to_string(f); 
     string problemLWriter3 = to_string(h) + 'x' + to_string(k); 
     cout << problemLWriter << endl; 
     cout << problemLWriter2 << endl; 
     cout << problemLWriter3 << endl; 
    } 
    polynomial(polys); 
    return 0; 

} 
+2

*,但它產生的一個非常奇怪的錯誤那幾個軟件工程師一直沒能解決*你應該說的是什麼錯誤。 – NathanOliver

+0

謝謝,編輯該信息 – goruda

+1

歡迎來到堆棧溢出!這聽起來像你可能需要學習如何使用調試器來遍歷代碼。使用一個好的調試器,您可以逐行執行您的程序,並查看它與您期望的偏離的位置。如果你打算做任何編程,這是一個重要的工具。進一步閱讀:** [如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver

回答

3

的問題是與線當您創建answerWriter字符串:

string answerWriter = "Derivative= " + '$' + to_string(a * 2) + 'x' + to_string(b) + '$'; 

"Derivative= "是一個字符數組,而不是一個string。你不能連接字符。

它改成這樣:

string answerWriter = string("Derivative")+ '$' + to_string(a * 2) + 'x' + to_string(b) + '$'; 
+0

引號中的文本是文本常量,在大多數用途中會衰減爲char *,包括這個。將一個char常量''$''添加到'char *'中會使地址前進。它不連接。如果添加的任何一邊是字符串,就會得到串聯,比如'「Derivative =」+('$'+ to_string(a * 2))' – JSF

+0

@Andrea - 它不是char *,而是一個字符數組,然後當數字 - 如美元符號的ASCII碼 - 被添加到它時,很容易衰減到char *。 – user433534

+0

謝謝Andrea和JSF! – goruda