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

时间:2016-07-12 15:44:30

标签: c++

嗨,大家好我是编程和工作的新手,通过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)的数字,但是尽管我设置了变量&#34; ratio&#34;作为一个double,它会在非整数比率的情况下从答案中删除任何小数。谁能告诉我哪里出错?

5 个答案:

答案 0 :(得分:4)

分割整数时,结果是整数(使用整数算术):

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);

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

double val1; 
double val2;

施放至少一个与double的比率参数:

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

答案 1 :(得分:0)

如果对整数类型执行原始除法(截断小数部分),则结果类型为double的事实无关紧要。

所以要解决你的问题,要么:

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

答案 2 :(得分:0)

我从Stroustrup&#34;编程,原理和实践使用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 with\n";
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;
}

此代码中没有任何新内容,前面几章介绍了所有内容。

答案 3 :(得分:0)

如果您只需要两个数字之比,以n:m的形式说a,b(其中n> = 1),则只需找到GCD(a,b)并将结果除以a,b。 / p>

例如:

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

答案 4 :(得分:-2)

#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;
       }
}