Professional Documents
Culture Documents
K1
K1
class fraction {
int numerator, denominator;
static int counter;
public:
fraction(int num = 1, int den = 1) : numerator(num), denominator(den) {}
void input();
void output();
int divider(int numerator, int denominator);
fraction addition(fraction second);
fraction subtraction(fraction second);
fraction multiplication(fraction second);
fraction division(fraction second);
int intdivision(fraction second);
fraction remainder(fraction second);
void countnext() { counter += 1; }
void countcout() { cout << counter; }
};
int fraction::counter = 0;
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
srand(time(NULL));
cout << "Лабораторна робота K1\n";
cout << "Калькулятор простих дробів.\n";
fence();
fraction first, second, result;
char ans, stop, zero;
for (; ; ) {
cout << "Введіть перший дріб:\n"; first.input();
cout << "Введіть другий дріб:\n"; second.input();
fence();
ans = menu();
fence();
first.output();
switch (ans) {
case '1': cout << " + "; break;
case '2': cout << " - "; break;
case '3': cout << " * "; break;
case '4': cout << " / "; break;
case '5': cout << " // "; break;
case '6': cout << " % "; break;
}
second.output(); cout << " = ";
switch (ans) {
case '1': result = first.addition(second); break;
case '2': result = first.subtraction(second); break;
case '3': result = first.multiplication(second); break;
case '4': result = first.division(second); break;
case '5': cout << first.intdivision(second); break;
case '6': result = first.remainder(second); break;
}
if (ans != '5') {
result.output();
}
cout << endl;
result.countnext();
fence();
cout << "Продовжити роботу?\n"
"[1] Так.\n"
"[2] Ні.\n";
do {
cout << "Ваш вибір (1 - 2): ";
cin >> stop;
} while (!strchr("12", stop));
switch (stop) {
case '1': fence(); break;
case '2': fence();
cout << "Загальна кількість операцій: "; result.countcout(); cout << endl;
cout << "Завершення роботи.\n";
fence(); return 0;
}
}
}
char menu() {
char ans;
cout << "Оберіть операцію.\n";
cout << "[1] Додавання.\n"
"[2] Віднімання.\n"
"[3] Множення.\n"
"[4] Ділення.\n"
"[5] Цілочисельне ділення.\n"
"[6] Визначення остачі.\n";
do {
cout << "Ваш вибір (1 - 6): ";
cin >> ans;
} while (!strchr("123456", ans));
return ans;
}
void fence() {
cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n";
}
void fraction::input() {
do {
cout << "Чисельник (Від 1 до Неск.): "; cin >> numerator;
} while (numerator < 1);
do {
cout << "Знаменник (Від 1 до Неск.): "; cin >> denominator;
} while (denominator < 1);
}
void fraction::output() {
int div = divider(numerator, denominator);
numerator /= div;
denominator /= div;
if (numerator == 0) {
cout << numerator;
}
else {
cout << numerator << "/" << denominator;
}
}