是否有可能在c#中创建自己的语法或类似的东西,或者使用我自己的字符串格式来...? [英] is it possible to make your own syntax in c# or something equivalent or make my own string format to wok with...?

查看:44
本文介绍了是否有可能在c#中创建自己的语法或类似的东西,或者使用我自己的字符串格式来...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Assalam o alaikum朋友我是c#和.Net平台的新手,我有一个任务要做我想要执行一些加法,减法和其他一些数学控件,用户可以在其中添加两个数字。 ..(add:2,3)这应该是我的语法,适用于所有运算符(如sub:4,2或Mod:9,2或Mul:3,4)下面是我的代码,它是不完整的。如果有人可以告诉我或给我一个想法,请代表..并提前致谢





 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Net.Mail;

命名空间方法
{
class 计划
{
静态 void Main( string [] args)
{
/// / string opr = Console .Read(string.Format({0}:{1},{2},op_nm,op1,op2));
// 字符串a;
// a =add:2,3;

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

// } while(repi == true);
Console.WriteLine( 您想要的写功能。);
string op_syn;
op_syn = Console.ReadLine();
char [] choi = {' :'};
string [] distri_syn;
distri_syn = op_syn.Split(choi);
foreach 字符串 distri_syn)
{
Console.WriteLine( {0},字);

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


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

}
}
}

解决方案

坦率地说,这不好:为什么你是否使用Split打破只有一个实例的东西?

如果你的语法是

function name:parameter1 ,parameter2

然后通过检查行并将其分解为标记来开始:使用IndexOf返回:的位置,然后使用Substring两次将其分解为函数名称, 参数,然后用逗号做同样的事情来创建两个参数。



当您验证了远处的输入时,将参数转换为整数(或浮点数)或者使用TryParse加倍 - 你计划使用的任何类型,如果它们都有效,则将函数名和两个数字参数交给evaluate函数。它查看名称,然后执行操作 - 然后返回值。



这允许以后扩展:

添加:(Mul:3,4),(Mul:5,6)

因为这允许你将内部函数传递给evaluate方法,然后在下一次调用中使用结果


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


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;

        }
    }
}

解决方案

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.


这篇关于是否有可能在c#中创建自己的语法或类似的东西,或者使用我自己的字符串格式来...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆