logo

מניפולציות יחס ב- C ++ | סט 1 (חשבון)

C ++ מאפשר לנו לבצע כפל חיסור תוספת ופעולות חלוקה על שברים. שיטה אחת להוספת יחסים נדונה במאמר הבא - תוכנית להוסיף שני שברים. השיטה המשמשת כאן היא מייגעת וממושכת כך שתתגבר כי הוצגה שיטה טובה יותר ב- C ++. THE קובץ כותרת ב- C ++ מאפשר לנו לתפעל יחסים באמצעות כינוי תבניות מובנות שונות. קובץ הכותרת הוצג מ- C ++ 11 ואילך.

במאמר זה נדון ב מניפולציות ביחס אריתמטי ב- C ++. משתמשים בפונקציות הבאות:



jvm
  • Ratio_add
  • ratio_subtract
  • ratio_multiply
  • ratio_divide

1) Ratio_add: כינוי תבנית זה משמש הוסף שני יחסים ולהחזיר את לגרום לצורה הפשוטה ביותר ו זה מחזיר קבועים של שני חברים ב וכן זֶה מציין מספר ומכנה.
2) ratio_subtract: כינוי תבנית זה משמש הפחיתו שני יחסי ולהחזיר את לגרום לצורה הפשוטה ביותר ו זה מחזיר קבועים של שני חברים ב וכן זֶה מציין מספר ומכנה. זֶה מחסור יחס 2 מ- ratio1 ו 

CPP
// 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. Ratio_Multiply: כינוי תבנית זה משמש הכפל שני יחסים ולהחזיר את לגרום לצורה הפשוטה ביותר ו זה מחזיר קבועים של שני חברים ב וכן זֶה מציין מספר ומכנה.
4. Ratio_divide: כינוי תבנית זה משמש מחלקים שני יחסים ולהחזיר את לגרום לצורה הפשוטה ביותר ו זה מחזיר קבועים של שני חברים ב וכן זֶה מציין מספר ומכנה. זֶה מחלק את יחס 1 לפי ratio2 ו 

CPP
// 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
  • מניפולציות יחס ב- C ++ | סט 2 (השוואה)