C++计算 2 个数字的比例

C++ calculating the ratio of 2 numbers

本文关键字:数字 计算 C++      更新时间:2023-10-16

大家好,我对编程和学习Stroustrup的"使用C++的编程,原理和实践"相当陌生,在第3章结束时,我完全停滞不前,有一个练习,要求您编写一段代码,该代码执行涉及2个数字的大量计算,其中包括查找数字的比率。不幸的是,这本书中根本没有涉及这一点,我正在扯头发试图自己弄清楚,只能找到我小小的大脑的高级代码方法示例。

我目前拥有的代码是:

 double ratio;
    if (val2 > val1)
        ratio = (val2 / val1);
    if (val2 < val1)
        ratio = (val1 / val2);
    cout << "The ratio of " << val1 << " and " << val2 << " is 1:" << ratio << 'n';

这对于等于整个比率(例如 100 和 25)的数字效果很好,但是尽管我将变量"比率"设置为双精度,但在非整数比率的情况下,它会从答案中删除任何小数。谁能告诉我哪里出错了?

当除整数时,结果是整数(使用整数算术):

11 / 2 == 5
11 % 2 == 1 /* remainder */

当划分浮点值时,结果也是浮点数:

11.0 / 2 == 5.5
11 / 2.0 == 5.5
((double) 11) / 2 == 5.5

在您的情况下

 double ratio = (val2 / val1);

您有一个整数除法,只有在执行 Disvison 之后,它的结果才会被转换为 double 。您可以将val2val1声明为double

double val1; 
double val2;

比率的至少一个参数转换为double

double ratio = ((double)val2) / val1; 

结果类型double的事实并不重要,如果原始除法是在整数类型上执行的(截断小数部分)。

因此,要解决您的问题,请:

  • 对输入数字也使用浮点类型
  • 在除法之前将其中一个数字转换为浮点类型

我从Stroustrup的"编程,原理和实践使用C++"中做了整个问题。这是代码,虽然没有评论。

int main()
{
/** --------Numbers-----*/
int val1;
int val2;
double largest; //I'll store here the largest value 
double smallest; //I'll store here the smallest value

cout<< " Enter two Numbers to play withn";
while(cin>> val1>>val2){
    if(val1<val2){
            cout<< "smallest: "<<val1<<endl;
            cout<< "largest: "<<val2<<endl;
           //If the above argument succeeds, largest and smallest will get their values
            largest=val2;  
            smallest=val1;}
    if(val1>val2){
            cout<< "smallest: "<<val2<<endl;
            cout<< "largest: "<<val1<<endl;
          //If the above argument succeeds, largest and smallest will get their values
            largest=val1;
            smallest=val2;}
    int their_sum=val1+val2;
    int their_product=val1*val2;
    int their_diff=val1-val2;
    double ratio1; 
    ratio1=largest/smallest;
    cout<<"Sum: "<<their_sum<<endl;
    cout<<"Difference: "<<their_diff<<endl;
    cout<<"Product: "<<their_product<<endl;
    cout<<"Ratio: "<<ratio1;
                     }
 return 0;
}

这段代码中没有什么新东西,前面的章节已经介绍了所有内容。

如果你需要两个数字的比率,比如 n:m 形式的 a,b(其中 n>=1),那么只需找到 GCD(a,b) 并将 a,b 除以这个结果。

例如:

a=4,b=6; 
GCD(a,b)=2; 
n=4/2=>2 
m=6/2=>3 
so ratio of 4 and 6 is 2:3
 #include<iostream>
 using namespace std;
 class Test
 {
     public:
     void check()
     {
          int x,y;
          cout<<"Enter 1st number";
          cin>>x;
          cout<<"Enter 2nd number";
          cin>>y;
          int a;
          int d=  gcd(x,y);
          cout<< x/d << " : " << y / d << endl;      
     }
      int gcd(int x, int y) // 14, 21
      {
          int d;
          if(y>x)
          {
              y=x+y; 
              x=y-x; 
              y=y-x; 
          }
          for(int i=1; i<=y; i++)
          {
              if(x%i==0 && y%i==0 )
              {
                  d=i;
              }
          }
          return d;
       }
   };

 int main()
 {
     Test t;
     t.check();
     return 0;
 }
#include <iostream>
using namespace std;
int main()
{
int val1,val2;
cout << " Enter two integer values followed by enter" << endl << endl;
cin >> val1;
cin >> val2;
    if(val1 < val2) // To determine which value is larger and which one is smaller
    {
        cout << val1 << " is smaller than" << val2 << endl << endl << "And"                  << val2 << " is larger than " << val1 << endl<<endl;
            }   
    enter code here
    else if( val2 < val1)
    {
        cout<<val2 <<" is smaller than"<< val1<<endl<<endl<<"And"<< val1 << " is larger than "<< val2<< endl << endl;
    }
cout << "The sum of "<< val1<<" and "<<val2<<" is "<< val1+val2<<endl<<endl;
// diplaying the sum of the two numbers
    enter code here
cout << " The difference between "<<val1<< " and "<<val2<< " is " << val1-val2<<endl;
// displays the difference of val2 from val1
cout << " The difference between "<<val2<< " and "<<val1<< " is " << val2-val1<<endl;
// displays thr difference of val1 fromval2
    enter code here
    enter code here
cout << " The product of " <<val1<< " and " << val2<< " is " << val1*val2<< endl<<endl;
// displaying the product of val1 and val2
    enter code here
    enter code here
    enter code here
// now to diplay the ratio of the two numbers
double ratio1;
cout << " The ratio of "<<val1<<" and "<<val2<<" is ";
       if(val1 < val2)
       {
         ratio1= ((double)val2) /val1;
           cout <<  ratio1;
       }
       else if(val1 > val2)
        {
          ratio1= ((double)val1) /val2;
           cout <<  ratio1;
       }
}