再次运行程序 [英] Running program again

查看:65
本文介绍了再次运行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所以这是我的程序来匹配子字符串。但我想改变输入输出在我的程序中的工作方式。



首先我想问一下用户要输入多少个字符串然后输入实际输入。输出应该存储在一个数组中然后显示。我已经尝试了很多东西但是无法实现它。所以有关如何做的任何指针?



例如:



输入:



Hi so this is my program here to match sub strings.But I want to change the way input output works in my program.

First I want to ask the user how many strings he wants to input and then typing in the actual input.The output should be stored in an array and then displayed.I have tried many things but can't implement it.So any pointers on how to do it?

Example:

Input:

4
i love hackerrank
hackerrank is an awesome place for programmers
hackerrank
i think hackerrank is a great place to hangout





输出:



Output:

2
1
0
-1









这是我的实际代码。 />






This is my actual code.

int main()
{

    vector<string> token_store;
    string s,token;



    getline(cin,s);
    std::istringstream iss(s);


    while(iss>>token)
        token_store.push_back(token);   //splitting strings into tokens


    int len=token_store.size();        //storing size of vector

    if(token_store[0]=="hack" && token_store[len-1]=="hack")cout<<"first and last same";     //if first and last word of input string is hack
    else if(token_store[0]=="hack")cout<<"first matches";                                    //if first word of input string is hack
    else if(token_store[len-1]=="hack")cout<<"last matches";                                 //if last word of input string is hack

}

推荐答案

请参阅我对该问题的评论,并尝试检查您对该功能的要求。



您的建议中至少有两个问题。首先,正如我所说,目前尚不清楚为什么需要第二次运行。这绝不是一个不错的选择。



第二个问题是:用户不喜欢写出她/她想要输入多少字符串的想法。对于某些代码来说,这将是一个完美的要求,但人类更愿意改变主意并获得更多自由。通常的做法是建议用户输入一个特殊字符串来表示输入结束,例如:输入下一行,空行(按回车键)退出。



但即使这样也不好。一个不错的应用程序可以是一些GUI,也可以是仅限控制台。但是,如果这是一个仅限控制台的应用程序,那么将它与交互式设置不太好,因为它会失去仅控制台应用程序的所有好处(其中一个主要好处是可以从批处理文件中运行,否则GUI更好)。



那么,对于这样一个优秀/文化的控制台程序应该是什么要求?简单。所有输入都应该在一个命令行中。如果此类输入过于庞大,则一个或多个命令行参数可能需要指定一个或多个文件名,并且这些文件可用于输入更多详细信息。仅在此方法中,具有可变用户输入的仅控制台应用程序才非常方便。请参阅:

http://en.wikipedia.org/wiki/Main_function #C_and_C.2B.2B [ ^ ],

http://en.wikipedia.org/wiki/Command -line_argument#Arguments [ ^ ] ,

http://en.wikipedia.org/wiki/Batch_file#Windows_NT [ ^ ]。



-SA
Please see my comment to the question and try to review your requirements to the functionality.

You have at least two problems in your suggestions. First, as I say, it's not clear why the second run would be required. It is never a nice option.

The second problem is: the users would not like the idea of writing how many string she/he wants to input. It would be a perfect requirement for some code, but human beings prefer to change their minds and get more freedom. The usual approach is to suggest the user to input a "special string" to indicate "end of input", for example: "Enter next line, empty line (press Enter) to exit".

But even this is not nice. A nice application can be either with some GUI, or a console-only. But if this is a console-only application, its not nice to have it interactive, as it would loose all the benefits of a console-only application (and one of the main benefits is the possibility to run if from a batch file, otherwise GUI is better).

So, what should be the requirements for such a nice/cultured console-only program? Simple. All input should be in one command line. If such input tends to be too big, one or more of the command-line arguments may expect one or more file names specified, and those files can be used to input more detail. Only in this approach, the console-only application with variable user input can be really convenient. Please see:
http://en.wikipedia.org/wiki/Main_function#C_and_C.2B.2B[^],
http://en.wikipedia.org/wiki/Command-line_argument#Arguments[^],
http://en.wikipedia.org/wiki/Batch_file#Windows_NT[^].

—SA


这篇关于再次运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆