file.c
来自「一遗传算法的例子源程序」· C语言 代码 · 共 82 行
C
82 行
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "lithos.h"
static char token[256];
static int readChar(FILE *f)
{
int c = fgetc(f);
if (feof(f))
{
printf("Unexpected end of file\n");
exit(EXIT_FAILURE);
}
return c;
}
static void readComment(FILE *f)
{
int c;
do
c = readChar(f);
while (c != ']');
}
static void readToken(FILE *f)
{
int c;
int i = 0;
do
{
c = readChar(f);
if (c == '[')
readComment(f);
}
while (isspace(c) || c == '[');
do
{
token[i++] = c;
c = readChar(f);
}
while (isalpha(c) || isdigit(c) || c == '-');
if (c == '[')
readComment(f);
token[i] = 0;
}
char *readString(FILE *f)
{
readToken(f);
return token;
}
int readInt(FILE *f)
{
readToken(f);
return atoi(token);
}
void writeString(FILE *f, char *s)
{
fprintf(f, "%s", s);
}
void writeInt(FILE *f, int n)
{
fprintf(f, "%d", n);
}
void writeCommentedInt(FILE *f, char *comment, int n)
{
int i;
fputc('[', f);
writeString(f, comment);
fputc(']', f);
for (i = 22 - strlen(comment); i > 0; i--)
fputc(' ', f);
writeInt(f, n);
fputc('\n', f);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?