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(®ister_a);
}
else if ( strcmp(token, "B") == 0 ) {
/* Check for other registers to which to write */
inst_write_register_n(®ister_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);
}