C语言数据结构--预定义头文件

#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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值