Добавил:
Beka_on
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:системний аналіз / програмування / лабораторні / 12 / lr12
.cpp#include <iostream>
#include <fstream>
#include <string>
#include <limits>
#include <sstream>
using namespace std;
// Функція для розбиття рядка на слова за пробілами
void splitString(const string& line, string& name, int& quantity, double& price) {
istringstream iss(line);
iss >> name >> quantity >> price;
}
int main() {
ifstream file("products.txt");
if (!file.is_open()) {
cerr << "??????? ????????? ?????.\n";
return 1;
}
string name;
int quantity;
double price;
double totalCost = 0;
double maxPrice = numeric_limits<double>::min();
double minPrice = numeric_limits<double>::max();
string mostExpensive, cheapest;
while (!file.eof()) {
string line;
getline(file, line);
if (line.empty()) {
continue;
}
splitString(line, name, quantity, price);
double cost = quantity * price;
totalCost += cost;
if (price > maxPrice) {
maxPrice = price;
mostExpensive = name;
}
if (price < minPrice) {
minPrice = price;
cheapest = name;
}
}
file.close();
cout << "Загальна вартість: " << totalCost << endl;
cout << "Найдорожчий товар: " << mostExpensive << " (" << maxPrice << ")" << endl;
cout << "Найдешевший товар: " << cheapest << " (" << minPrice << ")" << endl;
return 0;
}
Соседние файлы в папке 12