注册 登录
编程论坛 C++教室

一段需要修改的C++程序,大家有兴趣的看看

八画小子 发布于 2010-12-08 23:10, 1749 次点击
今天有人问我这样一个问题,我把它放在这里大家一起看看吧。
程序代码:
#include <iostream>
#include <string>
int main()
{
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;

    string str_c = "carnivore";
    string str_p = "pianist";
    string str_t = "tree";
    string str_g = "game";
    string str_prefix = "A maple is a ";
    string str_reInputInfo = "Please enter a c, p, t, or g: ";
    char ch;
    bool bool_hasInput = false;

    cout << "Please enter one of the following choices:" << endl;
    cout << "c) " << str_c << "\t\t\t"
        << "p) " << str_p << "\n"
        << "t) " << str_t << "\t\t\t\t"
        << "g) " << str_g << "\n";
    do
    {
        while (cin >> ch)
        {
            switch(ch)
            {
            case 'c':cout << str_prefix + str_c + '.'; goto label;
            case 'p':cout << str_prefix + str_p + '.'; goto label;
            case 't':cout << str_prefix + str_t + '.'; goto label;
            case 'g':cout << str_prefix + str_g + '.'; goto label;
            }
            cout << str_reInputInfo;
        }
        cin.clear();
    }while(1);
    label:

    return 0;
}
问题:(1)能否修改程序让程序不使用goto语句?
      (2)这个程序可以避免全部的数字、符号、无效字母和多数像ctrl+C这样的输入,可不能避免ctrl+Z的输入,当输入ctrl+Z后不能按预期的那样提示信息。如何修改程序?
16 回复
#2
Lyone2010-12-09 13:56
1:label后面就一个return 0 了?那就直接把goto label写成return 0 不行么?
#3
八画小子2010-12-09 18:39
你说的也可以,但我放在帖子里的程序是我把源程序(源程序是一个非常庞大的程序段)极度精简后的程序,如果在源程序中使用这样的修改,不利于阅读,而且稍有不慎会出现程序流程上的逻辑错误,所以我们一般不在程序的中间,尤其是在main()函数这样的重要函数的中间添加程序的出口。
#4
xin3109232010-12-09 22:08
你说的也不是不行,但你在中间添加程序的出口后是不是会影响整个程序的运行呢,或者改变程序原意。
#5
pangding2010-12-09 23:25
这就用 goto 也没事。这个地方就(至少你给的这个程序)就属于用 goto 反而使程序的流程更清晰的一个例子。
#6
南国利剑2010-12-10 00:48
同意楼上的看法.
这里如果不用goto似乎没有更好的方法!

值得大家一起来讨论一下。

我置顶了。


[ 本帖最后由 南国利剑 于 2010-12-10 00:51 编辑 ]
#7
makebest2010-12-10 09:17
要不用 gogo 就要多写些语句了, 定义一个变量, 加到循环控制条件中, 在 goto 的地方给这个变量赋值就可以退出循环了
要避免 ctrl-Z 就不行了, 因为这个按键有特殊功能, 不会被程序接收, 它表示输入结束,  cin>>ch 条件不成立.
#8
最近不在2010-12-10 21:07
程序代码:
class CCmdProc;
typedef void (CCmdProc::*PFUN)();
struct CmdProc
{
    char cCmd[15];
    char cInfo[15];
    PFUN pfun;
};

程序代码:
CmdProc obj[] = {
    {"help",        "查询命令",       CCmdProc::help   },
    {"exit",        "退出程序",    CCmdProc::exit   },
    {"create",        "创建链表",    CCmdProc::create },
    {"add",         "添加节点",    CCmdProc::add    },
    {"del",         "删除节点",    CCmdProc::del    },
    {"insert",      "插入节点",    CCmdProc::insert },
    {"clear",       "清空链表",    CCmdProc::clear  },
    {"size",        "查询人数",    CCmdProc::size   },
    {"pass",        "查询及格",    CCmdProc::pass   },
    {"find",        "查询信息",    CCmdProc::find   },
    {"show",        "显示信息",    CCmdProc::show   },
    {"fail",        "显示不及格",  CCmdProc::fail   }
};

const int g_size = sizeof(obj)/sizeof(CmdProc);

程序代码:
void CCmdProc::MenuProc()
{
    //Bar();
    help();
    while(1)
    {
        cout<<'>';
        CwjbString CTemp;
        cin>>CTemp;
        for(int i = 0; i < g_size; i++)
        {
            if(obj[i].cCmd == CTemp)
            {
                (this->*obj[i].pfun)();
                break;
            }
        }
        if(i == g_size)
        {
            cout<<"找不到该命令!"<<endl;
        }
    }
}
#9
最近不在2010-12-10 21:23
while(a && b)
#10
八画小子2010-12-11 21:02
感谢各位朋友能够关注我提出的这个问题,谢谢大家。
#11
coolxy2010-12-18 18:36
  do
    {
        while (cin >> ch)
        {
            switch(ch)
            {
            case 'c':cout << str_prefix + str_c + '.'; break;
            case 'p':cout << str_prefix + str_p + '.'; break;
            case 't':cout << str_prefix + str_t + '.'; break;
            case 'g':cout << str_prefix + str_g + '.'; break;
            }
            break;
            cout << str_reInputInfo;
        }
        break;
        cin.clear();
            }while(1);
   
这样你试试,
   
#12
八画小子2010-12-18 18:39
没有解决第二个问题,而且程序看起来有点不清晰
#13
梦想天奇2010-12-21 14:37
  啊……我太水了……
#14
点线面2011-01-06 18:26
程序代码:
    do
    {
        while (cin >> ch)
        {
            switch(ch)
            {
            case 'c':cout << str_prefix + str_c + '.'; ch=26;break;
            case 'p':cout << str_prefix + str_p + '.'; ch=26;break;
            case 't':cout << str_prefix + str_t + '.'; ch=26;break;
            case 'g':cout << str_prefix + str_g + '.'; ch=26;break;
            }
            cout << str_reInputInfo;
        }
        cin.clear();
    }while(ch!=102);
楼主试一试这个程序怎样

[ 本帖最后由 点线面 于 2011-1-6 18:31 编辑 ]
#15
h3705445432011-01-07 00:56
暂时不懂
#16
qs02142011-01-07 15:52
个人觉得这里用goto语句就很好!

值得大家一起来讨论一下。

#17
方杰斌2011-01-07 22:07
不懂   但我要顶一下
1