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

How to do ratio's and greater than and less than in C++

本文关键字:小于 C++ 大于 何做比      更新时间:2023-10-16

我从"编程和原理与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;
}
首先,

您需要删除if(a>b)和if(a <b)之后的分号。>

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

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

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

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

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