1

I am trying to turn this from java to C# and have done almost all of it except the tokenizer. I know that you use split in C# but I cannot seem to figure it out. The program needs to split up the equation that the user enters (4/5 + 3/4) is the format without the parenthesis. Any help would be great.

// read in values for object 3
Console.Write("Enter the expression (like 2/3 + 3/4 or 3 - 1/2): ");
string line = Console.ReadLine();

// Works with positives and neagative values!
// Split equation into first number/fraction, operator, second number/fraction
StringTokenizer st = new StringTokenizer(line, " ");
string first = st.nextToken();
char op = (st.nextToken()).charAt(0);
string second = st.nextToken();

I will need the symobol (+, -, *, or /) later and need to check to see whether it is a whole number which I do right after this in my code. Below is kind of what I have tried but I am stuck with the char.

char delimeters = ' ';
string[] tokens = line.Split(delimeters);
string first = tokens[0];
char c = tokens[1]
  • I'm not sure if I undedrstand you're question correctly, but iI think it should be `char c = tokens[1][0]` – Lennart May 08 '13 at 00:52
  • I was not sure if that would work? And if I need to clear anything up let me know. – Connor Elliott May 08 '13 at 00:55
  • btw, if you're going to use float.Parse, be aware of localization. (http://stackoverflow.com/questions/147801/best-way-to-parse-float) – Lennart May 08 '13 at 01:07

3 Answers3

2

tokens is an array of strings, so token[1] is a string, and you can't assign a string to a char. That's why in the javacode is written charAt(0). Converting that to C# gives

char delimeters = ' ';
string[] tokens = line.Split(delimeters);
string first = tokens[0];
char c = tokens[1][0];
Lennart
  • 345
  • 4
  • 17
1

The equivalent of Java's

String first = st.nextToken();
char op = (st.nextToken()).charAt(0);
String second = st.nextToken();

would be

string first = tokens[0];
char c = tokens[1][0];
string second = tokens[2];

Most likely, you would need to do this in a loop. The first would be read once, and then you would read operator and operand while more data is available, like this:

List<string> operands = new List<string> {tokens[0]};
List<char> operators = new List<char>();
for (int i = 1 ; i+1 < tokens.Length ; i += 2) {
    operators.Add(tokens[i][0]);
    operands.Add(tokens[i+1]);
}

After this loop your operators would contain N characters representing operators, and operands would contain N+1 strings representing the operands.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

I just coded this (i.e. i didn't try it)... but here ya go.

//you should end up with the following
// tokens[0] is the first value, 2/3 or 3 in your example
// tokens[1] is the operation, + or - in your example
// tokens[2] is the second value, 3/4 or 1/2 in your example

char delimeters = ' ';
string[] tokens = line.Split(delimeters);


char fractionDelimiter = '/';
// get the first operand
string first = tokens[0];
bool result = int.TryParse(first, out whole);
double operand1 = 0;

//if this is a fraction we have more work...
// we need to split again on the '/'
if (result) {
   operand1 = (double)whole;
} else {
   //make an assumption that this is a fraction

   string fractionParts = first.Split(fractionDelimiter);
   string numerator = fractionParts[0];
   string denominator = fractionParts[1];
   operand1 = int.Parse(numerator) / int.Parse(denominator);
}

// get the second operand
string second = tokens[2];
bool secondResult = int.TryParse(second, out secondWhole);
double operand2 = 0;

if (secondResult) {
   operand2 = (double)secondWhole;
} else {
   //make an assumption that this is a fraction

   string secondFractionParts = second.Split(fractionDelimiter);
   string secondNumerator= secondFractionParts[0];
   string secondDenominator = secondFractionParts[1];
   operand2 = int.Parse(secondNumerator) / int.Parse(secondDenominator);
}

The rest should be as simple as finding out what the operation is and doing the work with operand1 and operand2

Matt Self
  • 19,520
  • 2
  • 32
  • 36
  • Wouldn't the operand cause an error when trying to split it into a String array? thats what im confused with.??? – Connor Elliott May 09 '13 at 16:28
  • That is why I threw in the TryParse first. If TryParse was successful then we don't need to split and the "out" value will be set. If TryParse wasn't successful (it returned false) then we can infer that we have a slash and that we need to Split. – Matt Self May 09 '13 at 20:40