There is a code in which data is entered from the keyboard. How to change the code to enter data from a file?

using System; using System.Collections; using System.Text.RegularExpressions; class Program { public static void PrintValues(IEnumerable myCollection) { foreach (Object obj in myCollection) Console.Write("{0}", obj); Console.ReadLine(); } static void Main(string[] args) { Object qwer2; Stack stk = new Stack(); string term, qwer1, a1, a2, a3; Console.WriteLine("Write term in OPN: "); term = Console.ReadLine(); string[] split = term.Split(new Char[] { ' ' }); foreach (string s in split) { if (s.Trim() != "") { Regex rgx = new Regex("[A-Za-z0-9]"); foreach (char str in s) if (rgx.IsMatch(s)) stk.Push(s); else { if (s == "+" || s == "-") { qwer2 = stk.Pop(); a3 = qwer2.ToString(); a2 = s.ToString(); qwer2 = stk.Pop(); a1 = qwer2.ToString(); qwer1 = string.Concat("(" + a1 + a2 + a3 + ")"); stk.Push(qwer1); } else { qwer2 = stk.Pop(); a3 = qwer2.ToString(); a2 = s.ToString(); qwer2 = stk.Pop(); a1 = qwer2.ToString(); qwer1 = string.Concat(a1 + a2 + a3); stk.Push(qwer1); } } } } PrintValues(stk); } } 
  • All the necessary code should be inserted directly into the question, the link can only be a supplement - Grundy

1 answer 1

Change Console.ReadLine to StreamReader.ReadLine . There is an example in the documentation . And one more .


In your case, the entry point is one, instead of

 term = Console.ReadLine(); 

need to write

 using (var sr = new StreamReader(path_to_your_file)) term = sr.ReadLine(); 
  • And can you be more specific with my example? And then I'm confused at all. - alex77
  • one
    @ alex77: Well, you really want to chew. Added in response. - VladD
  • Thank you very much, you helped me a lot. One more question. For the program to calculate the value of the output expression, do you need to insert the code for the "Calculator" program? - alex77