Decimal reading

I wrote it appropriately because I was free.

 Store in 100byte memory
[1][2][3]...[99][100] 
 ^ Start position ^ full


 When you read one letter, it points to the next position

[1][2][3]...[99][100]
 ^ 3 letters read


char buff[100];
const char* n = "100\n2\na5";

char terminator;
char delim;
int fail;
int pos;
 
terminator = '\0';
delim = '\n';
fail = 0;
pos = 0;
 
for(; pos<100 && n[pos] != terminator; ++pos) {
  if(isdigit(n[pos]) || n[pos] == delim) {
    buff[pos] = n[pos];
  }
  else if(n[pos] != delim) {
    fail = 1;
    break;
  }
}
/*Pos here:6th byte, 'a'Notoko*/

that? int full Unlimited buffer overflow.

Character array-> Make it in the middle (`・ ω ・ ´) Shakin


int z = 0;
int i = 0;
 
while(i < pos) {
  if(buff[i] != delim) {
  z = z*10 + buff[i]-'0';
  }
  else break;
   ++i;
  }
}

pos is dozo, either static or recursive. Recursive call recommended.

Either way, it's a buffer, not a Stream.


add to

Expected to read one row buffered If it is not a number, it will be skipped.


  int n;
  if( 1 < (scanf("%d", &n)) ) {
    while( getchar() != '\n'); /* (-Man-)May the line feed code come*/
    scanf("%d", &n);
  }


  char c;
  int n;
  int sig;
 
  c = 0;
  n = 0;
  sig = 1;
 
  while((c=getchar()) != '\n') {
    if(c=='-') sig = -1;
    if(isdigit(c)) {
      n = n*10 + c-'0';
    }
  }
  n *= sig;
  return n;

I got some advice ヾ (\ * ´∀ ` \ *) ノ ャ ャ ャ

Recommended Posts