Добавил:
Tushkan
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Курсовой проект / calc
.cpp// ---------------------------------------------------------------------------
#pragma hdrstop
#include "struct.cpp"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
class Calc {
public:
const UnicodeString calc(list<ItemD>input);
};
int GetNum(char s) {
switch(s) {
case '0':return 0;
case '1':return 1;
case '2':return 2;
case '3':return 3;
case '4':return 4;
case '5':return 5;
case '6':return 6;
case '7':return 7;
case '8':return 8;
case '9':return 9;
case 'a':case 'A':return 10;
case 'b':case 'B':return 11;
case 'c':case 'C':return 12;
case 'd':case 'D':return 13;
case 'e':case 'E':return 14;
case 'f':case 'F':return 15;
}
return 0;
}
char* GetCh(int s) {
switch(s) {
case 0: return "0";
case 1: return "1";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10:return "A";
case 11:return "B";
case 12:return "C";
case 13:return "D";
case 14:return "E";
case 15:return "F";
}
return "Z";
}
int Get10(UnicodeString s) {
s.Delete(1, 2);
int res = 0;
for (int i = 8, k = 1; i > 0; i--, k *= 16)
res = res + GetNum(s[i]) * k;
return res;
}
UnicodeString Get16(int s) {
UnicodeString res = "";
for (; s > 0; s = s / 16) res = GetCh(s % 16) + res;
for (; res.Length() < 8; res = "0" + res);
res.Delete(1, res.Length() - 8);
res = "0x"+res;
return res;
}
UnicodeString sum16(UnicodeString op1, UnicodeString op2) {
return Get16(Get10(op1)+Get10(op2));
}
UnicodeString razn16(UnicodeString op1, UnicodeString op2) {
return Get16(Get10(op1)-Get10(op2));
}
UnicodeString proiz16(UnicodeString op1, UnicodeString op2) {
return Get16(Get10(op1)*Get10(op2));
}
UnicodeString chast16(UnicodeString op1, UnicodeString op2) {
if (Get10(op2) != 0) return Get16(Get10(op1)/Get10(op2));
else return Get16(0);
}
UnicodeString sign16(UnicodeString op1) {
if (Get10(op1) == 0) return op1;
else return "0x00000001";
}
const UnicodeString Calc::calc(list<ItemD>input) {
list<ItemD>::iterator op1;
list<ItemD>::iterator op2;
while (true) {
list<ItemD>::iterator it;
for (it = input.begin(); (it != input.end()) && ((*it).type != 6); it++ );
op2 = it;
for (it = op2; it != input.end(); it++ ) {
if (((*it).type == 3) || ((*it).type == 4) || ((*it).type == 5)) break;
lf((*it).type == 6) { op1 = op2; op2 = it; }
}
if (it == input.end()) break;
el {
if ((*it).value == "+") {
(*op1).value = sum16((*op1).value, (*op2).value);
(*op1).type = 6;
(*op2).type = -1;
}
lf((*it).value == "-") {
(*op1).value = razn16((*op1).value, (*op2).value);
(*op1).type = 6;
(*op2).type = -1;
}
lf ((*it).value == "*") {
(*op1).value = proiz16((*op1).value, (*op2).value);
(*op1).type = 6;
(*op2).type = -1;
}
lf((*it).value == "/") {
(*op1).value = chast16((*op1).value, (*op2).value);
(*op1).type = 6;
(*op2).type = -1;
}
lf ((*it).value == "sign") {
(*op2).value = sign16((*op2).value);
(*op2).type = 6;
}
(*it).type = -1;
}
}
return (*op1).value;
}