分式化简:如果分子和分母有最大公约数,哪么除以最大公约数,让分式简化。下面代码设计都是围绕这点展开。
具体分式化简代码实现分步:
1.编写一个函数实现分式化简,输入参数两个int&类型(传引用主要是为了将化简后的分子和分母代回),代表分子和分母。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
void reduction(int& fz,int& fm) { if(fm == 0) //分母等于0,就不在化简 return; int r, x = fz, y = fm; while (y != 0) { r = x % y; x = y; y = r; } fz /= x; fm /= x; if (fz==fm) fz=1,fm=1; } |
2.让用户输入要化简的分子和分母
1 2 3 4 5 6 7 8 |
cout << "请输入分子:" <<endl; scanf("%d", &fz); fm=0; while(fm==0){ //输入分母不为0停止 cout << "请输入分母:" <<endl; scanf("%d", &fm); } |
3.计算后输出化简后分子,分母。三种情况分子分母相等,分母等于1,剩于情况。
1 2 3 4 5 6 |
if(fz==fm) cout << "分式化简后:" << 1 <<endl; else if(fm == 1) cout << "分式化简后:" << fz <<endl; else cout << "分式化简后:" << fz << "/" <<fm <<endl; |
分式化简代码:
测试结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
请输入分子: 2 请输入分母: 4 分式化简后:1/2 请输入分子: 4 请输入分母: 2 分式化简后:2 请输入分子: |
This is nicely expressed. . fluconazole tablets
Very good forum posts. Thanks! fluconazole tablets
Regards. I like this. canadianpharmacy
You made your position very effectively!! generic for viagra
Thanks! Helpful stuff. buy provigil
Many thanks, I enjoy this. https://viagradocker.com/
Many thanks, I enjoy it! retin
Information clearly utilized.. walgreens cbd products
Truly a lot of valuable facts! cheap drugs
You reported that well. viagra without a doctor 100 mg
You actually revealed this wonderfully. canadian pharmacy viagra brand
Lovely data. Appreciate it! tamsulosin 0 4 mg
Beneficial posts. With thanks. provigil side effects
This is nicely said. . provigil side effects
Reliable data. Thanks a lot. lisinopril 10mg
Amazing forum posts. Many thanks! doxycycline hyclate 100mg
You’ve made your point. canadian viagra
You actually stated that really well. tamsulosin medication
You’ve made the point! canada pharmaceuticals online