C:如何使scanf()输入具有以下两种格式之一?

我需要执行此程序,该程序将两个三角形进行比较。


基本上,除了用户在其中输入初始数据的部分之外,其他所有东西都可以正常工作。我的主要问题是条件之一是用户可以输入三角形的三个边的长度或三个顶点的X,Y坐标。


我需要它像以下两种方式一样工作:

此输入意味着用户使用了边长:


{ 5 , 5 , 5 }

此输入意味着用户使用了顶点的X,Y坐标:


{ [ 1 ; 1 ] , [ 3 ; 1 ] , [ 2 ; 2 ] }

这是我尝试解决该问题的代码,但是由于某些原因,如果用户使用顶点输入第一个条件(该条件是否不是边长),则会使所有内容混乱。


#include <stdio.h>


int main() {

    double a, b, c, A[2], B[2], C[2];

    char s;


    if(scanf(" { [ %lf ; %lf  ] , [ %lf ; %lf  ] , [ %lf ; %lf  ] }%c", 

            &A[0], &A[1], &B[0], &B[1], &C[0], &C[1], &s) != 7 && s != '\n') {

        s = ' ';


        if(scanf(" { %lf , %lf , %lf }%c", &a, &b, &c, &s) != 4 && s != '\n') {

            printf("error\n");

            return 1;

        }


    }


    // rest of the code...


    printf("success\n");

    return 0;

}

如果我交换这两个条件,它将切换并且仅当用户使用顶点输入时它才起作用...


是否有可能使它像这样简单地工作?


largeQ
浏览 720回答 2
2回答

慕容708150

最好使用char buf[big_enough * 2]; fgets(buf, sizeof buf, stdin)读取行然后解析它(最好使用sscanf(buf, " { [ %lf ...和)sscanf(buf, " { %lf ...。但是,如果代码必须保留scanf():OP的第一个scanf(" { [ %lf ...消费了'{'预期的第二个scanf( " { %lf ...代替:if(scanf(" { [ %lf ; %lf&nbsp; ] , [ %lf ; %lf&nbsp; ] , [ %lf ; %lf&nbsp; ] }%c",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &A[0], &A[1], &B[0], &B[1], &C[0], &C[1], &s) != 7 && s != '\n') {&nbsp; &nbsp; s = ' ';&nbsp; &nbsp; //&nbsp; &nbsp; no&nbsp; {&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; v&nbsp; &nbsp; if(scanf(" %lf , %lf , %lf }%c", &a, &b, &c, &s) != 4 && s != '\n') {&nbsp; &nbsp; &nbsp; &nbsp; printf("error\n");&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; }}首选fgets()方式:// Form a reasonable, yet generous buffer#define I (50 /* rough estimate of characters use to read a double, adjust as needed */)//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { [ 1 ; 1 ] , [ 3 ; 1 ] , [ 2 ; 2 ] }\n\0#define LINE_SIZE_EXPECTED (4 + I+3+I&nbsp; +7&nbsp; +I+3+I&nbsp; +7&nbsp; +I+3+I+6)char buf[LINE_SIZE_EXPECTED * 2]; // Lets us use 2x for extra spaces, leading zeros, etc.if (fgets(buf, sizeof buf, stdin)) {&nbsp; // Consider using "%n" to detect a complete scan and check for no trailing junk&nbsp; int n = 0;&nbsp; sscanf(buf, " { [ %lf ; %lf&nbsp; ] , [ %lf ; %lf&nbsp; ] , [ %lf ; %lf&nbsp; ] } %n",&nbsp; &nbsp; &nbsp; &A[0], &A[1], &B[0], &B[1], &C[0], &C[1], &n);&nbsp; if (n && buf[n] == '\0') {&nbsp; &nbsp; // successful scan&nbsp; } else {&nbsp; &nbsp; n = 0;&nbsp; &nbsp; sscanf(" { %lf , %lf , %lf } %n", &a, &b, &c, &n);&nbsp; &nbsp; if (n && buf[n] == '\0') {&nbsp; &nbsp; &nbsp; // successful scan&nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; // both scans failed&nbsp; &nbsp; }&nbsp; }}

慕工程0101907

您应该使用sscanf。以下code可能有效:#include <stdio.h>int main() {&nbsp; &nbsp; double a, b, c, A[2], B[2], C[2];&nbsp; &nbsp; char *s = NULL;&nbsp; &nbsp; size_t n = 0;&nbsp; &nbsp; getline(&s, &n, stdin);&nbsp; &nbsp; if(sscanf(s, " { [ %lf ; %lf&nbsp; ] , [ %lf ; %lf&nbsp; ] , [ %lf ; %lf&nbsp; ] }", &A[0], &A[1], &B[0], &B[1], &C[0], &C[1]) != 6&nbsp; &nbsp; &nbsp; &nbsp; && sscanf(s, " { %lf , %lf , %lf }", &a, &b, &c) != 3) {&nbsp; &nbsp; &nbsp; &nbsp; printf("error\n");&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; }&nbsp; &nbsp; // rest of the code...&nbsp; &nbsp; printf("success\n");&nbsp; &nbsp; return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP