|
|
发表于 31-10-2011 09:32 PM
|
显示全部楼层
回复 40# 可爱的医师小小
- #include <iostream>
- using namespace std;
- /* Task C */
- int main()
- {
- char x;
- cout << "This program will determine is that the";
- cout <<" alphabet input is vowel.\n";
- do
- {
- cout << "\nPlease enter an alphabet.\nEnter 'z'to exit.\n";
- cin >> x;
- if( x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'||
- x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U')
- {
- cout << "Character is a VOWEL.\n";
- }
- else if( x != 'a' || x != 'e' || x != 'i' || x != 'o' || x != 'u' ) // 多餘
- {
- cout << "Character entered is " << x <<"\n";
- }
-
- }while ( x != 'z' && x != 'Z');
- return 0;
- }
-
-
复制代码
沒什麼大問題 ,只是這個 if( x != 'a' || x != 'e' || x != 'i' || x != 'o' || x != 'u' ) 多餘的,因為你一開始已經判斷了,所以不需要這一行也可以。還有你的 do while, 你的while 可以放在bracket 後面嗎 =.= ? 我看的時候還以為你多一個 while loop |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 31-10-2011 09:54 PM
|
显示全部楼层
回复 可爱的医师小小
沒什麼大問題 ,只是這個 if( x != 'a' || x != 'e' || x != 'i' || x != 'o ...
invisible 发表于 31-10-2011 09:32 PM  好的,谢谢你! |
|
|
|
|
|
|
|
|
|
|
发表于 31-10-2011 11:18 PM
|
显示全部楼层
本帖最后由 geekman 于 31-10-2011 11:39 PM 编辑
你可以通过toupper()或者tolower()来减少你的if()的evaluation次数:
- x = toupper(x);
- if(x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U')
- {
- //do whatever
- }
复制代码 或者
- x = tolower(x);
- if(x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
- {
- //do whatever
- }
复制代码- while ( x != 'z' && x != 'Z');
复制代码 你确定是 && ?如果你的意图是让使用者按 Z 键来结束while loop,那你得到的结果将是完全相反的。不管使用者按什么键都会break loop的,因为你的条件是x必须同时不是大写和小写的 Z 才会继续 (反过来说,就是你的条件是x必须同时为大写和小写的 Z,或者 x 是 Z,z 以外的任何字母/符号都会停止循环)。
还有,用switch会更简洁,因为你不必evaluate else if():- x = tolower(x);
- switch(x)
- {
- case 'a':
- case 'e':
- case 'i':
- case 'o':
- case 'u': //it's a vowel
- break;
- default: //it isn't
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 31-10-2011 11:30 PM
|
显示全部楼层
你可以通过toupper()或者tolower()来减少你的if()的evaluation次数:或者你确定是 && ?如果你的意图 ...
geekman 发表于 31-10-2011 11:18 PM 
又学到东西了,谢谢大大 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 31-10-2011 11:39 PM
|
显示全部楼层
你可以通过toupper()或者tolower()来减少你的if()的evaluation次数:或者你确定是 && ?如果你的意图 ...
geekman 发表于 31-10-2011 11:18 PM  是哦,我测试了,行得通哦 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 31-10-2011 11:48 PM
|
显示全部楼层
本帖最后由 可爱的医师小小 于 31-10-2011 11:51 PM 编辑
你可以通过toupper()或者tolower()来减少你的if()的evaluation次数:或者你确定是 && ?如果你的意图 ...
geekman 发表于 31-10-2011 11:18 PM  其实那个&&的是我从我老师的例子参考来的,不是很明白。
- while ( x != 'Z' || x != 'z');
复制代码- while ( x == 'z' && x == 'Z');
复制代码
如果是这样的话就行不通…… |
|
|
|
|
|
|
|
|
|
|
发表于 31-10-2011 11:53 PM
|
显示全部楼层
while(condition1 && condition2)
=> while condition1 AND condition2 is true, loop goes on,也就是说两个条件都必须是true才会继续
while(condition1 || condition2)
=> while condition1 OR condition2 is true, loop goes on,也就是说任何一个条件是true都会继续。
其实最好的做法就是我给你的方案:用tolower()或者toupper(),然后只需要evaluate一个condition:
- x = tolower(x);
- do
- {
- //whatever
- }(while x != 'z');
复制代码 简单明了。 |
|
|
|
|
|
|
|
|
|
|
发表于 31-10-2011 11:55 PM
|
显示全部楼层
回复 46# 可爱的医师小小
行不通的原因是x不可能同时是大写和小写的 Z |
|
|
|
|
|
|
|
|
|
|
发表于 8-10-2012 09:54 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|