Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have a string like this :

s=2+4
(s[0]=2,s[1]=+,s[2]=4).
how can i extract 2, 4 from it ?

C#
using System;

class program
{
    public static void Main()
    {
        int n,x=0,y=0,a,a1,b1,b,f=0,z,sum=0,q=0,l=0;
        char c;
        string s,s1,s2,s3;
        int [] sum1=new int [4];
        Console.WriteLine("Let's go :");
        s = Console.ReadLine();
        for (int i=0;i<s.length;i++)>
        {
            switch( s[i])
            {
                case '0':
                    n = Convert.ToInt32(s[q]);
                    string[,] zero = new string[7, 3] { { "0", "0", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", "0", "0" }, { "0", "0", "0" } };
                    for (int h=0;h<7;h++)
                    {
                        for (int j=0;j<3;j++)
                        {
                            Console.Write(zero[h,j]);
                        }
                        Console.WriteLine();
                     
                    }
                   sum1[q]=n;
                   q = q + 1;


                    
                    break;
                case '1':
                    n = Convert.ToInt32(s[i]);
                   string [,] one=new string[7,3] {{"1","1","1"},{" " ," ","1"},{" "," ","1"},{" "," ","1"},{" "," ","1"},{" "," ","1"},{" "," ","1"}};
                   for (int h = 0; h < 7; h++)
                   {
                       for (int j = 0; j < 3; j++)
                       {
                           Console.Write(one[h, j]);
                       }
                       Console.WriteLine();

                   }
                  
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '2':
                    n = Convert.ToInt32(s[i]);
                   string[,] two= new string[7, 3] { { "2","2","2" }, { " "," ","2" }, { " "," ","2" }, { "2","2","2" }, { "2"," "," " }, { "2"," "," " }, {"2","2","2" } };
                   for (int h = 0; h < 7; h++)
                   {
                       for (int j = 0; j < 3; j++)
                       {
                           Console.Write(two[h, j]);
                       }
                       Console.WriteLine();
                   }
                   x = 2;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '3':
                    n = Convert.ToInt32(s[i]);
                    string[,] three = new string[7, 3] { {"3","3","3" }, { " ", " ", "3" }, { " "," ","3" }, { "3","3","3" }, { " "," ","3" }, { " "," ","3" }, {"3","3","3" } };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(three[h, j]);
                        }
                        Console.WriteLine();
                    }
                    y = 3;
                     sum1[q]=n;
                   q = q + 1;

                    break;
                case '4':
                    n = Convert.ToInt32(s[i]);
                    string[,] four = new string[7, 3] { { "4"," ","4" }, {"4"," ","4"}, {"4"," ","4" }, { "4","4","4"}, { " "," ","4"}, { " ", " ","4"}, { " "," ","4"} };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(four[h, j]);
                        }
                        Console.WriteLine();
                    }
                    x = 4;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '5':
                    n = Convert.ToInt32(s[i]);
                    string[,] five = new string[7, 3] { { "5","5","5" }, {"5"," "," " }, {"5"," "," " }, { "5","5","5" }, { " "," ", "5" }, { " ", " ", "5" }, { "5","5","5" } };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(five[h, j]);
                        }
                        Console.WriteLine();
                    }
                    y = 5;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '6':
                    n = Convert.ToInt32(s[i]);
                    string[,] six = new string[7, 3] { { "6","6","6" }, {"6", " "," " }, {"6", " ", " " }, {"6","6","6" }, {"6"," ","6"}, {"6"," ","6" }, {"6","6","6" } };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(six[h, j]);
                        }
                        Console.WriteLine();
                    }
                    x = 6;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '7':
                    n = Convert.ToInt32(s[i]);
                    string[,] seven = new string[7,4] { { "7","7","7"," " }, { " ", " ", "7"," " }, { " ", " ", "7" ," "}, { " "," ","7"," " }, { "7","7","7","7" }, { " ", " ", "7"," " }, { " ", " ","7"," " } };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            Console.Write(seven[h, j]);
                        }
                        Console.WriteLine();
                    }
                    y = 7;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '8':
                    n = Convert.ToInt32(s[i]);
                    string[,] eight = new string [7,3 ] { {"8","8","8" }, {"8"," ","8" }, { "8"," ","8" }, { "8","8","8" }, { "8"," ","8" }, { "8"," ","8" }, {"8","8","8"} };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(eight[h, j]);
                        }
                        Console.WriteLine();
                    }
                    x = 8;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '9':
                    n = Convert.ToInt32(s[i]);
                    string[,] nine = new string[7, 3] { {"9","9","9" }, {"9"," ","9" }, {"9"," ","9" }, {"9","9","9"}, {" "," ","9"}, {" "," ","9" }, {"9","9","9"} };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(nine[h, j]);
                        }
                        Console.WriteLine();
                    }
                    y = 9;
                     sum1[q]=n;
                   q = q + 1;
                    break;
              
                case '+':
                 string[,] plus = new string [3,3] {{" " ,"+"," " },{"+" ,"+","+"},{" ","+"," "}};
                 for (int h = 0; h < 3; h++)
                 {
                     for (int j = 0; j < 3; j++)
                     {
                         Console.Write(plus[h, j]);
                     }
                     Console.WriteLine();
                 }
                

                    break;
                case '-':
                    string[,] min = new string[3, 3] { { " ", " ", " " }, { "-", "-", "-" }, { " ", " ", " " } };
                    for (int h = 0; h < 3; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(min[h, j]);
                        }
                        Console.WriteLine();
                        c = '-';
                    }
                    break;
                case '*':
                   string[,] mul = new string[3, 3] { { "*", " ", "*" }, { " ", "*", " " }, { "*", " ", "*" } };
                   for (int h = 0; h < 3; h++)
                   {
                       for (int j = 0; j < 3; j++)
                       {
                           Console.Write(mul[h, j]);
                       }
                       Console.WriteLine();
                   }
                    break;
                case '/':
                    string[,] div = new string[3, 3] { { " ", " ", "/" }, { " ", "/", " " }, { "/", " ", " " } };
                    for (int h = 0; h < 3; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(div[h, j]);
                        }
                        Console.WriteLine();
                    }
                    break;
                default :
                   
                    break;

            }

        }
        string[,] equel = new string[3, 3] { { "=", "=", "= " }, { " ", " ", " " }, { "=", "=", "=" } };
        for (int h = 0; h < 3; h++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.Write(equel[h, j]);
            }
            Console.WriteLine();
        }

//here i must extract two number and put their sum in f.problem is here.
      
        switch(s[1])
        {
            case'+':
               f = a+b;
                break;
            case'-':
                f = a - b;
                break;
            case'*':
                f = a * b;
                break;
            case'/':
                f = a / b;
                break;
        }
        
        switch(f)
        {

            case 0:
               
                string[,] zero = new string[7, 3] { { "0", "0", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", "0", "0" }, { "0", "0", "0" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(zero[h, j]);
                    }
                    Console.WriteLine();

                }
               



                break;
            case 1:
                
                string[,] one = new string[7, 3] { { "1", "1", "1" }, { " ", " ", "1" }, { " ", " ", "1" }, { " ", " ", "1" }, { " ", " ", "1" }, { " ", " ", "1" }, { " ", " ", "1" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(one[h, j]);
                    }
                    Console.WriteLine();

                }

               
                break;
            case  2:
               
                string[,] two = new string[7, 3] { { "2", "2", "2" }, { " ", " ", "2" }, { " ", " ", "2" }, { "2", "2", "2" }, { "2", " ", " " }, { "2", " ", " " }, { "2", "2", "2" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(two[h, j]);
                    }
                    Console.WriteLine();
                }
               
                break;
            case 3:
               
                string[,] three = new string[7, 3] { { "3", "3", "3" }, { " ", " ", "3" }, { " ", " ", "3" }, { "3", "3", "3" }, { " ", " ", "3" }, { " ", " ", "3" }, { "3", "3", "3" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(three[h, j]);
                    }
                    Console.WriteLine();
                }
              

                break;
            case 4:
            
                string[,] four = new string[7, 3] { { "4", " ", "4" }, { "4", " ", "4" }, { "4", " ", "4" }, { "4", "4", "4" }, { " ", " ", "4" }, { " ", " ", "4" }, { " ", " ", "4" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(four[h, j]);
                    }
                    Console.WriteLine();
                }
             
                break;
            case 5:
              
                string[,] five = new string[7, 3] { { "5", "5", "5" }, { "5", " ", " " }, { "5", " ", " " }, { "5", "5", "5" }, { " ", " ", "5" }, { " ", " ", "5" }, { "5", "5", "5" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(five[h, j]);
                    }
                    Console.WriteLine();
                }
               
                break;
            case 6:
              
                string[,] six = new string[7, 3] { { "6", "6", "6" }, { "6", " ", " " }, { "6", " ", " " }, { "6", "6", "6" }, { "6", " ", "6" }, { "6", " ", "6" }, { "6", "6", "6" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(six[h, j]);
                    }
                    Console.WriteLine();
                }
                
                break;
            case 7:
               
                string[,] seven = new string[7, 4] { { "7", "7", "7", " " }, { " ", " ", "7", " " }, { " ", " ", "7", " " }, { " ", " ", "7", " " }, { "7", "7", "7", "7" }, { " ", " ", "7", " " }, { " ", " ", "7", " " } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        Console.Write(seven[h, j]);
                    }
                    Console.WriteLine();
                }
                
                break;
            case 8:
            
                string[,] eight = new string[7, 3] { { "8", "8", "8" }, { "8", " ", "8" }, { "8", " ", "8" }, { "8", "8", "8" }, { "8", " ", "8" }, { "8", " ", "8" }, { "8", "8", "8" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(eight[h, j]);
                    }
                    Console.WriteLine();
                }
               
                break;
            case 9:
               
                string[,] nine = new string[7, 3] { { "9", "9", "9" }, { "9", " ", "9" }, { "9", " ", "9" }, { "9", "9", "9" }, { " ", " ", "9" }, { " ", " ", "9" }, { "9", "9", "9" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(nine[h, j]);
                    }
                    Console.WriteLine();
                }

                break; 
           
        }





        Console.ReadKey();
    }

}
Posted
Updated 10-Mar-15 12:23pm
v4
Comments
Andreas Gieriet 10-Mar-15 17:37pm    
Can you please update the question and put the string into quotes in order to allow the rest of us to understand what exactly you want to extract.
What exactly to you tell us with the terms in parenthesis.
Please take the time to describe the problem if you expect us to take some of our time to answer.
Thanks
Andi
Andreas Gieriet 10-Mar-15 18:30pm    
What the heck is the expected behavior of this program code? Do you expect us to decipher this? Variable names without decent meaning (a, a1, b, ...), huge switch-case with a lot of almost copy-paste code, no functions to partition the code into digestible chunks, no visible purpose of the program, ...
You make it hard for us to help you...
Regards
Andi

Short answer: scan it.
I.e. split the text into a stream of tokens and then process one token after the other (e.g. to store them in a list).

Before scanning, define what the tokens are, e.g. numbers and operators.
- What exactly is a number? Only one digit or multiple digits in sequence?
- What operators are supported?
- Any white spaces allowed between the tokens? Any comments supported?
- ...?

Depending on the answers above, you may chose different approaches.

The very simplest token definition is
- Number = 0...9 // each number consists of one digit only
- Operator = + // only the plus operator supported
- no spaces
- no comments
C#
public class Scanner
{
    private string _text;
    public Scanner(string text) { _text = text; }
    public IEnumerator<string> GetEnumerator()
    {
        foreach(char c in _text)
        {
            string s = null;                       // default = error
            if (char.IsDigit(c)) s = c.ToString(); // number = one digit
            else if (c == '+') s = c.ToString();   // operator = +
            yield return s;
        }   
    }
}
Using by token wise forwarding:
C#
string text = "2+4";
var tokens = new Scanner(text).GetEnumerator();
while(tokens.MoveNext())
{
    string token = tokens.Current;
    if (token == null) Console.WriteLine("Error");
    else               Console.WriteLine(token);
}
Alternatively using by iterator:
C#
string text = "2+4";
foreach (string token in new Scanner(text))
{
    if (token == null) Console.WriteLine("Error");
    else               Console.WriteLine(token);
}

This scanner class is a bit overkill for the simple definition. But you have now a framework to extend to more complex tokens. Eventually, you will switch to Regex tokenizing.
Cheers
Andi
 
Share this answer
 
v6
Comments
[no name] 10-Mar-15 19:15pm    
Andreas, there appears to be something missing from the Scanner-class which should enumerate.. ;-)
- Sebastian
Andreas Gieriet 10-Mar-15 20:01pm    
Thanks for your feedback. Indeed, the tokenizer was broken. I fixed it.
Cheers
Andi
Sergey Alexandrovich Kryukov 1-Apr-15 12:21pm    
Sure, a 5.
—SA
Andreas Gieriet 1-Apr-15 16:04pm    
Thanks for your 5!
Cheers
Andi
Try Regex:
C#
using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string str = "s=2+4+76";
        Match match = Regex.Match(str, @"\d+");
        while (match.Success)
        {
            Console.WriteLine(match.Value);
            match = match.NextMatch();
        }
    }
}
 
Share this answer
 
Parse it into elements, using string manipulations and/or Regex (https://msdn.microsoft.com/en-us/library/system.string%28v=vs.110%29.aspx[^], https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28v=vs.110%29.aspx[^]). Then use methods int.TryParse (uint.TryParse, long.TryParse, whatever types you need); see, for example, https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx[^].

The detail of parsing depends on the format of your strings, which you did not bother to share with us. An example is not spec, but it's possible that you need to make a fully-fledged parser of some kins. Please see, for example, http://www.codeproject.com/search.aspx?q=%22C%23%22+expression+parser&doctypeid=1[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900