#include #include //#include "main.h" #include #include #include using namespace std; //Prototypy: bool isCorrect(double, char); void showMenu(bool); int getCommand(); double readTemperature(char); void addToTable(double, char, double, char); void showTable(bool); string doubleToString(double, int, char); double CtoF(double); double CtoK(double); double FtoC(double); double FtoK(double); double KtoC(double); double KtoF(double); //Zmienne globalne: const int SIZE = 10; double vectorTempWczytana[SIZE]; double vectorTempPrzeliczona[SIZE]; char vectorTempWczytanaZnak[SIZE]; char vectorTempPrzeliczonaZnak[SIZE]; int indexNext = 0; int main() { double temp_wczytana; double temp_przeliczona; int rozkaz; showMenu(true); while(1) { rozkaz = getCommand(); switch(rozkaz) { case 0: showMenu(false); break; case 1: //C->F temp_wczytana = readTemperature('C'); temp_przeliczona = CtoF(temp_wczytana); addToTable(temp_wczytana, 'C', temp_przeliczona, 'F'); cout << temp_wczytana << "[C] = " << temp_przeliczona << "[F]" << endl; break; case 2: //C->K temp_wczytana = readTemperature('C'); temp_przeliczona = CtoK(temp_wczytana); addToTable(temp_wczytana, 'C', temp_przeliczona, 'K'); cout << temp_wczytana << "[C] = " << temp_przeliczona << "[K]" << endl; break; case 3: //F->C temp_wczytana = readTemperature('F'); temp_przeliczona = FtoC(temp_wczytana); addToTable(temp_wczytana, 'F', temp_przeliczona, 'C'); cout << temp_wczytana << "[F] = " << temp_przeliczona << "[C]" << endl; break; case 4: //F->K temp_wczytana = readTemperature('F'); temp_przeliczona = FtoK(temp_wczytana); addToTable(temp_wczytana, 'F', temp_przeliczona, 'K'); cout << temp_wczytana << "[F] = " << temp_przeliczona << "[K]" << endl; break; case 5: //K->C temp_wczytana = readTemperature('K'); temp_przeliczona = KtoC(temp_wczytana); addToTable(temp_wczytana, 'K', temp_przeliczona, 'C'); cout << temp_wczytana << "[K] = " << temp_przeliczona << "[C]" << endl; break; case 6: //K->K temp_wczytana = readTemperature('K'); temp_przeliczona = KtoF(temp_wczytana); addToTable(temp_wczytana, 'K', temp_przeliczona, 'F'); cout << temp_wczytana << "[K] = " << temp_przeliczona << "[F]" << endl; break; case 7: showTable(true); break; case 99: return 0; break; } } system("pause"); return 0; } /** * Funkcja wyswietlania menu. */ void showMenu(bool clearScreen) { if(clearScreen) system("cls"); cout << "========= MENU =========" << endl; cout << "0: Pokaz menu" << endl; cout << "1: [C] -> [F]" << endl; cout << "2: [C] -> [K]" << endl; cout << "3: [F] -> [C]" << endl; cout << "4: [F] -> [K]" << endl; cout << "5: [K] -> [C]" << endl; cout << "6: [K] -> [F]" << endl; cout << "7: Pokaz historie" << endl; cout << "99: Wyjscie" << endl; cout << endl; } /** * Funkcja wczytuje numer polecenia wprowadzony z klawiatury i zwraca go jako typ int. * @return int - liczba oznaczajÄ…ca numer polecenia */ int getCommand() { int command; while(1) { //to samo: while(!dataOk) cout << "Polecenie [0=menu, 99=exit]: "; if((cin >> command)) { break; } else { cin.clear(); cin.ignore(10000, '\n'); cout << "Error: nieprawidlowy numer polecenia!" << endl; cout << endl; } } return command; } /** * Funkcja wczytuje temperature o podanym znaku oraz sprawdza, czy miesci sie w zakresie * (przy pomocy fuknkcji isCorrect(double, char); * @param znakTemp char - znak temperatury: C, K lub F * @return double - wczytane stopnie */ double readTemperature(char znakTemp) { double temperatura; while(true) { cout << "Wpisz wartosc temperatury do przeliczenia [" << znakTemp << "]: "; if((cin >> temperatura)) { bool isOk = isCorrect(temperatura, znakTemp); if(isOk == true) { break; } else { cout << "Temperatura " << temperatura << "[" << znakTemp << "] poza skala!" << endl; continue; } } else { cin.clear(); cin.ignore(10000, '\n'); cout << "Error: wpisany ciag znak nimozliwy do konwersji na typ double!" << endl; cout << endl; } } return temperatura; } /** * Procedura dodajaca ostatnio wykonane przeliczenie temperatur do pamieci w tablicach. * @param tempStart double - temperatura poczatkowa * @param signStart char - znak temp. poczatkowej * @param tempEnd double - temperatura przeliczona * @param signEnd char - znak temp. przeliczonej */ void addToTable(double tempStart, char signStart, double tempEnd, char signEnd) { if(indexNext >= SIZE) { cout << "Tablice danych pelne. Nie mozna dodac nastepnego wpisu do historii temperatur!" << endl; } else { vectorTempWczytana[indexNext] = tempStart; vectorTempWczytanaZnak[indexNext] = signStart; vectorTempPrzeliczona[indexNext] = tempEnd; vectorTempPrzeliczonaZnak[indexNext] = signEnd; indexNext++; } } /** * Procedura pokazuja tablice danych - przeliczone temperatury. */ void showTable(bool simple) { int i=0; if(simple) { cout << endl; cout << "********************************" << endl; for(i=0; i "; cout << vectorTempPrzeliczona[i] << "" << vectorTempPrzeliczonaZnak[i] << endl; } } else { cout << endl; cout << "********************************" << endl; } } /** * Funkcja przeliczajaca temperature ze stopni Celsjusza na stopnie Fahrenheita * @param stopnie double - temperatura w [C] * @return double - temperatura w [F] */ double CtoF(double stopnie) { double wynik; wynik = stopnie * (9.0/5.0) + 32; return wynik; } double CtoK(double stopnie) { return stopnie + 273.15; } double FtoC(double stopnie) { double wynik; wynik = (stopnie-32.0) * (5.0/9.0); return wynik; } double FtoK(double stopnie) { double celsius = FtoC(stopnie); double kelwin = CtoK(celsius); return kelwin; } double KtoC(double stopnie) { return stopnie - 273.15; } double KtoF(double stopnie) { double celsius = KtoC(stopnie); double fahrenheit = CtoF(celsius); return fahrenheit; } /** * Funkcja sprawdzajaca, czy dana temperatura miesci sie w dozwolonym zakresie * @param stopnie double - wczytana temperatura * @param znakTemperatury char - C, F lub F * @return bool - true, jesli temperatura jest ok, false w przeciwnym wypadku */ bool isCorrect(double stopnie, char znakTemperatury) { if(znakTemperatury == 'C' || znakTemperatury == 'c') { if(stopnie < -273.15) return false; else return true; } else if(znakTemperatury == 'K' || znakTemperatury == 'k') { if(stopnie < 0) return false; else return true; } else if(znakTemperatury == 'F' || znakTemperatury == 'f') { if(stopnie < -459.67) return false; else return true; } else { return false; } } //https://www.facebook.com/229943587138279/photos/a.229953990470572.59741.229943587138279/727266917405941/?type=3&theater