Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <sstream>
- #include <conio.h>
- #include <iostream>
- using namespace std;
- HANDLE g_hConsol = GetStdHandle(STD_OUTPUT_HANDLE);
- void gotoxy(const int x, const int y){
- COORD coord = {x, y};
- SetConsoleCursorPosition(g_hConsol, coord);
- }
- struct operacje{
- float dane; // 32000 (+); 32001 (-); 32020 (*); 32021 (/)
- string daneS;
- byte uje;
- };
- operacje kalkulator[100];
- int nD,wynikD;
- string dzialanieS;
- // KONWERSJE {
- string dajString(float liczba){
- stringstream FloatToStr;
- string str;
- FloatToStr << liczba;
- FloatToStr >> str;
- FloatToStr.clear();
- return str;
- }
- float dajZmienno(string co){
- float y=atof(co.c_str());
- return y;
- }
- // KONWERSJE }
- // LICZENIE WYNIKU {
- int ileTegoJest(byte czego){
- int ile=0;
- for(int i=0;i<=nD;i++){
- switch(czego){
- case 1:{
- if(kalkulator[i].dane==32021) ile++;
- break;
- }
- case 2:{
- if(kalkulator[i].dane==32020) ile++;
- break;
- }
- case 3:{
- if(kalkulator[i].dane==32001) ile++;
- break;
- }
- case 4:{
- if(kalkulator[i].dane==32000) ile++;
- break;
- }
- }
- }
- return ile;
- }
- void przesun(int skad){ // po obliczeniu uswanie znaku i jednej liczby
- for(int s=skad;s<=nD-2;++s){
- kalkulator[s].dane=kalkulator[s+2].dane;
- }
- }
- void konw(){
- int k,a1,a2,a3,a4,ileR;
- for(int i=0;i<=nD;i++){
- if((kalkulator[i].daneS!="+")&&(kalkulator[i].daneS!="-")&&(kalkulator[i].daneS!="*")&&(kalkulator[i].daneS!="/")) kalkulator[i].dane=dajZmienno(kalkulator[i].daneS);
- else{
- if(kalkulator[i].daneS=="+") kalkulator[i].dane=32000;
- if(kalkulator[i].daneS=="-") kalkulator[i].dane=32001;
- if(kalkulator[i].daneS=="*") kalkulator[i].dane=32020;
- if(kalkulator[i].daneS=="/") kalkulator[i].dane=32021;
- }
- }
- float arytm;
- for(k=nD;k!=0;){
- ileR=ileTegoJest(1);int i=0;
- if(ileR>0){
- for(a1=0;a1<ileR;){
- if(kalkulator[i].dane==32021){
- arytm=kalkulator[i-1].dane/kalkulator[i+1].dane;
- kalkulator[i-1].dane=arytm;
- przesun(i);
- ++a1;k-=2;nD-=2;
- i=0;
- }
- ++i;
- }
- }
- ileR=ileTegoJest(2);i=0;
- if(ileR>0){
- for(a2=0;a2<ileR;){
- if(kalkulator[i].dane==32020){
- arytm=kalkulator[i-1].dane*kalkulator[i+1].dane;
- kalkulator[i-1].dane=arytm;
- przesun(i);
- ++a2;k-=2;nD-=2;
- i=0;
- }
- ++i;
- }
- }
- ileR=ileTegoJest(3);i=0;
- if(ileR>0){
- for(a3=0;a3<ileR;){
- if(kalkulator[i].dane==32001){
- arytm=kalkulator[i-1].dane-kalkulator[i+1].dane;
- kalkulator[i-1].dane=arytm;
- przesun(i);
- ++a3;k-=2;nD-=2;
- i=0;
- }
- ++i;
- }
- }
- ileR=ileTegoJest(4);i=0;
- if(ileR>0){
- for(a4=0;a4<ileR;){
- if(kalkulator[i].dane==32000){
- arytm=kalkulator[i-1].dane+kalkulator[i+1].dane;
- kalkulator[i-1].dane=arytm;
- przesun(i);
- ++a4;k-=2;nD-=2;
- i=0;
- }
- ++i;
- }
- }
- }
- cout<<kalkulator[0].dane;
- }
- // LICZENIE WYNIKU}
- // DODATKOWE OPERACJE {
- string silnia(float co){
- long long siln=1;
- if (co>0){
- for(int i=1;i<=co;i++){
- siln*=i;
- }
- return dajString(siln);
- }else return "0";
- }
- string pot(float coP, float coW){
- float pote=1;
- int ujemna=0;
- if(coW!=0){
- if(coW<0){ujemna=1;coW*=-1;}
- for(int i=0;i<coW;i++){
- if(ujemna==0) pote*=coP; else pote*=coP;
- }
- }
- if(ujemna==0){return dajString(pote);} else{return dajString(1/pote);}
- }
- string nwd(float nwd1,float nwd2){
- int nwdi1=nwd1,nwdi2=nwd2;
- while(nwdi1!=nwdi2){
- if(nwdi1<nwdi2) nwdi2-=nwdi1; else nwdi1-=nwdi2;
- }
- return dajString(nwdi1);
- }
- string nww(float nww1,float nww2){
- int nwwi1=nww1,nwwi2=nww2;
- int k1=nwwi1,k2=nwwi2,w;
- while(k1!=k2){
- if(k1<k2) k2-=k1; else k1-=k2;
- }
- w=(nwwi1*nwwi2)/k1;
- return dajString(w);
- }
- // DODATKOWE OPERACJE }
- void wyswietlacz(){
- gotoxy(0,13);
- cout<<"Wyswietlacz: ";
- for(int i=0;i<=nD;i++){
- if(kalkulator[i].daneS!=""){
- if(kalkulator[i].uje==1) dzialanieS="("+kalkulator[i].daneS+")"; else dzialanieS=kalkulator[i].daneS;
- cout<<dzialanieS;
- }
- }
- gotoxy(0,0);}
- void menu(byte ktore){
- switch(ktore){
- case 1:{
- cout<<"+ - dodawanie\n";
- cout<<"- - odejmowanie\n";
- cout<<"* - mnozenie\n";
- cout<<"/ - dzielenie\n";
- cout<<"! - silnia\n";
- cout<<"^ - potega\n";
- cout<<"n - najmniejsza wspolna wielokrotnosc\n";
- cout<<"d - najwiekszy wspolny dzielnik\n";
- cout<<"b - wartosc bezwzgledna\n";
- cout<<"u - liczba ujemna\n";
- cout<<"= - wynik\n";
- cout<<"`(pod tylda) - Wylacz\n";
- break;}
- case 2:{
- break;}
- }
- }
- void restartK(){
- for(int i=0;i<100;i++){
- kalkulator[i].dane=0;
- kalkulator[i].daneS="";
- kalkulator[i].uje=0;
- }
- }
- int main(){
- char wybor;
- int wyborL,dozZn,j,ost;
- string pom,sil,podP,wykP,nww1,nww2,nwd1,nwd2,bez,dozwoloneZnaki="+-*/u^!ndb=`.,1234567890";
- float bez1;
- while((wyborL!=2)&&(wybor!='`')){
- restartK();dzialanieS="";nD=0;
- do{
- system("cls");
- wyswietlacz();
- menu(1);
- do{pocz:dozZn=0;wybor=getch();
- if(wybor==',') wybor='.';
- for(j=0;j<dozwoloneZnaki.length();j++){
- if(wybor==dozwoloneZnaki[j]||(wybor==8)){
- dozZn=1;break;
- }
- }
- if((ost<4)&&(j<4)) goto pocz;
- }while(dozZn==0);
- ost=j;
- kalkulator[nD+1].uje=0;
- switch(wybor){
- case '+':{
- nD+=2;kalkulator[nD-1].daneS='+';break;}
- case '-':{
- nD+=2;kalkulator[nD-1].daneS='-';break;}
- case '*':{
- nD+=2;kalkulator[nD-1].daneS='*';break;}
- case '/':{
- nD+=2;kalkulator[nD-1].daneS='/';break;}
- case 'u':{pom="-"+kalkulator[nD].daneS;swap(pom,kalkulator[nD].daneS);kalkulator[nD].uje=1;break;}
- case '!':{
- do{
- cin.sync();cin.clear();
- gotoxy(0,12);cout<<"! z: ";
- gotoxy(5,12);
- cin>>sil;
- cin.fail();
- }while((dajZmienno(sil)<0)||(cin.fail()==1));
- kalkulator[nD].daneS=silnia(dajZmienno(sil));
- break;
- }
- case '^':{
- cin.sync();cin.clear();
- gotoxy(0,12);cout<<"podstawa potegi z: ";
- gotoxy(20,12);
- cin>>podP;
- gotoxy(28,12);
- cout<<"wykladnik: ";cin>>wykP;
- kalkulator[nD].daneS=pot(dajZmienno(podP),dajZmienno(wykP));
- break;
- }
- case 'n':{
- cin.sync();cin.clear();
- gotoxy(0,12);cout<<"Pierwsza liczba: ";
- gotoxy(16,12);
- cin>>nww1;
- gotoxy(18,12);
- cout<<"Druga liczba: ";cin>>nww2;
- kalkulator[nD].daneS=nww(dajZmienno(nww1),dajZmienno(nww2));
- break;
- }
- case 'd':{
- cin.sync();cin.clear();
- gotoxy(0,12);cout<<"Pierwsza liczba: ";
- gotoxy(16,12);
- cin>>nwd1;
- gotoxy(18,12);
- cout<<"Druga liczba: ";cin>>nwd2;
- kalkulator[nD].daneS=nwd(dajZmienno(nwd1),dajZmienno(nwd2));
- break;
- }
- case 'b':{
- cin.sync();cin.clear();
- gotoxy(0,12);cout<<"Liczba: ";
- gotoxy(9,12);
- cin>>bez;
- bez1=dajZmienno(bez);
- if (bez1<0) bez1*=-1;
- kalkulator[nD].daneS=dajString(bez1);
- break;
- }
- case 8:{
- nD=0;kalkulator[0].daneS="";
- break;
- }
- }
- if(j>9) kalkulator[nD].daneS+=wybor;
- }while((wybor!='=')&&(wybor!='`'));
- while((wybor!='`')&&(wybor!='x')){
- cout<<"Wynik: ";
- konw();getch();system("cls");
- cout<<"1 - nowe dzialanie\n";
- cout<<"2 - Wylacz\n";
- cout<<"Wybor: ";do{cin>>wyborL;}while((wyborL<1)||(2<wyborL));
- wybor='x';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment