//:C03:Solution-03.cpp
/*3. Write a program that uses a while loop to read words from standard
input (cin) into a string. This is an "infinite" while loop, which you
break out of (and exit the program) using a break statement. For each
word that is read, evaluate it by first using a sequence of if statements
to "map" an integral value to the word, and then use a switch
statement that uses that integral value as its selector (this sequence
of events is not meant to be good programming style; it’s just supposed
to give you exercise with control flow). Inside each case, print something
meaningful. You must decide what the "interesting" words are and what
the meaning is. You must also decide what word will signal the end of
the program. Test the program by redirecting a file into the program’
s standard input (if you want to save typing,this file can be your program’
s source file).
*/
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(int argc, char* argv[])
{
if(argc==1){
cout<<"please direct to a .txt file!"<<endl;
return -1;
}
string word;
int wordmap;
bool flag=true;
ifstream file(argv[1]);
while(flag==true) {
file>>word;
if(word=="go!go!go!")
wordmap=1;
else if(word=="alibaba")
wordmap=2;
else if(word=="quit")
wordmap=3;
else if(word=="nike")
wordmap=4;
else
wordmap=5;
switch(wordmap){
case 1:
cout<<"Ah!Le!Ah!Le!Ah!Le!"<<endl<<endl;break;
case 2:
cout<<"zhima kai men!"<<endl<<endl;break;
case 3:
cout<<"You've quited the program."<<endl<<endl;
flag=false;break;
case 4:
cout<<"nothing is impossible!"<<endl<<endl;break;
case 5:
cout<<"Please type something meaningful."<<endl<<endl;break;
}
}
}
/*3. Write a program that uses a while loop to read words from standard
input (cin) into a string. This is an "infinite" while loop, which you
break out of (and exit the program) using a break statement. For each
word that is read, evaluate it by first using a sequence of if statements
to "map" an integral value to the word, and then use a switch
statement that uses that integral value as its selector (this sequence
of events is not meant to be good programming style; it’s just supposed
to give you exercise with control flow). Inside each case, print something
meaningful. You must decide what the "interesting" words are and what
the meaning is. You must also decide what word will signal the end of
the program. Test the program by redirecting a file into the program’
s standard input (if you want to save typing,this file can be your program’
s source file).
*/
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(int argc, char* argv[])
{
if(argc==1){
cout<<"please direct to a .txt file!"<<endl;
return -1;
}
string word;
int wordmap;
bool flag=true;
ifstream file(argv[1]);
while(flag==true) {
file>>word;
if(word=="go!go!go!")
wordmap=1;
else if(word=="alibaba")
wordmap=2;
else if(word=="quit")
wordmap=3;
else if(word=="nike")
wordmap=4;
else
wordmap=5;
switch(wordmap){
case 1:
cout<<"Ah!Le!Ah!Le!Ah!Le!"<<endl<<endl;break;
case 2:
cout<<"zhima kai men!"<<endl<<endl;break;
case 3:
cout<<"You've quited the program."<<endl<<endl;
flag=false;break;
case 4:
cout<<"nothing is impossible!"<<endl<<endl;break;
case 5:
cout<<"Please type something meaningful."<<endl<<endl;break;
}
}
}
本文提供了一个使用C++实现的程序实例,该程序通过无限循环读取标准输入中的单词,并根据特定条件映射为整数值,再利用switch语句进行处理并输出相应的信息。
9111

被折叠的 条评论
为什么被折叠?



