2013-05-08 83 views
1

我想把它從java變成C#並且除了tokenizer之外幾乎完成了它。我知道你在C#中使用拆分,但我似乎無法弄清楚。程序需要分解用戶輸入的公式(4/5 + 3/4)是沒有括號的格式。任何幫助都會很棒。在c#中的Tokenizer/Split?

// 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(); 

我將需要symobol(+, - ,*,或/)後,需要進行檢查,看它是否是一個整數,我在我的代碼後做這一權利。下面是我嘗試過的一些東西,但是我被卡住了。

char delimeters = ' '; 
string[] tokens = line.Split(delimeters); 
string first = tokens[0]; 
char c = tokens[1] 
+0

如果我正確undedrstand你的問題那麼簡單,而是二認爲應該是'char c =令牌[1] [0]' – Lennart 2013-05-08 00:52:29

+0

我不確定這是否可行?如果我需要澄清任何事情,請讓我知道。 – 2013-05-08 00:55:19

+0

順便說一句,如果你打算使用float.Parse,請注意本地化。 (http://stackoverflow.com/questions/147801/best-way-to-parse-float) – Lennart 2013-05-08 01:07:22

回答

2
tokens

是一個字符串數組,所以令牌[1]是一個字符串,也可以不分配一個字符串轉換爲炭。這就是爲什麼在javacode中寫入charAt(0)。該轉換爲C#提供了

char delimeters = ' '; 
string[] tokens = line.Split(delimeters); 
string first = tokens[0]; 
char c = tokens[1][0]; 
1

的等效Java的

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

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

最有可能的,你需要做的這一個循環。該first將被讀一次,然後,你會讀operatoroperand而更多的數據是可用的,就像這樣:

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

這個循環後您的operators將包含N字符代表的運營商,並operands包含表示N+1串操作數。

0

我剛剛編碼(即我沒有嘗試)......但在這裏你走了。

//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); 
} 

其餘應儘可能找出操作是什麼,做與操作數,我不知道工作的操作數

+0

試圖將操作數分裂成一個字符串數組時會不會導致錯誤?多數民衆贊成在什麼即時消息混淆。 – 2013-05-09 16:28:32

+0

這就是爲什麼我首先投入TryParse。如果TryParse成功,那麼我們不需要分割並設置「out」值。如果TryParse不成功(它返回false),那麼我們可以推斷出我們有一個斜線,並且我們需要Split。 – 2013-05-09 20:40:11