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 buff = "0000000000000000";
for (int i = 15; i >= 0; i--)
{
if (p == 0)
{
if ((k[i] == '1') && (k1[i] == '1'))
{
buff[i] = '0';
p = 1;
else
buff[i] = '1';
}
}
else if ((k[i] == '0') && (k1[i] == '0'))
buff[i] = '0';
else
{
if ((k[i] == '1') && (k1[i] == '1'))
{
buff[i] = '1';
p = 1;
}
else if ((k[i] == '0') && (k1[i] == '0'))
{
buff[i] = '1';
p = 0;
}
else
{
buff[i] = '0';
p = 1;
}
}
}
return buff;
}
string Transf(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;
}
string Fix(string str)
{
int Num = 0;
for (int i = 3; i >= 0; i--)
{
if (i == 3)
{
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 == 2)
{
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 == 1)
{
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 == 0)
{
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;
}
}
return str;
}
string OutBCD(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 FNum, SNum, StrSum, FixedStr;
cout << "Enter the 1st num: ";
cin >> a;
cout << endl << "Enter the 2nd num: ";
cin >> b;
FNum = Transf(a, FNum);
cout << "\nThe 1st BCD num: " << OutBCD(FNum);
SNum = Transf(b, SNum);
cout << "\nThe 2nd BCD num: " << OutBCD(SNum);
StrSum = Sum(FNum, SNum);
FixedStr = Fix(StrSum);
cout << "\n\nThe sum of BCD numbers = " << OutBCD(FixedStr);
_getch();
return 0;
}