2016-01-06 105 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