Files
dz3/main.cpp
2024-11-05 11:34:17 +03:00

148 lines
4.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
#include <fstream>
#include <string>
#include <cmath> // для функции pow
using namespace std;
void z1() {
float s, n, p, r;
cout << "Вы взяли в кредит сумму s, под процент p, на n лет : " << endl;
cin >> s >> p >> n;
if ((s <= 0) || (n < 0) || (p < 0)) {
cout << "ошибка" << endl;
}
else if (p == 0) {
cout << "m = " << s / (12 * n) << endl;
}
else {
r = p / 100;
cout << "m = " << (s * r * pow((1 + r), n)) / (12 * (pow((1 + r), n) - 1)) << endl;
}
}
void z2() {
float s, m, n;
cout << "Введите s, m, n: ";
cin >> s >> m >> n;
bool flag = false;
double l = 0;
double r = 1000000;
double mid;
while ((l <= r) && (flag != true)) {
mid = (l + r) / 2;
double R = mid / 100;
double mt = (s * R * pow(1 + R, n)) / (12 * (pow(1 + R, n) - 1));
if (abs(mt - m) < 0.0001) {
flag = true;
break;
}
else if (mt < m) {
l = mid;
}
else {
r = mid;
}
}
if (flag) cout << "Найден процент: " << mid << endl;
else cout << "Процент не найден" << endl;
}
void z3() {
string filename = "output.txt"; // Имя файла
ofstream outfile; // Открытие файла для записи
// Открытие файла с проверкой
outfile.open(filename);
if (!outfile.is_open()) {
cerr << "Ошибка: Не удалось открыть файл для записи!" << endl;
return;
}
string lines;
// Запись в файл
cout << "Первая строка:\n";
cin >> lines;
outfile << lines << endl;
// Закрытие файла после записи
outfile.close();
cout << "Данные успешно записаны в файл." << endl;
// Переоткрытие файла для чтения
ifstream infile;
infile.open(filename);
// Проверка на открытие
if (!infile.is_open()) {
cerr << "Ошибка: Не удалось открыть файл для чтения!" << endl;
return;
}
// Чтение и вывод содержимого файла
string line;
cout << "Содержимое файла:\n";
while (getline(infile, line)) {
cout << line << endl;
}
// Закрытие файла после чтения
infile.close();
}
// Задание 4 в отдельном файле
void sorting(string& s) {
int n = s.length();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
char c1 = tolower(s[j]);
char c2 = tolower(s[j + 1]);
if (c1 > c2 || (c1 == c2 && s[j] > s[j + 1])) {
swap(s[j], s[j + 1]);
}
}
}
}
void z5() {
string s;
cout << "Введите строку: "<<endl;
cin >> s;
sorting(s); // Сортируем строку
cout << "Отсортированная строка: " << s << endl;
}
int main() {
int choice;
do {
cout << "Выберите задание (1-5, 0 для выхода):" << endl;
cout << "1. Расчет ежемесячных платежей" << endl;
cout << "2. Поиск процента по ежемесячным платежам" << endl;
cout << "3. Работа с файлами (запись и чтение)" << endl;
cout << "5. Сортировка строки" << endl;
cout << "0. Выход" << endl;
cin >> choice;
switch (choice) {
case 1:
z1();
break;
case 2:
z2();
break;
case 3:
z3();
break;
case 5:
z5();
break;
case 0:
cout << "Выход из программы." << endl;
break;
default:
cout << "Неверный выбор, попробуйте снова." << endl;
}
} while (choice != 0);
return 0;
}