c# - case statement for logical operators -
i getting "cannot implicitly convert string char" error. trying following code, how should specify cast in case statement <= operator. help/direction appreciated. thanks!
char test1; switch(test1) { case '+': //do break; case '-' : case '*' : case '<=' : //method1 break method 1: private void method1(char test2) { if(test2 == '+' || test2 == '*' || test2.tostring() == "<=") { token.add(test2.tostring()); } }
ok, have method1 this:
private void method1(char test2) { if(test2 == '+' || test2 == '-' || test2 == '/' || test2 == '*') { //code tokens.add(test2); } char test1; switch(test1) { case '+': case '-' : case '*': case '/': method1(test1); break;
i trying tokens if there expression (a+b)*(a-b). tokens list of string here. but, trying here check logical operators if exist in expression...ex: (a+b)<=5, in case, want check if next token <=, if add list of tokens, goal have 1 method handle operators(+,-, *,/,<=,>=, ==, !=) , call in switch case statement.
you can use actiondictionary instead of switch case statement:
private void test(string operant) { dictionary<string, action> actionmap = new dictionary<string, action>(); // map operant action methods actionmap.add("+", new action(addtoken)); actionmap.add("-", new action(addtoken)); actionmap.add("*", new action(addtoken)); actionmap.add(">=", () => { // anynomous method token.add(">="); }); actionmap.add("/", new action(divide)); actionmap.add("<=", new action(lessthanorequal)); // list keep continue here foreach (string key in actionmap.keys) { if (key == operant) { actionmap[key](); } } } private void addtoken() { } private void divide() { } private void lessthanorequal() { } // .. , on
Comments
Post a Comment