C++允许我们对分数进行加法、减法、乘法和除法运算。下面的文章讨论了一种添加比率的方法—— 程序将两个分数相加。 这里使用的方法冗长冗长,因此克服了C++中引入了一种更好的方法。这个
在本文中,我们将讨论 C++中的算术比率运算 使用以下功能:
- 比率
- 比率减去
- 倍率
- 比率除以
1) 比率_加上: 此模板别名用于 加上两个比率 并归还 以最简单的形式产生 .它返回两个成员常量, 号码 , 和 兽穴 表示分子和分母。
2) 比率_减去: 此模板别名用于 减去两个比率 并归还 以最简单的形式产生 .它返回两个成员常量, 号码 , 和 兽穴 表示分子和分母。信息技术 从比率1中减去比率2 .
CPP
// C++ program to demonstrate the working of // ratio_add and ratio_subtract #include <iostream> #include <ratio> // for ratio manipulation using namespace std; int main() { // Declaring ratios typedef ratio<5, 4> ratio1; typedef ratio<3, 4> ratio2; // Summing two ratios typedef ratio_add<ratio1, ratio2> sum; // Subtracting two ratios typedef ratio_subtract<ratio1, ratio2> diff; // printing sum of ratios cout << "The sum of ratios is : " ; cout << sum::num << "/" << sum::den; cout << endl; // printing difference of ratios cout << "The difference of ratios is : " ; cout << diff::num << "/" << diff::den; cout << endl; return 0; } |
The sum of ratios is : 2/1The difference of ratios is : 1/2
3.比率乘以: 此模板别名用于 乘以两个比率 并归还 以最简单的形式产生 .它返回两个成员常量, 号码 , 和 兽穴 表示分子和分母。
4.比率除以: 此模板别名用于 除以两个比率 并归还 以最简单的形式产生 .它返回两个成员常量, 号码 和 兽穴 表示分子和分母。信息技术 将比率1除以比率2 .
CPP
// C++ program to demonstrate the working of // ratio_multiply and ratio_divide #include <iostream> #include <ratio> // for ratio manipulation using namespace std; int main() { // Declaring ratios typedef ratio<5, 4> ratio1; typedef ratio<3, 4> ratio2; // Multiplying two ratios typedef ratio_multiply<ratio1, ratio2> prod; // Dividing two ratios typedef ratio_divide<ratio1, ratio2> div ; // printing product of ratios cout << "The product of ratios is : " ; cout << prod::num << "/" << prod::den; cout << endl; // printing division of ratios cout << "The division of ratios is : " ; cout << div ::num << "/" << div ::den; cout << endl; return 0; } |
The product of ratios is : 15/16The division of ratios is : 5/3
下一集请参阅本文:
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。