12.05.2017, 22:57. Показов 737. Ответов 0
Есть код, переводящий числа из римской системы счисления в арабскую
| C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
| #pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
textBox2->Text = "";
int num1 = 0, l = textBox1->Text->Length, operation;
bool isf = false;
if (l > 1) {
for (int i = l - 1; i > 0; i--) {
if (textBox1->Text[i] == 'I' && textBox1->Text[i - 1] == 'I') num1 += 1;
else if (textBox1->Text[i] == 'I' && textBox1->Text[i - 1] == 'V') num1 += 1;
else if (textBox1->Text[i] == 'I' && textBox1->Text[i - 1] == 'X') num1 += 1;
else if (textBox1->Text[i] == 'I' && textBox1->Text[i - 1] == 'L') num1 += 1;
else if (textBox1->Text[i] == 'I' && textBox1->Text[i - 1] == 'C') num1 += 1;
else if (textBox1->Text[i] == 'I' && textBox1->Text[i - 1] == 'D') num1 += 1;
else if (textBox1->Text[i] == 'I' && textBox1->Text[i - 1] == 'M') num1 += 1;
else if (textBox1->Text[i] == 'V' && textBox1->Text[i - 1] == 'I') { num1 += 4; if (i == 1) { isf = true; } else { if (textBox1->Text[i - 2] == 'I' || textBox1->Text[i - 2] == 'V') operation = 5; break; } i--; }
else if (textBox1->Text[i] == 'V' && textBox1->Text[i - 1] == 'V') { operation = 5; break; }
else if (textBox1->Text[i] == 'V' && textBox1->Text[i - 1] == 'X') num1 += 5;
else if (textBox1->Text[i] == 'V' && textBox1->Text[i - 1] == 'L') num1 += 5;
else if (textBox1->Text[i] == 'V' && textBox1->Text[i - 1] == 'C') num1 += 5;
else if (textBox1->Text[i] == 'V' && textBox1->Text[i - 1] == 'D') num1 += 5;
else if (textBox1->Text[i] == 'V' && textBox1->Text[i - 1] == 'M') num1 += 5;
else if (textBox1->Text[i] == 'X' && textBox1->Text[i - 1] == 'I') { num1 += 9; if (i == 1) { isf = true; } else { if (textBox1->Text[i - 2] == 'I' || textBox1->Text[i - 2] == 'V') operation = 5; break; } i--; }
else if (textBox1->Text[i] == 'X' && textBox1->Text[i - 1] == 'V') { operation = 5; break; }
else if (textBox1->Text[i] == 'X' && textBox1->Text[i - 1] == 'X') num1 += 10;
else if (textBox1->Text[i] == 'X' && textBox1->Text[i - 1] == 'L') num1 += 10;
else if (textBox1->Text[i] == 'X' && textBox1->Text[i - 1] == 'C') num1 += 10;
else if (textBox1->Text[i] == 'X' && textBox1->Text[i - 1] == 'D') num1 += 10;
else if (textBox1->Text[i] == 'X' && textBox1->Text[i - 1] == 'M') num1 += 10;
else if (textBox1->Text[i] == 'L' && textBox1->Text[i - 1] == 'I') { operation = 5; break; }
else if (textBox1->Text[i] == 'L' && textBox1->Text[i - 1] == 'V') { operation = 5; break; }
else if (textBox1->Text[i] == 'L' && textBox1->Text[i - 1] == 'X') { num1 += 40; if (i == 1) { isf = true; } else { if (textBox1->Text[i - 2] == 'I' || textBox1->Text[i - 2] == 'V' || textBox1->Text[i-2] == 'X' || textBox1->Text[i-2] == 'L') operation = 5; break; } i--; }
else if (textBox1->Text[i] == 'L' && textBox1->Text[i - 1] == 'L') { operation = 5; break; }
else if (textBox1->Text[i] == 'L' && textBox1->Text[i - 1] == 'C') num1 += 50;
else if (textBox1->Text[i] == 'L' && textBox1->Text[i - 1] == 'D') num1 += 50;
else if (textBox1->Text[i] == 'L' && textBox1->Text[i - 1] == 'M') num1 += 50;
else if (textBox1->Text[i] == 'C' && textBox1->Text[i - 1] == 'I') { operation = 5; break; }
else if (textBox1->Text[i] == 'C' && textBox1->Text[i - 1] == 'V') { operation = 5; break; }
else if (textBox1->Text[i] == 'C' && textBox1->Text[i - 1] == 'X') { num1 += 90; if (i == 1) { isf = true; } else { if (textBox1->Text[i - 2] == 'I' || textBox1->Text[i - 2] == 'V' || textBox1->Text[i - 2] == 'X' || textBox1->Text[i - 2] == 'L') operation = 5; break; } i--; }
else if (textBox1->Text[i] == 'C' && textBox1->Text[i - 1] == 'L') { operation = 5; break; }
else if (textBox1->Text[i] == 'C' && textBox1->Text[i - 1] == 'C') num1 += 100;
else if (textBox1->Text[i] == 'C' && textBox1->Text[i - 1] == 'D') num1 += 100;
else if (textBox1->Text[i] == 'C' && textBox1->Text[i - 1] == 'M') num1 += 100;
else if (textBox1->Text[i] == 'D' && textBox1->Text[i - 1] == 'I') { operation = 5; break; }
else if (textBox1->Text[i] == 'D' && textBox1->Text[i - 1] == 'V') { operation = 5; break; }
else if (textBox1->Text[i] == 'D' && textBox1->Text[i - 1] == 'X') { operation = 5; break; }
else if (textBox1->Text[i] == 'D' && textBox1->Text[i - 1] == 'L') { operation = 5; break; }
else if (textBox1->Text[i] == 'D' && textBox1->Text[i - 1] == 'C') { num1 += 400; if (i == 1) { isf = true; } else { if (textBox1->Text[i - 2] == 'I' || textBox1->Text[i - 2] == 'V' || textBox1->Text[i - 2] == 'X' || textBox1->Text[i - 2] == 'L' || textBox1->Text[i - 2] == 'C' || textBox1->Text[i - 2] == 'D') operation = 5; break; } i--; }
else if (textBox1->Text[i] == 'D' && textBox1->Text[i - 1] == 'D') { operation = 5; break; }
else if (textBox1->Text[i] == 'D' && textBox1->Text[i - 1] == 'M') num1 += 500;
else if (textBox1->Text[i] == 'M' && textBox1->Text[i - 1] == 'I') { operation = 5; break; }
else if (textBox1->Text[i] == 'M' && textBox1->Text[i - 1] == 'V') { operation = 5; break; }
else if (textBox1->Text[i] == 'M' && textBox1->Text[i - 1] == 'X') { operation = 5; break; }
else if (textBox1->Text[i] == 'M' && textBox1->Text[i - 1] == 'L') { operation = 5; break; }
else if (textBox1->Text[i] == 'M' && textBox1->Text[i - 1] == 'C') { num1 += 900; if (i == 1) { isf = true; }
else { if (textBox1->Text[i - 2] == 'I' || textBox1->Text[i - 2] == 'V' || textBox1->Text[i - 2] == 'X' || textBox1->Text[i - 2] == 'L' || textBox1->Text[i - 2] == 'C' || textBox1->Text[i - 2] == 'D') operation = 5; break; } i--; }
else if (textBox1->Text[i] == 'M' && textBox1->Text[i - 1] == 'M') num1 += 1000;
}
if (isf == false) {
if (textBox1->Text[0] == 'I') num1 += 1;
else if (textBox1->Text[0] == 'V') num1 += 5;
else if (textBox1->Text[0] == 'X') num1 += 10;
else if (textBox1->Text[0] == 'L') num1 += 50;
else if (textBox1->Text[0] == 'C') num1 += 100;
else if (textBox1->Text[0] == 'D') num1 += 500;
else if (textBox1->Text[0] == 'M') num1 += 1000;
}
}
if (l == 1) {
if (textBox1->Text == "I") num1 = 1;
else if (textBox1->Text == "V") num1 = 5;
else if (textBox1->Text == "X") num1 = 10;
else if (textBox1->Text == "L") num1 = 50;
else if (textBox1->Text == "C") num1 = 100;
else if (textBox1->Text == "D") num1 = 500;
else if (textBox1->Text == "M") num1 = 1000;
}
if (operation == 5) textBox2->Text = "Error";
if (textBox2->Text == "Error") textBox2->Text = "Error";
else textBox2->Text = Convert::ToString(num1);
num1 = 0;
}
};
} |
|
Так же в римской системе счисления есть ряд правил для записи числа, в коде учтены все (вроде), кроме одного - в записи не может идти 4 одинаковых символа подряд. Если добавляю в программу следующие строки
| C++ |
1
2
3
4
5
6
7
8
9
10
11
12
| if (l >= 4) {
for (int i = 0; i < l; i++) {
if (textBox1->Text[i] == textBox1->Text[i + 1]) {
if (textBox1->Text[i + 1] == textBox1->Text[i + 2]) {
if (textBox1->Text[i + 2] == textBox1->Text[i + 3]) {
operation = 5;
break;
}
}
}
}
} |
|
То при работе программы и наличии 4 подряд идущих символов в textBox2 выводится сообщение об ошибке, однако если ошибки нет, то программа перестаёт переводить любые римские числа, длина которых превышает либо равна 4. При этом завершается отладка с сообщением:
Необработанное исключение типа "System.IndexOutOfRangeException" в Проект3.exe
Дополнительные сведения: Индекс находился вне границ массива.
В чём заключается ошибка и как добавить проверку на 4 подряд идущих символа?