c++ - Parsing data, scanf? -
c++ - Parsing data, scanf? -
i quite new progamming, parse info in format this:
4 ((182, 207), (385, 153), (638, 639), (692, 591))
first number states number of pairs occur. want save first number of each pair x axis , sec number of each pair y axis. in head wanted save entire line via scanf , seek work around amount of brackets , commas not sure if right method or how implement properly. not want utilize built in containers or string. tried straight away via scanf doing like
for(int i= 0; < pair_count;i++){ scanf("(%d, %d)",tabx[i],taby[i]) }
but not work :(. don't know how format scanf correctly guess or thought on how wrong.
you have input matching characters. it's bit tricky, because ,
not nowadays after lastly pair of numbers.
following sample code solves problem.
#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]; (int = 0; < pair_count-1; ++i) { if (scanf("(%d, %d), ", &tabx[i], &taby[i]) < 2) { fprintf(stderr, "input error!\n"); homecoming exit_failure; } } if (scanf("(%d, %d))", &tabx[pair_count-1], &taby[pair_count-1]) < 2) { fprintf(stderr, "input error!\n"); homecoming exit_failure; } (int = 0; < pair_count; ++i) { printf("%d %d\n", tabx[i], taby[i]); } delete[] tabx; delete[] taby; }
alternatively, can read entire input string , replace (
, )
, ,
(space). after can parse numbers. on other hand, removes validation of info format.
c++ parsing scanf
Comments
Post a Comment