简单平均计算器c++不能正常工作
Simple average calculator c++ not working correctly
大家好,我是c++新手,但对java有一些很好的经验。
我想做一个简单的程序,需要5个整数(作为裁判分数)。最大和最小的分数被丢弃,必须从剩下的中间3个分数中计算平均值。
我认为这将是相当简单的,但由于某些原因,我的程序总是给我一个比它应该的稍微大一点的答案。
我还应该说明,答案可以是实数,而输入必须是整数。
下面是我到目前为止得到的,它几乎完成了,但有一些隐藏的学生错误,我没有发现几个小时。
我知道可能有其他更有效的方法,但这是我能想到的最好的方法,因为我才刚刚开始使用c++。
任何帮助将非常感激!!
#include <iostream>
using namespace std;
int getSmallest(int first, int second, int third, int fourth, int fifth);
int getLargest(int first, int second, int third, int fourth, int fifth);
double calculateAverage(int largest, int smallest, int sum);
int main()
{
int first, second, third, fourth, fifth;
int smallest, largest, sum;
//double ave;
//read input of 5 scores from judges
cin >> first;
cin >> second;
cin >> third;
cin >> fourth;
cin >> fifth;
smallest = getSmallest ( first, second, third, fourth, fifth );
largest = getLargest ( first, second, third, fourth, fifth );
sum = (first + second + third + fourth + fifth);
//ave = calculateAverage(largest, smallest, sum);
//cout << ave << endl;
cout << "The average is " << (double)calculateAverage(largest, smallest, sum) << endl;
return 0;
}
int getSmallest(int first, int second, int third, int fourth, int fifth)
{
int smallest = 0;
if ( first <= smallest )
{
smallest = first;
}
if ( second <= smallest )
{
smallest = second;
}
if ( third <= smallest )
{
smallest = third;
}
if ( fourth <= smallest )
{
smallest = fourth;
}
if ( fifth <= smallest )
{
smallest = fifth;
}
return smallest;
}
int getLargest(int first, int second, int third, int fourth, int fifth)
{
int largest = 0;
if ( first >= largest )
{
largest = first;
}
if ( second >= largest )
{
largest = second;
}
if ( third >= largest )
{
largest = third;
}
if ( fourth >= largest )
{
largest = fourth;
}
if ( fifth >= largest )
{
largest = fifth;
}
return largest;
}
double calculateAverage(int largest, int smallest, int sum)
{
return (((double)sum) - ((double)largest + (double)smallest)) / 3.0;
}
在getSmallest
例程中,您必须设置
int smallest = INT_MAX;
或smallest
无论您输入什么都将为0。
(包括<climits>
, INT_MAX
可用)
编辑:它工作,但不是有效的。您可以保存一个测试(在这种情况下不需要INT_MAX
),因为第一个条件将始终为真:
int getSmallest(int first, int second, int third, int fourth, int fifth)
{
int smallest = first;
if ( second <= smallest )
{
smallest = second;
}
同样的优化也适用于getLargest
int getLargest(int first, int second, int third, int fourth, int fifth)
{
int largest = first;
if ( second >= largest )
{
largest = second;
}
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- C++程序已停止工作-求解常微分方程