Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Центр помощи > [C#]перегрузка операций для сравнения строк


Автор: sandell0 18.11.2010, 21:58
Собственно задание вкратце - есть класс строка и его наследник класс битовая строка. для битовой строки перегружены арифметические и логические операции.
Проблема в ошибке Property or indexer 'string.this[int]' cannot be assigned to -- it is read only    (строки 49,52,53,57,58,85,87,94,95,99,100,122)
Есть конструктивные предложения?


Код

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

namespace laba5
{
    class stroka
    {
        public string[] ololo = new string[20];
        public int dlina;
        public stroka(string[] lol)
        {
            ololo=lol;
        }
        public stroka(char a)
        {
            ololo[0]=Convert.ToString(a);
        }
        public stroka()
        {}
        public void dlina_method(int a)
        {
            this.dlina=a;
        }
        public void chistka()
        {
            this.ololo=null;
            this.dlina=0;
        }

    }
    class bit_stroka:stroka
    {
        
        public bool znak;
        public string ololo="                    "; 
        public bit_stroka(string lol)
        {
            ololo = lol;
        }
        ~bit_stroka()
        {
        }
        public static bit_stroka operator + (bit_stroka m1, bit_stroka m2)
        {
            bit_stroka str= new bit_stroka("                    ");
            for (int i = 19; i > 0; i--)
            {
                str.ololo[i] = Convert.ToChar(Convert.ToInt32(m1.ololo[i]) + Convert.ToInt32(m2.ololo[i]));
                if (str.ololo[i] =='2')
                {
                    str.ololo[i - 1] = Convert.ToChar(Convert.ToInt32(str.ololo[i]) +1);
                    str.ololo[i]='0';
                }
                if (str.ololo[i] == '3')
                {
                    str.ololo[i - 1]=Convert.ToChar(Convert.ToInt32(str.ololo[i]) +1);
                    str.ololo[i] = '1';
                }
            }
        }
        public static bool operator ==(bit_stroka m1, bit_stroka m2)
        {   
            bool x;
            if (m1.ololo == m2.ololo)
                x = true;
            else x = false;
            return x;
        }
        public static bool operator !=(bit_stroka m1, bit_stroka m2)
        {
            bool x;
            if (m1.ololo != m2.ololo)
                x = true;
            else x = false;
            return x;
        }
        public bit_stroka dop_kod(bit_stroka m1)
        {
            if(m1.znak==false)
            {
                for (int i = 19; i > 0; i--)
                {
                    if (m1.ololo[i] == '0')
                        m1.ololo[i] = '1';
                    else
                        m1.ololo[i] = '0';
                }
                m1.ololo[19] = Convert.ToChar(Convert.ToInt32(m1.ololo[19]) + 1);
                for (int i = 19; i > 0; i--)
                {
                    if (m1.ololo[i] == '2')
                    {
                        m1.ololo[i - 1] = Convert.ToChar(Convert.ToInt32(m1.ololo[i]) + 1);
                        m1.ololo[i] = '0';
                    }
                    if (m1.ololo[i] == '3')
                    {
                        m1.ololo[i - 1] = Convert.ToChar(Convert.ToInt32(m1.ololo[i]) + 1);
                        m1.ololo[i] = '1';
                    }
                }
                m1.znak = true;
            }
            return m1;
        }
        public bit_stroka prisvaivanie(string str)
        {
            bit_stroka m1=new bit_stroka(str);
            return m1;
        }


    }
    class Program
    {
        static void Main(string[] args)
        {
            bit_stroka m1, m2;
            m1.prisvaivanie("0000000110001");
            m2.prisvaivanie("0000000011001");
            m1.dop_kod;
            m1 = m1 + m2;
            if (m2 == m1)
                Console.WriteLine("равно");
            else
                Console.WriteLine("Не равно");
            Console.WriteLine("{0}\n{1}", m1, m2);
        }
    }
}



Автор: ИванМ 18.11.2010, 22:37
sandell0, оператор [] (this[]) для строк всегда readonly. Как вариант -  перевести string в char[], изменить нужный символ и конвертнуть обратно. Еще вариант - преобразовать string в char*, но тогда будет незащищенный код.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)