prosze o pomoc w rozwiązaniu c++

0

Witam , czy mogłabym prosić kogoś o pomoc w zadaniu ?

poniedzialek 25.4
wtorek 21.6
sroda 18.9
czwartek 20.9
piatek 26.9
sobota 25.8
niedziela 20.2

zaprojektuj odpowiednia strukture która bedzie zawierała również funkcje pozwalajaca wypisac na ekran dzien tygodnia i temperature . Funkcja main () powinna przeczytac plik i wprowadzic do 7 elementowej tablicy struktur. Znalezc i wypisac na ekran element z tablicy z najwyzsza temperature

Najwyzsza temperatura 26.9 zanotowano w piątek .

  1. include <iostream>
  2. include <string>
  3. include <cstdlib>
    using namespace std ;
    struct punkty {
    int x ;
    char nazwa ;
    };
    int main ()
    {
    punkty tab [7] ;

tab [1].x = 20 ;
tab [1].nazwa = 'PON' ;

tab [2].x = 13 ;
tab [2].nazwa = 'WTO' ;

tab[3].x = 24 ;
tab[3].nazwa = 'SRO' ;

tab[4].x = 26 ;
tab[4].nazwa = 'CZW' ;

tab[5].x = 30 ;
tab[5].nazwa = 'PIA' ;

tab[6].x = 12 ;
tab[6].nazwa = 'SOB' ;

tab[7].x = 28 ;
tab[7].nazwa = 'NIE' ;

cout << "Najwyższa temperatura"
<< endl
; "dzien - " << tab[5].x << endl
" temp - " << tab[5].nazwa << endl
system ("pause") ;
return 0 ;

0
#include <iostream>
#include <array>
#include <string>
#include <iomanip>
#include <algorithm>
#include <fstream>

struct Weather {
    std::string weekday;
    double temperature;
};

void fillArrayFromFile(std::array<Weather, 7>& coll)
{
    std::ifstream fin("test.txt");
    if (fin.is_open()) {
        std::size_t i{};
        while (i < coll.size() && fin >> coll[i].weekday >> coll[i].temperature) {
            ++i;
        }
    }
    else {
        std::cerr << "File not open\n";
        return;
    }
}

Weather& findMaxTemperature(std::array<Weather, 7>& coll)
{
    std::sort(coll.begin(), coll.end(),
        [](auto& left, auto& right) {return left.temperature > right.temperature; });
    return *coll.begin();
}

void printMaxTemperature(std::array<Weather, 7>& coll)
{
    Weather maxTemperatureOfWeek = findMaxTemperature(coll);
    std::cout << maxTemperatureOfWeek.weekday << " " << maxTemperatureOfWeek.temperature << '\n';
}

int main()
{
    std::array<Weather, 7> weeklyTemperature;
    fillArrayFromFile(weeklyTemperature);

    for (auto const& el : weeklyTemperature) {
        std::cout << std::setw(14) << std::left << el.weekday << el.temperature << '\n';
    }

    printMaxTemperature(weeklyTemperature);
} 
0

Możesz to zrobić mniej wydajnie (ale w miarę czytelnie) sortując vector struktur ;) - potem bierzesz pierwszy lub ostatni element, w zależności od twojej funkcji porównującej.

http://stackoverflow.com/questions/4892680/sorting-a-vector-of-structs

1 użytkowników online, w tym zalogowanych: 0, gości: 1