👤

VA ROG FRUMOS AJUTATIMA AM NEVOIE DE EL IN C++

VA ROG FRUMOS AJUTATIMA AM NEVOIE DE EL IN C class=

Răspuns :

Ar trebui sa functioneze. Daca intampini vreo problema lasa un reply si o sa corectez.

#include <iostream>

#include <vector>

#include <algorithm>

#include <fstream>

using namespace std;

struct Product

{

int code, numUnits, unitPrice, totalCost;

 

Product(int, int, int);

 

 

bool operator<(const Product product)

{

 return totalCost < product.totalCost;

}

};

Product::Product(int a, int b, int c)

{

code = a;

numUnits = b;

unitPrice = c;

totalCost = b * c;

}

int main()

{

ifstream fin("marfa.in");

ofstream fout("marfa.out");

int code, numUnits, unitPrice, totalCost;

std::vector<Product> products;

 

// Reading the products and appending only the ones with 'unitPrice' > 10 to 'products'

while(fin >> code >> numUnits >> unitPrice)

{

 fin >> code >> numUnits >> unitPrice;

 Product product(code, numUnits, unitPrice);

 if (product.unitPrice >= 10)

  products.emplace_back(product);

}

// Sorting the products

std::sort(products.begin(), products.end());

 

// Displaying the products

for (auto & product : products)

{

 fout << product.code << " "

      << product.numUnits << " "

      << product.unitPrice << " "

      << product.totalCost << endl;

}

}