gpt4 book ai didi

c - 从标准输入中划分数字和字符串

转载 作者:太空宇宙 更新时间:2023-11-04 02:08:30 24 4
gpt4 key购买 nike

我想知道是否可以从标准输入中分界数字和字符。就我而言,我知道文本的大小不固定。

例如,我想从键盘读取READ REGISTER A(使用gets() fgets()),使用 strcmp 通过 switch 语句选择我指的是什么情况。但问题是,当我写 WRITE REGISTER A 0x002 时,我必须 strcmp 字符串 WRITE REGISTER A 和将值 (0x002) 写入寄存器。我不能 strcmp 因为值不固定。我提供的输入有时有其他格式(例如 INITIALIZE、RESET 等)。

我知道使用 sscanf 我可以提取参数(十六进制),但我不能 strcmp

我该如何处理?

最佳答案

我建议不惜一切代价避免使用 scanf(),并使用 strtok() 或类似的方法:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXSIZE 1000

int main(void) {
char buffer[MAXSIZE];
char * token;
int n = 1;

puts("Enter your command:");
if ( fgets(buffer, MAXSIZE, stdin) == NULL ) {
fputs("Error getting input", stderr);
return EXIT_FAILURE;
}

token = strtok(buffer, " ");
while ( token ) {
printf("Token %d is %s\n", n++, token);
token = strtok(NULL, " ");
}

return EXIT_SUCCESS;
}

显然,您需要以某种方式处理单个标记,或者存储它们以供以后处理。您可以 strcmp() 单个标记来检查您是否有“WRITE”、“REGISTER”或其他任何内容,然后使用 strtol() 将最后一个标记转换为数字

这是一个完整的工作示例,一个简单的从左到右的解析通常是不合适的,但它将展示一种工作技术(编辑:添加了一些更好的错误处理):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXSIZE 1000

static long register_a = 0;
static long register_b = 0;
static const char * token_delims = " \r\n\t";

static void inst_write(void);
static void inst_write_register(void);
static void inst_write_register_n(long * p_register);
static void error_quit(const char * msg);

int main(void) {
char buffer[MAXSIZE];
char * token;

/* Get input */

puts("Enter your command:");
if ( fgets(buffer, MAXSIZE, stdin) == NULL ) {
error_quit("couldn't get input");
}

/* Tokenize and parse first token */

token = strtok(buffer, token_delims);
if ( token == NULL ) {
error_quit("no instruction specified");
}
else if ( strcmp(token, "WRITE") == 0 ) {
inst_write();
}
else if ( strcmp(token, "READ") == 0 ) {

/* Check for other instructions like this */
}
else {
error_quit("unrecognized instruction");
}

/* Output register contents */

printf("Register A contains: %ld\n", register_a);
printf("Register B contains: %ld\n", register_b);

return EXIT_SUCCESS;
}

/* Processes a WRITE instruction */

void inst_write(void) {
char * token = strtok(NULL, token_delims);

if ( token == NULL ) {
error_quit("missing WRITE operand");
}
else if ( strcmp(token, "REGISTER") == 0 ) {
inst_write_register();
}
else if ( strcmp(token, "MEMLOC") == 0 ) {

/* Check for other things to which to write */

}
else {
error_quit("unrecognized WRITE operand");
}
}

/* Processes a WRITE REGISTER instruction */

void inst_write_register(void) {
char * token = strtok(NULL, token_delims);

if ( token == NULL ) {
error_quit("missing WRITE REGISTER operand");
}
else if ( strcmp(token, "A") == 0 ) {
inst_write_register_n(&register_a);
}
else if ( strcmp(token, "B") == 0 ) {

/* Check for other registers to which to write */

inst_write_register_n(&register_b);
}
else {
error_quit("unrecognized register");
}
}

/* Processes the operand of a WRITE REGISTER [X] instruction, and
* stores it in the appropriate register.
*
* Arguments:
* p_register -- pointer to the register in which to store
*/

void inst_write_register_n(long * p_register) {
char * token = strtok(NULL, token_delims);

if ( token == NULL ) {
error_quit("missing WRITE REGISTER [X] operand");
}
else {
char * endptr;
long n = strtol(token, &endptr, 16);

if ( endptr == token ) {
error_quit("WRITE REGISTER [X] operand should be a hex integer");
}
else {
*p_register = n;
}
}
}

/* General error handler, prints the supplied message and exit()s */

void error_quit(const char * msg) {
fprintf(stderr, "Error: %s\n", msg);
exit(EXIT_FAILURE);
}

关于c - 从标准输入中划分数字和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17772340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com