Обновить main.cpp
This commit is contained in:
138
main.cpp
138
main.cpp
@@ -7,6 +7,7 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <iomanip> // Для форматирования вывода
|
||||
using namespace std;
|
||||
|
||||
// Функция перевода из двоичной строки в десятичное число
|
||||
@@ -210,9 +211,142 @@ void z4_16() {
|
||||
|
||||
cout << "Результат вычислений: y = " << y << endl;
|
||||
}
|
||||
string convertToBase(int number, int base) {
|
||||
string result;
|
||||
const char digits[] = "0123456789";
|
||||
while (number > 0) {
|
||||
result += digits[number % base];
|
||||
number /= base;
|
||||
}
|
||||
reverse(result.begin(), result.end());
|
||||
return result.empty() ? "0" : result; // Если число 0, вернуть "0"
|
||||
}
|
||||
void z5_16() {
|
||||
string inputFile = "input.txt"; // Исходный файл
|
||||
string outputFile = "output.txt"; // Файл для результатов
|
||||
ifstream inFile(inputFile);
|
||||
ofstream outFile(outputFile);
|
||||
|
||||
if (!inFile.is_open()) {
|
||||
cerr << "Не удалось открыть файл " << inputFile << endl;
|
||||
|
||||
}
|
||||
|
||||
if (!outFile.is_open()) {
|
||||
cerr << "Не удалось открыть файл " << outputFile << endl;
|
||||
|
||||
}
|
||||
|
||||
int base;
|
||||
cout << "Введите целевую систему счисления (от 2 до 9): ";
|
||||
cin >> base;
|
||||
|
||||
if (base < 2 || base > 9) {
|
||||
cerr << "Система счисления должна быть в пределах от 2 до 9." << endl;
|
||||
|
||||
}
|
||||
|
||||
string hexNumber;
|
||||
while (inFile >> hexNumber) {
|
||||
int decimalNumber = stoi(hexNumber, nullptr, 16); // Преобразование из 16-й в 10-ю систему
|
||||
string converted = convertToBase(decimalNumber, base);
|
||||
outFile << converted << endl;
|
||||
}
|
||||
|
||||
cout << "Перевод завершен. Результаты сохранены в файл " << outputFile << endl;
|
||||
|
||||
inFile.close();
|
||||
outFile.close();
|
||||
}
|
||||
// Структура для хранения данных о стране
|
||||
struct Country {
|
||||
string name;
|
||||
int gold;
|
||||
int silver;
|
||||
int bronze;
|
||||
int totalMedals;
|
||||
int points;
|
||||
|
||||
// Вычислить общее число медалей и очков
|
||||
void calculate() {
|
||||
totalMedals = gold + silver + bronze;
|
||||
points = gold * 7 + silver * 6 + bronze * 5;
|
||||
}
|
||||
};
|
||||
bool compareByGold(const Country& a, const Country& b) {
|
||||
if (a.gold != b.gold) {
|
||||
return a.gold > b.gold; // Сортировка по золотым медалям
|
||||
}
|
||||
return a.totalMedals > b.totalMedals; // Вторичная сортировка по общему количеству медалей
|
||||
}
|
||||
void z5_22() {
|
||||
vector<Country> countries;
|
||||
int n;
|
||||
|
||||
cout << "Введите количество стран: ";
|
||||
cin >> n;
|
||||
|
||||
// Ввод данных
|
||||
for (int i = 0; i < n; ++i) {
|
||||
Country country;
|
||||
cout << "Введите название страны (пожалуйста на Английском): ";
|
||||
cin >> country.name;
|
||||
cout << "Введите количество золотых, серебряных и бронзовых медалей: ";
|
||||
cin >> country.gold >> country.silver >> country.bronze;
|
||||
country.calculate();
|
||||
countries.push_back(country);
|
||||
}
|
||||
|
||||
// Сортировка стран
|
||||
sort(countries.begin(), countries.end(), compareByGold);
|
||||
|
||||
// Сохранение данных в файл
|
||||
ofstream outFile("olympic_results.txt");
|
||||
if (!outFile) {
|
||||
cerr << "Ошибка открытия файла для записи!" << endl;
|
||||
|
||||
}
|
||||
|
||||
outFile << left << setw(15) << "Страна"
|
||||
<< setw(10) << "Золото"
|
||||
<< setw(10) << "Серебро"
|
||||
<< setw(10) << "Бронза"
|
||||
<< setw(15) << "Всего Медалей"
|
||||
<< setw(10) << "Очки" << endl;
|
||||
|
||||
for (const auto& country : countries) {
|
||||
outFile << left << setw(15) << country.name
|
||||
<< setw(10) << country.gold
|
||||
<< setw(10) << country.silver
|
||||
<< setw(10) << country.bronze
|
||||
<< setw(15) << country.totalMedals
|
||||
<< setw(10) << country.points << endl;
|
||||
}
|
||||
outFile.close();
|
||||
|
||||
// Вывод данных на экран
|
||||
cout << left << setw(15) << "Страна"
|
||||
<< setw(10) << "Золото"
|
||||
<< setw(10) << "Серебро"
|
||||
<< setw(10) << "Бронза"
|
||||
<< setw(15) << "ВсегоМедалей"
|
||||
<< setw(10) << "Очки" << endl;
|
||||
|
||||
for (const auto& country : countries) {
|
||||
cout << left << setw(15) << country.name
|
||||
<< setw(10) << country.gold
|
||||
<< setw(10) << country.silver
|
||||
<< setw(10) << country.bronze
|
||||
<< setw(15) << country.totalMedals
|
||||
<< setw(10) << country.points << endl;
|
||||
}
|
||||
|
||||
cout << "Результаты сохранены в файл 'olympic_results.txt'." << endl;
|
||||
|
||||
}
|
||||
int main() {
|
||||
setlocale(LC_ALL, "");
|
||||
setlocale(LC_ALL, "Russian");
|
||||
/*z1launcher();*/
|
||||
z4_16();
|
||||
z5_22();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user