【ThinkingInC++】21、关于C的习题(3)

/**
*功能:使用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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值