Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用从文件中读取这些命令?_C#_Regex_Linked List - Fatal编程技术网

C# 如何使用从文件中读取这些命令?

C# 如何使用从文件中读取这些命令?,c#,regex,linked-list,C#,Regex,Linked List,我正在为我的C#班做一个学校项目,我很难弄清楚如何从我的文本文件中解析这些命令 我把这个放在里面了 I:3 I:6 基本上,我试图从文件中读取这些命令并执行它们,这将向我的链接列表中添加一个节点,并将其显示到控制台。我已经逐行解析了文件,但不确定如何提取和执行命令。因此,当它读入I:3时,控制台将显示Node1:3,依此类推。谢谢,这就是我目前所拥有的: public class LinkedList { Node head; //the head of list publ

我正在为我的C#班做一个学校项目,我很难弄清楚如何从我的文本文件中解析这些命令

我把这个放在里面了

I:3 

I:6
基本上,我试图从文件中读取这些命令并执行它们,这将向我的链接列表中添加一个节点,并将其显示到控制台。我已经逐行解析了文件,但不确定如何提取和执行命令。因此,当它读入
I:3
时,控制台将显示
Node1:3
,依此类推。谢谢,这就是我目前所拥有的:

public class LinkedList {
    Node head; //the head of list
    public class Node {
        public int data;
        public Node next;
        //constructor
        public Node(int d) {
            data = d;
            next = null;
        } //end of constructor
    }
    public void printList() { //traversing list and printing the contents starting from head(1)
        Node n = head;
        while (n != null) {
            Console.Write(n.data + " ");
            n = n.next;
        }
    }
    public void push(int new_data) {
        Node new_node = new Node(new_data); //allocate new node, put in data
        new_node.next = head; //make next of new node as head
        head = new_node; //move the head to point to new node
    }
    //main method to create a linked list with 3 nodes
    public static void Main(String[] args) {
        //starting with an empty list
        LinkedList llist = new LinkedList();
        llist.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);
        //now 3 nodes have been allocated. head, second, and third
        llist.head.next = second; //linking the head (1st) with the second node. these are both now linked.
        second.next = third; //linking second with third. now linked. all 3 are linked now
        //  llist.printList(); 
        int counter = 0;
        string line;
        //read file display line by line
        string pattern =
            new System.IO.StreamReader(@"C:\\Users\text.txt");
        while ((line = file.ReadLine()) != null) {
            System.Console.WriteLine(line);
            counter++;
        }
        file.Close();
        System.Console.WriteLine("There were {0} lines.", counter);
        // Suspend the screen.  
        System.Console.ReadLine();
    }
} //end of

当学习一门语言时,实际上任何时候我们有一个复杂的算法,我们首先拿一支笔/纸,写下步骤:

读取一个文件,逐行处理,在冒号上拆分该行,并取冒号后面的位,将其解析为整数,然后将其放入列表中

将其转换为代码注释:

//Read a file,
//process it line by line,
// split the line on colon
// take the bit after the colon,
// parse it to an integer ,
// push it into the list
现在将代码放入:

//Read a file,
string[] lines = File.ReadAllLines(@"c:\temp\nodes.txt");

// process it line by line,
foreach(string line in lines){

  // split the line on colon
  string[] bits = line.Split(':');

  // take the bit after the colon and parse it to an integer
  int x = int.Parse(bits[1]);

  //push it into the list
  myList.Push(x);
}

也许你也能看到算法的演变;实施的步骤与我们提出的第一个过程有细微的不同-这没关系,我只是想证明我们不会盲目地坚持我们设计的一系列步骤,但总的来说,我们有一个算法设计,使用我们在说话和思考时本机使用的高级语言,我们将其翻译成我们正在学习的语言——你用外语来做这件事,代码应该没有什么不同。留下评论,作为你试图做什么的指示,比如在数学考试中展示你的工作——如果你的评论有效,但代码无效,那么至少你的主管可以看到你试图做什么,并帮助纠正你在学习语言时对语言的理解出现错误的地方,事实上,任何时候我们有一个复杂的算法,我们首先拿一支笔/纸写下步骤:

读取一个文件,逐行处理,在冒号上拆分该行,并取冒号后面的位,将其解析为整数,然后将其放入列表中

将其转换为代码注释:

//Read a file,
//process it line by line,
// split the line on colon
// take the bit after the colon,
// parse it to an integer ,
// push it into the list
现在将代码放入:

//Read a file,
string[] lines = File.ReadAllLines(@"c:\temp\nodes.txt");

// process it line by line,
foreach(string line in lines){

  // split the line on colon
  string[] bits = line.Split(':');

  // take the bit after the colon and parse it to an integer
  int x = int.Parse(bits[1]);

  //push it into the list
  myList.Push(x);
}

也许你也能看到算法的演变;实施的步骤与我们提出的第一个过程有细微的不同-这没关系,我只是想证明我们不会盲目地坚持我们设计的一系列步骤,但总的来说,我们有一个算法设计,使用我们在说话和思考时本机使用的高级语言,我们将其翻译成我们正在学习的语言——你用外语来做这件事,代码应该没有什么不同。留下评论,作为你试图做什么的指示,比如在数学考试中展示你的工作——如果你有评论,但代码不起作用,那么至少你的主管可以看到你试图做什么,并帮助纠正你对语言理解的错误

谢谢,这不仅有帮助,而且我也在学习。如果我还想将I包含在它解析的内容中(因此输出将是I:3而不是3),我会编辑int x=int.Parse行吗?从列表中删除节点会有类似的过程吗?当然,在冒号上拆分时,I将位于
位[0]
。如果I是“insert”的缩写,那么你可以说
If(bits[0]==“I”)list.Push(x)-如果存在用于删除的D,则应用类似的模式。用英语写下你的算法,然后在它下面填充代码谢谢,这不仅有帮助,而且我也在学习。如果我还想将I包含在它解析的内容中(因此输出将是I:3而不是3),我会编辑int x=int.Parse行吗?从列表中删除节点会有类似的过程吗?当然,在冒号上拆分时,I将位于
位[0]
。如果I是“insert”的缩写,那么你可以说
If(bits[0]==“I”)list.Push(x)-如果存在用于删除的D,则应用类似的模式。用英语写出你的算法,然后把代码填在下面