简体   繁体   中英

C++ Comparing consecutive integers of a string

I am writing a program to determine if a given set of integers is increasing, decreasing, or neither.

For example a string of "123456" would be increasing, and "54321" is decreasing.

My solution:

string my_str = "12345";

f = my_str.at(0);
s = my_str.at(1);
t = my_str.at(2);
l = my_str.at(3);
p = my_str.at(4);

if(f<=s<=t<=l<=p){

cout << "The string is increasing" << endl;

}

 if(f>=s>=t>=l>=p){

cout << "The string is decreasing" << endl;

}

Now I am not sure if this were even to work, but I understand the concept I am just having trouble putting this into code on C++. So is this the best way to go about it?

Edit: I understand this code is in-complete its just supposed to be a generalization to help me get a better understanding. The small code I posted prints out increasing regardless of the inputs, and it's not reading the my_str input, I had thought it would just output increasing or decreasing after running the program.

You need to generalize your approach. I mean what if you had a string of length 20? You would write down all the neighboring needed relationships for an increasing sequence one by one? Of course not, since that's a repeated task, a job that we take care with loops!

Here is an example:

#include <iostream>
#include <string>

int main()
{
    std::string str = "543621";
    bool increasing = true;
    for(size_t i = 0; i < str.size() - 1; ++i)
        if(!(str[i] <= str[i + 1])) {
            increasing = false;
            break;
        }
    std::cout << "Increasing: " << increasing << std::endl;

    bool decreasing = true;
    for(size_t i = 0; i < str.size() - 1; ++i)
        if(!(str[i] >= str[i + 1])) {
            decreasing = false;
            break;
        }
    std::cout << "Decreasing: " << decreasing << std::endl;

    if(!increasing && !decreasing)
        std::cout << "Neutral" << std::endl;
}

Output:

Increasing: 0
Decreasing: 0
Neutral

The example first checks for "Increasing" string, then for "Decreasing" and neither of these are true, it assumes that it's neutral (neither).

Of course you could optimize this example to stop checking for "Decreasing", if increasing is set to true after the for loop, but I decided to keep it simple for demonstrative purposes.

You can manage it with only 1 cycle:

#include <iostream>
#include <string>

int main()
{
    std::string str = "543621";
    bool increasing = true;
    bool decreasing = true;
    for(size_t i = 0; i < str.size() - 1 && (increasing || decreasing); ++i)
    {
        if(str[i] > str[i + 1]) {
            increasing = false;
        }
        if(str[i] < str[i + 1]) {
            decreasing = false;
        }
    }
    std::cout << "Increasing: " << increasing << std::endl;
    std::cout << "Decreasing: " << decreasing << std::endl;

    if(!increasing && !decreasing)
        std::cout << "Neutral" << std::endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM