如何做比率大于和小于C ++

时间:2012-11-24 05:42:38

标签: c++

我正在编写这个程序来自“使用C ++进行编程和原理”,我需要编写一个程序,该程序采用过于整数并找到总和,差异,大于和小于值和比率。

出于某种原因,我无法获得大于和小于工作。它实际上并不执行该功能。它只是简单地打印数字,即:4将小于2。

我的第二个问题是我如何编写一个能为我做比率的等式?

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open() {char ch;cin>>ch;}
int main()
{
    int a;
    int b;
    cout<<"Enter two values.\n";
    cin>>a; cin>>b;
    if (a > b);cout<< a << " Is greater than " << b << "\n";
    if(a < b);cout<< a << " Is less than " << b << "\n";
    cout<<a << " plus " << b << " is " << a+b << "\n"; 
    cout<<a << " minus " << b << " is " << a-b << "\n";
    keep_window_open();
    return 0;
}

2 个答案:

答案 0 :(得分:2)

首先,您需要在if(a&gt; b)和if(a&lt; b)之后删除半冒号。

要做比率,我建议找到a和b之间的最大公因子,然后执行以下行:

cout<<"Ratio of "<<a<<" and "<<b<<" is "<<(a/gcd)<<":"<<(b/gcd);

其中gcd是a和b的最大公因子。

答案 1 :(得分:0)

我认为以下代码段会回答您的所有问题。

#include<iostream.h>

void main()
  {
  float a,b;
  cout<<"Enter 2 numbers";
  cin>>a>>b;

  cout<<"Plus = "<<(a+b)<<"\n";
  cout<<"Minus = "<<(a-b)<<"\n";
  cout<<"Greater = "<<((a>b)?a:b)<<"\n";
  cout<<"Smaller = "<<((a<b)?a:b)<<"\n";
  cout<<"Ratio = 1:"<<(1/(((a<b)?a:b)/((a>b)?a:b)));
  }