使用最小状态机:
#include <stdio.h>
#include <string.h>
int main(int qrgc, char **argv)
{
char buff[111];
char one[10], two[10];
char left[10], right[10];
int num;
size_t len;
int state,rc;
state =0;
while(fgets (buff, sizeof buff, stdin)) {
len = strlen (buff);
while (len && buff[len-1] == '\n') buff[--len] = 0;
switch (state) {
case 0:
rc = sscanf(buff, "%s %s", left, right);
if (rc < 2) goto quit;
state=1;
break;
case 1:
rc = sscanf(buff, "%d", &num);
if (rc < 1) goto quit;
state=2;
break;
default:
if (!len) {state = 0; continue; }
rc = sscanf(buff, "%s %s", one, two);
if (rc < 2) goto quit;
break;
}
}
quit: ;
return 0;
}