首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么消除while重复cout

2012-03-12 
如何消除while重复coutRequires: start 0 end 0 0 digit 9//Modifies: nothing//Effects: run

如何消除while重复cout
Requires: start > 0; end > 0; 0 <= digit <= 9
//Modifies: nothing
//Effects: runs the range of numbers from start to end inclusive
// if (end < start), it will swap start and end
// prints the number if any digit of the number is the
// same as 'digit'
// printIfHoldsDigit(5, 10, 7) //prints: 7
// printIfHoldsDigit(5, 30, 8) //prints: 8, 18, 28
// printIfHoldsDigit(1, 30, 2) prints: 2, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 
// and yes you need to print the commas
以上是function的要求


以下是我的code,现在有的问题是如果运行(1,30,2),22会出现两次,不知道有什么办法可以消除这个问题?初学的在此谢过了!!!
如果发帖格式有问题也请见谅,新人一枚= =

C/C++ code
void printIfHoldsDigit(int start, int end, int digit){    int s=start;    int e=end;    if(s>e)    {        s=end;        e=start;    }    while(s<=e)    {        int temp=s;        while(temp>0)        {            if(temp%10==digit)            {                                if(e<=10)                {                    cout <<s;                }                else if(10<e&&e<=digit*10)                {                    if(e/10==(temp+10)/10)                    {                        cout <<s;                    }                    else                    {                        cout <<s<<", ";                    }                }                else if(e>digit*10)                {                    if(s+1==e)                    {                        cout <<s;                    }                    else                    {                        cout <<s<<", ";                    }                }            }            temp=temp/10;        }        s=s+1;    }}


[解决办法]
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
 if (条件1) break;
 //...
 if (条件2) continue;
 //...
 if (条件3) return;
 //...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
 a=fgetc(f);
 //...
 b=fgetc(f);//可能此时已经feof了!
 //...
}
而这样写就没有问题:
whlie (1) {
 a=fgetc(f);
 if (feof(f)) break;
 //...
 b=fgetc(f);
 if (feof(f)) break;
 //...
}
类似的例子还可以举很多。

[解决办法]
代码没整好再发一遍
C/C++ code
/* 因为22这个情况比较特殊, 你的循环执行了两次,22%10==2执行一次,     22/10后为2%10又等于2再执行一次 */#include <iostream>using std::cout;void printIfHoldsDigit(int start, int end, int digit){    int s=start;    int e=end;    if(s>e)    {        s=end;        e=start;    }    while(s<=e)    {        int temp=s;        int nFlag = 0;    //加个标志        while(temp>0)        {            if(!nFlag && temp%10==digit)     //加个标志判断            {                nFlag = 1;     //加个标志赋值                if(e<=10)                {                    cout <<s;                }                else if(10<e&&e<=digit*10)                {                    if(e/10==(temp+10)/10)                    {                        cout <<s;                    }                    else                    {                        cout <<s<<", ";                    }                }                else if(e>digit*10)                {                    if(s+1==e)                    {                        cout <<s;                    }                    else                    {                        cout <<s<<", ";                    }                }            }            temp=temp/10;        }        s=s+1;    }}int main(void){    printIfHoldsDigit(5, 10, 8);    return 0;} 

热点排行
Bad Request.