Re: C++ Programmierübung



  • #include <iostream>
    #include <string>
    #include <list>
    #include <map>
    
    class Customer {
    public:
    	Customer(unsigned int customerId, bool steadyCustomer, std::string favoriteIceCream) : 
    		customerId_(customerId),
    		steadyCustomer_(steadyCustomer),
    		favoriteIceCream_(favoriteIceCream) {};
    
    	bool operator== (const Customer& rhs) const { return customerId_ == rhs.customerId_; };
    	unsigned int customerId() const { return customerId_; };
    	bool steadyCustomer() const { return steadyCustomer_; };
    	std::string favoriteIceCream() const { return favoriteIceCream_; };
    private:
    	unsigned int customerId_;
    	bool steadyCustomer_;
    	std::string favoriteIceCream_;
    };
    
    std::list<Customer> customerList;
    
    void dataInput()
    {
    	bool more;
    	do {
    		std::cout << "ID?\n";
    		unsigned int id;
    		std::cin >> id;
    
    		std::cout << "Stammkunde?\n";
    		bool steadyCustomer;
    		std::cin >> steadyCustomer;
    
    		std::cout << "Lieblingseis?\n";
    		std::string favoriteIceCream;
    		std::cin >> favoriteIceCream;
    
    		customerList.push_back(Customer(id, steadyCustomer, favoriteIceCream));
    
    		std::cout << "weitere Eingabe?\n";
    		std::cin >> more;
    	} while (more);
    
    }
    
    std::map<std::string, int> calcIceCreamToPointsMap(bool skipNormalCustomers)
    {
    	std::map<std::string, int> m;
    
    	for (std::list<Customer>::const_iterator it = customerList.begin();
    	     it != customerList.end(); ++it) {
    		if (!skipNormalCustomers || (*it).steadyCustomer()) {
    			int toAdd = (*it).steadyCustomer() ? 3 : 1;
    			std::map<std::string, int>::iterator mit = m.find((*it).favoriteIceCream());
    			if (mit == m.end()) {
    			       m[(*it).favoriteIceCream()] = toAdd;
    			} else {
    				(*mit).second += toAdd;
    			}
    		}
    	}
    
    	return m;
    }
    
    void printIceCreamToPointsMap(const std::map<std::string, int>& m)
    {
    	for (std::map<std::string, int>::const_iterator it = m.begin();
    	     it != m.end(); ++it) {
    		std::cout << (*it).first << ": " << (*it).second << "\n";
    	}
    }
    
    void printHitList()
    {
    	std::map<std::string, int> m = calcIceCreamToPointsMap(false);
    	printIceCreamToPointsMap(m);
    }
    
    void printHitListFromSteadyCustomers()
    {
    	std::map<std::string, int> m = calcIceCreamToPointsMap(true);
    	printIceCreamToPointsMap(m);
    }
    
    void findIceCream()
    {
    	std::cout << "Eissorte?\n";
    	std::string iceCream;
    	std::cin >> iceCream;
    
    	std::map<std::string, int> m = calcIceCreamToPointsMap(true);
    	std::map<std::string, int>::const_iterator it = m.find(iceCream);
    	if (it != m.end()) {
    		std::cout << (*it).first << ": " << (*it).second << "\n";
    	}
    }
    
    void printAvgPoints()
    {
    	std::map<std::string, int> m = calcIceCreamToPointsMap(false);
    	int sum = 0;
    
    	for (std::map<std::string, int>::const_iterator it = m.begin();
    	     it != m.end(); ++it) {
    		sum += (*it).second;
    	}
    
    	std::cout << "Durchschnitt: " << static_cast<float>(sum) / customerList.size() << "\n";
    }
    
    int main()
    {
    	for (;;) {
    		std::cout << "1) Daten eingeben\n";
    		std::cout << "2) Hitliste der Eissorten\n";
    		std::cout << "3) Hitliste der Eissorten von Stammkunden\n";
    		std::cout << "4) Eissorte suchen\n";
    		std::cout << "5) Durchschnittliche Punktzahl\n";
    		std::cout << "6) Ende\n";
    		std::cout << "\n";
    		int selection;
    		std::cin >> selection;
    		switch (selection) {
    		case 1:
    			dataInput();
    			break;
    		case 2:
    			printHitList();
    			break;
    		case 3:
    			printHitListFromSteadyCustomers();
    			break;
    		case 4:
    			findIceCream();
    			break;
    		case 5:
    			printAvgPoints();
    			break;
    		case 6:
    			return 0;
    		default:
    			;
    		}
    	}
    }
    


  • Warum tust du sowas? 😮



  • weil er denkt, dass der typ der die aufgabe gepostet hat ein mädel ist... 🙄



  • Weil er Zeigen will was für ein toller Coder er ist. 🙄



  • welches Lizenzmodell? 😃



  • loooooooooooooooooooooooooooooooooOoooooooooooooooooooooooooooooool ppeD!


Anmelden zum Antworten