01/10/2018, 00:02

Hỏi về lỗi trong lập trình CSharp

Chào Mọi người, buổi tối vui vẻ ạ
Em đang làm bài tập về mã hóa Huffman. Em viết bằng C#.
Mục đích là tạo ra một cây, sao cho các nút có trọng số(tần xuất xuất hiện của ký tự) nhỏ nhất sẽ ở xa gốc nhất.
Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using Init;
namespace Init1
{
    class Program
    {
        
        public static Node CreatCay(int n){
            int dem = 0;
            int i = 0;
            SortedList sl = new SortedList();
            while (dem < n)
            {
                Console.WriteLine("Nhap vao phan ki tu thu {0}: ", i + 1);
                char c = char.Parse(Console.ReadLine());
                Console.WriteLine("Nhap vap tan xuat cua phan tu thu {0}: ", i + 1);
                int x = int.Parse(Console.ReadLine());
                Node Cay = new Node(c, x, null, null);
                sl.Add(x, Cay);
                Cay.clear();
                dem += x;
                i
            }
            Node iNIT = new Node();
            while (sl.Count != 1) {
                if (sl.GetByIndex(i) is Node && sl.GetByIndex(i+1) is Node)
                {
                    iNIT.Left = (Node)sl.GetByIndex(i);
                    iNIT.Right = (Node)sl.GetByIndex(i);
                }
                sl.RemoveAt(i); sl.RemoveAt(i + 1);
                iNIT.clear();
            }
            if (sl.Count == 1) { iNIT = (Node)sl.GetByIndex(0); };
            return iNIT;
        }
        static void Main(string[] args)
        {
            Node root =   CreatCay(50);
            Console.ReadLine();
        }
    }
}

Class Node:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Init
{
    public class Node

    {
        private Node right_;
        private Node left_;
        private char data_;
        private int tanxuat_;

        public Node(){}
        public Node(char d, int t, Node pl, Node pr)
        {
            data_ = d;
            tanxuat_ = t;
            right_ = pr;
            left_ = pl;
        }
        public char Data
        {
            get
            {
                return data_; 
            }
            set
            {
                data_ = value;
            }
        }
        
        public int TanXuat
        {
            get
            {
                return tanxuat_;
            }
            set
            {
                tanxuat_ = value;
            }
        }
        public Node Left
        {
            get { return left_; }
            set {   left_= value; }
        }
        public Node Right
        {
            set { right_ = value; }
            get { return right_; }
        }
        public void clear()
        {
            this.Data = char.MinValue;
            this.TanXuat = int.MinValue;
            this.left_ = null;
            this.right_ = null;
        }
        public static bool operator >(Node p, Node q){
            return (p.tanxuat_ > q.tanxuat_);
        }
        public static bool operator <(Node p, Node q)
        {
            return (p.tanxuat_ < q.tanxuat_);
        }
    }
}

Khi em thực hiện debug thì gặp phải lỗi như thế này:

Mọi người ai biết về lỗi này, chỉ giúp em với ạ.
Em cảm ơn ạ.

Tao Không Ngu. viết 02:05 ngày 01/10/2018

Không ép kiểu về int được. Bạn xem lại phần nhập liệu hoặc cho đọc từ file luôn xem.

Phong Vũ viết 02:14 ngày 01/10/2018

Em cảm ơn ạ.

Văn Dương viết 02:02 ngày 01/10/2018

Lỗi do chuỗi truyền vào hàm Parse không đúng định dạng ( chứa ký tự không hợp lệ )

Phong Vũ viết 02:09 ngày 01/10/2018

Em cảm ơn ạ Em thử đỏi sang int x = Convert.ToInt32(Console.Readline()) thì không lỗi nưa.

Phong Vũ viết 02:04 ngày 01/10/2018

Mọi người cho em hỏi là hàm xóa bộ nhớ đệm trong C# là gì thế ạ?

Văn Dương viết 02:03 ngày 01/10/2018

GC nó sẽ tự động xoá.
Hoặc có thể gọi GC.Colect() để thúc đẩy quá trình thu hồi.

Phong Vũ viết 02:15 ngày 01/10/2018

Em cảm ơn anh ạ

Bài liên quan
0