第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

C:如何使scanf()輸入具有以下兩種格式之一?

C:如何使scanf()輸入具有以下兩種格式之一?

C
largeQ 2020-02-03 12:41:34
我需要執(zhí)行此程序,該程序?qū)蓚€(gè)三角形進(jìn)行比較?;旧?,除了用戶在其中輸入初始數(shù)據(jù)的部分之外,其他所有東西都可以正常工作。我的主要問(wèn)題是條件之一是用戶可以輸入三角形的三個(gè)邊的長(zhǎng)度或三個(gè)頂點(diǎn)的X,Y坐標(biāo)。我需要它像以下兩種方式一樣工作:此輸入意味著用戶使用了邊長(zhǎng):{ 5 , 5 , 5 }此輸入意味著用戶使用了頂點(diǎn)的X,Y坐標(biāo):{ [ 1 ; 1 ] , [ 3 ; 1 ] , [ 2 ; 2 ] }這是我嘗試解決該問(wèn)題的代碼,但是由于某些原因,如果用戶使用頂點(diǎn)輸入第一個(gè)條件(該條件是否不是邊長(zhǎng)),則會(huì)使所有內(nèi)容混亂。#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;}如果我交換這兩個(gè)條件,它將切換并且僅當(dāng)用戶使用頂點(diǎn)輸入時(shí)它才起作用...是否有可能使它像這樣簡(jiǎn)單地工作?
查看完整描述

2 回答

?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊

最好使用char buf[big_enough * 2]; fgets(buf, sizeof buf, stdin)讀取行然后解析它(最好使用sscanf(buf, " { [ %lf ...和)sscanf(buf, " { %lf ...。


但是,如果代碼必須保留scanf():


OP的第一個(gè)scanf(" { [ %lf ...消費(fèi)了'{'預(yù)期的第二個(gè)scanf( " { %lf ...


代替:


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 = ' ';


    //    no  {

    //        v

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

        printf("error\n");

        return 1;

    }


}

首選fgets()方式:


// Form a reasonable, yet generous buffer

#define I (50 /* rough estimate of characters use to read a double, adjust as needed */)

//                          { [ 1 ; 1 ] , [ 3 ; 1 ] , [ 2 ; 2 ] }\n\0

#define LINE_SIZE_EXPECTED (4 + I+3+I  +7  +I+3+I  +7  +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)) {

  // Consider using "%n" to detect a complete scan and check for no trailing junk

  int n = 0;

  sscanf(buf, " { [ %lf ; %lf  ] , [ %lf ; %lf  ] , [ %lf ; %lf  ] } %n",

      &A[0], &A[1], &B[0], &B[1], &C[0], &C[1], &n);

  if (n && buf[n] == '\0') {

    // successful scan

  } else {

    n = 0;

    sscanf(" { %lf , %lf , %lf } %n", &a, &b, &c, &n);

    if (n && buf[n] == '\0') {

      // successful scan

    } else

      // both scans failed

    }

  }

}


查看完整回答
反對(duì) 回復(fù) 2020-02-03
?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

您應(yīng)該使用sscanf。


以下code可能有效:


#include <stdio.h>


int main() {

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

    char *s = NULL;

    size_t n = 0;


    getline(&s, &n, stdin);


    if(sscanf(s, " { [ %lf ; %lf  ] , [ %lf ; %lf  ] , [ %lf ; %lf  ] }", &A[0], &A[1], &B[0], &B[1], &C[0], &C[1]) != 6

        && sscanf(s, " { %lf , %lf , %lf }", &a, &b, &c) != 3) {


        printf("error\n");

        return 1;

    }


    // rest of the code...


    printf("success\n");

    return 0;

}


查看完整回答
反對(duì) 回復(fù) 2020-02-03
  • 2 回答
  • 0 關(guān)注
  • 748 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)