Click here to Skip to main content
15,891,993 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

this is code for getting First Operand, Second Operand and Operator;
C#
string op1 = screentxt.Text.Split('+')[0].ToString();

            int index = screentxt.Text.IndexOf('+');
            string op2 = screentxt.Text.Substring(index + 1);

            char[] str = screentxt.Text.Trim().ToCharArray();
            string op="";

            foreach (char c in str)
            {
                if (char.IsSymbol(c))
                    op = c.ToString();
            }

If my string contains '+' sign then it return me '+' but not for the other chars like -, *, /. Help to remove this error..
Posted

Hi Krunal,

Use the below line to find the same.

C#
if(screentxt.Text.Contains("+"))
{
  string op1 = screentxt.Text.Split('+')[0].ToString();
  int index = screentxt.Text.IndexOf('+');
  string op2 = screentxt.Text.Substring(index + 1);
  string op= "+";
}


Above code block only execute when your textbox contains + sign.
 
Share this answer
 
v3
Comments
[no name] 21-Nov-12 4:49am    
Na I don't want like this.. I want value of two operands and operator as different strings..
If I understand correctly, you are able to extract character (+, * etc) from a string.

But Your char.IsSymbol returns true only for + symbol and not for others.

This is expected behaviour as IsSymbol works little strangely. It returns true only if character belonngs to one of the following Unicode categories. MathSymbol, CurrencySymbol, ModifierSymbol, and OtherSymbol.

And guess what, * , - doesn't fall in any of this category. Check yourself with following code

C#
Console.WriteLine(Char.GetUnicodeCategory('*').ToString());

Console.WriteLine(Char.GetUnicodeCategory('-').ToString());


The best way to identify then is by putting switch case and manual character code comparison.

Hope that helps
Milind
 
Share this answer
 
Comments
[no name] 21-Nov-12 5:23am    
Thanks mate for this knowledge. :) Really helpful :)
MT_ 21-Nov-12 5:24am    
Welcome.
If it helps, mark it as answer/upvote.
Milind
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Text;
using System.Reflection;

///
/// Summary description for Class1.
///

public class Evaluator
{
#region Construction
public Evaluator(EvaluatorItem[] items)
{
ConstructEvaluator(items);
}

public Evaluator(Type returnType, string expression, string name)
{
EvaluatorItem[] items = { new EvaluatorItem(returnType, expression, name) };
ConstructEvaluator(items);
}

public Evaluator(EvaluatorItem item)
{
EvaluatorItem[] items = { item };
ConstructEvaluator(items);
}

private void ConstructEvaluator(EvaluatorItem[] items)
{
ICodeCompiler comp = (new CSharpCodeProvider().CreateCompiler());
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("system.dll");
cp.ReferencedAssemblies.Add("system.data.dll");
cp.ReferencedAssemblies.Add("system.xml.dll");
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;

StringBuilder code = new StringBuilder();
code.Append("using System; \n");
code.Append("using System.Data; \n");
code.Append("using System.Data.SqlClient; \n");
code.Append("using System.Data.OleDb; \n");
code.Append("using System.Xml; \n");
code.Append("namespace ADOGuy { \n");
code.Append(" public class _Evaluator { \n");
foreach (EvaluatorItem item in items)
{
code.AppendFormat(" public {0} {1}() ",
item.ReturnType.Name,
item.Name);
code.Append("{ ");
code.AppendFormat(" return ({0}); ", item.Expression);
code.Append("}\n");
}
code.Append("} }");

CompilerResults cr = comp.CompileAssemblyFromSource(cp, code.ToString());
if (cr.Errors.HasErrors)
{
StringBuilder error = new StringBuilder();
error.Append("Error Compiling Expression: ");
foreach (CompilerError err in cr.Errors)
{
error.AppendFormat("{0}\n", err.ErrorText);
}
throw new Exception("Error Compiling Expression: " + error.ToString());
}
Assembly a = cr.CompiledAssembly;
_Compiled = a.CreateInstance("ADOGuy._Evaluator");
}
#endregion

#region Public Members
public int EvaluateInt(string name)
{
return (int)Evaluate(name);
}

public string EvaluateString(string name)
{
return (string)Evaluate(name);
}

public bool EvaluateBool(string name)
{
return (bool)Evaluate(name);
}

public object Evaluate(string name)
{
MethodInfo mi = _Compiled.GetType().GetMethod(name);
return mi.Invoke(_Compiled, null);
}
#endregion

#region Static Members
static public double EvaluateToDouble(string code)
{
Evaluator eval = new Evaluator(typeof(double), code, staticMethodName);
return (double)eval.Evaluate(staticMethodName);
}

static public int EvaluateToInteger(string code)
{
Evaluator eval = new Evaluator(typeof(int), code, staticMethodName);
return (int)eval.Evaluate(staticMethodName);
}

static public string EvaluateToString(string code)
{
Evaluator eval = new Evaluator(typeof(string), code, staticMethodName);
return (string)eval.Evaluate(staticMethodName);
}

static public bool EvaluateToBool(string code)
{
Evaluator eval = new Evaluator(typeof(bool), code, staticMethodName);
return (bool)eval.Evaluate(staticMethodName);
}

static public object EvaluateToObject(string code)
{
Evaluator eval = new Evaluator(typeof(object), code, staticMethodName);
return eval.Evaluate(staticMethodName);
}
#endregion

#region Private
const string staticMethodName = "__foo";
Type _CompiledType = null;
object _Compiled = null;
#endregion
}

public class EvaluatorItem
{
public EvaluatorItem(Type returnType, string expression, string name)
{
ReturnType = returnType;
Expression = expression;
Name = name;
}

public Type ReturnType;
public string Name;
public string Expression;
}



/*
// USE THE FOLLOWING CODE TO EXECUTE...

string sAnswer = Evaluator.EvaluateToDouble("10+15+20-18*2/8").ToString();
*/
 
Share this answer
 
Comments
MT_ 21-Nov-12 13:15pm    
explain what are you trying to show in the code. Posting such big code without any explanaion doesn't help really for OP or others to understand.

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