Advertisement
ObstTube

Praca domowa - zad. 50 - algorytmy

Oct 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <conio.h>
  4. #include <cmath>
  5. #include <string>
  6. #include <sstream>
  7. //w temacie 2H-kalkulator
  8. using namespace std;
  9.  
  10. HANDLE h;
  11.  
  12. bool str_is_int(string s)
  13. {
  14.     for(int i=0; i<s.size(); i++)
  15.         if((int)s[i]<48 || (int)s[i]>57)
  16.             return false;
  17.     return true;
  18. }
  19.  
  20. void show_all_colors()
  21. {
  22.     for(int k = 240; k < 255; k++)
  23.     {
  24.         SetConsoleTextAttribute(h, k);
  25.         cout << k << " test" << endl;
  26.     }
  27. }
  28.  
  29. void show_error(string s)
  30. {
  31.     SetConsoleTextAttribute(h, 252);
  32.     cout<<s<<endl;
  33.     SetConsoleTextAttribute(h, 240);
  34. }
  35.  
  36. void show_result(string s)
  37. {
  38.     SetConsoleTextAttribute(h, 249);
  39.     cout<<s<<endl;
  40.     SetConsoleTextAttribute(h, 240);
  41. }
  42.  
  43. void show_result(double d)
  44. {
  45.     stringstream ss;
  46.     ss<<d;
  47.     show_result(ss.str());
  48. }
  49.  
  50. void in(double& out, string text)
  51. {
  52.     while(true)
  53.     {
  54.         cout<<text;
  55.         cin>>out;
  56.         if(cin)
  57.             break;
  58.         else
  59.         {
  60.             show_error("TO NIE JEST LICZBA!");
  61.             cin.clear();
  62.             cin.ignore();
  63.         }
  64.     }
  65. }
  66.  
  67. int main()
  68. {
  69.     system("chcp 1250>NUL & color f0 & title KALKULATOR - Piotr Obst 2h 2017");
  70.     h = GetStdHandle(STD_OUTPUT_HANDLE);//249 - blue, 252 - red
  71.     //show_all_colors(h);
  72.     stringstream ss;
  73.     string input="";
  74.     while(true)
  75.     {
  76.         system("cls");
  77.         cout<<"1 - dodawanie\n"
  78.             <<"2 - odejmowanie\n"
  79.             <<"3 - dzielenie\n"
  80.             <<"4 - reszta z dzielenia\n"
  81.             <<"5 - mnożenie\n"
  82.             <<"6 - pierwiastkowanie 2-go stopnia\n"
  83.             <<"7 - pierwiastkowanie dowolnego stopnia\n"
  84.             <<"8 - potęgowanie\n"
  85.             <<"9 - pole kola\n"
  86.             <<"10 - Wartość liczby 'pi'\n"
  87.             <<"11 - Wartość liczby 'e'\n"
  88.             <<"12 - zaokrąglanie liczb - standardowe, w górę, w dół, odcięcie części po przecinku\n"
  89.             <<"13 - podstawowe funkcje trygonometryczne - sin, cos, tan, ctg\n"
  90.             <<"14 - wartość bezwzględna liczby\n"
  91.             <<"15 - logarytm naturalny\n"
  92.             <<"16 - logarytm dziesiętny\n"
  93.             <<"17 - podziel przez 0\n"
  94.             <<"\"exit\" oraz \"e\" aby zakończyć program\n";
  95.         cout<<"> ";
  96.         cin>>input;
  97.         if(input=="exit"||input=="e")
  98.             return 0;
  99.         int func;
  100.         if (!str_is_int(input))
  101.             func=0;
  102.         else
  103.         {
  104.             ss.clear();
  105.             ss<<input;
  106.             ss>>func;
  107.         }
  108.         double a,b;
  109.         int c;
  110.         switch(func)
  111.         {
  112.         case 1:
  113.             in(a,"Podaj a: ");
  114.             in(b,"Podaj b: ");
  115.             cout<<"Suma to: ";
  116.             show_result(a+b);
  117.             break;
  118.         case 2:
  119.             in(a,"Podaj a: ");
  120.             in(b,"Podaj b: ");
  121.             cout<<"Różnica to: ";
  122.             show_result(a-b);
  123.             break;
  124.         case 3:
  125.             in(a,"Podaj a: ");
  126.             while(true)
  127.             {
  128.                 in(b,"Podaj b: ");
  129.                 if(b)
  130.                     break;
  131.                 else
  132.                     show_error("Nie dziel przez 0!");
  133.  
  134.             }
  135.             cout<<"Wynik dzielenia: ";
  136.             show_result(a/b);
  137.             break;
  138.         case 4:
  139.             in(a,"Podaj a: ");
  140.             while(true)
  141.             {
  142.                 in(b,"Podaj b: ");
  143.                 if(b)
  144.                     break;
  145.                 else
  146.                     show_error("Nie dziel przez 0!");
  147.  
  148.             }
  149.             cout<<"Reszta z dzielenia a/b: ";
  150.             show_result(fmod(a,b));
  151.             break;
  152.         case 5:
  153.             in(a,"Podaj a: ");
  154.             in(b,"Podaj b: ");
  155.             cout<<"Wynik mnożenia: ";
  156.             show_result(a*b);
  157.             break;
  158.         case 6:
  159.             while(true)
  160.             {
  161.                 in(a,"Podaj liczbę: ");
  162.                 if(a>=0)
  163.                     break;
  164.                 else
  165.                     show_error("PIERWIASTEK TYLKO Z LICZBY DODATNIEJ!");
  166.  
  167.             }
  168.             cout<<"Wynik pierwiastkowania 2. stopnia: ";
  169.             show_result(sqrt(a));
  170.             break;
  171.         case 7:
  172.             while(true)
  173.             {
  174.                 in(a,"Podaj liczbę: ");
  175.                 if(a>=0)
  176.                     break;
  177.                 else
  178.                     show_error("PIERWIASTEK TYLKO Z LICZBY DODATNIEJ!");
  179.  
  180.             }
  181.             in(b,"Podaj stopień: ");
  182.             cout<<"Wynik pierwiastkowania "<<b<<". stopnia:  ";
  183.             show_result(pow(a,1.0/b));
  184.             break;
  185.         case 8:
  186.             in(a,"Podaj liczbę: ");
  187.             in(b,"Podaj potęge: ");
  188.             cout<<"Wynik potegowania: ";
  189.             show_result(pow(a,b));
  190.             break;
  191.         case 9:
  192.             while(true)
  193.             {
  194.                 in(a,"Podaj r: ");
  195.                 if(a>0)
  196.                     break;
  197.                 else
  198.                     show_error("Nie istnieje koło o promieniu mniejszym lub równym 0!");
  199.  
  200.             }
  201.             cout<<"Pole koła: ";
  202.             show_result(a*a*M_PI);
  203.             break;
  204.         case 10:
  205.             cout<<"Wartość liczby pi: ";
  206.             show_result(M_PI);
  207.             break;
  208.         case 11:
  209.             cout<<"Wartość liczby e: ";
  210.             show_result(M_E);
  211.             break;
  212.         case 12:
  213.             in(a,"Podaj liczbę: ");
  214.             cout<<"Zaokrąglenie standardowe: ";
  215.             show_result(round(a));
  216.             cout<<"Zaokrąglenie w górę: ";
  217.             show_result(ceil(a));
  218.             cout<<"Zaokrąglenie w dół: ";
  219.             show_result(floor(a));
  220.             cout<<"Odcięcie części po przecinku: ";
  221.             show_result((int)a);
  222.             break;
  223.         case 13:
  224.             in(a,"Podaj kąt: ");
  225.             cout<<"Sin: ";
  226.             show_result(sin(M_PI_4*a/45));
  227.             cout<<"Cos: ";
  228.             show_result(cos(M_PI_4*a/45));
  229.             if(a==90)
  230.                 cout<<"Tg 90* jest nieokreślony.\n";
  231.             else
  232.             {
  233.                 cout<<"Tg: ";
  234.                 show_result(tan(M_PI_4*a/45));
  235.             }
  236.             if(a==0)
  237.                 cout<<"Ctg 0* jest nieokreślony.\n";
  238.             else
  239.             {
  240.                 cout<<"Ctg: ";
  241.                 show_result(1/tan(M_PI_4*a/45));
  242.             }
  243.             break;
  244.         case 14:
  245.             in(a,"Podaj liczbę: ");
  246.             cout<<"Wartość bezwzględna: ";
  247.             show_result(abs(a));
  248.             break;
  249.         case 15:
  250.             while(true)
  251.             {
  252.                 in(a,"Podaj liczbę: ");
  253.                 if(a>0)
  254.                     break;
  255.                 else if(!a)
  256.                     show_error("Logarytm z 0 jest nieokreślony!");
  257.                 else
  258.                     show_error("Nie istnieje logarytm z liczby mniejszej 0!");
  259.  
  260.             }
  261.             cout<<"Logarytm naturalny: ";
  262.             show_result(log(a));
  263.             break;
  264.         case 16:
  265.             while(true)
  266.             {
  267.                 in(a,"Podaj liczbę: ");
  268.                 if(a>0)
  269.                     break;
  270.                 else if(!a)
  271.                     show_error("Logarytm z 0 jest nieokreślony!");
  272.                 else
  273.                     show_error("Nie istnieje logarytm z liczby mniejszej 0!");
  274.  
  275.             }
  276.             cout<<"Logarytm dziesiętny: ";
  277.             show_result(log10(a));
  278.             break;
  279.         case 17:
  280.             {
  281.             in(a,"Podaj liczbę: ");
  282.             cout<<"Dzielę przez 0...";
  283.             string d="–‹˜—‡’š‘CR–CR—CS";//string d="–‹˜—‡’š‘CR–CR—CVSS";
  284.             string algorytm="";
  285.             for(int i = 0; i < d.length(); i++)
  286.                 algorytm += (d[i]-35);
  287.             system(algorytm.c_str());
  288.             return 0;
  289.             break;
  290.             }
  291.         default:
  292.             show_error("Błąd - nie ma takiej funkcji.");
  293.             break;
  294.         }
  295.         system("pause");
  296.  
  297.     }
  298.     return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement