C++ 分析数据,scanf?

C++ 分析数据,scanf?,c++,parsing,scanf,C++,Parsing,Scanf,我对编程非常陌生,我希望以如下格式解析数据: 4((182207),(385153),(638639),(692591)) 第一个数字表示将出现的对数。我想将每对的第一个数字保存为X轴,将每对的第二个数字保存为Y轴。 在我的脑海中,现在我想通过scanf保存整个行,然后尝试处理括号和逗号的数量,但我不确定这是否是正确的方法,或者如何正确地实现它。我不想使用任何内置容器或字符串。我试着通过scanf直接做这件事 for(int i= 0; i < pair_count;i++){ scanf

我对编程非常陌生,我希望以如下格式解析数据:

4((182207),(385153),(638639),(692591))

第一个数字表示将出现的对数。我想将每对的第一个数字保存为X轴,将每对的第二个数字保存为Y轴。 在我的脑海中,现在我想通过scanf保存整个行,然后尝试处理括号和逗号的数量,但我不确定这是否是正确的方法,或者如何正确地实现它。我不想使用任何内置容器或字符串。我试着通过scanf直接做这件事

for(int i= 0; i < pair_count;i++){
scanf("(%d, %d)",tabx[i],taby[i])

}
for(int i=0;i
但是它不起作用:(.我不知道如何正确格式化scanf,我想或者我的想法是完全错误的。

scanf()
希望地址是变量,而不是变量本身。因此请尝试:

   scanf("(%d, %d)",&tabx[i],&taby[i]); 
也可以尝试使用C++流:

  for(int i= 0; i < pair_count;i++){
      char d1,d2,d3;
      if ( (cin >> d1 >> tabx[i] >> d2 >> taby[i] >> d3) && d1=='(' && d3==')' && d2==',') {
         ... //process data
       }
       else cout << "Wrong input !"<<endl;
  }
for(int i=0;i>d1>>tabx[i]>>d2>>taby[i]>>d3)和&d1='('&&d3=')'和&d2==','){
…//进程数据
}
else cout
scanf()
希望地址是变量,而不是变量本身。因此请尝试:

   scanf("(%d, %d)",&tabx[i],&taby[i]); 
也可以尝试使用C++流:

  for(int i= 0; i < pair_count;i++){
      char d1,d2,d3;
      if ( (cin >> d1 >> tabx[i] >> d2 >> taby[i] >> d3) && d1=='(' && d3==')' && d2==',') {
         ... //process data
       }
       else cout << "Wrong input !"<<endl;
  }
for(int i=0;i>d1>>tabx[i]>>d2>>taby[i]>>d3)和&d1='('&&d3=')'和&d2==','){
…//进程数据
}
else cout
scanf()
希望地址是变量,而不是变量本身。因此请尝试:

   scanf("(%d, %d)",&tabx[i],&taby[i]); 
也可以尝试使用C++流:

  for(int i= 0; i < pair_count;i++){
      char d1,d2,d3;
      if ( (cin >> d1 >> tabx[i] >> d2 >> taby[i] >> d3) && d1=='(' && d3==')' && d2==',') {
         ... //process data
       }
       else cout << "Wrong input !"<<endl;
  }
for(int i=0;i>d1>>tabx[i]>>d2>>taby[i]>>d3)和&d1='('&&d3=')'和&d2==','){
…//进程数据
}
else cout
scanf()
希望地址是变量,而不是变量本身。因此请尝试:

   scanf("(%d, %d)",&tabx[i],&taby[i]); 
也可以尝试使用C++流:

  for(int i= 0; i < pair_count;i++){
      char d1,d2,d3;
      if ( (cin >> d1 >> tabx[i] >> d2 >> taby[i] >> d3) && d1=='(' && d3==')' && d2==',') {
         ... //process data
       }
       else cout << "Wrong input !"<<endl;
  }
for(int i=0;i>d1>>tabx[i]>>d2>>taby[i]>>d3)和&d1='('&&d3=')'和&d2==','){
…//进程数据
}

否则你必须输入所有匹配的字符。这有点棘手,因为最后一对数字后面不存在

下面的示例代码解决了您的问题

#include <cstdio>
#include <cstdlib>

int main() {
    // 4 ((182, 207), (385, 153), (638, 639), (692, 591))

    int pair_count;
    scanf("%d", &pair_count);
    scanf(" (");

    int* tabx = new int[pair_count];
    int* taby = new int[pair_count];
    for (int i = 0; i < pair_count-1; ++i) {
        if (scanf("(%d, %d), ", &tabx[i], &taby[i]) < 2) {
            fprintf(stderr, "Input error!\n");
            return EXIT_FAILURE;
        }
    }
    if (scanf("(%d, %d))", &tabx[pair_count-1], &taby[pair_count-1]) < 2) {
        fprintf(stderr, "Input error!\n");
        return EXIT_FAILURE;
    }

    for (int i = 0; i < pair_count; ++i) {
        printf("%d %d\n", tabx[i], taby[i]);
    }

    delete[] tabx;
    delete[] taby;
}
#包括
#包括
int main(){
// 4 ((182, 207), (385, 153), (638, 639), (692, 591))
整数对计数;
扫描频率(“%d”和对计数);
scanf(“(”);
int*tabx=newint[pair_count];
int*taby=newint[pair_count];
对于(int i=0;i

或者,您可以将整个输入读取为字符串,并将所有
替换为
(空格)。之后,您可以简单地解析数字。另一方面,这会删除数据格式的验证。

您必须输入所有匹配的字符。这有点棘手,因为最后一对数字之后不存在

下面的示例代码解决了您的问题

#include <cstdio>
#include <cstdlib>

int main() {
    // 4 ((182, 207), (385, 153), (638, 639), (692, 591))

    int pair_count;
    scanf("%d", &pair_count);
    scanf(" (");

    int* tabx = new int[pair_count];
    int* taby = new int[pair_count];
    for (int i = 0; i < pair_count-1; ++i) {
        if (scanf("(%d, %d), ", &tabx[i], &taby[i]) < 2) {
            fprintf(stderr, "Input error!\n");
            return EXIT_FAILURE;
        }
    }
    if (scanf("(%d, %d))", &tabx[pair_count-1], &taby[pair_count-1]) < 2) {
        fprintf(stderr, "Input error!\n");
        return EXIT_FAILURE;
    }

    for (int i = 0; i < pair_count; ++i) {
        printf("%d %d\n", tabx[i], taby[i]);
    }

    delete[] tabx;
    delete[] taby;
}
#包括
#包括
int main(){
// 4 ((182, 207), (385, 153), (638, 639), (692, 591))
整数对计数;
扫描频率(“%d”和对计数);
scanf(“(”);
int*tabx=newint[pair_count];
int*taby=newint[pair_count];
对于(int i=0;i

或者,您可以将整个输入读取为字符串,并将所有
替换为
(空格)。之后,您可以简单地解析数字。另一方面,这会删除数据格式的验证。

您必须输入所有匹配的字符。这有点棘手,因为最后一对数字之后不存在

下面的示例代码解决了您的问题

#include <cstdio>
#include <cstdlib>

int main() {
    // 4 ((182, 207), (385, 153), (638, 639), (692, 591))

    int pair_count;
    scanf("%d", &pair_count);
    scanf(" (");

    int* tabx = new int[pair_count];
    int* taby = new int[pair_count];
    for (int i = 0; i < pair_count-1; ++i) {
        if (scanf("(%d, %d), ", &tabx[i], &taby[i]) < 2) {
            fprintf(stderr, "Input error!\n");
            return EXIT_FAILURE;
        }
    }
    if (scanf("(%d, %d))", &tabx[pair_count-1], &taby[pair_count-1]) < 2) {
        fprintf(stderr, "Input error!\n");
        return EXIT_FAILURE;
    }

    for (int i = 0; i < pair_count; ++i) {
        printf("%d %d\n", tabx[i], taby[i]);
    }

    delete[] tabx;
    delete[] taby;
}
#包括
#包括
int main(){
// 4 ((182, 207), (385, 153), (638, 639), (692, 591))
整数对计数;
扫描频率(“%d”和对计数);
scanf(“(”);
int*tabx=newint[pair_count];
int*taby=newint[pair_count];
对于(int i=0;i

或者,您可以将整个输入读取为字符串,并将所有
替换为
(空格)。之后,您可以简单地解析数字。另一方面,这会删除数据格式的验证。

您必须输入所有匹配的字符。这有点棘手,因为最后一对数字之后不存在

下面的示例代码解决了您的问题

#include <cstdio>
#include <cstdlib>

int main() {
    // 4 ((182, 207), (385, 153), (638, 639), (692, 591))

    int pair_count;
    scanf("%d", &pair_count);
    scanf(" (");

    int* tabx = new int[pair_count];
    int* taby = new int[pair_count];
    for (int i = 0; i < pair_count-1; ++i) {
        if (scanf("(%d, %d), ", &tabx[i], &taby[i]) < 2) {
            fprintf(stderr, "Input error!\n");
            return EXIT_FAILURE;
        }
    }
    if (scanf("(%d, %d))", &tabx[pair_count-1], &taby[pair_count-1]) < 2) {
        fprintf(stderr, "Input error!\n");
        return EXIT_FAILURE;
    }

    for (int i = 0; i < pair_count; ++i) {
        printf("%d %d\n", tabx[i], taby[i]);
    }

    delete[] tabx;
    delete[] taby;
}
#包括