C/C++
Программирование на c++
Написать программу, которая вводит с клавиатуры данные, содержащие информацию: название товара, год его выпуска, цена за единицу и количество и создает массив структур. Подсчитать общую стоимость товаров на складе.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Product {
unsigned short year;
unsigned quantity;
float price;
string name;
Product() : year(1900), quantity(0), price(0) {}
private:
friend istream& operator>>(istream& inp, Product& p) {
cout << "Наименование товара: ";
getline(inp, p.name);
p.year = p.integer(inp, "Год выпуска: ");
p.quantity = p.integer(inp, "Количество: ");
p.price = p.real(inp, "Цена за единицу: ");
return inp;
}
friend ostream& operator<<(ostream& out, const Product& p) {
out << setw(40) << left << p.name << ' '
<< setw(5) << right << p.year << " г. в."
<< setw(8) << p.quantity << ' '
<< setw(10) << p.price << '\n';
return out;
}
static unsigned integer(istream& inp, const char* msg = "") {
auto value = 0U;
cout << msg;
inp >> value;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return value;
}
static float real(istream& inp, const char* msg = "") {
auto value = 0.0F;
cout << msg;
inp >> value;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return value;
}
};
unsigned integer(const char* msg) {
cout << msg;
unsigned value;
cin >> value;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return value;
}
int main() {
system("chcp 1251 > nul");
cout.setf(ios::fixed);
cout.precision(2);
auto n = integer("Количество наименований товара на складе: ");
puts("");
auto goods = new Product[n];
for (auto i = 0U; i < n; ++i) {
cin >> goods[i];
puts("");
}
system("cls");
for (auto i = 0U; i < n; ++i) cout << goods[i];
puts("");
float sum = 0.0F;
for (auto i = 0U; i < n; ++i) sum += goods[i].price * goods[i].quantity;
cout << "Общая стоимость товаров на складе: " << sum << '\n';
system("pause > nul");
}
#include <iomanip>
#include <string>
using namespace std;
struct Product {
unsigned short year;
unsigned quantity;
float price;
string name;
Product() : year(1900), quantity(0), price(0) {}
private:
friend istream& operator>>(istream& inp, Product& p) {
cout << "Наименование товара: ";
getline(inp, p.name);
p.year = p.integer(inp, "Год выпуска: ");
p.quantity = p.integer(inp, "Количество: ");
p.price = p.real(inp, "Цена за единицу: ");
return inp;
}
friend ostream& operator<<(ostream& out, const Product& p) {
out << setw(40) << left << p.name << ' '
<< setw(5) << right << p.year << " г. в."
<< setw(8) << p.quantity << ' '
<< setw(10) << p.price << '\n';
return out;
}
static unsigned integer(istream& inp, const char* msg = "") {
auto value = 0U;
cout << msg;
inp >> value;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return value;
}
static float real(istream& inp, const char* msg = "") {
auto value = 0.0F;
cout << msg;
inp >> value;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return value;
}
};
unsigned integer(const char* msg) {
cout << msg;
unsigned value;
cin >> value;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return value;
}
int main() {
system("chcp 1251 > nul");
cout.setf(ios::fixed);
cout.precision(2);
auto n = integer("Количество наименований товара на складе: ");
puts("");
auto goods = new Product[n];
for (auto i = 0U; i < n; ++i) {
cin >> goods[i];
puts("");
}
system("cls");
for (auto i = 0U; i < n; ++i) cout << goods[i];
puts("");
float sum = 0.0F;
for (auto i = 0U; i < n; ++i) sum += goods[i].price * goods[i].quantity;
cout << "Общая стоимость товаров на складе: " << sum << '\n';
system("pause > nul");
}
Извини, до ООП в своей шараге ещё не дошли
Александр Осинцев
мы тоже, но нам это задали. веселуха
Похожие вопросы
- Программирование на C++
- Программирование на C++
- Помоги написать лабу по программированию на c++
- Помогите решить задачу по программированию на C++
- Программирование на C. Помогите бездарю
- На каком языке программирования написан C (Си)?
- Программирование на c++
- Программирование на C++
- Помогите с программированием на C++
- Задание по программированию язык C/СИ