#ifndef YDYWJ
#define YDYWJ
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>
#include<ctype.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define MALLOC(n,type) (type*)malloc((n)*sizeof(type));
#define REALLOC(pos,n,type) (type*)realloc(pos,(n)*sizeof(type));
typedef int Status;
typedef int ElemType_Int;
typedef char ElemType_Char;
int Scanf(FILE* fp, char* format, ...);
void ClearWindows();
void FailedOpenFile();
#endif // YDYWJ
#ifndef SCANF_C
#define SCANF_C
//该文件旨在实现从文件中获取各种类型的数据
#include"Head.h"
int Scanf(FILE* fp, char* format, ...) {
int* i;
char* ch, * s;
float* f;
int count, k, len, n;
int tmp;
va_list ap;
len = strlen(format);
va_start(ap, format);
for (count = 0, k = 2; k <= len; k += 2) {
while ((tmp = getc(fp)) != EOF) {
if (tmp >= 0 && tmp <= 127 && tmp != 10) {
ungetc(tmp, fp);
break;
}
}
if (tmp == EOF)
break;
if (format[k - 1] == 'c') {
ch = va_arg(ap, char*);
if (tmp != EOF)
count += fscanf(fp, "%c", ch);
}
if (format[k - 1] == 'd') {
i = va_arg(ap, int*);
while ((tmp = getc(fp)) != EOF) {
if ((tmp >= '0' && tmp <= '9') || tmp == '-' || tmp == '+') {
ungetc(tmp, fp);
break;
}
}
if (tmp != EOF)
count += fscanf(fp, "%d", i);
}
if (format[k - 1] == 'f') {
f = va_arg(ap, float*);
while ((tmp = getc(fp)) != EOF) {
if ((tmp >= '0' && tmp <= '9') || tmp == '+' || tmp == '-' || tmp == '.') {
ungetc(tmp, fp);
break;
}
}
if (tmp != EOF)
count += fscanf(fp, "%f", f);
}
if (format[k - 1] == 's') {
s = va_arg(ap, char*);
while ((tmp = fgetc(fp) != EOF) && (!isprint(tmp) || tmp == ' '));
n = 0;
if (!feof(fp)) {
ungetc(tmp, fp);
while ((tmp = getc(fp)) != EOF) {
if (isprint(tmp) && tmp != ' ')
s[n++] = tmp;
else
break;
}
ungetc(tmp, fp);
}
s[n] = 0;
count++;
}
}
va_end(ap);
return count;
}
//控制台清屏
void ClearWindows() {
system("cls");
}
//文件打开失败
void FailedOpenFile() {
printf("open file failed\n");
}
#endif // !SCANF_C
部分函数摘自https://www.cnblogs.com/kangjianwei101/category/791617.html