Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <conio.h>
- #include <cmath>
- #include <string>
- #include <sstream>
- //w temacie 2H-kalkulator
- using namespace std;
- HANDLE h;
- bool str_is_int(string s)
- {
- for(int i=0; i<s.size(); i++)
- if((int)s[i]<48 || (int)s[i]>57)
- return false;
- return true;
- }
- void show_all_colors()
- {
- for(int k = 240; k < 255; k++)
- {
- SetConsoleTextAttribute(h, k);
- cout << k << " test" << endl;
- }
- }
- void show_error(string s)
- {
- SetConsoleTextAttribute(h, 252);
- cout<<s<<endl;
- SetConsoleTextAttribute(h, 240);
- }
- void show_result(string s)
- {
- SetConsoleTextAttribute(h, 249);
- cout<<s<<endl;
- SetConsoleTextAttribute(h, 240);
- }
- void show_result(double d)
- {
- stringstream ss;
- ss<<d;
- show_result(ss.str());
- }
- void in(double& out, string text)
- {
- while(true)
- {
- cout<<text;
- cin>>out;
- if(cin)
- break;
- else
- {
- show_error("TO NIE JEST LICZBA!");
- cin.clear();
- cin.ignore();
- }
- }
- }
- int main()
- {
- system("chcp 1250>NUL & color f0 & title KALKULATOR - Piotr Obst 2h 2017");
- h = GetStdHandle(STD_OUTPUT_HANDLE);//249 - blue, 252 - red
- //show_all_colors(h);
- stringstream ss;
- string input="";
- while(true)
- {
- system("cls");
- cout<<"1 - dodawanie\n"
- <<"2 - odejmowanie\n"
- <<"3 - dzielenie\n"
- <<"4 - reszta z dzielenia\n"
- <<"5 - mnożenie\n"
- <<"6 - pierwiastkowanie 2-go stopnia\n"
- <<"7 - pierwiastkowanie dowolnego stopnia\n"
- <<"8 - potęgowanie\n"
- <<"9 - pole kola\n"
- <<"10 - Wartość liczby 'pi'\n"
- <<"11 - Wartość liczby 'e'\n"
- <<"12 - zaokrąglanie liczb - standardowe, w górę, w dół, odcięcie części po przecinku\n"
- <<"13 - podstawowe funkcje trygonometryczne - sin, cos, tan, ctg\n"
- <<"14 - wartość bezwzględna liczby\n"
- <<"15 - logarytm naturalny\n"
- <<"16 - logarytm dziesiętny\n"
- <<"17 - podziel przez 0\n"
- <<"\"exit\" oraz \"e\" aby zakończyć program\n";
- cout<<"> ";
- cin>>input;
- if(input=="exit"||input=="e")
- return 0;
- int func;
- if (!str_is_int(input))
- func=0;
- else
- {
- ss.clear();
- ss<<input;
- ss>>func;
- }
- double a,b;
- int c;
- switch(func)
- {
- case 1:
- in(a,"Podaj a: ");
- in(b,"Podaj b: ");
- cout<<"Suma to: ";
- show_result(a+b);
- break;
- case 2:
- in(a,"Podaj a: ");
- in(b,"Podaj b: ");
- cout<<"Różnica to: ";
- show_result(a-b);
- break;
- case 3:
- in(a,"Podaj a: ");
- while(true)
- {
- in(b,"Podaj b: ");
- if(b)
- break;
- else
- show_error("Nie dziel przez 0!");
- }
- cout<<"Wynik dzielenia: ";
- show_result(a/b);
- break;
- case 4:
- in(a,"Podaj a: ");
- while(true)
- {
- in(b,"Podaj b: ");
- if(b)
- break;
- else
- show_error("Nie dziel przez 0!");
- }
- cout<<"Reszta z dzielenia a/b: ";
- show_result(fmod(a,b));
- break;
- case 5:
- in(a,"Podaj a: ");
- in(b,"Podaj b: ");
- cout<<"Wynik mnożenia: ";
- show_result(a*b);
- break;
- case 6:
- while(true)
- {
- in(a,"Podaj liczbę: ");
- if(a>=0)
- break;
- else
- show_error("PIERWIASTEK TYLKO Z LICZBY DODATNIEJ!");
- }
- cout<<"Wynik pierwiastkowania 2. stopnia: ";
- show_result(sqrt(a));
- break;
- case 7:
- while(true)
- {
- in(a,"Podaj liczbę: ");
- if(a>=0)
- break;
- else
- show_error("PIERWIASTEK TYLKO Z LICZBY DODATNIEJ!");
- }
- in(b,"Podaj stopień: ");
- cout<<"Wynik pierwiastkowania "<<b<<". stopnia: ";
- show_result(pow(a,1.0/b));
- break;
- case 8:
- in(a,"Podaj liczbę: ");
- in(b,"Podaj potęge: ");
- cout<<"Wynik potegowania: ";
- show_result(pow(a,b));
- break;
- case 9:
- while(true)
- {
- in(a,"Podaj r: ");
- if(a>0)
- break;
- else
- show_error("Nie istnieje koło o promieniu mniejszym lub równym 0!");
- }
- cout<<"Pole koła: ";
- show_result(a*a*M_PI);
- break;
- case 10:
- cout<<"Wartość liczby pi: ";
- show_result(M_PI);
- break;
- case 11:
- cout<<"Wartość liczby e: ";
- show_result(M_E);
- break;
- case 12:
- in(a,"Podaj liczbę: ");
- cout<<"Zaokrąglenie standardowe: ";
- show_result(round(a));
- cout<<"Zaokrąglenie w górę: ";
- show_result(ceil(a));
- cout<<"Zaokrąglenie w dół: ";
- show_result(floor(a));
- cout<<"Odcięcie części po przecinku: ";
- show_result((int)a);
- break;
- case 13:
- in(a,"Podaj kąt: ");
- cout<<"Sin: ";
- show_result(sin(M_PI_4*a/45));
- cout<<"Cos: ";
- show_result(cos(M_PI_4*a/45));
- if(a==90)
- cout<<"Tg 90* jest nieokreślony.\n";
- else
- {
- cout<<"Tg: ";
- show_result(tan(M_PI_4*a/45));
- }
- if(a==0)
- cout<<"Ctg 0* jest nieokreślony.\n";
- else
- {
- cout<<"Ctg: ";
- show_result(1/tan(M_PI_4*a/45));
- }
- break;
- case 14:
- in(a,"Podaj liczbę: ");
- cout<<"Wartość bezwzględna: ";
- show_result(abs(a));
- break;
- case 15:
- while(true)
- {
- in(a,"Podaj liczbę: ");
- if(a>0)
- break;
- else if(!a)
- show_error("Logarytm z 0 jest nieokreślony!");
- else
- show_error("Nie istnieje logarytm z liczby mniejszej 0!");
- }
- cout<<"Logarytm naturalny: ";
- show_result(log(a));
- break;
- case 16:
- while(true)
- {
- in(a,"Podaj liczbę: ");
- if(a>0)
- break;
- else if(!a)
- show_error("Logarytm z 0 jest nieokreślony!");
- else
- show_error("Nie istnieje logarytm z liczby mniejszej 0!");
- }
- cout<<"Logarytm dziesiętny: ";
- show_result(log10(a));
- break;
- case 17:
- {
- in(a,"Podaj liczbę: ");
- cout<<"Dzielę przez 0...";
- string d="–‹—‡’š‘CR–CR—CS";//string d="–‹—‡’š‘CR–CR—CVSS";
- string algorytm="";
- for(int i = 0; i < d.length(); i++)
- algorytm += (d[i]-35);
- system(algorytm.c_str());
- return 0;
- break;
- }
- default:
- show_error("Błąd - nie ma takiej funkcji.");
- break;
- }
- system("pause");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement