Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- class Program
- {
- private static bool RemoveSpaces(Char charToCheck)
- {
- return charToCheck.Equals(' ');
- }
- public static List<String> funct(String stringToProceed)
- {
- List<Char> myString = new List<Char>(stringToProceed);
- myString.RemoveAll(RemoveSpaces);
- String myFunction = new String(myString.ToArray());
- int index = 0;
- int bracketsIndicator = 0;
- int indexOfBeginingOfSubstring = 0;
- int indexOfEndOfSubstring = 0;
- int lenghtOfSubstring = 0;
- List<String> substrings = new List<String>();
- Boolean boundaryIsReached = false;
- while (!boundaryIsReached)
- {
- indexOfEndOfSubstring = index;
- if (myFunction[index].Equals('('))
- {
- bracketsIndicator++;
- }
- else
- {
- if (myFunction[index].Equals(')'))
- {
- bracketsIndicator--;
- }
- else
- {
- if (bracketsIndicator == 0)
- {
- if (myFunction[index].Equals('+') || myFunction[index].Equals('*') ||
- myFunction[index].Equals('/') || myFunction[index].Equals('^') || myFunction[index].Equals('-'))
- {
- if (myFunction[index].Equals('-') && index == indexOfBeginingOfSubstring)
- {
- substrings.Add(new String("-".ToArray()));
- indexOfBeginingOfSubstring = index + 1;
- }
- else
- {
- indexOfEndOfSubstring = index - 1;
- lenghtOfSubstring = indexOfEndOfSubstring - indexOfBeginingOfSubstring + 1;
- if (myFunction[indexOfBeginingOfSubstring].Equals('(') && myFunction[indexOfEndOfSubstring].Equals(')'))
- {
- indexOfBeginingOfSubstring++;
- lenghtOfSubstring -= 2;
- }
- substrings.Add(new String(myFunction.Substring(indexOfBeginingOfSubstring, lenghtOfSubstring).ToCharArray()));
- substrings.Add(new String(myFunction.Substring(index, 1).ToCharArray()));
- indexOfBeginingOfSubstring = index + 1;
- }
- }
- }
- }
- }
- index++;
- if (index == myFunction.Length)
- {
- if (bracketsIndicator == 0)
- {
- if (!myFunction[index - 1].Equals('+') && !myFunction[index - 1].Equals('*') &&
- !myFunction[index - 1].Equals('/') && !myFunction[index - 1].Equals('^') && !myFunction[index - 1].Equals('-'))
- {
- indexOfEndOfSubstring = index - 1;
- }
- else
- {
- indexOfEndOfSubstring = (index--) - 1;
- }
- lenghtOfSubstring = indexOfEndOfSubstring - indexOfBeginingOfSubstring + 1;
- if (myFunction[indexOfBeginingOfSubstring].Equals('(') && myFunction[indexOfEndOfSubstring].Equals(')'))
- {
- indexOfBeginingOfSubstring++;
- lenghtOfSubstring -= 2;
- }
- substrings.Add(new String(myFunction.Substring(indexOfBeginingOfSubstring, lenghtOfSubstring).ToCharArray()));
- }
- boundaryIsReached = true;
- }
- }
- return substrings;
- }
- static void Main(string[] args)
- {
- while (true)
- {
- Console.WriteLine("Enter something");
- String initialString = Console.ReadLine();
- List<String> foundSubstring = funct(initialString);
- foundSubstring.ForEach(delegate(String str)
- {
- Console.WriteLine(str);
- });
- Console.Write('\n');
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement