简体   繁体   中英

How can I read in these commands from a file using?

I'm working a school project for my C# class, and I'm having trouble figuring out how to parse in these commands from my text file.

I've got this inside of it

I:3 

I:6

Basically I'm trying to get these commands read in from the file and execute them which will add a node to my linked list and display that to a console. I've parsed the file line by line but am unsure as to how I'd extract the commands & execute them. So when it reads in I:3 , the console would display Node1:3 , and so on and so forth. Thank you, this is what I have so far:

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

When learning a language, and indeed any time we have a complex algorithm we first take a pen/paper and write out the steps:

Read a file, process it line by line, split the line on colon and take the bit after the colon, parse it to an integer , push it into the list

Turn it into code comments:

//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

Now put the code in:

//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);
}

You can perhaps see the evolution of the algorithm too; the implemented steps are subtly different than the first process we came up with - this is ok, I just wanted to demonstrate that we don't stick slavishly to the set of steps we devise but overall we have an algorithm design in the high level language we use natively when speaking and thinking and we translate that into the language we are learning - you do this with a foreign language, and code should be no different. Leave the comments in as indication of what you're trying to do, like showing your working in a maths test - if you have comments that work but code that doesn't then at least your supervisor can see what you were trying to do and help correct where your understanding of the language went wrong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM