HammingCode Decoding Project

이번학기 "컴퓨터 구조" 라는 과목을 수강 하면서 HammingCode Decoding하는 프로그램을 만들게 되였습니다. 비록 잘 만든것은 아니지만 아주 혹시라도 누군가에게 도움이 될지..... 하는 생각에 소스를 올립니다.

제약점: 값을 16bits 로 제한 했습니다. 그러니깐, 최대로 parity bit를 5개를 입력 받고 디코딩시 에러검출 해서  parity bit를 제외한 값만 출력하는 프로그램 입니다. 혹시나 로직 상에서 잘 못된 부분도 있으리라 생각을 합니다. ?? ㅋㅋ 아무튼 소스 올립니다. 소스는 C#으로 작성 되였습니다. VisualStudio.Net 기반에 Console Application 으로 만들어 졌습니다.
============================================================================================

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

namespace HammingCode_DecodingPrj
{
    class Program
    {
        int type;               //Even Parity: 1 -> 우수   Odd Parity: 2 -> 기수
        int parityBit;          //Parity bit 수 저장
        string value;           //입력 값 저장
        int errNum;             //메뉴선택시 정확 여부 상태 저장

        public static void Main(string[] args)
        {
            Program program = new Program();
        Start:
            program.errNum = program.GetMenu();
            if (program.errNum < 0)
                goto Finish;                  //짝수 혹은 홑수 선택
            else if(program.errNum == 1)
                goto Start;

            program.GetParityBitNumber();       //Parity bit  수 입력

            program.GetValue();                 //입력 값

            program.CompilingProcess();         //에러 검출 & 수정

            program.GetResult();                //결과 가져오기
            Console.WriteLine("The hammingcode result is: " + program.value);
            Console.WriteLine(System.Environment.NewLine);

            goto Start;

        Finish:
            Console.WriteLine(System.Environment.NewLine);
            Console.WriteLine("--- Thank you for using HammingCode Free Software ---");
            Console.WriteLine(System.Environment.NewLine);
        }

        public int GetMenu()
        {
            Console.WriteLine("********** Select Types **********");
            Console.Write("\t1. Even Parity Type \n\t2. Odd Parity Type\n\t3. Exit" +
                " \n----------------------------------\n");
            Console.Write("Answer: ");
            string tempType = Console.ReadLine();

            try
            {
                if (Convert.ToInt32(tempType) == 1 || Convert.ToInt32(tempType) == 2 || Convert.ToInt32(tempType) == 3)
                {
                    this.type = Convert.ToInt32(tempType);
                    if (this.type == 3)
                        return -1;
                }
                else
                {
                    Console.WriteLine("Your answer is: " + tempType);
                    Console.WriteLine("It is wrong number. Select 1 or 2 or 3!");
                    return 1;
                }
            }
            catch(Exception e)
            {
                Console.WriteLine("Your answer is: " + tempType);
                Console.WriteLine("It is wrong number. Select 1 or 2 or 3!");
                return 1;
            }

            return 0;
        }

        public void GetParityBitNumber()
        {
            Console.Write("\n\tEnter parity bit number.\n");
            Console.Write("Answer: ");
            string tempParityBit = Console.ReadLine();

            try
            {
                if (Convert.ToInt32(tempParityBit) > 4)
                {
                    Console.WriteLine("Your answer is: " + tempParityBit);
                    Console.WriteLine("Parity bit count can not over 4. Reenter parity bit number!");
                    GetParityBitNumber();
                }
                else
                    this.parityBit = Convert.ToInt32(tempParityBit);
            }
            catch (Exception e)
            {
                Console.WriteLine("Your answer is: " + tempParityBit);
                Console.WriteLine("It is wrong number. Reenter parity bit number!");
                GetParityBitNumber();
            }
        }

        public void GetValue()
        {
            Console.Write("\n\tEnter binary value. Maximum length is 16bit.\n");
            Console.Write("Answer: ");
            string tempValue = Console.ReadLine();

            try
            {
                if (tempValue.Length > 16 | tempValue.Length < this.parityBit)
                {
                    Console.WriteLine("Your answer is: " + tempValue);
                    Console.WriteLine("It must over parity bit count and less than 16bits. Please enter correctly!");
                    GetValue();
                }
                for (int i = 0; i < tempValue.Length; i++)
                {
                    int singleBit = Convert.ToInt32(tempValue.Substring(i,1));
                    if (singleBit > 1)
                    {
                        Console.WriteLine("Your answer is: " + tempValue);
                        Console.WriteLine("It isn't a binary value. Please enter correctly!");               
                        GetValue();
                        tempValue = Convert.ToString(this.value);
                    }
                }
                this.value = tempValue;
            }
            catch (Exception e)
            {
                Console.WriteLine("Your answer is: " + tempValue);
                Console.WriteLine("It isn't a binary value. Please enter correctly!");
                GetValue();
            }
        }

        public void GetResult()
        {
            string tempValue = Convert.ToString(this.value);
            int[] tempParity;
            if (this.parityBit <= 2)
            {
                tempParity = new int[2];
                tempParity[0] = 1;
                tempParity[1] = 2;
            }
            else
            {
                tempParity = new int[this.parityBit];
                tempParity[0] = 1;
                tempParity[1] = 2;
                for (int i = 2; i < this.parityBit; i++)
                {
                    int temp = 1;
                    for (int j = 0; j < i; j++)
                    {
                        temp *= 2;
                    }
                    tempParity[i] = temp;
                }
            }

            for (int i = 0; i < tempParity.Length; i++)
            {

                tempValue = tempValue.Remove(tempParity[i] - 1, 1);

                for (int j = 0; j < tempParity.Length; j++)
                {
                    tempParity[j] = tempParity[j] - 1;
                }
            }

            this.value = tempValue;
        }

        public void CompilingProcess()
        {
            int d1 = 0, d2 = 0, d3 = 0, d4 = 0;
            int[] p1, p2, p3, p4;
            if (this.parityBit == 1)
            {
            }
            else if (this.parityBit == 2)
            {
                p1 = new int[]{ 1,3 };
                p2 = new int[] { 2,3 };
                if (this.type == 1)
                {
                    for (int i = 0; i < p1.Length; i++)
                    {
                        d1 += Convert.ToInt32(this.value.Substring(p1[i] - 1, 1));
                    }
                    d1 = d1 % 2 == 0 ? 0 : 1;
                    for (int i = 0; i < p2.Length; i++)
                    {
                        d2 += Convert.ToInt32(this.value.Substring(p2[i] - 1, 1));
                    }
                    d2 = d2 % 2 == 0 ? 0 : 2;

                    if ((d1 + d2) > 0)
                    {
                        if (this.value[d1 + d2 -1] == '1')
                        {
                            this.value = this.value.Insert((d1 + d2 - 1), "0");
                            this.value = this.value.Remove((d1 + d2), 1);
                        }
                        else
                        {
                            this.value = this.value.Insert((d1 + d2 - 1), "1");
                            this.value = this.value.Remove((d1 + d2), 1);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < p1.Length; i++)
                    {
                        d1 += Convert.ToInt32(this.value.Substring(p1[i] - 1, 1));
                    }
                    d1 = d1 % 2 == 0 ? 1 : 0;
                    for (int i = 0; i < p2.Length; i++)
                    {
                        d2 += Convert.ToInt32(this.value.Substring(p2[i] - 1, 1));
                    }
                    d2 = d2 % 2 == 0 ? 2 : 0;

                    if ((d1 + d2) > 0)
                    {
                        if (this.value[d1 + d2 -1] == '1')
                        {
                            this.value = this.value.Insert((d1 + d2 - 1), "0");
                            this.value = this.value.Remove((d1 + d2), 1);
                        }
                        else
                        {
                            this.value = this.value.Insert((d1 + d2 - 1), "1");
                            this.value = this.value.Remove((d1 + d2), 1);
                        }
                    }
                }
            }
            else if (this.parityBit == 3)
            {
                p1 = new int[] { 1, 3, 5, 7 };
                p2 = new int[] { 2, 3, 6, 7 };
                p3 = new int[] { 4, 5, 6, 7 };
                if (this.type == 1)
                {
                    for (int i = 0; i < p1.Length; i++)
                    {
                        d1 += Convert.ToInt32(this.value.Substring(p1[i] - 1,1));
                    }
                    d1 = (d1 % 2 == 0) ? 0 : 1;
                    for (int i = 0; i < p2.Length; i++)
                    {
                        d2 += Convert.ToInt32(this.value.Substring(p2[i] - 1,1));
                    }
                    d2 = (d2 % 2 == 0) ? 0 : 2;
                    for (int i = 0; i < p3.Length; i++)
                    {
                        d3 += Convert.ToInt32(this.value.Substring(p3[i] - 1,1));
                    }
                    d3 = (d3 % 2 == 0) ? 0 : 4;

                    if ((d1 + d2 + d3) > 0)
                    {
                        if (this.value[d1 + d2 +d3 - 1] == '1')
                        {
                            this.value = this.value.Insert((d1 + d2 + d3 - 1),"0");
                            this.value = this.value.Remove((d1 + d2 + d3), 1);
                        }
                        else
                        {
                            this.value = this.value.Insert((d1 + d2 + d3 - 1), "1");
                            this.value = this.value.Remove((d1 + d2 + d3), 1);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < p1.Length; i++)
                    {
                        d1 += Convert.ToInt32(this.value.Substring(p1[i] - 1,1));
                    }
                    d1 = d1 % 2 == 0 ? 1 : 0;
                    for (int i = 0; i < p2.Length; i++)
                    {
                        d2 += Convert.ToInt32(this.value.Substring(p2[i] - 1,1));
                    }
                    d2 = d2 % 2 == 0 ? 2 : 0;
                    for (int i = 0; i < p3.Length; i++)
                    {
                        d3 += Convert.ToInt32(this.value.Substring(p3[i] - 1,1));
                    }
                    d3 = d3 % 2 == 0 ? 4 : 0;

                    if ((d1 + d2 + d3) > 0)
                    {
                        if (this.value[d1 + d2 + d3 -1] == '1')
                        {
                            this.value = this.value.Insert((d1 + d2 + d3 - 1), "0");
                            this.value = this.value.Remove((d1 + d2 + d3), 1);
                        }
                        else
                        {
                            this.value = this.value.Insert((d1 + d2 + d3 - 1), "1");
                            this.value = this.value.Remove((d1 + d2 + d3), 1);
                        }
                    }
                }
            }
            else
            {
                p1 = new int[] { 1, 3, 5, 7, 9 };
                p2 = new int[] { 2, 3, 6, 7, 10 };
                p3 = new int[] { 4, 5, 6, 7 };
                p4 = new int[] { 8, 9, 10 };
                if (this.type == 1)
                {
                    for (int i = 0; i < p1.Length; i++)
                    {
                        d1 += Convert.ToInt32(this.value.Substring(p1[i] - 1,1));
                    }
                    d1 = d1 % 2 == 0 ? 0 : 1;
                    for (int i = 0; i < p2.Length; i++)
                    {
                        d2 += Convert.ToInt32(this.value.Substring(p2[i] - 1, 1));
                    }
                    d2 = d2 % 2 == 0 ? 0 : 2;
                    for (int i = 0; i < p3.Length; i++)
                    {
                        d3 += Convert.ToInt32(this.value.Substring(p3[i] - 1, 1));
                    }
                    d3 = d3 % 2 == 0 ? 0 : 4;
                    for (int i = 0; i < p4.Length; i++)
                    {
                        d4 += Convert.ToInt32(this.value.Substring(p4[i] - 1, 1));
                    }
                    d4 = d4 % 2 == 0 ? 0 : 8;

                    if ((d1 + d2 + d3 + d4) > 0)
                    {
                        if (this.value[d1 + d2 + d3 + d4 -1] == '1')
                        {
                            this.value = this.value.Insert((d1 + d2 + d3 + d4 - 1), "0");
                            this.value = this.value.Remove((d1 + d2 + d3 + d4), 1);
                        }
                        else
                        {
                            this.value = this.value.Insert((d1 + d2 + d3 + d4 - 1), "1");
                            this.value = this.value.Remove((d1 + d2 + d3 + d4), 1);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < p1.Length; i++)
                    {
                        d1 += Convert.ToInt32(this.value.Substring(p1[i] - 1, 1));
                    }
                    d1 = d1 % 2 == 0 ? 1 : 0;
                    for (int i = 0; i < p2.Length; i++)
                    {
                        d2 += Convert.ToInt32(this.value.Substring(p2[i] - 1, 1));
                    }
                    d2 = d2 % 2 == 0 ? 2 : 0;
                    for (int i = 0; i < p3.Length; i++)
                    {
                        d3 += Convert.ToInt32(this.value.Substring(p3[i] - 1, 1));
                    }
                    d3 = d3 % 2 == 0 ? 4 : 0;
                    for (int i = 0; i < p4.Length; i++)
                    {
                        d4 += Convert.ToInt32(this.value.Substring(p4[i] - 1, 1));
                    }
                    d4 = d4 % 2 == 0 ? 8 : 0;

                    if ((d1 + d2 + d3 + d4) > 0)
                    {
                        if (this.value[d1 + d2 + d3 + d4 -1] == '1')
                        {
                            this.value = this.value.Insert((d1 + d2 + d3 + d4 - 1), "0");
                            this.value = this.value.Remove((d1 + d2 + d3 + d4), 1);
                        }
                        else
                        {
                            this.value = this.value.Insert((d1 + d2 + d3 + d4 - 1), "1");
                            this.value = this.value.Remove((d1 + d2 + d3 + d4), 1);
                        }
                    }
                }
            }


        }
    }
}

 

posted @ 2009-05-25 23:40  OOK  阅读(99)  评论(0编辑  收藏  举报