0
所以我一直在研究這個程序分析某個輸入文件,除了關鍵字列表之外,幾乎所有東西都正確輸出。爲什麼這個列表只打印出最後一個字符?
關於繼承人的關鍵字列表和相關功能的代碼片段:
public class Token //Token structure to be added for each Token in the tokenTable
{
public string value;
public string type;
}
internal static class Define_input_output
{
public const string inFile = "input.txt"; //set file to read
public const string outFile = "output.txt"; //set file to write to
}
public static void placeKeywords_in_keywordsTable(List<string> keywords, List<Token> keywordTable)
{
Token newToken = new Token();
for (int i = 0; i < keywords.Count; i++) //loop through each keyword
{
newToken.value = keywords[i]; //add keyword[i] to newToken's value
newToken.type = "KW"; //assign "kw" to newToken's type
keywordTable.Add(newToken); //push newToken to keywordTable
}
}
public static void print_Tables_to_outputFile(StreamWriter output, List<Token> keywordTable)
{
output.Write("\r\n");
output.Write("-".PadRight(21, '-'));
output.Write("\r\n");
output.Write(' ');
output.Write("Keyword ".PadRight(11));
output.Write("|".PadRight(2));
output.Write("Index");
output.Write("\r\n");
output.Write("-".PadRight(21, '-'));
output.Write("\r\n");
for (int i = 0; i < keywordTable.Count; i++) //loop through each element in keywordTable
{
output.Write(' ');
output.Write((keywordTable[i].value).PadRight(10));
output.Write(" |".PadRight(7));
output.Write(i);
output.Write("\r\n");
}
}
static int Main()
{
StreamReader input = new StreamReader(Define_input_output.inFile);//input file
StreamWriter output = new StreamWriter(Define_input_output.outFile);//output file
List<string> keywords = new List<string>() { "else", "if", "int", "return", "void", "while", "+", "-", "*", "/", "<", "<=", ">", ">=", "==", "!=", "=", ";", ",", "(", ")", "[", "]", "{", "}", "/*", "*/" };
List<Token> keywordTable = new List<Token>();
placeKeywords_in_keywordsTable(keywords, keywordTable); //place all the keywords into keywordTable
while (!input.EndOfStream) //read each line until end of file
{
read_inputFile(input, output, keywordTable);
}
print_Tables_to_outputFile(output, keywordTable);
input.Close(); //close input file
output.Close(); //close output file
return 0;
}
問題IM具有隻是這個「* /」字符被打印出來的關鍵詞表。其索引26次(這是索引的正確數量,因爲這是多少個字符在列表中)。 我通過從列表中刪除最後兩個字符進行測試,並且「}」字符開始打印26次。
我不知道有什麼缺失,因爲我仔細查看了代碼多次,我只是無法找到問題所在。 任何幫助表示讚賞。謝謝!
真棒!謝謝 – masterofcatastrophe
@masterofcatastrophe - 歡迎您:) –
肯定的事情,我只是在等待計時器哈哈 – masterofcatastrophe