asmodeus94

kalkulator

Apr 23rd, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.86 KB | None | 0 0
  1. #include <windows.h>
  2. #include <sstream>
  3. #include <conio.h>
  4. #include <iostream>
  5. using namespace std;
  6. HANDLE g_hConsol = GetStdHandle(STD_OUTPUT_HANDLE);
  7. void gotoxy(const int x, const int y){
  8.         COORD coord = {x, y};
  9.         SetConsoleCursorPosition(g_hConsol, coord);
  10. }
  11. struct operacje{
  12. float dane; // 32000 (+); 32001 (-); 32020 (*); 32021 (/)
  13. string daneS;
  14. byte uje;
  15. };
  16. operacje kalkulator[100];
  17. int nD,wynikD;
  18. string dzialanieS;
  19. // KONWERSJE {
  20. string dajString(float liczba){
  21. stringstream FloatToStr;
  22. string str;
  23. FloatToStr << liczba;
  24. FloatToStr >> str;
  25. FloatToStr.clear();
  26. return str;
  27. }
  28. float dajZmienno(string co){
  29. float y=atof(co.c_str());
  30. return y;
  31. }
  32. // KONWERSJE }
  33. // LICZENIE WYNIKU {
  34. int ileTegoJest(byte czego){
  35. int ile=0;
  36.     for(int i=0;i<=nD;i++){
  37.         switch(czego){
  38.             case 1:{
  39.                 if(kalkulator[i].dane==32021) ile++;
  40.                 break;
  41.             }
  42.             case 2:{
  43.                 if(kalkulator[i].dane==32020) ile++;
  44.                 break;
  45.             }
  46.             case 3:{
  47.                 if(kalkulator[i].dane==32001) ile++;
  48.                 break;
  49.             }
  50.             case 4:{
  51.                 if(kalkulator[i].dane==32000) ile++;
  52.                 break;
  53.             }
  54.         }
  55.     }
  56. return ile;
  57. }
  58. void przesun(int skad){ // po obliczeniu uswanie znaku i jednej liczby
  59.     for(int s=skad;s<=nD-2;++s){
  60.         kalkulator[s].dane=kalkulator[s+2].dane;
  61.     }
  62. }
  63. void konw(){
  64. int k,a1,a2,a3,a4,ileR;
  65. for(int i=0;i<=nD;i++){
  66.     if((kalkulator[i].daneS!="+")&&(kalkulator[i].daneS!="-")&&(kalkulator[i].daneS!="*")&&(kalkulator[i].daneS!="/")) kalkulator[i].dane=dajZmienno(kalkulator[i].daneS);
  67.     else{
  68.         if(kalkulator[i].daneS=="+") kalkulator[i].dane=32000;
  69.         if(kalkulator[i].daneS=="-") kalkulator[i].dane=32001;
  70.         if(kalkulator[i].daneS=="*") kalkulator[i].dane=32020;
  71.         if(kalkulator[i].daneS=="/") kalkulator[i].dane=32021;
  72.     }
  73. }
  74. float arytm;
  75. for(k=nD;k!=0;){
  76.    
  77.     ileR=ileTegoJest(1);int i=0;
  78.     if(ileR>0){
  79.     for(a1=0;a1<ileR;){
  80.         if(kalkulator[i].dane==32021){
  81.             arytm=kalkulator[i-1].dane/kalkulator[i+1].dane;
  82.             kalkulator[i-1].dane=arytm;
  83.             przesun(i);
  84.             ++a1;k-=2;nD-=2;
  85.             i=0;
  86.         }
  87.         ++i;
  88.     }
  89.     }
  90.    
  91.     ileR=ileTegoJest(2);i=0;
  92.     if(ileR>0){
  93.     for(a2=0;a2<ileR;){
  94.         if(kalkulator[i].dane==32020){
  95.             arytm=kalkulator[i-1].dane*kalkulator[i+1].dane;
  96.             kalkulator[i-1].dane=arytm;
  97.             przesun(i);
  98.             ++a2;k-=2;nD-=2;
  99.             i=0;
  100.         }
  101.         ++i;               
  102.     }
  103.     }
  104.     ileR=ileTegoJest(3);i=0;
  105.     if(ileR>0){
  106.     for(a3=0;a3<ileR;){
  107.         if(kalkulator[i].dane==32001){
  108.             arytm=kalkulator[i-1].dane-kalkulator[i+1].dane;
  109.             kalkulator[i-1].dane=arytm;
  110.             przesun(i);
  111.             ++a3;k-=2;nD-=2;
  112.             i=0;
  113.         }
  114.         ++i;
  115.                
  116.     }
  117.     }
  118.     ileR=ileTegoJest(4);i=0;
  119.     if(ileR>0){
  120.     for(a4=0;a4<ileR;){
  121.         if(kalkulator[i].dane==32000){
  122.             arytm=kalkulator[i-1].dane+kalkulator[i+1].dane;
  123.             kalkulator[i-1].dane=arytm;
  124.             przesun(i);
  125.             ++a4;k-=2;nD-=2;
  126.             i=0;
  127.         }
  128.         ++i;
  129.            
  130.     }
  131.     }
  132. }
  133.    
  134. cout<<kalkulator[0].dane;
  135. }
  136. // LICZENIE WYNIKU}
  137. // DODATKOWE OPERACJE {
  138. string silnia(float co){
  139. long long siln=1;
  140. if (co>0){
  141. for(int i=1;i<=co;i++){
  142.     siln*=i;
  143. }
  144. return dajString(siln);
  145. }else return "0";
  146. }
  147. string pot(float coP, float coW){
  148. float pote=1;
  149. int ujemna=0;
  150. if(coW!=0){
  151.     if(coW<0){ujemna=1;coW*=-1;}
  152.     for(int i=0;i<coW;i++){
  153.         if(ujemna==0) pote*=coP; else pote*=coP;
  154.     }
  155. }
  156. if(ujemna==0){return dajString(pote);} else{return dajString(1/pote);}
  157. }
  158. string nwd(float nwd1,float nwd2){
  159. int nwdi1=nwd1,nwdi2=nwd2;
  160. while(nwdi1!=nwdi2){
  161.     if(nwdi1<nwdi2) nwdi2-=nwdi1; else nwdi1-=nwdi2;
  162. }
  163. return dajString(nwdi1);
  164. }
  165. string nww(float nww1,float nww2){
  166. int nwwi1=nww1,nwwi2=nww2;
  167. int k1=nwwi1,k2=nwwi2,w;
  168. while(k1!=k2){
  169.     if(k1<k2) k2-=k1; else k1-=k2;
  170. }
  171. w=(nwwi1*nwwi2)/k1;
  172. return dajString(w);
  173. }
  174. // DODATKOWE OPERACJE }
  175. void wyswietlacz(){
  176. gotoxy(0,13);
  177. cout<<"Wyswietlacz: ";
  178. for(int i=0;i<=nD;i++){
  179.     if(kalkulator[i].daneS!=""){
  180.     if(kalkulator[i].uje==1) dzialanieS="("+kalkulator[i].daneS+")"; else dzialanieS=kalkulator[i].daneS;
  181.     cout<<dzialanieS;
  182.     }
  183. }
  184. gotoxy(0,0);}
  185. void menu(byte ktore){
  186. switch(ktore){
  187. case 1:{
  188. cout<<"+ - dodawanie\n";
  189. cout<<"- - odejmowanie\n";
  190. cout<<"* - mnozenie\n";
  191. cout<<"/ - dzielenie\n";
  192. cout<<"! - silnia\n";
  193. cout<<"^ - potega\n";
  194. cout<<"n - najmniejsza wspolna wielokrotnosc\n";
  195. cout<<"d - najwiekszy wspolny dzielnik\n";
  196. cout<<"b - wartosc bezwzgledna\n";
  197. cout<<"u - liczba ujemna\n";
  198. cout<<"= - wynik\n";
  199. cout<<"`(pod tylda) - Wylacz\n";
  200. break;}
  201. case 2:{
  202. break;}
  203. }
  204. }
  205. void restartK(){
  206.     for(int i=0;i<100;i++){
  207.         kalkulator[i].dane=0;
  208.         kalkulator[i].daneS="";
  209.         kalkulator[i].uje=0;
  210.     }
  211. }
  212. int main(){
  213. char wybor;
  214. int wyborL,dozZn,j,ost;
  215. string pom,sil,podP,wykP,nww1,nww2,nwd1,nwd2,bez,dozwoloneZnaki="+-*/u^!ndb=`.,1234567890";
  216. float bez1;
  217. while((wyborL!=2)&&(wybor!='`')){
  218. restartK();dzialanieS="";nD=0;
  219. do{
  220. system("cls");
  221. wyswietlacz();
  222. menu(1);
  223. do{pocz:dozZn=0;wybor=getch();
  224. if(wybor==',') wybor='.';
  225. for(j=0;j<dozwoloneZnaki.length();j++){
  226.     if(wybor==dozwoloneZnaki[j]||(wybor==8)){
  227.         dozZn=1;break;
  228.     }
  229. }
  230. if((ost<4)&&(j<4)) goto pocz;
  231.  
  232. }while(dozZn==0);
  233. ost=j;
  234. kalkulator[nD+1].uje=0;
  235. switch(wybor){
  236. case '+':{
  237.         nD+=2;kalkulator[nD-1].daneS='+';break;}
  238. case '-':{
  239.         nD+=2;kalkulator[nD-1].daneS='-';break;}
  240. case '*':{
  241.         nD+=2;kalkulator[nD-1].daneS='*';break;}
  242. case '/':{
  243.         nD+=2;kalkulator[nD-1].daneS='/';break;}
  244. case 'u':{pom="-"+kalkulator[nD].daneS;swap(pom,kalkulator[nD].daneS);kalkulator[nD].uje=1;break;}
  245. case '!':{
  246.     do{
  247.     cin.sync();cin.clear();
  248.     gotoxy(0,12);cout<<"! z:              ";
  249.     gotoxy(5,12);
  250.     cin>>sil;
  251.     cin.fail();
  252.     }while((dajZmienno(sil)<0)||(cin.fail()==1));
  253.     kalkulator[nD].daneS=silnia(dajZmienno(sil));
  254.     break;
  255.     }
  256. case '^':{
  257.     cin.sync();cin.clear();
  258.     gotoxy(0,12);cout<<"podstawa potegi z:                         ";
  259.     gotoxy(20,12);
  260.     cin>>podP;
  261.     gotoxy(28,12);
  262.     cout<<"wykladnik: ";cin>>wykP;
  263.     kalkulator[nD].daneS=pot(dajZmienno(podP),dajZmienno(wykP));
  264.     break;
  265.     }
  266. case 'n':{
  267.     cin.sync();cin.clear();
  268.     gotoxy(0,12);cout<<"Pierwsza liczba:                            ";
  269.     gotoxy(16,12);
  270.     cin>>nww1;
  271.     gotoxy(18,12);
  272.     cout<<"Druga liczba: ";cin>>nww2;
  273.     kalkulator[nD].daneS=nww(dajZmienno(nww1),dajZmienno(nww2));
  274.     break;
  275. }
  276. case 'd':{
  277.     cin.sync();cin.clear();
  278.     gotoxy(0,12);cout<<"Pierwsza liczba:                            ";
  279.     gotoxy(16,12);
  280.     cin>>nwd1;
  281.     gotoxy(18,12);
  282.     cout<<"Druga liczba: ";cin>>nwd2;
  283.     kalkulator[nD].daneS=nwd(dajZmienno(nwd1),dajZmienno(nwd2));
  284.     break;
  285. }
  286. case 'b':{
  287.     cin.sync();cin.clear();
  288.     gotoxy(0,12);cout<<"Liczba:                            ";
  289.     gotoxy(9,12);
  290.     cin>>bez;
  291.     bez1=dajZmienno(bez);
  292.     if (bez1<0) bez1*=-1;
  293.     kalkulator[nD].daneS=dajString(bez1);
  294.     break;
  295. }
  296. case 8:{
  297.     nD=0;kalkulator[0].daneS="";
  298.     break;
  299. }
  300. }
  301. if(j>9) kalkulator[nD].daneS+=wybor;
  302. }while((wybor!='=')&&(wybor!='`'));
  303.     while((wybor!='`')&&(wybor!='x')){
  304.     cout<<"Wynik: ";
  305.     konw();getch();system("cls");
  306.     cout<<"1 - nowe dzialanie\n";
  307.     cout<<"2 - Wylacz\n";
  308.     cout<<"Wybor: ";do{cin>>wyborL;}while((wyborL<1)||(2<wyborL));
  309.     wybor='x';
  310.     }
  311. }
  312. return 0;
  313. }
Advertisement
Add Comment
Please, Sign In to add comment