Connor Elliott
Connor Elliott

Reputation: 49

Tokenizer/Split in c#?

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]

Upvotes: 1

Views: 455

Answers (3)

Matt Self
Matt Self

Reputation: 20125

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

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726559

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.

Upvotes: 1

Lennart
Lennart

Reputation: 345

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];

Upvotes: 2

Related Questions