C/C++の宿題を片付けます 89代目
■ このスレッドは過去ログ倉庫に格納されています
0461デフォルトの名無しさん
2007/06/10(日) 19:57:38#include <stdio.h>
#include <stdlib.h>
void push(char **s, int *nc, int *sz, int c)
{
if (*nc == *sz) *s = realloc(*s, *sz *= 2);
(*s)[(*nc)++] = c;
}
char *getline(FILE *fp)
{
char *s = malloc(1);
int c, nc = 0, sz = 1;
while ((c = getc(fp)) != EOF && c != '\n')
push(&s, &nc, &sz, c);
if (nc == 0 && c == EOF) return 0;
if (c == '\n') push(&s, &nc, &sz, '\n');
push(&s, &nc, &sz, 0);
return s;
}
int main(void)
{
char *s, **lines = malloc(sizeof(char*));
int nc = 0, sz = 1;
while (s = getline(stdin)) {
if (nc == sz) lines = realloc(lines, sizeof(char*)*(sz*=2));
lines[nc++] = s;
}
while (nc) fputs(lines[--nc], stdout);
return 0;
}
■ このスレッドは過去ログ倉庫に格納されています