Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
having X2+X1+3X3 ----> X1+X2+3X3

What I have tried:

I tried splitting but apparently I'm doing something wrong so any help?
Posted
Updated 24-Jun-18 20:26pm
Comments
[no name] 24-Jun-18 13:52pm    
Nobody can tell you what you are doing wrong if you do not show how you do it ;)
Patrice T 24-Jun-18 13:55pm    
Your secret code is wrong and need a secret correction.

you need the multiply operator:
C++
X1 + X2 + 3 * X3
or do you want it that way:
C++
X + X * X + 3 * X * X * X
which you can write as that:
C++
X * ( 1 + X * ( 1 + 3 * X ) ) )
 
Share this answer
 
Comments
[no name] 24-Jun-18 14:09pm    
Btw the three is from my side.
Patrice T 24-Jun-18 14:17pm    
Are sure X3 is X^3 ?
Member 13480832 24-Jun-18 15:05pm    
I don't know. I was given the equation as it is
KarstenK 25-Jun-18 4:29am    
You should know and understand your task. You gotta ask!!!
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;
}
 
Share this answer
 
Comments
Arthur V. Ratz 25-Jun-18 2:29am    
Probably this one is better since your question is about C++ programming.
Arthur V. Ratz 25-Jun-18 2:37am    
I know that both of my solutions might come to be complex and difficult to understand but probably there's no other solution to this problem apparently. Specifically I use vector of structure objects to hold the data on each coefficient and each subscript, and then sort this vector by the values of subscript variables. Without it you'll probably never sort this equation.
KarstenK 25-Jun-18 4:28am    
The solution doesnt really NOT match with the knowledge of the asker. So he shouldnt use this, because the risk "please explain" will be a trap. ;-)
Arthur V. Ratz 25-Jun-18 4:33am    
I'm sorry, but I was ready to help not really knowing that this is "do my homework question". In advance, I will simply not answer the questions like this. I'm sorry. :) Thanks for your comment.
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;
}
 
Share this answer
 
v2
Comments
Arthur V. Ratz 25-Jun-18 2:27am    
What's wrong this solution ? It really works.
Patrice T 25-Jun-18 3:45am    
I am not the downvoter,
but you provide a full code to something that smells like HomeWork and OP showed nothing..
Arthur V. Ratz 25-Jun-18 4:33am    
I'm sorry, but I was ready to help not really knowing that this is "do my homework question". In advance, I will simply not answer the questions like this. I'm sorry. :) Thanks for your comment.
Member 13480832 25-Jun-18 5:16am    
It's not "homework".... it's a task given by a friend for fun to expand our knowledge!
Patrice T 26-Jun-18 16:54pm    
'it's a task given by a friend'
This count as HomeWork. I guess all the interest is to see if you can solve the problem yourself, not begging us to solve it.
You'd better spend time to learn analyze methods and algorithms.
Here is a book from famous authors, it will help you to improve your skills: Structured Programming.pdf[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900