Advertisement
Guest User

12

a guest
Oct 31st, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.  
  12.         private static bool RemoveSpaces(Char charToCheck)
  13.         {
  14.             return charToCheck.Equals(' ');
  15.         }
  16.  
  17.         public static List<String> funct(String stringToProceed)
  18.         {
  19.  
  20.             List<Char> myString = new List<Char>(stringToProceed);
  21.  
  22.             myString.RemoveAll(RemoveSpaces);
  23.  
  24.             String myFunction = new String(myString.ToArray());
  25.  
  26.             int index = 0;
  27.             int bracketsIndicator = 0;
  28.             int indexOfBeginingOfSubstring = 0;
  29.             int indexOfEndOfSubstring = 0;
  30.             int lenghtOfSubstring = 0;
  31.  
  32.             List<String> substrings = new List<String>();
  33.  
  34.             Boolean boundaryIsReached = false;          
  35.  
  36.             while (!boundaryIsReached)
  37.             {
  38.                 indexOfEndOfSubstring = index;
  39.                 if (myFunction[index].Equals('('))
  40.                 {
  41.                     bracketsIndicator++;
  42.                 }
  43.                 else
  44.                 {
  45.                     if (myFunction[index].Equals(')'))
  46.                     {
  47.                         bracketsIndicator--;
  48.                     }
  49.                     else
  50.                     {
  51.                         if (bracketsIndicator == 0)
  52.                         {
  53.                             if (myFunction[index].Equals('+') || myFunction[index].Equals('*') ||
  54.                                 myFunction[index].Equals('/') || myFunction[index].Equals('^') || myFunction[index].Equals('-'))
  55.                             {
  56.                                 if (myFunction[index].Equals('-') && index == indexOfBeginingOfSubstring)
  57.                                 {
  58.                                     substrings.Add(new String("-".ToArray()));
  59.                                     indexOfBeginingOfSubstring = index + 1;
  60.                                 }
  61.                                 else
  62.                                 {
  63.                                     indexOfEndOfSubstring = index - 1;
  64.                                     lenghtOfSubstring = indexOfEndOfSubstring - indexOfBeginingOfSubstring + 1;
  65.                                     if (myFunction[indexOfBeginingOfSubstring].Equals('(') && myFunction[indexOfEndOfSubstring].Equals(')'))
  66.                                     {
  67.                                         indexOfBeginingOfSubstring++;
  68.                                         lenghtOfSubstring -= 2;
  69.                                     }
  70.                                     substrings.Add(new String(myFunction.Substring(indexOfBeginingOfSubstring, lenghtOfSubstring).ToCharArray()));
  71.                                     substrings.Add(new String(myFunction.Substring(index, 1).ToCharArray()));
  72.                                     indexOfBeginingOfSubstring = index + 1;
  73.                                 }
  74.                             }
  75.                         }
  76.                     }
  77.                 }
  78.  
  79.                 index++;
  80.  
  81.                 if (index == myFunction.Length)
  82.                 {
  83.                     if (bracketsIndicator == 0)
  84.                     {
  85.                         if (!myFunction[index - 1].Equals('+') && !myFunction[index - 1].Equals('*') &&
  86.                             !myFunction[index - 1].Equals('/') && !myFunction[index - 1].Equals('^') && !myFunction[index - 1].Equals('-'))
  87.                         {
  88.                             indexOfEndOfSubstring = index - 1;
  89.                         }
  90.                         else
  91.                         {
  92.                             indexOfEndOfSubstring = (index--) - 1;
  93.                         }
  94.                         lenghtOfSubstring = indexOfEndOfSubstring - indexOfBeginingOfSubstring + 1;
  95.                         if (myFunction[indexOfBeginingOfSubstring].Equals('(') && myFunction[indexOfEndOfSubstring].Equals(')'))
  96.                         {
  97.                             indexOfBeginingOfSubstring++;
  98.                             lenghtOfSubstring -= 2;
  99.                         }
  100.                         substrings.Add(new String(myFunction.Substring(indexOfBeginingOfSubstring, lenghtOfSubstring).ToCharArray()));
  101.                     }
  102.                     boundaryIsReached = true;
  103.                 }
  104.             }
  105.             return substrings;
  106.         }
  107.  
  108.         static void Main(string[] args)
  109.         {
  110.            
  111.             while (true)
  112.             {
  113.                 Console.WriteLine("Enter something");
  114.                 String initialString = Console.ReadLine();
  115.  
  116.                 List<String> foundSubstring = funct(initialString);
  117.  
  118.                 foundSubstring.ForEach(delegate(String str)
  119.                 {
  120.                     Console.WriteLine(str);
  121.                 });
  122.                
  123.  
  124.                 Console.Write('\n');
  125.             }
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement