Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- #include<windows.h>
- using namespace std;
- char numerals[36] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D',
- 'E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
- 'S','T','U','V','W','X','Y','Z'};
- void gotoxy(int x, int y);
- int getNum(char c);
- char getChar(int n);
- void clear(char num1[50]);
- int getLength(char num[50]);
- bool isZero(char num[50]);
- void print(char num1[50]);
- bool isInSystem(char c,int syst);
- void cinlong(char mas[50]);
- void copyNum(char a[50],char b[50]);
- void mult(char num1[50],char num2[50],char res[50],int syst);
- void reverse(char arr[50], int start, int end) ;
- void getFirstNumeralsNumber(char num1[50],char num2[50],int k);
- int compareNumbers(char num1[50],char num2[50]);
- void sumNum(char num1[50],char num2[50],char res[50],int syst);
- void plusOne(char num1[50],int syst);
- void minusNum(char num1[50],char num2[50],char res[50],int syst);
- void catOne(char num[50],int n);
- void power(char num1[50],int k,char res[50],int syst);
- void toDecimal(char num1[50],char num2[50],int syst);
- bool isValidNum(char ch);
- void divNum(char num1[50],char num2[50],char resDiv[50],char ost[50],int syst);
- void decToN(char num1[50],char res[50],int n);
- int getCountNum(int a);
- int makeSpaces(int a);
- void printRes(char num1[50],char num2[50],char res[50],int syst,char op);
- bool isNumInSystem(char num[50],int syst);
- void myScanfSyst(int& syst);
- void myScanf(char num[50]);
- int main()
- {
- char num1[50],num2[50],res[50],
- buf[50],buf1[50],buf2[50],resBuf[50],count[50],
- prevResBuf[50],copyNum1[50],resDiv[50];
- int len1 = 0,
- len2 = 0,
- len3 = 0,
- len4 = 0;
- int syst = 0;
- while(true)
- {
- syst = 0;
- clear(res);
- cout << "num1: \n";
- fflush(stdin);
- myScanf(num1);
- cout << "num2: \n";
- myScanf(num2);
- cout << "syst: \n";
- myScanfSyst(syst);
- if(!isNumInSystem(num1,syst)) cout << "num1 isn't correct\n";
- if(!isNumInSystem(num2,syst)) cout << "num2 isn't correct\n";
- if(isNumInSystem(num1,syst) && isNumInSystem(num2,syst)) break;
- }
- while(1)
- {
- int item = 1;
- char ans = ' ';
- while((int)ans != 13)
- {
- system("cls");
- cout << "\t\t"<<"Menu"<<endl<<endl;
- cout << "num1:";
- print(num1);
- cout << endl;
- cout<<"num2:";
- print(num2);
- cout << endl;
- cout<<"syst:";
- cout << syst<<endl;
- cout<<endl;
- cout << "1 - plus";
- if(item == 1) cout<<"<-" <<endl; else cout << endl;
- cout << "2 - minus";
- if(item == 2) cout<<"<-" <<endl; else cout << endl;
- cout << "3 - multiplication";
- if(item == 3) cout<<"<-" <<endl; else cout << endl;
- cout << "4 - division";
- if(item == 4) cout<<"<-" <<endl; else cout << endl;
- cout << "5 - change first number";
- if(item == 5) cout<<"<-" <<endl; else cout << endl;
- cout << "6 - change number system";
- if(item == 6) cout<<"<-" <<endl; else cout << endl;
- cout << "7 - exit";
- if(item == 7) cout<<"<-" <<endl; else cout << endl;
- ans = getch();
- switch(ans)
- {
- case 72:
- {
- if(item == 1) item = 7; else item--;
- break;
- }
- case 80:
- {
- if(item == 7) item = 1; else item++;
- break;
- }
- }
- }
- int mem = 0;
- int maxLen = 0;
- if(item == 1)
- {
- sumNum(num1,num2,res,syst);
- printRes(num1,num2,res,syst,'+');
- system("pause>0");
- }
- if(item == 2)
- {
- if(compareNumbers(num1,num2) >= 0)
- {
- minusNum(num1,num2,res,syst);
- printRes(num1,num2,res,syst,'-');
- }
- else
- {
- cout << "first < second";
- }
- system("pause>0");
- }
- if(item == 3)
- {
- mult(num1,num2,res,syst);
- printRes(num1,num2,res,syst,'*');
- system("pause>0");
- }
- if(item == 4)
- {
- char ost[50];
- divNum(num1,num2,res,ost,syst);
- printRes(num1,num2,res,syst,'\\');
- system("pause>0");
- }
- if(item == 5)
- {
- system("cls");
- print(num1);
- char ch = ' ';
- int len1 = getLength(num1);
- int curX = len1-1;
- gotoxy(curX,0);
- while((int)(ch = getch()) != 13)
- {
- if(ch == 75)
- {
- curX--;
- gotoxy(curX,0);
- }
- else if(ch == 77)
- {
- if(curX+1<=len1-1)
- {
- curX++;
- }
- gotoxy(curX,0);
- }
- else if(ch == 72)
- {
- if(num1[len1-1 - curX] < numerals[syst-1])
- {
- num1[len1-1 - curX] = getChar(getNum(num1[len1-1 - curX]) + 1);
- cout << num1[len1-1 - curX];
- }
- //print(num1);
- gotoxy(curX,0);
- }
- else if(ch == 80)
- {
- if(num1[len1-1 - curX] > '0')
- num1[len1-1 - curX] = getChar(getNum(num1[len1-1 - curX]) - 1);
- cout << num1[len1-1 - curX];
- //print(num1);
- gotoxy(curX,0);
- }
- else if(isValidNum(ch))
- {
- if(ch != num1[len1 - 1 - curX])
- {
- num1[len1 - 1 - curX] = ch;
- cout << num1[len1 - 1 - curX];
- //print(num1);
- gotoxy(curX,0);
- }
- }
- else if ((ch >= 'A') && (ch <= numerals[syst-1]))
- {
- if(ch != num1[len1 - 1 - curX])
- {
- num1[len1 - 1 - curX] = ch;
- cout << num1[len1 - 1 - curX];
- gotoxy(curX,0);
- }
- //print(num1);
- }
- }
- gotoxy(0,1);
- }
- if(item == 6)
- {
- int t = 0;
- cout << "For num1: ";
- print(num1);
- cout << endl;
- cout << "For num2: ";
- print(num2);
- cout << endl;
- cout<<"Number System from "<<syst<<" to: ";
- cin>>t;
- toDecimal(num1,buf,syst);
- decToN(buf,res,t);
- cout << "num1: ";
- copyNum(num1,res);
- print(num1);
- cout << endl;
- toDecimal(num2,buf,syst);
- decToN(buf,res,t);
- copyNum(num2,res);
- cout << "num2: ";
- print(num2);
- cout << endl;
- syst = t;
- system("pause>0");
- }
- if(item == 7)
- {
- break;
- }
- }
- return 0;
- }
- int getNum(char c)
- {
- for(int i = 0;i<36;i++)
- {
- if(numerals[i] == c) return i;
- }
- }
- char getChar(int n)
- {
- return numerals[n];
- }
- void clear(char num1[50])
- {
- for(int i = 0;i<50;i++)
- {
- num1[i] = '0';
- }
- }
- int getLength(char num[50])
- {
- int i = 49;
- while(num[i--]=='0');
- i++;
- return i+1;
- }
- bool isZero(char num[50])
- {
- for(int i = 49;i>=0;i--)
- {
- if(num[i] != '0') return false;
- }
- return true;
- }
- void print(char num1[50])
- {
- if(isZero(num1)) cout << "0";
- else
- {
- int i = 49;
- while(num1[i--] == '0');
- i++;
- for(i;i>=0;i--)
- {
- cout << num1[i];
- }
- }
- }
- bool isInSystem(char c,int syst)
- {
- for(int i = 0;i<syst;i++)
- {
- if(c == numerals[i]) return true;
- }
- return false;
- }
- bool isNumInSystem(char num[50],int syst)
- {
- int len = getLength(num);
- //cout << "*";
- //for(int i = 0;i<50;i++) cout << num[i];
- //cout << "*";
- //cout << endl;
- //cout << "len = "<<len<<endl;
- int k = 0;
- for(int j = 0;j<len;j++)
- {
- for(int i = 0;i<syst;i++)
- {
- if(num[j] == numerals[i]) {k++;break;}
- }
- }
- //cout << "k = "<<k<<endl;
- if(k != len) return false;
- return true;
- }
- void myScanfSyst(int& syst)
- {
- char a[50] = {0};
- char num[50] = {0};
- int len = 0;
- while(true)
- {
- len = 0;
- while((a[len] = (getchar()))!= '\n')
- {
- len++;
- }
- for(int i=0;i<50;++i) num[i] = '0';
- for(int i=0;i<len;i++)
- {
- num[len - i - 1] = a[i];
- }
- //cout << "isNumInSystem(num,10) = "<<isNumInSystem(num,10)<<endl;
- //cout << "atoi(a) = "<<atoi(a)<<endl;
- if(!isNumInSystem(num,10) || ((atoi(a) < 2) || (atoi(a) > 36))) cout << "not correct\n";
- else break;
- }
- syst = atoi(a);
- }
- void cinlong(char mas[50])
- {
- string s;
- cin >> s;
- for(int i=0;i<50;++i)
- mas[i] = '0';
- int len = s.length();//method class string
- for(int i=0;i<len;i++)
- mas[len - i - 1] = (s[i]);
- }
- void copyNum(char a[50],char b[50])
- {
- for(int i = 0;i<50;i++) a[i] = b[i];
- }
- void reverse(char arr[50], int start, int end)
- {
- char temp;
- while (start < end)
- {
- temp = arr[start];
- arr[start] = arr[end];
- arr[end] = temp;
- start++;
- end--;
- }
- }
- void mult(char num1[50],char num2[50],char res[50],int syst)
- {
- int resBuf[50] = {0};
- for (int i = 0; i < 50; i++)
- {
- for (int j = 0; j < 50; j++)
- {
- if(i+j<50)
- resBuf[i + j] += (getNum(num1[i])) * (getNum(num2[j]));
- }
- }
- for (int i = 0; i < 50-1; i++)
- {
- resBuf[i + 1] += (resBuf[i] / syst);
- resBuf[i]= resBuf[i] % syst;
- }
- for(int i = 0;i<50;i++) res[i] = getChar(resBuf[i]);
- }
- void getFirstNumeralsNumber(char num1[50],char num2[50],int k)
- {
- int i = 49;
- while(num1[i--]=='0');
- i++;
- k--;
- clear(num2);
- for(int j = i;j>=i-k;j--)
- {
- num2[i-j] = num1[j];
- }
- reverse(num2,0,k);
- }
- int compareNumbers(char num1[50],char num2[50])
- {
- if(getLength(num1) > getLength(num2)) return 1;
- else if(getLength(num2) > getLength(num1)) return -1;
- else
- {
- for(int i = getLength(num1);i>=0;i--)
- {
- if(num1[i] > num2[i]) return 1;
- else if(num2[i] > num1[i]) return -1;
- }
- return 0;
- }
- }
- void sumNum(char num1[50],char num2[50],char res[50],int syst)
- {
- int mem = 0;
- int maxLen = max(getLength(num1),getLength(num2));
- for(int i = 0;i<=maxLen;i++)
- {
- res[i] = getChar((getNum(num1[i]) + getNum(num2[i]) + mem)%syst);
- //res[i] = (((num1[i]-'0') + (num2[i]-'0') + mem)%syst)+'0';
- mem = (getNum(num1[i]) + getNum(num2[i]) + mem)/syst;
- }
- }
- void plusOne(char num1[50],int syst)
- {
- char num2[50];
- clear(num2);
- num2[0] = '1';
- int mem = 0;
- int maxLen = max(getLength(num1),1);
- for(int i = 0;i<=maxLen;i++)
- {
- int save = (getNum(num1[i]) + getNum(num2[i]) + mem);
- num1[i] = getChar((getNum(num1[i]) + getNum(num2[i]) + mem)%syst);
- mem = save / syst;
- }
- }
- void minusNum(char num1[50],char num2[50],char res[50],int syst)
- {
- char buf[50];
- clear(buf);
- copyNum(buf, num1);
- int curSum = 0;
- int maxLen = max(getLength(num1),getLength(num2));
- for(int i = 0;i<=maxLen;i++)
- {
- if((getNum(num1[i])) < (getNum(num2[i])))
- {
- int j = i+1;
- while(num1[j] == '0') {num1[j] = numerals[syst-1];j++;}
- num1[j] = getChar((getNum(num1[j]))-1);
- curSum = (getNum(num1[i])) + syst;
- res[i] = getChar(curSum - (getNum(num2[i])));
- }
- else res[i] = getChar(getNum(num1[i]) - getNum(num2[i]));
- }
- copyNum(num1,buf);
- }
- void catOne(char num[50],int n)
- {
- int len = getLength(num);
- for(int i = len-1;i>=0;i--)
- num[i+1] = num[i];
- num[0] = getChar(n);
- }
- void power(char num1[50],int k,char res[50],int syst)
- {
- clear(res);
- res[0] = '1';
- for(int i = 0;i<k;i++)
- {
- mult(res,num1,res,syst);
- }
- }
- void toDecimal(char num1[50],char num2[50],int syst)
- {
- char res[50],resBuf[50],powerBuf[50],copyRes[50];
- clear(resBuf);
- clear(powerBuf);
- clear(res);
- char num[50];clear(num);
- char systNum[50];clear(systNum);
- systNum[0] = numerals[syst];
- for(int i = 0;i<getLength(num1);i++)
- {
- clear(resBuf);
- clear(powerBuf);
- num[0] = num1[i];
- power(systNum,i,powerBuf,10);
- mult(powerBuf,num,resBuf,10);
- copyNum(copyRes,res);
- sumNum(copyRes,resBuf,res,10);
- }
- copyNum(num2,res);
- }
- bool isValidNum(char ch)
- {
- if((ch == '0') || (ch == '1') || (ch == '2') || (ch == '3') || (ch == '4') || (ch == '5') || (ch == '6')
- || (ch == '7') || (ch == '8') || (ch == '9'))
- return true;
- return false;
- }
- void divNum(char num1[50],char num2[50],char resDiv[50],char ost[50],int syst)
- {
- clear(ost);
- clear(resDiv);
- char copyNum1[50],buf[50],resBuf[50],
- prevResBuf[50],res[50],
- count[50];
- clear(res);
- if(compareNumbers(num1,num2)>0)
- {
- clear(resDiv);
- copyNum(copyNum1,num1);
- int len1 = getLength(num1);
- int k = 0;
- int len2 = getLength(num2);
- clear(buf);
- //cout << "buf: ";
- //print(buf);
- int cnt = 0;
- while(compareNumbers(buf,num2) < 0)
- {
- getFirstNumeralsNumber(num1,buf,getLength(num2)+cnt);
- cnt++;
- }
- if (cnt>1) len1--;
- while(1)
- {
- clear(resBuf);
- clear(prevResBuf);
- if(k != 0)
- {
- //cout << "k+len2 = "<<k+len2<<endl;
- //cout << "len1+1 = "<<len1+1<<endl;
- if(k+len2 == len1+1) break;
- copyNum(buf,res);
- catOne(buf,getNum(copyNum1[len1 - (len2+k)]));
- }
- // cout << "buf:\n";
- // print(buf);
- clear(count);
- clear(prevResBuf);
- int cnt = 0;
- do
- {
- cnt++;
- copyNum(prevResBuf,resBuf);
- plusOne(count,syst);
- //cout << "count:\n";
- //print(count);
- mult(count,num2,resBuf,syst);
- //cout << "resBuf:\n";
- //print(resBuf);
- //cout << "buf:\n";
- //print(buf);
- //system("pause>0");
- }while(compareNumbers(resBuf,buf) <= 0);
- catOne(resDiv,cnt-1);
- clear(res);
- //cout << " ";
- //print(buf);
- //cout<<"-"<<endl;
- //cout << " ";
- //print(prevResBuf);
- //cout<<"-----"<<endl;
- //cout << " ";
- minusNum(buf,prevResBuf,res,syst);
- //cout << "res:\n";
- //print(res);
- //print(res);
- k++;
- //system("pause>0");
- }
- //print(resDiv);
- copyNum(ost,res);
- //cout << "ost: "<<ost[0]<<ost[1]<<endl;
- char c = getChar(getNum(ost[1])*10+getNum(ost[0]));
- clear(ost);
- ost[0] = c;
- //print(ost);
- }
- else if(compareNumbers(num1,num2)==0)
- {
- clear(resDiv);
- resDiv[0] = '1';
- //print(resDiv);
- clear(ost);
- }
- else {resDiv[0] = '0';copyNum(ost,num1);}
- if(isZero(resDiv)) ;//cout << "0";
- }
- void decToN(char num1[50],char res[50],int n)
- {
- char num2[50],buf[50],resDiv[50];
- clear(num2);
- num2[0] = numerals[n];
- clear(res);
- char ost[50];
- clear(ost);
- clear(buf);
- int k = 0;
- do
- {
- clear(ost);
- divNum(num1,num2,resDiv,ost,10);
- copyNum(num1,resDiv);
- int len = getLength(ost);
- if(len == 0) {buf[k] = '0';k++;}
- for(int i = len-1;i>=0;i--)
- {
- buf[k] = ost[i];
- k++;
- }
- }while(compareNumbers(resDiv,res) != 0);
- copyNum(res,buf);
- }
- int getCountNum(int a)
- {
- int cnt = 0;
- while(a > 0)
- {
- a /= 10;
- cnt++;
- }
- return cnt;
- }
- int makeSpaces(int a)
- {
- for (int i = 0;i<a;i++)
- {
- cout << " ";
- }
- }
- void printRes(char num1[50],char num2[50],char res[50],int syst,char op)
- {
- char buf[50],buf1[50],buf2[50];
- cout << "("<<syst<<") ";
- print(num1);
- //--------------------------------------
- cout <<"\t\t\t("<<10<<") ";
- //-----------print num1 in decimal---------
- toDecimal(num1,buf,syst);
- print(buf);
- cout << endl;
- //-------------
- makeSpaces(getCountNum(syst)+2);
- cout<<op<<"\t\t\t";
- makeSpaces(getCountNum(syst)+3+getCountNum(10)+2+getLength(num1));
- cout<<endl;
- makeSpaces(getCountNum(syst)+3+abs(getLength(num1)-getLength(num2)));
- print(num2);
- cout<<"\t\t\t";
- toDecimal(num2,buf2,syst);
- makeSpaces(getCountNum(10)+3+abs(getLength(buf)-getLength(buf2)));
- print(buf2);
- cout << endl;
- makeSpaces(getCountNum(syst)+3);
- for(int i = 0;i<getLength(num1);i++)
- cout<<"-";
- cout<<"\t\t\t";
- makeSpaces(getCountNum(10)+3);
- for(int i = 0;i<getLength(buf);i++)
- cout<<"-";
- cout<<endl;
- makeSpaces(getCountNum(10)+3+abs(getLength(num1)-getLength(num2)));
- print(res);
- if(getLength(res) < getLength(num2))
- {
- makeSpaces(getCountNum(10)+3);
- for(int i = 0;i<getLength(num1)-1;i++) cout << " ";
- }
- cout<<"\t\t\t";
- makeSpaces(getCountNum(10)+3+abs(getLength(buf)-getLength(buf2)));
- toDecimal(res,buf,syst);
- print(buf);
- }
- void gotoxy(int x, int y)
- {
- static HANDLE h = NULL;
- if(!h)
- h = GetStdHandle(STD_OUTPUT_HANDLE);
- COORD c = { x, y };
- SetConsoleCursorPosition(h,c);
- }
- void myScanf(char num[50])
- {
- char a1[50] = {0};
- char a2[50] = {0};
- int len = 0;
- while(true)
- {
- len = 0;
- while((a1[len] = (getchar()))!= '\n')
- {
- a2[len] = a1[len];
- len++;
- }
- int k = 0;
- for(int i = 0;i < len; i++)
- {
- for(int j = 0;j<36;j++)
- {
- if(a2[i] >='a' && a2[i] <= 'z') a2[i] = a2[i] - 'a' + 'A';
- if(a2[i] == numerals[j]) {k++;break;}
- }
- }
- if(k != len) cout << "not correct numerals\n";
- else break;
- }
- for(int i=0;i<50;++i) num[i] = '0';
- for(int i=0;i<len;i++)
- {
- num[len - i - 1] = a2[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement