需要给以下C++代码添加comments!希望高手帮忙!

来源:百度知道 编辑:UC知道 时间:2024/05/17 08:38:18
还需要添加两句代码 一个是确保输入的词组不为空 还有一个是确保输出的数字最少等于2 需要这段程序的comments是有关于程序要求和必须确保的条件的(也就是新添加的两句代码)还有就是关于表示loop不变量的comments以及 关于两个逻辑性质的 1.在代码中什么时候判断这次是由a-z 还是反过来由z-a.2.怎么判断最后返回的结果是对的而不是“退一位的” 希望哪位高手帮帮我!感激不尽(财富值目前为0实在对不起大家但真的很需要这个 谢谢!)

#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