如何做比率和大于和小于C++
How to do ratio's and greater than and less than in 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;
}
首先,
您需要删除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)));
}
相关文章:
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 从值小于256的uint16到uint8的Endian安全转换
- 将stl字符串缩小到小于15个字符的容量
- 如何检查两个 std::向量在小于 O(n) 的时间复杂度内是否相等
- 为什么我只能在C++中使用可变长度数组分配小于 10 mb 的内存?
- 如何为字符串生成唯一但一致的 N 位哈希(小于 64 位)?
- sizeof(size_t) 可以小于 sizeof(int) 吗?
- C++:如何计算二叉树中其值模块高度小于 2 的节点数?
- unique_ptr < 0 或小于运算符做什么?
- C++模板元编程(版本小于 17,最好是 11)
- 从小于或等于某个 N 的数字列表中最小化或找到 n 个理想的子集和
- c++curl返回413请求实体过大,但是post大小远小于max大小
- 如何在随机数列表中查找大于或小于的元素
- GetWindowRect()返回的大小小于游戏的实际可见窗口的可能原因是什么
- 创建一个循环,该循环将输出大于零且小于 60(不包括 60)的所有 5 的倍数
- std::p artition 的问题,用于分离小于枢轴的元素
- 我可以写出小于 -0.5 两个 ulps 的常量表达式双精度吗?
- 数字小于机器精度的安全算术?
- 每个元素小于某个特定数字的子数组的总和
- 以小于一毫秒的精度计算时间的功能