Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C# by noam ( 14 years ago )
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace myProject
{
    public partial class Formula : Form
    {
        private int[] start;
        private int[] end;

        
        public Formula()
        {
            InitializeComponent();

            start = new int[textBox1.Text.Length / 2];
            end = new int[textBox1.Text.Length / 2];
        }


        private void Formula_Load(object sender, EventArgs e)
        {

        }

        private void generateButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Convert.ToString(IsValidInput(textBox1.Text)));
            MessageBox.Show(FigureInput(textBox1.Text));

        }

        bool IsBracket(char character)
        {
            return (character == '(' | character == ')');
        }

        bool IsNumeric(char character)
        {
            return "0123456789".Contains(character);
            // or return Char.IsNumber(character);
        }

        bool IsLetter(char character)
        {
            return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigjklmnopqrstuvwxyz".Contains(character);

        }

        bool IsRecognized(char character)
        {
            return IsBracket(character) | IsNumeric(character) | IsLetter(character);
        }

        bool ExistBrackets(string st)
        {
            for (int i = 0; i < st.Length; i++)
            {
                if (IsBracket(st[i]))
                    return false;
            }
            return true;
        }

        string AddChainDeleteBracks(int open, int close, string input)
        {
            string to="",from="";
            //get the local chain multipule the number in input[open-1]

            //the number of the times the chain should be multiplied
            for (int i = input[open - 1]; i > 0; i--)
            {
                //the content
                for (int m = open + 1; m < close; m++)
                {
                    to = to + input[m];
                }
            }

            //get the chain i want to replace with "to"
            for (int j = open - 1; j <= close; j++)
            {
                from = from + input[j];
            }
            String output = input.Replace(from, to);
            return output;
        }

        public bool IsValidInput(string input)
        {
            if (String.IsNullOrEmpty(input) || IsBracket(input[0]))
            {
                return false;
            }
            var bracketsCounter = 0;
            for (var i = 0; i < input.Length; i++)
            {
                var character = input[i];
                if (!IsRecognized(character))
                {
                    return false;
                }
                if (IsBracket(character))
                {
                    if (character == '(')
                    {
                        if (!IsNumeric(input[i - 1]))
                            return false;
                        bracketsCounter++;
                    }
                    if (character == ')')
                        bracketsCounter--;
                }


                if (bracketsCounter < 0)
                {
                    return false;
                }
            }

            return bracketsCounter==0;
        }

        private string FigureInput(string st)
        {
            int i,close;
            PopulateStartEnd();
            //get the index in the start array which is populated
            for (i = start.Length - 1; start[i] != 0; i--) ;
            //populate the last letters if there is outside the brackets
            while (!ExistBrackets(st))
            {
                //loop on the latest opening brackets in start array
                for (; i >= 0; i++)
                {
                    //find the suitable closing brackets in the end array
                    for (close = 0; ((end[close] > start[i]) && (end[close] != null)); close++) ;
                    st=AddChainDeleteBracks(i, close, st);
                }
            }

            return st;

        }
        private void PopulateStartEnd()
       {
           //assuming that start and end are empty
           int cS=0,cE=0;
           //populate start
           for (int i = 0; i < (textBox1.Text.Length); i++)
           {
               if (textBox1.Text[i] == '(')
               {
                   start[cS] = i;
                   cS++;
               }
               if (textBox1.Text[i] == ')')
               {
                   end[cE] = i;
                   cE++;
               }
           }

           }

        private void AddFirst(char[] arr, char c)
        {
            char temp;
            for (int i = 0; i < arr.Length; i++)
            {
                temp = arr[i];
                arr[i] = c;
                c = temp;
            }
        }

        private int GetCloserEnd(int num)
        {
            for (int i = 0; i < end.Length; i++)
            {
                if (end[i] > num)
                    return i - 1;
            }
            return 0;
        }
                


    }
    }

 

Revise this Paste

Your Name: Code Language: