关于初学者计划的谜题

时间:2016-01-06 17:31:00

标签: c++ memory

在看了一个小时的C ++教程后,我写了这个愚蠢的小程序。它是垃圾,但它产生了一个非常奇怪的错误,一些软件工程师无法解决,所以现在它对我们很有意思。

应该吐出二项式和三项式,然后给出三项式的导数。美元符号可以帮助我复制到LaTeX。

编辑:输出中的确切错误是它的发布: 你需要“inear方程式?-12x + 8 $” 代替 “Derivative =”

感谢您的评论。

对于那些弄清楚输出为何如此奇怪的人来说,这是一个很高的。

输出:

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;

}

1 个答案:

答案 0 :(得分: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) + '$';