0

For the next example:

User input sample: "1 + 3 - 2 * 4"

string input = Console.ReadLine();
string[] getElement = input.split(' ');

//So I can have
//getElement[0] = 1
//getElement[1] = +
//getElement[2] = 3
//...

//I have a "for()" cicle here and at one moment I have the following instruction:

if(getElement[i] == "+")
    int add = Convert.ToInt32(getElement[i - 1]) + Convert.ToInt32(getElement[i + 1]);
    //In this example it means: add = 1 + 3

My question is how I can remove positions [0],[1],[2] of my string[] getElement and replace them for "add" value?

I don't know how to use Replace() in this case. Should I need to Resize Array? Need suggestions.

pravprab
  • 2,301
  • 3
  • 26
  • 43
M. Coutinho
  • 305
  • 1
  • 8
  • 29
  • 1
    You shall use a stack in that case – A.K. Feb 03 '14 at 10:36
  • Maybe use a [`List`](http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx) instead of a `string[]`? – Hans Kesting Feb 03 '14 at 10:39
  • Can you please tell clearly, whether you just want to replace OP with text or want to evaluate the result. – Milan Raval Feb 03 '14 at 10:40
  • If you want to evaluate expression you can refer : http://stackoverflow.com/questions/5838918/evaluate-c-sharp-string-with-math-operators – Milan Raval Feb 03 '14 at 10:42
  • I want to replace the result of some calculation. Since I will always have 2 numbers and 1 operand, i want to replace those 3 positions for the result of that calculation. – M. Coutinho Feb 03 '14 at 10:42
  • @MilanRaval sorry but i think i can't use that. I can only have the following: using System; using System.Collections.Generic; using System.IO; – M. Coutinho Feb 03 '14 at 10:46
  • Is the quest about, how to get the right answer from an expression, or how to replace 3 elements with 1? – Jodrell Feb 03 '14 at 10:49
  • You can use: input.Replace(string.Concat(element[0],element[1],element[2]),"Result") – Milan Raval Feb 03 '14 at 10:50
  • @Jodrell both in this case, since i started to solve it like this way. – M. Coutinho Feb 03 '14 at 10:52
  • To add in this, you also need to consider parentheses and precedence of evaluation – Milan Raval Feb 03 '14 at 10:53
  • Well, you need to parse the whole expression first before you start evluating it. See my and Roy Dictus's answer for ideas. http://stackoverflow.com/a/21525052/659190, http://stackoverflow.com/a/21525202/659190 – Jodrell Feb 03 '14 at 10:58
  • @NeverHopeless's answer solved my problem, but all you guys here showed to me that i can do this in many ways. Thank you for your tips and suggestions. – M. Coutinho Feb 03 '14 at 11:03
  • I'm trying to show you, that you can't do it in the way you are trying to do it. Its one of those fundamentals of computer science. You can lead a horse to water ... – Jodrell Feb 03 '14 at 11:10

5 Answers5

2

You can use a List<string> instead of an array. You can address individual pieces in a list just like you can in an array, and you can remove items.

Just make sure you test that getElement[i - 1] or getElement[i + 1] will not produce an IndexOutOfRangeException first...

Note: For very simple operations such as those in your example, this is a reasonable approach, but for anything more complicated -- such as support for parentheses or functions -- you'll need to implement a proper parser based on a grammar. You can use the Gold Parsing System to build such a parser (see http://www.goldparser.org).

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • I will use List to see what are my options, but i understood your point. About goldparser i guess i don't need that to solve this problem but, after some quick reading, it would be a good option for big projects. – M. Coutinho Feb 03 '14 at 12:03
1

Try like this:

string input = Console.ReadLine();
string[] getElement = input.split(' ');

if(getElement[i] == "+")
{
    int add = Convert.ToInt32(getElement[i - 1]) + Convert.ToInt32(getElement[i + 1]);
    input = input.Replace(getElement[i - 1] + ' ' + getElement[i] + ' ' + getElement[i + 1]),add.ToString())
}

This will remove all 1+3 from the input expression and replace it with the new value stored under add variable.

Make sure to handle indexOutOfRangeException as suggested by other experts.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
0

You can do it with

input.Replace(string.Concat(element[0],element[1],element[2]),"Result")
Milan Raval
  • 1,880
  • 1
  • 16
  • 33
  • 3
    I think the OP wants to replace elements 0-2 with 1 element which is the result of the addition. – Jodrell Feb 03 '14 at 10:38
0

Don't use Arrays for it, use Lists. You can't remove and add elements to an array, but you can do it in a List.

List<String> myList = new List<String>();
myList.add("something");
myList.removeAt(0);
Zhafur
  • 1,626
  • 1
  • 13
  • 31
0

To address this problem effectively, you'll need to use the Shunting-yard Algorithm to convert your expression to a Reverse Polish Notation representation. That way, you'll obey the BODMAS order of operations and end up with the right result.

Performing the operations, as they are parsed seems easy but will back fire with any resaonably complicated test cases. i.e. whenever the operands appear outside the order of operation. e.g.

3 - 2 + 4 * 2 / ( 2 ^ 2 + 3 )

Jodrell
  • 34,946
  • 5
  • 87
  • 124