Click here to Skip to main content
15,913,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Assalam o alaikum Friends i am new to c# and .Net Platform and i have a task to do i which i want to perform some addition,subtraction and some other mathematics controls in which the user would be to add two numbers like... (add: 2,3) this should be my syntax to work with for all operators(like sub: 4,2 or Mod: 9,2 or Mul: 3,4) below is my code which is incomplete. if anyone can tell me or give me an idea so please rep.. and thanks in advance


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;

namespace methods
{
    class Program
    {
        static void Main(string[] args)
        {
            ////string opr = Console.Read(string.Format("{0}: {1},{2}",op_nm,op1,op2));
            //string a;
            //a="add: 2,3";

            ////if(a == Console.ReadLine().ToString(string.Format("{0}: {1},{2}",));
            //do{

            //}while(repi == true);
            Console.WriteLine("Write function you desire.");
            string op_syn;
            op_syn = Console.ReadLine();
            char[] choi = {':'};
            string[] distri_syn;
            distri_syn = op_syn.Split(choi);
            foreach (string word in distri_syn)
            {
                Console.WriteLine("{0}",word);

            }
            for (int i = 0; i < distri_syn.Length; i++ )
            {
                if(distri_syn[0]=="add" & distri_syn[1]==": " /*& distri_syn[2]==""*/ & distri_syn[3] == "," )
                {


                }
            }
                //Console.WriteLine("{} ");
                Console.Read();
        }
        public static void add(int)
        {
            long op1, op2;

        }
    }
}
Posted

1 solution

Frankly, that's not good: why are you using Split to break something which should only have a single instance?
If your syntax is
"function name" : "parameter1" , "parameter2" 
Then start off by checking the line, and breaking it into tokens: use IndexOf to return the position of the ":" and then Substring twice to break it into "function name" and "parameters", then do the same thing with comma to create the two parameters.

When you have validated the input that far, convert your parameters to integer (or float, or double - whatever type you are planning to use) with TryParse, and if they both work, hand the function name and the two numeric parameters to an "evaluate" function. It looks at the name, and performs the operation - it then returns the value.

This allows later expansion:
Add: (Mul: 3, 4), (Mul: 5, 6)
Since this allows you to pass the inner functions to the evaluate method and then use the results in the next call.
 
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