Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Переводчик из десятичного числа в код Грея
- private char[] Dec2Gray(int a)
- {
- char[] digits = new char[2];
- digits[0] = '0'; digits[1] = '1';
- string res = "";
- int d;
- while (a != 0)
- {
- d = a % 2;
- res = digits[d] + res;
- a = a / 2;
- }
- char[] dec = new char[res.Length];
- for (int i = 0; i < res.Length; i++)
- {
- dec[i] = res[i];
- }
- char[] gray = new char[res.Length];
- gray[0] = dec[0];
- for (int i = 1; i < res.Length; i++)
- {
- if (dec[i - 1] == '1')
- {
- if (dec[i] == '1') gray[i] = '0';
- else gray[i] = '1';
- }
- else gray[i] = dec[i];
- }
- return gray;
- }
- // Переводчик из кода Грея в десятичное число
- private int Gray2Dec(string gray)
- {
- char[] bin = new char[gray.Length];
- bin[0] = gray[0];
- int flag = 0;
- if (gray[0] == '1') flag = 1;
- for (int i = 1; i < gray.Length; i++)
- {
- if (flag % 2 == 1)
- {
- if (gray[i] == '1') { bin[i] = '0'; flag++; }
- else bin[i] = '1';
- }
- else { bin[i] = gray[i]; if (bin[i] == '1') flag++; }
- }
- char[] digits = new char[2];
- digits[0] = '0'; digits[1] = '1';
- int dec, ves, j;
- dec = 0; ves = 1;
- for (int i = (bin.Length) - 1; i >= 0; i--)
- {
- j = 0;
- while (digits[j] != bin[i]) j++;
- dec = dec + ves * j;
- ves = ves * 2;
- }
- return dec;
- }
Advertisement
Add Comment
Please, Sign In to add comment