示例#1
0
        private void run_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileIn  = new OpenFileDialog();
            SaveFileDialog fileOut = new SaveFileDialog();

            if ((fileIn.ShowDialog() != DialogResult.OK) || (fileOut.ShowDialog() != DialogResult.OK))
            {
                MessageBox.Show("Не выбран входной или выходной файл.");
                return;
            }

            //read input file
            string buffer = "";

            try
            {
                buffer = File.ReadAllText(fileIn.FileName);
                if (buffer.Length == 0)
                {
                    throw new Exception("File is empty.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            //create stack
            //MyStack<char> stack = new MyStack<char>();
            DataTypes.Stack <char> stack = new DataTypes.Stack <char>();
            for (int i = 0; i < buffer.Length; i++)
            {
                stack.Push(buffer[i]);
            }
            //get number
            try
            {
                buffer = StackLogic.GetNumber(stack);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            //write result
            try
            {
                File.WriteAllText(fileOut.FileName, buffer);
                MessageBox.Show(String.Format("Результат {0} в : {1}", buffer, fileOut.FileName));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
示例#2
0
        public static string GetNumber(DataTypes.Stack <char> stack)
        {
            if (stack.Size == 0)
            {
                throw new Exception("Stack is empty.");
            }
            string tmp = "";

            while (stack.Size != 0)
            {
                char c = stack.Pop();
                if ((c >= '0') && (c <= '9'))
                {
                    tmp += c;
                }
            }
            if (tmp.Length == 0)
            {
                throw new Exception("Number is empty.");
            }
            return(tmp);
        }