/**
*功能:使用while循环从标准输入(cin)中吧单词读入到string中。这是一个“无穷”
* while循环,可以使用break语句中断(和退出程序)。对于读入的单词用系列if语句吧
* 该单词“映射”为一个整数值,然后用该整数值作为一个switch语句的选择条件
* 的意义。同上判定那个单词是程序的结束标志,用文件输出啦测试程序
*时间:2014年8月15日08:22:17
*作者::cutter_point
*/
#include<iostream>
#include<cstdlib>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
//设计一个类,包括所有功能
class operatorText
{
public:
// operatorText()=default;
operatorText(string s=""):s(s){}
//标准输入(cin)中吧单词读入到string中
void inWord(istream &is);
//整数值作为一个switch语句的选择条件的意义
string theMeanOfInt();
//读入的单词用系列if语句吧该单词“映射”为一个整数值,只要在类内部行动就可以了
void wordToInt();
private:
//存放一个单词
string s;
//存放这个单词相应的整数
int is;
};
//标准输入(cin)中吧单词读入到string中
void operatorText::inWord(istream &is)
{
//输入一个单词,保存到类中
cout<<"\n输入单词:";
string ps;
is>>ps;
this->s=ps;
}
/*
读入的单词用系列if语句吧该单词“映射”为一个整数值,
void wordToInt(const string s, int &is);
*/
void operatorText::wordToInt()
{
if(s=="i" || s=="I")
{
is=5;
}
else if(s=="love" || s=="Love" || s=="LOVE")
{
is=5;
}
else if(s=="China")
{
is=5;
}
else
{
is=1;
cout<<"\n单词"<<s<<"是:"<<is<<endl;
}
}
/*
整数值作为一个switch语句的选择条件的意义
string theMeanOfInt();
*/
string operatorText::theMeanOfInt()
{
string ps;
switch(is)
{
case 1:
ps=s+":这个单词小case.";
//cout<<s<<":这个单词小case."<<endl;
break;
case 5:
ps=s+":重量级单词小心。";
//cout<<s<<":重量级单词小心。"<<endl;
break;
default:
ps="这是什么单词?";
//cout<<"这是什么单词?"<<endl;
}
return ps;
}
int main()
{
cout<<"输入“i”进入程序执行功能。"<<endl;
cout<<"输入”q“退出程序"<<endl;
char panDuan;
cin>>panDuan;
if(panDuan == 'i')
{
while(1)
{
//创建一个单词对象
operatorText ot("haha");
ot.inWord(cin); //输入单词
ot.wordToInt();
//输入单词的意义
cout<<ot.theMeanOfInt()<<endl;
//cout<<"asdasdasda"<<endl;
}
}
else if(panDuan == 'q')
{
system("pause");
return 0;
}
else
{
cout<<"不好意思,程序其他功能还在开发中"<<endl;
system("pause");
return 0;
}
}
【ThinkingInC++】21、关于C的习题(3)
最新推荐文章于 2016-08-02 19:34:00 发布