Бесконечный цикл - C# (178145)

Узнай цену своей работы

Формулировка задачи:

Сделал класс с методами для конвертации из двенадцатеричной системы счисления в двоичную. Бесконечный цикл возникает после создания экземпляра класса. Программа заходит в класс, экземпляр которого мы создаём, "выполняет" конструктор и возвращается на первую строчку метода Main();
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace TPO_lab5
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string ab = "123ab";
  13. TwelveToTwo ttt = new TwelveToTwo(ab);
  14. ttt.TwelveToTen();
  15. Console.WriteLine(ttt.number10);
  16. ttt.TenToTwo();
  17. foreach (int t in ttt.binar)
  18. Console.WriteLine(t);
  19. Console.ReadLine();
  20. }
  21. }
  22. }
Сам класс в котором реализована конвертация:
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace TPO_lab5
  7. {
  8. public class TwelveToTwo
  9. {
  10. public int number12;
  11. public int number10;
  12. public int number2;
  13. public Queue<int> twelves = new Queue<int>();
  14. public Stack<int> binar = new Stack<int>();
  15. public string sn
  16. {
  17. get
  18. {
  19. return sn;
  20. }
  21. set
  22. {
  23. byte tmp2 = 0;
  24. if (value == null) sn = sn;
  25. for (int i = 0; i < value.Length; i++)
  26. {
  27. if (value[i].Equals('0') || value[i].Equals('1') || value[i].Equals('2') || value[i].Equals('3') || value[i].Equals('4') || value[i].Equals('5') || value[i].Equals('6') || value[i].Equals('7') || value[i].Equals('8') || value[i].Equals('9') || value[i].Equals('A') || value[i].Equals('B') || value[i].Equals('a') || value[i].Equals('b'))
  28. tmp2 = 0;
  29. else tmp2 = 1;
  30. }
  31. if (tmp2 != 1) sn = value;
  32. if (tmp2 == 1) throw new ArgumentException("Недопустимые символы!");
  33. }
  34. }
  35. public TwelveToTwo(string num)
  36. {
  37. sn = num;
  38. }
  39. public Stack<int> TwelveToTwos()
  40. {
  41. TwelveToTen();
  42. TenToTwo();
  43. return binar;
  44. }
  45. //Перевод из двенадцатеричной СС в десятичную
  46. public void TwelveToTen()
  47. {
  48. for (int i = 0; i < sn.Length; i++)
  49. {
  50. if (sn[i].Equals('0') || sn[i].Equals('1') || sn[i].Equals('2') || sn[i].Equals('3') || sn[i].Equals('4') || sn[i].Equals('5') || sn[i].Equals('6') || sn[i].Equals('7') || sn[i].Equals('8') || sn[i].Equals('9')) twelves.Enqueue((int)Char.GetNumericValue(sn[i]));
  51. if (sn[i].Equals('A')) twelves.Enqueue(10);
  52. if (sn[i].Equals('B')) twelves.Enqueue(11);
  53. if (sn[i].Equals('a')) twelves.Enqueue(10);
  54. if (sn[i].Equals('b')) twelves.Enqueue(11);
  55. }
  56. int[] twelvesmas = new int[twelves.Count];
  57. twelves.CopyTo(twelvesmas, 0);
  58. int temp = twelvesmas.Length - 1;
  59. for (int i = 0; i < twelvesmas.Length; i++)
  60. {
  61. number10 += twelvesmas[i] * (int)Math.Pow(12, temp);
  62. temp--;
  63. }
  64. }
  65. //Перевод из десятичной СС в двоичную
  66. public void TenToTwo()
  67. {
  68. int temp;
  69. while (number10 != 1)
  70. {
  71. temp = number10 % 2;
  72. number10 = number10 / 2;
  73. binar.Push(temp);
  74. }
  75. binar.Push(1);
  76. string st = string.Join("", binar);
  77. }
  78. }
  79. }

Решение задачи: «Бесконечный цикл»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TPO_lab5
  6. {
  7.     public class TwelveToTwo
  8.     {
  9.         public Int32 Number12;
  10.         public Int32 Number10;
  11.         public Int32 Number2;
  12.         public Queue<Int32> Twelves = new Queue<Int32>();
  13.         public Stack<Int32> Binar = new Stack<Int32>();
  14.  
  15.         private String _sn;
  16.  
  17.         public String Sn
  18.         {
  19.             get
  20.             {
  21.                 return _sn;
  22.             }
  23.             set
  24.             {
  25.                 if (value == null)
  26.                     return;
  27.                 foreach (var ch in value)
  28.                 {
  29.                     if (!Char.IsDigit(ch) && !new[] { 'A', 'B', 'a', 'b' }.Any(_ => ch.Equals(_)))
  30.                     {
  31.                         throw new ArgumentException("Недопустимые символы!");
  32.                     }
  33.                 }
  34.                 _sn = value;
  35.             }
  36.         }
  37.         public TwelveToTwo(String num)
  38.         {
  39.             Sn = num;
  40.         }
  41.         public Stack<Int32> TwelveToTwos()
  42.         {
  43.             TwelveToTen();
  44.             TenToTwo();
  45.             return Binar;
  46.         }
  47.         //Перевод из двенадцатеричной СС в десятичную
  48.         public void TwelveToTen()
  49.         {
  50.             for (var i = 0; i < Sn.Length; i++)
  51.             {
  52.                 if (Char.IsDigit(Sn[i])) Twelves.Enqueue((Int32)Char.GetNumericValue(Sn[i]));
  53.                 if (Sn[i].Equals('A')) Twelves.Enqueue(10);
  54.                 if (Sn[i].Equals('B')) Twelves.Enqueue(11);
  55.                 if (Sn[i].Equals('a')) Twelves.Enqueue(10);
  56.                 if (Sn[i].Equals('b')) Twelves.Enqueue(11);
  57.             }
  58.             var twelvesmas = new Int32[Twelves.Count];
  59.             Twelves.CopyTo(twelvesmas, 0);
  60.             var temp = twelvesmas.Length - 1;
  61.             for (var i = 0; i < twelvesmas.Length; i++)
  62.             {
  63.                 Number10 += twelvesmas[i] * (Int32)Math.Pow(12, temp);
  64.                 temp--;
  65.             }
  66.         }
  67.         //Перевод из десятичной СС в двоичную
  68.         public void TenToTwo()
  69.         {
  70.             Int32 temp;
  71.             while (Number10 != 1)
  72.             {
  73.                 temp = Number10 % 2;
  74.                 Number10 = Number10 / 2;
  75.                 Binar.Push(temp);
  76.             }
  77.             Binar.Push(1);
  78.             var st = String.Join("", Binar);
  79.         }
  80.     }
  81. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут