avatar
Untitled

Guest 616 1st Mar, 2021

#include <iostream>
#include<fstream>

using namespace std;

struct Fraction {
    int num;
    int denom;
};

void readFile(Fraction a[100],int &n) {
    ifstream fin;

    fin.open("input.txt");
    if (!fin.is_open()) {
        cout << "Can not open file to read" << endl;
        return;
    }
    Fraction temp;

    fin >> n;
    fin.ignore(1, '\n');

    for (int i = 0;i < n;i++) {
        fin >> temp.num >> temp.denom;// >> endl;
        fin.ignore(1, '\n');
        a[i] = temp;
    }
    fin.close();
}

int compareFraction(Fraction a, Fraction b) {
    float t1, t2;

    t1 = (float) a.num / a.denom;
    t2 = (float)b.num / b.denom;

    if (t1 > t2) {
        return 1;
    }
    else if (t1 == t2) {
        return 0;
    }
    else
        return -1;
}
void ArrayAscending(Fraction a[100], int n) {
    for (int i = 0;i < n - 1;i++) {
        for (int j = i + 1;j < n;j++) {
            if (compareFraction(a[i], a[j]) == 1) {
                Fraction temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
}
void printArray(Fraction a[100], int n) {
    for (int i = 0;i < n;i++) {
        cout << a[i].num << "/" << a[i].denom << endl;
    }
}
int main()
{
    Fraction a[100];
    int n;

    readFile(a, n);
    ArrayAscending(a, n);
    printArray(a, n);
}
Markup
Description

No description

To share this paste please copy this url and send to your friends
RAW Paste Data