include stdafx include iostream include conio include string using nam

  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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
string sum(string k, string k1)
{
int p = 0;
string sumka = "0000000000000000";
for (int i = 15; i >= 0; i--)
{
if (p == 0)
{
if ((k[i] == '1') && (k1[i] == '1'))
{
sumka[i] = '0';
p = 1;
}
else if ((k[i] == '0') && (k1[i] == '0'))
sumka[i] = '0';
else
sumka[i] = '1';
}
else
{
if ((k[i] == '1') && (k1[i] == '1'))
{
sumka[i] = '1';
p = 1;
}
else if ((k[i] == '0') && (k1[i] == '0'))
{
sumka[i] = '1';
p = 0;
}
else
{
sumka[i] = '0';
p = 1;
}
}
}
return sumka;
}
string perevod(int a, string k)
{
int n, i = 4;
while (a != 0)
{
n = a % 10;
a = a / 10;
switch (n)
{
case 0:k = "0000" + k; break;
case 1:k = "0001" + k; break;
case 2:k = "0010" + k; break;
case 3:k = "0011" + k; break;
case 4:k = "0100" + k; break;
case 5:k = "0101" + k; break;
case 6:k = "0110" + k; break;
case 7:k = "0111" + k; break;
case 8:k = "1000" + k; break;
case 9:k = "1001" + k; break;
}
i = i - 1;
}
if (i >= 1)
for (n = 1; n <= i; n++)
k = "0000" + k;
return k;
}
void fix(string str)
{
int num = 0;
for (int i = 4; i > 0; i--)
{
if (i == 4)
{
if (str[15] = '1') num = 1 + num;
if (str[14] = '1') num = 2 + num;
if (str[13] = '1') num = 4 + num;
if (str[12] = '1') num = 8 + num;
if (num > 9) str = sum(str, "0000000000000110");
num = 0;
}
else if (i == 3)
{
if (str[11] = '1') num = 1 + num;
if (str[10] = '1') num = 2 + num;
if (str[9] = '1') num = 4 + num;
if (str[8] = '1') num = 8 + num;
if (num > 9) str = sum(str, "0000000001100000");
num = 0;
}
else if (i == 2)
{
if (str[7] = '1') num = 1 + num;
if (str[6] = '1') num = 2 + num;
if (str[5] = '1') num = 4 + num;
if (str[4] = '1') num = 8 + num;
if(num > 9) str = sum(str, "0000011000000000");
num = 0;
}
else if (i == 1)
{
if (str[4] = '1') num = 1 + num;
if (str[2] = '1') num = 2 + num;
if (str[1] = '1') num = 4 + num;
if (str[0] = '1') num = 8 + num;
if (num > 9) str = sum(str, "0110000000000000");
num = 0;
}
}
}
string PrintBCD(string s)
{
string out = "";
for (int i = 0; i < 16; i++)
{
if ((i != 0) && (i % 4 == 0))
{
out += ' ';
}
out += s[i];
}
return out;
}
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "rus");
int a, b;
string s1, s2, strsum;
cout << "Enter the first value: ";
cin >> a;
cout << endl << "Enter the second value: ";
cin >> b;
s1 = perevod(a, s1);
cout << "\nПервое число в двоично-десятичной системе: " << PrintBCD(s1);
s2 = perevod(b, s2);
cout << "\nВторое число в двоично-десятичной системе: " << PrintBCD(s2);
strsum = sum(s1, s2);
fix(strsum);
cout << "\nSum = " <<
PrintBCD(strsum);
_getch();
return 0;
}