我如何排序方程式?
问题描述:
具有X2 + X1 + 3X3 ----> X1 + X2 + 3X3
我的尝试:
我尝试分裂,但显然我做错了所以任何帮助?
having X2+X1+3X3 ----> X1+X2+3X3
What I have tried:
I tried splitting but apparently I'm doing something wrong so any help?
答
你需要乘法运算符:
X1 + X2 + 3 * X3
或者你想要那样:
X + X * X + 3 * X * X * X
您可以这样写:
X * ( 1 + X * ( 1 + 3 * X ) ) )
这里是C ++ 11中的另一个解决方案:
Here' one more solution in C++11:
#include <string>
#include <vector>
#include <cctype>
#include <iostream>
#include <algorithm>
typedef struct tagParam
{
int coeff;
int subscript;
} PARAM, *LPPARAM;
int main(int argc, char* argv[])
{
// Let eq is the string containing the equation to be sorted
std::string eq = "X2+X1+3X3+4X5+2X4+X7+X6";
// Allocating a vector to store coeff + subscript objects
std::vector<PARAM> params;
// Parsing string containing the equation to be sorted
for (auto FwdIt = eq.begin(); (FwdIt + 1) < eq.end(); FwdIt++)
{
// For each character perform a check if preceding character is coeff
// and succeeding character is subscript.
if (FwdIt != eq.end() && std::isdigit(*FwdIt) && \
std::isdigit(*(FwdIt + 2)) && *(FwdIt + 1) == 'X')
{
// If so, append the coeff and subscript to lpParams buffer previously allocated
PARAM param = { 0 };
std::memset((void*)¶m, 0x00, sizeof(PARAM));
param.coeff = *FwdIt - '0'; param.subscript = *(FwdIt + 2) - '0';
if (FwdIt + 3 < eq.end()) FwdIt += 3; params.push_back(param);
}
// Perform a check if the current character is 'X' and next character is subscript
else if (FwdIt != eq.end() && *FwdIt == 'X' && std::isdigit(*(FwdIt + 1)))
{
// If so, append subscript to lpParams buffer previously allocated
PARAM param = { 0 };
std::memset((void*)¶m, 0x00, sizeof(PARAM));
param.subscript = *(FwdIt + 1) - '0'; params.push_back(param);
if (FwdIt + 2 < eq.end()) FwdIt += 2;
}
}
// Perform a sort to order all coeff + subscript objects by the value of subscript
std::sort(params.begin(), params.end(), [&](const PARAM& param1, \
const PARAM& param2) { return param1.subscript < param2.subscript; });
std::string output = "\0";
// Constructing an output string
for (auto It = params.begin(); It != params.end(); It++)
// For each coeff + subscript object perform a check if the subscript
// is not equal to 0 and coeff is equal to zero (there's no coeff)
if (It->subscript != 0 && It->coeff == 0)
{
// If so, output X_subscript to string buffer
output += "X" + std::to_string(It->subscript);
if (It != params.end() - 1) output += "+";
}
// Otherwise, perform a check if both coeff and subscript are not zero
else if (It->subscript != 0 && It->coeff != 0)
{
// if so, output coeff_X_subscript to string buffer
output += std::to_string(It->coeff) + "X";
output += std::to_string(It->subscript);
if (It != params.end() - 1) output += "+";
}
// Output the resulting string buffer containing sorted equation
std::cout << "input = " << eq << "\n" << "output = " << output;
std::cin.get();
return 0;
}
这是我的解决方案:
Here's my solution:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
typedef struct tagParam
{
int coeff;
int subscript;
} PARAM, *LPPARAM;
int main(int argc, char* argv[])
{
// Let eq is a string buffer containing an equation to be parsed
static char eq[256] = "X2+X1+3X3";
// Allocate buffer for an array of subscript objects (coeff + subscript)
LPPARAM lpParams = new PARAM[100];
memset((void*)lpParams, 0x00, sizeof(PARAM) * 100);
int n = 0;
// Parsing string containing the equation to be sorted
for (int i = 0; eq[i] != '\0'; i++)
{
// For each character perform a check if preceding character is coeff
// and succeeding character is subscript.
if (isdigit(eq[i]) && isdigit(eq[i + 2]) && eq[i + 1] == 'X')
{
// If so, append the coeff and subscript to lpParams buffer previously allocated
lpParams[n].coeff = eq[i] - '0';
lpParams[n++].subscript = eq[i + 2] - '0';
i += 3;
}
// Perform a check if the current character is 'X' and next character is subscript
else if (eq[i] == 'X' && isdigit(eq[i + 1]))
{
// If so, append subscript to lpParams buffer previously allocated
lpParams[n++].subscript = eq[i + 1] - '0';
i += 2;
}
}
// Perform a sort to order all coeff + subscript objects by the value of subscript
for (int i = 0; lpParams[i].subscript != 0; i++)
{
int min = i;
for (int j = i + 1; lpParams[j].subscript != 0; j++)
min = (lpParams[j].subscript < lpParams[min].subscript) ? j : min;
PARAM temp = lpParams[i];
lpParams[i] = lpParams[min];
lpParams[min] = temp;
}
static char output[256] = "\0";
// Constructing an output string
for (int i = 0; lpParams[i].subscript != 0; i++)
// For each object in the array perform a check if the subscript
// is not equal to 0 and coeff is equal to zero (there's no coeff)
if (lpParams[i].subscript != 0 && lpParams[i].coeff == 0)
// If so, output X_subscript to string buffer
sprintf_s(output, 256, "%sX%d+", output, lpParams[i].subscript);
// Otherwise, perform a check if both coeff and subscript are not zero
else if (lpParams[i].subscript != 0 && lpParams[i].coeff != 0)
// if so, output coeff_X_subscript to string buffer
sprintf_s(output, 256, "%s%dX%d+", output, lpParams[i].coeff, lpParams[i].subscript);
// Shrink output buffer by one ending character (e.g. remove unnecessary '+' character)
output[strlen(output) - 1] = '\0';
// Output the resulting string buffer containing sorted equation
printf("input = %s\noutput = %s\n", eq, output);
_getch();
return 0;
}