Viết chương trình nhập vào hai phân số, tìm phân số lớn nhất và xuất kết quả.
//tìm phân số lớn nhất
#include<iostream>
using namespace std;
struct phanso
{
int tu,mau;
};
void nhap(phanso &x);
void xuat(phanso x);
phanso max(phanso x,phanso y);
void main()
{
phanso x,y,z;
cout<<"\nNHAP 2 PHAN SO:\nnhap phan so 1:";
nhap(x);
cout<<"\nnhap phan so 2:";
nhap(y);
cout<<"\nphan so lon nhat la: ";
z=max(x,y);
xuat(z);
system("pause");
}
phanso max(phanso x,phanso y)
{
int a,b;
a=x.tu*y.mau;
b=y.tu*x.mau;
if(a>b) return x;
return y;
}
void xuat(phanso x)
{
cout<<x.tu<<"/"<<x.mau;
}
void nhap(phanso &x)
{
char s[100],c[100];
int i,j,kt,d,dem;
do{
kt=0;
cout<<"\nnhap phan so dang a/b: ";
gets(s);
dem=0;d=0;
for(i=0;i<strlen(s);i++)
{
if(s[i]=='/' || (s[i]>='0' && s[i]<='9')) dem++;
if(s[i]=='/') d++;
}
if(dem!=strlen(s) || d!=1)
{
kt=1;
cout<<"\nloi dinh dang!";
continue;
}
i=0;
while(s[i]!='/') c[i]=s[i++];
c[i]='\0';
x.tu=atoi(c);
d=0;
for(j=i+1;j<strlen(s);j++) c[d++]=s[j];
c[d]='\0';
x.mau=atoi(c);
if(x.mau==0)
{
kt=1;
cout<<"\nloi toan hoc!";
}
}while(kt);
}
Wednesday, March 11, 2015
Home »
bai tap c
,
lap trinh c
» Viết chương trình nhập vào hai phân số, tìm phân số lớn nhất và xuất kết quả.
Viết chương trình nhập vào hai phân số, tìm phân số lớn nhất và xuất kết quả.
Related Posts:
hoán vị 1 số trong lập trình c//hoán vị các phần tử trong mảng , hoán vị 1 số nhập từ bàn phím trong lập trình c//ví dụ 123=>6 hoán vị#include<stdio.h>#include<conio.h>#include<stdlib.h>void nhap(int *a,int n){ for(int i=0;i<n;i++)… Read More
tạo 1 file trực tiếp từ bàn phím//tạo 1 file trực tiếp từ bàn phím, quá trình dừng lại khi ấn phím enter#include<stdio.h>void main(){ printf("ten file kem duong dan:"); char s[67]; char c; gets(s); FILE *t; t=fopen(s,"w"); if(t==NULL) printf("error");… Read More
đọc các chuổi ký tự từ tập tin và in nó lên màn hình//đọc các chuổi ký tự từ tập tin và in nó lên màn hình#include<stdio.h>#include<stdlib.h>void main(){ FILE *t; char s[100]; t=fopen("E:\\toan.txt","r"); if(t==NULL) { printf("error");exit(0); } while(fgets(s,100,… Read More
nhập 10 số thực vào 1 file văn bản có tên input//nhập 10 số thực vào 1 file văn bản cso tên input#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(){ FILE *t; float a; t=fopen("E:\\input.txt","w"); if(t!=NULL) { for(int i=1;i<=10;i++) { … Read More
in nội dung file lên màn hình//in nội dung file lên màn hình#include<stdio.h>#include<conio.h>#include<stdlib.h>int main(){ FILE *t; char s[100]; t=fopen("E:\\text.txt","r"); if(t==NULL) { printf("khong mo duoc\n"); exit(0);//ket thuc… Read More