Mình có đoạn code sau:
Code:
#include<malloc.h>
#include<conio.h>
#include<stdio.h>
typedef int ElementType;
typedef struct PhanSo{
ElementType Tu;
ElementType Mau;
PhanSo *Next;
};
typedef PhanSo *Position;
typedef Position List;
typedef struct ketqua{
Position a;
Position b;
};
/*Tao danh sach rong*/
void MakeNull_List(List &Header){
Header=(PhanSo*)malloc(sizeof( PhanSo));
Header->Next=NULL;
}
//chen 1 phan so
void Insert_List(ElementType X, ElementType Y, Position P, List &L){
Position T;
T=(PhanSo*)malloc(sizeof(PhanSo));
T->Tu=X;
T->Mau=Y;
T->Next=P->Next;
P->Next=T;
}
/*Nhap danh sach*/
void Read_List(List &L){
ElementType x,y;
Position P=L;
int n;
printf("Nhap vao so phan tu: "); scanf("%d",&n);
for (int i=1; i<=n; i++){
printf("Nhap vao phan tu thu %d \n",i);
printf("Tu= ");scanf("%d",&x);
printf("Mau= "); scanf("%d",&y);
while (y==0){
printf("Mau bi loi, mau khong duoc bang khong\n");
printf("Mau= "); scanf("%d",&y);
}
Insert_List(x,y,P,L);
P=P->Next;
}
}
//uoc chung lon nhat
ElementType ucln(ElementType X,ElementType Y){
int i;
if (X>Y){
i=Y;
while (i!=1){
if ((X%i==0)&&(Y%i==0))
return i;
i--;
}
}
else{
i=X;
while (i!=1){
if ((X%i==0)&&(Y%i==0))
return i;
i--;
}
}
return 1;
}
//toi gian phan so
void Toigian(List& L){
ElementType X,Y;
Position P=L;
while (P->Next!=NULL){
X=P->Next->Tu;
Y=P->Next->Mau;
printf("%d\n",ucln(X,Y));
P->Next->Tu=X/ucln(X,Y);
P->Next->Mau=Y/ucln(X,Y);
P=P->Next;
}
}
/*In danh sach ra man hinh*/
void Print_List(List& L){
Position P=L;
while(P->Next!=NULL){
printf("%d/%d ",P->Next->Tu,P->Next->Mau);
P=P->Next;
}
}
//xoa phan tu
void Delete_List(Position P,List& L){
Position T;
if (P->Next!=NULL){
T=P->Next;
P->Next=T->Next;
free(T);
}
}
//tim cap phan so co tich bang 1
void Search1(List& L){
ketqua k;
k.a->Tu=0;
Position P=L;
while (P->Next!=NULL){
Position Q=P->Next;
while (Q->Next!=NULL){
if (P->Next->Tu*Q->Next->Tu==P->Next->Mau*Q->Next->Mau){
k.a=P->Next;
k.b=Q->Next;
}
Q=Q->Next;
}
P=P->Next;
}
if (k.a->Tu!=0){
printf("Hai phan so co tich bang 1 tim duoc\n");
printf("%d/%d va %d/%d",k.a->Tu,k.a->Mau,k.b->Tu,k.b->Mau);
}
else
printf("Khong tim thay");
}
void Search2(List& L){
Position P=L;
int duyet;
while (P->Next!=NULL){
duyet=1;
if (P->Next->Tu>P->Next->Mau){
Delete_List(P,L);
duyet=0;
}
if (duyet)
P=P->Next;
}
}
int main(){
List L;
MakeNull_List(L);
Read_List(L);
Print_List(L);
Search1(L);
Search2(L);
printf("\n");
Print_List(L);
getch();
return 0;
}
Đây là bài về phân số
_Đọc phân số
_In phân số
_Search1(): tìm kiếm 2 phân số nhân nhau =1;
_Search2(): tìm và xóa phân số có tử lớn hơn mẫu.
Bài này mình chạy trong borland cpp thành công, nhưng không hiểu sao chạy trên dev-cpp thì nó báo lỗi của win, không chạy được (ngay phần Search1() )
Làm sao để nó chạy được đúng? Giúp mình với
