logic help for smallest/largest value

大憨熊 提交于 2019-12-13 09:45:35

问题


I went thru so many version of the algorithm to sort smallest and largest that my brain is fried. The book up to this point and searching online haven't helped at all. I'm having difficulties at saving the last. I used 3 in, 10 cm and 5 cm as test cases. Entering 3 in first, becomes the largest, entering 5 cm second becomes smallest and then 10 cm becomes smallest again. Tried different version for over 2 hours, even re-wrote that entire section. In the book Programming Principles and Practices using C++, its in the review section, before that I cant find anything to help me out.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int main()
{
    vector<double>all_meters;

    double smallest= 0,print_smallest, largest = 0,print_largest, num = 0;
    string unit, s_input, num_s_input, small_unit, large_unit;

    while(cin.good()){

        cout << "\n\t\t\t\tEnter '|' to exit.\n\n";
        cout << "\t\tNumber to compare followed by white space and unit:";
        cin >> num_s_input;

        if(num_s_input.compare("|") == 0 || (s_input.compare("|") == 0)){
            double sum = 0;
            for (double x : all_meters) sum+=x;
            cout << "Sum: " << setprecision(4) << sum <<  "m\n";
            cout << "Smallest number: " << print_smallest << small_unit << endl
                 << "Largest number: " << print_largest << large_unit << endl
                 << "Total number of values: " << all_meters.size() << endl
                 << "All the entered numbers converted to meters are: \n";
            for (double i = 0; i<all_meters.size(); ++i){
                cout << all_meters[i] << setprecision(2) <<"m ";
            }
            cout << "\nAlright now, goodbye then !\n" << endl;
            break;
            }
        else{
            cin >> s_input;
            num = strtod(num_s_input.c_str(), NULL);
            unit = s_input;

            double meter = 0;
            if(unit=="cm"){
                    meter = num / 100;}

            else if(unit=="in"){
                    meter = num / 39.370;}

            else if(unit=="ft"){
                    meter = num / 3.2808;}

            else if(unit=="m"){
                    meter = num;}

            else {
                cout << "\n\tYou entered wrong unit!\t\n";}

            if(largest==0){
                largest = meter;
                print_largest = num;
                large_unit = unit;
                cout << num << unit << " largest so far.\n";
            }
            else if(smallest==0&&meter<largest){
                smallest = meter;
                print_smallest = num;
                small_unit = unit;
                cout << num << unit << " smallest so far.\n";
            }
            else if(largest<meter){
                largest = meter;
                print_largest = num;
                large_unit = unit;
                cout << num << unit << " largest so far.\n";
            }
            else if(smallest>meter){
                smallest = meter;
                print_smallest = num;
                small_unit = unit;
                cout << num << unit << " smallest so far.\n";
            }
            all_meters.push_back(meter);
            sort(all_meters.begin(),all_meters.end());
        }
    }
}

Managed to solve it without using limit, added the new changes to the code. Thanks for the help guys !


回答1:


You need to choose a standard unit of measure. The question suggests meters, so use that (you can use float or double for this, depending on what precision you need).

The problem is then simple, create some variables for the sum, smallest seen, and largest seen, the for each new input, convert to the standard format, and update the variables.

The solution (to get you started, not working code) might look something like this:

// You can represent the different types of units as integers
float convertToMeters(float unconvertedValue, int unit) {
  // Convert unconvertedValue based on unit
}

float smallest = std::numeric_limits<float>::max();
float largest  = std::numeric_limits<float>::lowest();
float sum      = 0.0f;

// Update for each new input
while (new_input) {
  float convertedValue = convertToMeters(new_value, unit);

  // Update total
  sum += convertedValue;

  // Update smallest and largest
  if (convertedValue > largest) largest = convertedValue;
  else if (convertedValue < smallest) smallest = convertedValue;
}

As Nathan mentioned, #include <limits> for the limits.




回答2:


More than likely your problem comes from the fact that you are initializing smallest to 0. If you never enter anything smaller than 0 then smallest will never change.

When finding the minimum and maximum values you want to set the the initial value to the largest or smallest number respectively that it can hold. So in this case we would use

double smallest = std::numeric_limits<double>::max(); 
double largest = std::numeric_limits<double>::lowest()
double num = 0;

This was anything in your data set should be less than smallest and everything should be grater than largest.

This does require #include <limits>



来源:https://stackoverflow.com/questions/35046549/logic-help-for-smallest-largest-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!