接收来自用户的特定格式的输入 [英] Receiving input from a user in a specific format

查看:66
本文介绍了接收来自用户的特定格式的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个5 x 5的数组网格.我希望用户给用户一个在运行时输入单元格的选项.例如:对于第一行,用户可以输入
* * * _ *
_表示他以空格跳过了第四名
这意味着
a [0] [0] =''*''
a [0] [1] =''*''
a [0] [2] =''*''
a [0] [3] =''''//空
a [0] [4] =''*''

我还需要为用户跟踪5行输入(因为网格中有5行).我应该怎么做呢?对于特定的输入格式,应使用sscanf吗?


其次,我需要修改字符串,get,getline,gets,sscanf等的概念.对于一些不太难理解的链接,我会很感激,在这些链接中我可以读到这些内容.
P.S圣诞快乐! :-)
rephrased

Hi,
I have an array grid of 5 x 5. I want the user to user to give an option to input the cells at runtime. For eg: For the first row, the user can enter
* * * _ *
_ means he skipped the fourth place with a space
This would mean
a[0][0]=''*''
a[0][1]=''*''
a[0][2]=''*''
a[0][3]='' ''//empty
a[0][4]=''*''

I would also need to keep a track of 5 lines of input(as there are five rows in the grid) for the user. How should I go about this? For specific format of input, should sscanf be used?


Secondly, I need to revise the concepts of strings, get, getline, gets, sscanf, etc. I would be thankful for some not-so-hard-to-follow links where I could read about these things.
P.S Merry Christmas!! :-)
Edit :rephrased

推荐答案

google字符串标记生成器

请注意,令牌应为std :: vector< std :: string xmlns:std =#unknown"> &下面,但是codeproject由于某些原因更改了代码.

google string tokenizer

Note that tokens should be std::vector<std::string xmlns:std="#unknown"> & below, but codeproject changed the code for some reason.

//The tokenize function is borrowed form here:
//http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-7.html
//It is used to parse the input messages
void tokenize(const std::string & str,
	      std::vector<string> & tokens,
	      std::string const & delimiters) {
	tokens.resize(0);
    // Skip delimiters at beginning.
	std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
	// Find first "non-delimiter".
	std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
	while (std::string::npos != pos || std::string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
};


您可以尝试执行以下操作:

You could try something like this:

#include <stdio.h>

void main(void)
{
	char a[5][5];

	// Read data
	for(int i=0;i<5;i++){			// Row
		for(int j=0;j<5;j++)		// Column
			a[i][j] = getchar();	// Read the character
		getchar();			// Read the enter-key
	}
}



该程序将使用以下输入的字符填充数组a [] []:



This program will fill your array a[][] with the characters entered like so:

*****<enter>
*****<enter>
*****<enter>
*****<enter>
*****<enter>



请注意,假定输入的格式正确,并且如果出现任何错误,则没有反馈.要检查输入的字符是否有效,可以添加如下检查:



Note that the input is assumed to be in the correct format, and there is no feedback yet if anything goes wrong. To check if an entered character is valid, you could add a check like this:

// Read data
for(int i=0;i<5;i++){            // Row
    for(int j=0;j<5;j++){        // Column
        a[i][j] = getchar();    // Read the character
        if(a[i][j] != '' '' && a[i][j] != ''*''){
                    // Error handling code goes here
        }
    }
    getchar();          // Read the enter-key
}


这篇关于接收来自用户的特定格式的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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