24_25

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.8 kB
5
Indexable
Never
#include <stdio.h>

struct phanSo {
	int tuSo;
	int mauSo;
	
	int USCLN(int a, int b) {
    	if (a % b == 0) return b;
    	else return USCLN(b, a % b);
	}
	
	struct phanSo rutGon() {
		int uscln = USCLN(tuSo, mauSo);
		tuSo /= uscln;
		mauSo /= uscln;
	}
   
	int compare(phanSo fr) {
		// a = b then return 0;
		// a < b then return < 0;
		// a > b then return > 0;
		int ts1, ms1;
    	ts1 = tuSo * fr.mauSo - mauSo * fr.tuSo;
    	ms1 = mauSo * fr.mauSo;
    	return ts1 * ms1;
	}
	
	phanSo add(phanSo fr) { 
    	phanSo result = {(tuSo * fr.mauSo) + (fr.tuSo * mauSo), mauSo * fr.mauSo};
    	return result;
	}
		
};


void readFile(phanSo arr[1000], int &n);
void cau24(phanSo arr[1000], int n);
void cau25(phanSo arr[1000], int n);

int main() {
	int n;
	phanSo arr[1000];
	readFile(arr, n);
	cau24(arr, n);
	cau25(arr, n);
	return 0;
}

void readFile(phanSo arr[1000], int &n) {
	FILE *f;
	f = fopen("fraction.inp", "rt");

	fscanf(f, "%d", &n);
	for (int i = 0; i < n; i++) {
		fscanf(f, "%d %d", &arr[i].tuSo, &arr[i].mauSo);
	}
	fclose(f);
}

void cau24(phanSo arr[1000], int n) {
	for (int i = 0; i < n - 1; i++)
		for (int j = i + 1; j < n; j++)
			if(arr[i].compare(arr[j]) > 0) {
				phanSo ps = arr[i];
				arr[i] = arr[j];
				arr[j] = ps;
			}
			
	FILE *f;
	f = fopen("fraction.out", "wt");
	fprintf(f, "Bai 24:  ");
	for (int i = 0; i < n; i++) {
		fprintf(f, "%d/%d  ", arr[i].tuSo, arr[i].mauSo);
	}
	fprintf(f, "\n");
	fclose(f);
}

void cau25(phanSo arr[1000], int n) {
	phanSo s = {s.tuSo = 0, s.mauSo = 1};
	for (int i = 0; i < n; i++) {
		s = s.add(arr[i]);
	}
	
	FILE *f;
	f = fopen("fraction.out", "a");
	fprintf(f, "Bai 25:  ");
	s.rutGon();
	fprintf(f, "%d/%d", s.tuSo, s.mauSo);
	fclose(f);
}