Ratio manipulationer i C ++ | Set 1 (aritmetik)
C ++ tillåter oss att utföra multiplikering av multiplikation och uppdelning av tillägg på fraktioner. En metod för att lägga till förhållanden diskuteras i följande artikel - Program för att lägga till två fraktioner. Metoden som används här är tråkig och lång så för att övervinna att en bättre metod infördes i C ++. De
I den här artikeln kommer vi att diskutera Aritmetiska förhållanden manipulationer i C ++. Följande funktioner används:
- förhållande
- förhållande
- förhållande_multiply
- förhållande
1) Ratio_add: Denna mallalias är van vid Lägg till två förhållanden och returnera resultera i den enklaste formen . Den returnerar två-medlemmars konstanter på en och det Betecknar teller och nämnare.
2) Ratio_Subtract: Denna mallalias är van vid subtrahera två förhållanden och returnera resultera i den enklaste formen . Den returnerar två-medlemmars konstanter på en och det Betecknar teller och nämnare. Det Subtrahs Ratio2 från Ratio1 .
// C++ program to demonstrate the working of // ratio_add and ratio_subtract #include #include // 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 ; }
Produktion
The sum of ratios is : 2/1 The difference of ratios is : 1/2
3. Ratio_multiply: Denna mallalias är van vid multiplicera två förhållanden och returnera resultera i den enklaste formen . Den returnerar två-medlemmars konstanter på en och det Betecknar teller och nämnare.
4. Ratio_Divide: Denna mallalias är van vid Dela två förhållanden och returnera resultera i den enklaste formen . Den returnerar två-medlemmars konstanter på en och det Betecknar teller och nämnare. Det Dela Ratio1 efter förhållande2 .
// C++ program to demonstrate the working of // ratio_multiply and ratio_divide #include #include // 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 ; }
Produktion
The product of ratios is : 15/16 The division of ratios is : 5/3
För nästa uppsättning hänvisar du till den här artikeln:
- Ratio manipulationer i C ++ | Set 2 (jämförelse)