C/C++

Не получается умножить числитель, как и знаменатель.

#include
#include
using namespace std;
int main(int argc, char* argv[])
{
setlocale(LC_ALL, "Russian");
const int max = 100;
int n,v;
cout << "Введите число дробей: ";
cin >> n;
int kmin=1, kmax=1,a[max],b[max], r=1;
cout << "Введите числитель дроби: " << endl;
for (int x = 0; x < n; x++)
cin >> a[x];
cout << "Введите знаменатель дроби: " << endl;
for (int i = 0; i < n; i++)
cin >> b[i];
cout << "Числитель ";
for (int x = 0; x < n; x++)
cout << a[x] << " ";
cout << "\nЗнаменатель";
for (int i = 0; i < n; i++)
cout << b[i] << " ";
for (int i = 0; i < n; i++)
{
r *= b[i];
}
for (int i = 0; i < n; i++) && for (int x = 0; x < n; x++)
{
v = r / b[i];
b[i] *= v;
a[x] *= v;

}
cout << "\nЧислитель ";
for (int x = 0; x < n; x++)
cout << a[x] << " ";
cout << "\nЗнаменатель";
for (int i = 0; i < n; i++)
cout << b[i] << " ";
cout << " \nОбщий знаменатель= " <<r<< endl;
system("pause");
return 0;
}

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
class Fraction {
public:
Fraction() : num(0), den(1) {}
Fraction(long long num, long long den) : num(num), den(den) {
normalize();
}
long long& numerator() {
return num;
}
long long& denominator() {
return den;
}
protected:
long long lcm(long long a, long long b) {
return abs(a * b) / gcd(a, b);
}
private:
long long num;
long long den;
friend bool operator<(const Fraction& a, const Fraction& b) {
return double(a.num) / double(a.den) < double(b.num) / double(b.den);
}
friend ostream& operator<<(ostream& out, const Fraction& frac) {
return out << frac.num << "/" << frac.den;
}
void normalize() {
auto a = gcd(num, den);
if (a) {
num /= a;
den /= a;
if (den < 0) {
num *= -1;
den *= -1;
}
}
}
long long gcd(long long a, long long b) {
a = abs(a);
if (a) {
b = abs(b);
while (a != b) {
if (a > b) swap(a, b);
b -= a;
}
}
return a;
}
};
long long integer(const char* msg) {
cout << msg;
long long value;
cin >> value;
cin.ignore(cin.rdbuf()->in_avail());
return value;
}
class Fractions : public Fraction {
public:
Fractions(const vector<Fraction>& box) : box(box) {}
void common() {
auto m = lcm(box[0].denominator(), box[1].denominator());
for (auto i = 2U; i < box.size(); ++i) m = lcm(m, box[i].denominator());
for (auto& x : box) {
auto n = m / x.denominator();
x.numerator() *= n;
x.denominator() = m;
}
}
void ascending() {
sort(box.begin(), box.end());
}
private:
vector<Fraction> box;
friend ostream& operator<<(ostream& out, const Fractions& fracs) {
for (auto& x : fracs.box) out << x << '\n';
return out;
}
};
Fraction fraction() {
auto num = integer("Числитель: ");
auto den = integer("Знаменатель: ");
puts("");
return { num, den };
}
int main() {
system("chcp 1251 > nul");
auto n = integer("Количество чисел последовательности: ");
puts("");
vector<Fraction> box;
for (auto i = 0U; i < n; ++i) box.emplace_back(fraction());
Fractions fox(box);
fox.common();
fox.ascending();
cout << fox;
system("pause > nul");
}
РО
Рома Ольховой
92 366
Лучший ответ
А я бы все знаменатели привела к их наименьшему общему кратному, а потом бы отсортировали числители "пузырьком" (знаменатели сортировать не надо, они будут ужè одинаковыми !). А при выводе ещё бы поделила числитель и знаменатель на их наибольший общий делитель, приведя дроби таким образом к виду, в котором они были заданы:
#include <iostream>
#define l long long int
using namespace std;
l Abs(l x) { return (x > 0) ? x : -x; }
l Gcd(l x, l y)
{
x = Abs(x);
y = Abs(y);
l z = x % y;
return (z == 0) ? y : Gcd(y, z);
}
int main()
{
int i, j, n, nok = 1;
cout << "n = ?\b";
cin >> n;
l p[n], q[n], t;
for (i = 0; i < n; i++)
{
cout << i + 1 << " » ";
cin >> p[i] >> q[i];
nok *= q[i] / Gcd(nok, q[i]);
}
cout << endl;
for (i = 0; i < n; i++)
{
p[i] *= nok / q[i];
q[i] = nok;
cout << i + 1 << " » " << p[i]
<< '/' << q[i] << endl;
}
for (i = 0; i < n; i++)
for (j = n - 1; j > i; j--)
if (p[j - 1] > p[j])
{
t = p[j - 1];
p[j - 1] = p[j];
p[j] = t;
}
cout << endl;
for (i = 0; i < n; i++)
{
t = Gcd(p[i], q[i]);
cout << i + 1 << " » " << p[i]
<< '/' << q[i] << " = " << p[i] / t
<< '/' << q[i] / t << endl;
}
system("pause > nul");
return 0;
}
•°°• Beka •°°• спасибо большое