-
Notifications
You must be signed in to change notification settings - Fork 0
/
structfile.cpp
230 lines (227 loc) · 3.68 KB
/
structfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include<iostream>
#include<fstream>
using namespace std;
typedef struct PhanSo
{
int tu;
int mau;
}PS;
void InPhanSo(PS a)
{
cout<<"\n"<<a.tu<<"/"<<a.mau;
}
void DocFileVaoMang(PS a[100],int &n)
{
fstream FileIn;
FileIn.open("C:\\Users\\MSI\\Desktop\\ps.txt",ios_base::in);
if(FileIn.fail()) cout<<"File nay khong ton tai";
//int i=0;
n=-1;
while (!FileIn.eof())
{
FileIn>>a[++n].tu;
FileIn>>a[n].mau;
cout<<n;
}
//cout<<i<<"\t"<<n;
//cout<<a[3].tu<<"\t"<<a[3].mau;
FileIn.close();
}
XuatMangPhanSo(PS a[100],int n)
{
for(int i=0;i<n;i++)
{
InPhanSo(a[i]);
}
}
int UCLN(int a, int b)
{
while (a * b != 0)
{
if (a > b)
a %= b;
else
b %= a;
}
return a + b;
}
double Divide(PS a)
{
double c;
c=(float)a.tu/a.mau;
return c;
}
PS rutGon(PS a) // can tra ve phan so nen ta dung ham ham kieu struct tu dinh nghia
{
PS c;
c.tu = a.tu / UCLN(a.tu, a.mau);
c.mau = a.mau / UCLN(a.tu, a.mau);
return c;
}
void TimMax(PS a[100],int &n)
{
int vt;
PS Max=a[0];
for(int i=0;i<n;i++)
{
if(Divide(a[i])>Divide(Max))
{
Max=a[i];
vt=i;
}
}
cout<<"\nPhan so Max:";
InPhanSo(Max);
//cout<<"\nMax la:"<<Max;
//cout<<"\nVi tri:"<<vt;
}
void TimMin(PS a[100],int &n)
{
int vt;
double Min=(float)a[0].tu/a[0].mau;
for(int i=0;i<n;i++)
{
if((float)a[i].tu/a[i].mau<Min)
{
Min=(float)a[i].tu/a[i].mau;
vt=i;
}
}
cout<<"\nMin la:"<<Min;
cout<<"\nVi tri:"<<vt;
}
PS cong(PS a, PS b)
{
PS c;
c.tu = a.tu * b.mau + a.mau * b.tu;
c.mau = a.mau * b.mau;
return c;
}
void TinhTong(PS a[100],int &n)
{
PS tong=a[0];
PS b;//bien nay de gan gia tri rut gon
for(int i=1;i<n;i++)
{
//tong.tu+=tong.tu*a[i].mau+tong.mau*a[i].tu;
//tong.mau+=tong.mau*a[i].mau;
tong=cong(tong,a[i]);
}
cout<<"\nTong phan so:";
b=rutGon(tong);
InPhanSo(b);
}
void TimTongMax(PS a[100],int &n)
{
int vt1,vt2;
PS Max=a[0];
PS s;
for(int i=0;i<n-1;i++)
{
//s=(float)a[i].tu/a[i].mau + (float)a[i+1].tu/a[i+1].mau;
s=cong(a[i],a[i+1]);
if(Divide(s)>Divide(Max))
{
Max=s;
vt1=i;
vt2=i+1;
}
}
//cong(a[vt1],a[vt2]);
cout<<"\nTong lon nhat cua 2 phan so la:";
InPhanSo(Max);
cout<<"\nPhan so o vi tri:"<<vt1;
cout<<"\nPhan so o vi tri:"<<vt2;
}
void sapXepTangDan(PS a[100],int n)
{
int i,j;
PS temp;
for(i=0;i<n;i++) // Bubble sort
{
for(j=n-1;j>i;j--)
{
if(Divide(a[j])<Divide(a[j-1]))
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
XuatMangPhanSo(a,n);
}
void Tong2Max(PS a[100], int &n)
{
PS b;
PS max, max2;
if (Divide(a[0]) > Divide(a[1]))
{
max = a[0];
max2 = a[1];
}
else
{
max = a[1];
max2 = a[0];
}
for(int i = 2; i < n; i++)
{
if(Divide(max)<Divide(a[i]))
{
max2 = max;
max = a[i];
} else
if (Divide(max2)<Divide(a[i]))
max2=a[i];
}
//InPhanSo(max);
//InPhanSo(max2);
b=cong(max,max2);
cout<<"Tong cua 2 max la:";
rutGon(b);
InPhanSo(b);
}
void XoaMang(PS a[100],int &n,int k)
{
for(int i=k;i<n-1;i++)
{
a[i]=a[i+1];
}
n--;
}
void XoaMangMauChan(PS a[100],int n)
{
for(int i=0;i<n;i++)
{
if(a[i].mau%2==0)
{
XoaMang(a,n,i);
i--;
}
}
XuatMangPhanSo(a,n);
}
void XuatFile(PS a[100], int n)
{
ofstream asd;
asd.open("C:\\Users\\MSI\\Desktop\\xuat.txt",ios_base::in);
asd<<"In ra phan so: "<<endl;
for (int i=0;i<n;i++)
{
asd<<a[i].tu<<"/";
asd<<a[i].mau<<endl;
}
}
int main()
{
PS a[100];
int n;
// NhapMangPhanSo(a,n);
DocFileVaoMang(a,n);
XuatMangPhanSo(a,n);
XuatFile(a,n);
}
//Xoa cac phan so co mau la chan (2 ham)
//Sap xep mang phan so tang dan ( ham divide )
//Tinh tong cua hai phan so lon nhat