Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > .NET Frameworks (точка нет фреймворки) > C# (си шарп)
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 23.03.2013, 01:38   #1
803
Пользователь
 
Регистрация: 13.02.2012
Сообщений: 89
По умолчанию Class в Windows Form

Здравствуйте. Есть программа она была изначально написана для консольного приложения. Я пытаюсь переделать по Wibdows Form.
В консольном варианте нужная мне функция вызывалась в Main, а в Windows Form я хочу что бы по нажатию кнопки. Как это реализовать. Заранее спасибо.
Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace LZWForm
{
    public partial class Form1 : Form
    {
        class LzwStringTable
        {
            public LzwStringTable(int numBytesPerCode)
            {
                maxCode = (1 << (8 * numBytesPerCode)) - 1;
            }

            public void AddCode(string s)
            {
                if (nextAvailableCode <= maxCode)
                {
                    if (s.Length != 1 && !table.ContainsKey(s))
                        table[s] = nextAvailableCode++;
                }
                else
                {
                    throw new Exception("LZW string table overflow");
                }
            }

            public int GetCode(string s)
            {
                if (s.Length == 1)
                    return (int)s[0];
                else
                    return table[s];
            }

            public bool Contains(string s)
            {
                return s.Length == 1 || table.ContainsKey(s);
            }

            private Dictionary<string, int> table = new Dictionary<string, int>();
            private int nextAvailableCode = 256;
            private int maxCode;
        }

        class lZW
        {
            private const int NumBytesPerCode = 2;

            static int ReadCode(BinaryReader reader)
            {
                int code = 0;
                int shift = 0;

                for (int i = 0; i < NumBytesPerCode; i++)
                {
                    byte nextByte = reader.ReadByte();
                    code += nextByte << shift;
                    shift += 8;
                }

                return code;
            }

            static void WriteCode(BinaryWriter writer, int code)
            {
                int shift = 0;
                int mask = 0xFF;

                for (int i = 0; i < NumBytesPerCode; i++)
                {
                    byte nextByte = (byte)((code >> shift) & mask);
                    writer.Write(nextByte);
                    shift += 8;
                }
            }

            static void Compress(StreamReader input, BinaryWriter output)
            {
                LzwStringTable table = new LzwStringTable(NumBytesPerCode);

                char firstChar = (char)input.Read();
                string match = firstChar.ToString();

                while (input.Peek() != -1)
                {
                    char nextChar = (char)input.Read();
                    string nextMatch = match + nextChar;

                    if (table.Contains(nextMatch))
                    {
                        match = nextMatch;
                    }
                    else
                    {
                        WriteCode(output, table.GetCode(match));
                        table.AddCode(nextMatch);
                        match = nextChar.ToString();
                    }
                }

                WriteCode(output, table.GetCode(match));
            }

            static void TestCompress()
            {
                FileStream inputStream = new FileStream("Input.txt", FileMode.Open);
                StreamReader inputReader = new StreamReader(inputStream);

                FileStream outputStream = new FileStream("Compressed.txt", FileMode.Create);
                BinaryWriter outputWriter = new BinaryWriter(outputStream);

                Compress(inputReader, outputWriter);

                outputWriter.Close();
                outputStream.Close();

                inputReader.Close();
                inputStream.Close();
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
803 вне форума Ответить с цитированием
Старый 23.03.2013, 01:48   #2
803
Пользователь
 
Регистрация: 13.02.2012
Сообщений: 89
По умолчанию

На выполнение TestCompress();
803 вне форума Ответить с цитированием
Старый 23.03.2013, 13:11   #3
phomm
personality
Старожил
 
Аватар для phomm
 
Регистрация: 28.04.2009
Сообщений: 2,882
По умолчанию

Код:
public static void TestCompress()

private void button1_Click(object sender, EventArgs e)
        {
            lZW.TestCompress();
        }
phomm вне форума Ответить с цитированием
Старый 24.03.2013, 13:13   #4
803
Пользователь
 
Регистрация: 13.02.2012
Сообщений: 89
По умолчанию

Спасибо большое я разобрался.
803 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Windows Form volcka Помощь студентам 0 17.05.2011 18:52
Windows form Krasi Windows Forms 4 16.04.2011 01:54
Можно ли подключить к web form windows form? Manonia Windows Forms 0 20.10.2010 05:28
Windows Form S1avik Общие вопросы C/C++ 2 25.09.2010 20:19
Windows form в C++ xnise Помощь студентам 1 15.09.2010 16:31