需要给以下C++代码添加comments!希望高手帮忙!
来源:百度知道 编辑:UC知道 时间:2024/05/17 08:38:18
#include <iostream>
#include <string>
using namespace std;
int seq(const string &str)
{
if(str.size()<=2)
return str.size();
int count=2;
bool bigger;
if(str[0]<str[1]) bigger=false;
else bigger=true;
for(int i=1;i<str.size()-1;i++)
{
if(str[i]<=str[i+1]&&!bigger||str[i]>=str[i+1]&&bigger)
count++;
else
{
return count;
}
}
return count;
}
int main()
{
cout<<seq("addendum");
cout<<seq("troop");
cout<<seq("adder");
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int seq(const string &str)
{
if(str.size()<2)
cout << "input words must have at lease two characters and can't be empty." << endl; //to require input string be nonempty and at least have two characters.
else if(str.size()==2)
return str.size();
else
{
int count=2; //ensure that the output number is positive and at least 2.
bool bigger;
if(str[0]<str[1]) bigger=false; // using bool bigger to deside upward or downward.
else bigger=true;
for(int i=1;i<str.size()-1;i++) //here is a for loop use to count the maximum number of leading characters of str that are in alphabetical order.
//using i<str.size()-1 to make sure the return number is right.
{
if(str[i]<=str[i+1]&&!bigger||str[i]>=str[i+1]&&bigger) // using for statment to deside the input string is upward or dow