In general, you need to write a program for an engineering calculator. Wrote while the functions of multiplication and transfer to another ss. I did everything but the division for some reason does not work, just when I select the desired item nothing happens. The most important thing is to make it all happen in binary ss. I did it simply through a cycle of subtracting while one number is larger than another. (Void Chas) Can you tell me a mistake?
And in general, did I do it correctly, or are binary numbers divided / subtracted differently?
#include <iostream> #include <string> #include <math.h> using namespace std; string ToBin1(string st1, int ss11); string ToBin2(string st1, int ss12); int ToDec(string r); void Summ(string bin1, string bin2); void Diff(string bin1, string bin2); void Mult(string bin1, string bin2); void Chas(string bin1, string bin2); int Comp(string bin1, string bin2); int main() { int ss11, ss12; string st1, st2; setlocale(LC_ALL,"Russian");//руссификатор int action=0;//Переменные целого типа cout << "Введите первое число и его систему счисления: " << endl; cin >> st1 >> ss11; cout << ToBin1(st1,ss11) << endl; cout << "Введите второе число: " << endl; cin >> st2 >> ss12; cout << ToBin2(st2,ss12) << endl; bool flag = true; while(flag) { cout<<"------------------------Menu--------------------------"<<endl; cout<<"Выберите действие: "<<endl; cout<<"1)Сложить числа "<<endl; cout<<"2)Вычесть числа "<<endl; cout<<"3)Умножить числа "<<endl; cout<<"4)Разделить числа "<<endl; cout<<"5)Сравнить числа "<<endl; cout<<"6)Выход из программы"<<endl; cout<<"------------------------------------------------------"<<endl; cout<<"Ваш выбор :";//Запрос выбора пункта меню cin>>action;//Ввод выбора system ("cls");//очистка консоли switch (action) { case 1: Summ(ToBin1(st1,ss11), ToBin2(st2,ss12)); break; case 2: Diff(ToBin1(st1,ss11), ToBin2(st2,ss12)); break; case 3: Mult(ToBin1(st1,ss11), ToBin2(st2,ss12)); break; case 4: Chas(ToBin1(st1,ss11), ToBin2(st2,ss12)); break; case 5: Comp(ToBin1(st1,ss11), ToBin2(st2,ss12)); break; case 6: { exit(0); break; flag = false; } } } return 0; } string ToBin1(string st1, int ss11) { int i, k, p, dec; string bin1; string digits[16] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; string digits1 = "0123456789abcdefghijklmnopqrstuvwxyz"; for (size_t i = 0; i < st1.length(); i++) { size_t pos = digits1.find(tolower(st1[i])); if (pos == string::npos || pos >= ss11) { cerr << "Wrong symbol in value " << st1 << endl; system("pause"); return 0; } } dec = 0; p = st1.length() - 1; for (i = 0; p >= 0; i++, p--) { k = toupper(st1[i]); dec += ( (k>='A') ? 10+k-'A' : k-'0') * pow(ss11,p); } do { bin1.insert(0, digits[dec % 2]); dec /= 2; } while (dec != 0); return bin1; } string ToBin2(string st2, int ss12) { int i, k, p, dec; string bin2; string digits[16] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; string digits1 = "0123456789abcdefghijklmnopqrstuvwxyz"; for (size_t i = 0; i < st2.length(); i++) { size_t pos = digits1.find(tolower(st2[i])); if (pos == string::npos || pos >= ss12) { cerr << "Wrong symbol in value " << st2 << endl; system("pause"); return 0; } } dec = 0; p = st2.length() - 1; for (i = 0; p >= 0; i++, p--) { k = toupper(st2[i]); dec += ( (k>='A') ? 10+k-'A' : k-'0') * pow(ss12,p); } do { bin2.insert(0, digits[dec % 2]); dec /= 2; } while (dec != 0); return bin2; } int ToDec(string r) { int dec, p, k; dec = 0; p = r.length() - 1; for (int i = 0; p >= 0; i++, p--) { k = toupper(r[i]); dec += ( (k>='A') ? 10+k-'A' : k-'0') * pow(2,p); } return dec; } void Mult(string bin1, string bin2) { int p, k; string r(bin1.length()+bin2.length()-1,'0'); for (int i = bin1.length()-1; i >= 0; i--) { for (int j = bin2.length()-1; j >= 0; j--) { r[i+j] += (bin1[i] != '0' && bin2[j] != '0'); } } for (int i = r.length()-1; i > 0; i--) { r[i-1] += (r[i]-'0')/2; r[i] = (r[i]-'0')%2 + '0'; } while(r[0] > '1') { r = "0" + r; r[0] += (r[1]-'0')/2; r[1] = (r[1]-'0')%2 + '0'; } cout << r << endl << "The multiplication of these numbers is: " << ToDec(r) << endl; system("pause"); } void Summ(string bin1, string bin2) { cout << strtol(bin1.c_str(), NULL, 2) + strtol(bin2.c_str(), NULL, 2) << endl; system("pause"); } void Diff(string bin1, string bin2) { cout << strtol(bin1.c_str(), NULL, 2) - strtol(bin2.c_str(), NULL, 2) << endl; system("pause"); } int Comp(string bin1, string bin2) { int la = bin1.length(); int lb = bin2.length(); int flag = -1; size_t aLoc=-1,bLoc=-1; if (la == lb) { do { aLoc = bin1.find("1", aLoc + 1); bLoc = bin2.find("1", bLoc + 1); if(aLoc < bLoc) { cout << ToDec(bin1) << " > " << ToDec(bin2); system("pause"); return 0; } else if(aLoc>bLoc) { cout << ToDec(bin1) << " < " << ToDec(bin2); system("pause"); return 0; } }while(aLoc == bLoc && aLoc != string::npos); cout << ToDec(bin1) << "=" << ToDec(bin2); system("pause"); return 0; } else { if (la > lb) cout << ToDec(bin1) << " > " << ToDec(bin2) << endl; if (la < lb) cout << ToDec(bin1) << " < " << ToDec(bin2) << endl; } system("pause"); } void Chas(string bin1, string bin2) { int quot = 0, a; do { a = strtol(bin1.c_str(), NULL, 2) - strtol(bin2.c_str(), NULL, 2); quot += 1; } while (ToDec(bin1) >= ToDec(bin2)); cout << "Частное: " << quot << endl << "Остаток: " << a << endl; system ("pause"); }
bin2frombin1and write the answer ina. And then comparesbin2andbin1, but I think you already understood: since the variablesbin2frombin1do not change, how many they did not compare the result will always be the same. Total: the eternal cycle. Try thiswhile (ToDec(a) >= ToDec(bin2));- Ilya