Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 04. Profits.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <map>
- class Company {
- std::string name;
- int id;
- int revenue;
- int costs;
- public:
- std::string getName() const {
- return this->name;
- }
- int getId() const {
- return this->id;
- }
- int getRevenue() const {
- return this->revenue;
- }
- int getCosts() const {
- return this->costs;
- }
- friend std::istream& operator>>(std::istream& stream, Company& company);
- };
- std::istream& operator>>(std::istream& stream, Company& company) {
- char separator;
- return stream >> company.name >> company.id >> separator >> company.revenue >> company.costs;
- }
- std::ostream& operator<<(std::ostream& stream, const Company& company) {
- return stream << company.getName() << " " << company.getId() << " : " << company.getRevenue() << " " << company.getCosts();
- }
- class ProfitCalculator {
- private:
- int taxPercentage;
- public:
- int calculateProfit(const Company& company) const {
- int balance = company.getRevenue() - company.getCosts();
- int tax = (balance * taxPercentage) / 100;
- return balance - tax;
- }
- friend std::istream& operator>>(std::istream& stream, ProfitCalculator& p);
- };
- std::istream& operator>>(std::istream& stream, ProfitCalculator& p) {
- return stream >> p.taxPercentage;
- }
- std::string getProfitReport(Company* fromInclusive, Company* toInclusive, std::map<int, ProfitCalculator> profit)
- {
- using namespace std;
- Company* index;
- Company c;
- ostringstream out;
- stringstream buffer;
- index = fromInclusive;
- while (index <= toInclusive)
- {
- out << index->getName();
- out << " = ";
- //Create copy
- buffer << *index;
- buffer >> c;
- out << profit[index->getId()].calculateProfit(c) << endl;
- index++;
- //Clear buffer
- buffer.str("");
- buffer.clear();
- }
- return out.str();
- }
- int main() {
- using namespace std;
- vector<Company> companies;
- string line;
- //Initializes the vector<Company>
- while (getline(cin, line) && line != "end") {
- istringstream lineIn(line);
- Company c;
- lineIn >> c;
- companies.push_back(c);
- }
- map<int, ProfitCalculator> profitCalculatorsByCompany;
- line = "";
- while (getline(cin, line) && line != "end") {
- istringstream lineIn(line);
- int companyId;
- lineIn >> companyId;
- ProfitCalculator calculator;
- lineIn >> calculator;
- profitCalculatorsByCompany[companyId] = calculator;
- }
- Company* fromInclusive = &companies[0];
- Company* toInclusive = &companies[companies.size() - 1];
- cout << getProfitReport(fromInclusive, toInclusive, profitCalculatorsByCompany) << endl;
- return 0;
- }
- /*
- INPUT
- acme 424242420 : 43000 1000
- softuni_foundation 20140414 : 0 0
- itjoro 878968302 : 100 25
- end
- 878968302 0
- 424242420 10
- 20140414 30
- end
- */
Advertisement
Add Comment
Please, Sign In to add comment