2016-10-22 50 views
0

imはスタックとキューに関係するC#プログラムで作業しています。待ち行列は入力文字列をエンキューし、その間に待ち行列からの入力に対してスタック操作が行われるとデキューします。スタックが空です...なぜですか?

現在、checkMatch()に何が起こっているのか、プログラムはスタックが空であるという例外エラーを出します。 Iveはデバッグ/ステップオーバーを使用していました。私はStackがcheckMatch関数で本当に空であるのを見ています。 私は同じプログラムをC++でテストしていますが、C++ではこのエラーはまったく起こりません。実際には、私が望む出力が得られます。

しかし、1日を通して長い研究の後、浅いコピー、クローンなどを含むたくさんのものを試してみます。私はまだプログラムがcheckMatch関数を入力するまでにスタックに何かを含めるように見えます。私のデバッグ活動中、iveはスタックと入力がcheckMatch関数でリセットされていることに気づきました。なぜこのスタックの空の例外を受け取っているのですか?

public static void loadtheInput(Queue<string> input) 
{ 
    input.Enqueue("A"); 
    input.Enqueue("B"); 
    input.Enqueue("C"); 
    input.Enqueue("D"); 
} 

public static void printtheLine(StreamWriter DisplayOutTxt, Queue<string> sameMatch, Stack<string> stack, Queue<string> input, string operations) 
{ 
    string returnMatched = ""; 
    string returnStack = ""; 
    string returnInput = ""; 

    if (stack.Count == 0) //if stack is empty, printtheLine so DisplayOutTxt the table header 
    { 
     stack.Push("A"); 
     stack.Push("C"); 
    } 
    returnMatched = printQueue(matched); 
    returnStack = printStack(stack); 
    returnInput = printQueue(input); 
} 

public static string printStack(Stack<string> stack) 
{ 
    string DisplayOutTxt = ""; 
    while (stack.Count > 0) 
    { 
     DisplayOutTxt += stack.Peek(); 
     stack.Pop(); 
    } 
    return DisplayOutTxt; 
} 

private static string printQueue(Queue<string> queue) 
{ 
    string DisplayOutTxt = ""; 

    if (queue.Count == 0) //if the queue is empty 
    { 
     DisplayOutTxt = " "; //set DisplayOutTxt to a space 
    } 
    else 
    { 
     while (queue.Count > 0) //queue not empty 
     { 
      DisplayOutTxt += queue.Peek(); //concat front of queue to DisplayOutTxt 
      queue.Dequeue(); //dequeue the front string 
     } 
    } 
    return DisplayOutTxt; 
} 

public static void checkMatch(StreamWriter DisplayOutTxt, Queue<string> sameMatch, Stack<string> stack, Queue<string> input, ref string operations) 
{ 
    printtheLine(DisplayOutTxt, sameMatch, stack, input, operations); //print line of DisplayOutTxt 

    //here is where i start facing the problem. stack (and input) are both empty once they step into this checkMatch function! 
    //I think its a reference issue, but i just cant figure out what to do after everything Ive tried 

    if (stack.Peek() == input.Peek()) //if the next stuff in stack and input match each other 
    { 
     // some code is here 
    } 
} 

static int Main() 
{ 
    StreamWriter DisplayOutTxt = new StreamWriter("output.txt"); 

    Queue<string> sameMatch = new Queue<string>(); 
    Stack<string> stack = new Stack<string>(); 
    Queue<string> input = new Queue<string>(); 

    string operations = ""; 
    loadtheInput(input); //load input into input queue and load all productions into parse table 

    while (input.Count > 0) //while input vector is not empty 
    { 
     checkMatch(DisplayOutTxt, sameMatch, stack, input, ref operations); //call function to check for sameMatch stuff 
    } 
    DisplayOutTxt.Flush(); 
    DisplayOutTxt.Close(); 
    return 0; 
} 

heres私はcheckMatch機能が例外エラー画像

heresを入力した時点でのスタック数を決定するために行ったいくつかのデバッグ/ステップオーバーの画像:ここ

コードです

答えて

1

あなたのprintStack関数では、スタックをクリアしています。それぞれの項目をルーピングおよびポップすることによって。

hereを参照すると、スタックアイテムをポップすることなく印刷できます。

C#では、Stackパラメータは参照型であるため、関数で変更すると元のStackに変更が加えられます。しかし、StackはIEnumerableを実装しているため、元のアイテムを変更せずにアイテムを列挙できます。あなたは

public static string printStack(IEnumerable<string> stack) 
{ 
    string DisplayOutTxt = ""; 

    foreach (var obj in stack) 
    { 
     DisplayOutTxt += obj; 
    } 

    return DisplayOutTxt; 
} 

しかし、はるかに容易に

returnStack = string.Join("", stack); 
+0

を行うことであろう。このようなものを使用することができ

私はそれはいくつかのループとしなければならなかった気持ちを持っていました! btw、あなたが投稿したリンクを確認したところ、彼らはどのようにそれをやったのか分かりましたbutu私はprintStack()にそれをどのように適用するか分かりません。私はポップを削除しますか?私はそれをしましたが、それは問題を早く解決しませんでした。私はおそらく順序を変更するのですか? – masterofcatastrophe

+0

returnStack、戻り値DisplayOutTxtで置き換えますか? – masterofcatastrophe

+0

いいえ、IEnumerableコレクション内のすべてのアイテムを一緒に結合するメソッドは必要ありません。string.Join()メソッドを使用すると、空の文字列が区切り文字になります。基本的には、.NETフレームワークに既に存在するものを実装したばかりです。 –

関連する問題