Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

c++ - logic help for smallest/largest value

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 << "
				Enter '|' to exit.

";
        cout << "		Number 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
";
            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: 
";
            for (double i = 0; i<all_meters.size(); ++i){
                cout << all_meters[i] << setprecision(2) <<"m ";
            }
            cout << "
Alright now, goodbye then !
" << 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 << "
	You entered wrong unit!	
";}

            if(largest==0){
                largest = meter;
                print_largest = num;
                large_unit = unit;
                cout << num << unit << " largest so far.
";
            }
            else if(smallest==0&&meter<largest){
                smallest = meter;
                print_smallest = num;
                small_unit = unit;
                cout << num << unit << " smallest so far.
";
            }
            else if(largest<meter){
                largest = meter;
                print_largest = num;
                large_unit = unit;
                cout << num << unit << " largest so far.
";
            }
            else if(smallest>meter){
                smallest = meter;
                print_smallest = num;
                small_unit = unit;
                cout << num << unit << " smallest so far.
";
            }
            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 !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...