C++의 비율 조작 | 세트 1(산술)
C++를 사용하면 분수에 대한 덧셈 뺄셈 곱셈과 나눗셈 연산을 수행할 수 있습니다. 비율을 추가하는 한 가지 방법은 다음 문서에서 설명합니다. 두 개의 분수를 더하는 프로그램입니다. 여기서 사용된 방법은 지루하고 시간이 오래 걸리는 것을 극복하기 위해 C++에서 더 나은 방법이 도입되었습니다. 그만큼
이 기사에서는 다음에 대해 논의할 것입니다. C++의 산술 비율 조작. 다음 기능이 사용됩니다:
- 비율_추가
- 비율_빼기
- 비율_곱하기
- 비율_나누기
1) 비율_추가: 이 템플릿 별칭은 다음과 같은 용도로 사용됩니다. 두 가지 비율을 추가 그리고 반환 가장 간단한 형태로 결과 . 두 멤버 상수를 반환합니다. 에 그리고 그것 분자와 분모를 나타냅니다.
2) 비율_빼기: 이 템플릿 별칭은 다음과 같은 용도로 사용됩니다. 두 비율을 빼세요 그리고 반환 가장 간단한 형태로 결과 . 두 멤버 상수를 반환합니다. 에 그리고 그것 분자와 분모를 나타냅니다. 그것 ratio1에서 ratio2를 뺍니다. .
// 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 ; }
산출
The sum of ratios is : 2/1 The difference of ratios is : 1/2
3. 비율_곱하기: 이 템플릿 별칭은 다음과 같은 용도로 사용됩니다. 두 비율을 곱하다 그리고 반환 가장 간단한 형태로 결과 . 두 멤버 상수를 반환합니다. 에 그리고 그것 분자와 분모를 나타냅니다.
4. 비율_나누기: 이 템플릿 별칭은 다음과 같은 용도로 사용됩니다. 두 비율로 나누기 그리고 반환 가장 간단한 형태로 결과 . 두 멤버 상수를 반환합니다. 에 그리고 그것 분자와 분모를 나타냅니다. 그것 ratio1을 ratio2로 나눕니다. .
// 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 ; }
산출
The product of ratios is : 15/16 The division of ratios is : 5/3
다음 세트에 대해서는 이 문서를 참조하십시오:
- C++의 비율 조작 | 세트 2 (비교)