⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 课题四 保留字(或关键字)统计程序.cpp

📁 字符统计程序,统计输入并输出字符由vc实现
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ListNum = 66;
char *keyList[]={//建立关键字保留字表
	"asm","auto","bad_cast","bad_typeid","bool","break","case","catch","char","class",
	"const","const_cast","continue","default","delete","do","double","dynamic_cast","else",
	"enum","except","explicit","extern","false","finally","float","for","friend","goto",
	"if","inline","int","long","mutable","namespace","new","operator","private","protected",
	"public","register","reinterpret_cast","return","short","signed","sizeof","static",
	"static_cast","struct","switch","template","this","throw","true","try","type_info",
	"typedef","typeid","typename","union","unsigned","using","virtual","void","volatile","while"};
bool isValid(char ch)//判断所得到的数据是否是有效数据!
{
	if ((ch>='0')&&(ch<='9'))
		return true;
	if ((ch>='A')&&(ch<='Z'))
		return true;
	if ((ch>='a')&&(ch<='z'))
		return true;
	if (ch=='_')
		return true;
	return false;
}
bool find(char key[])//函数作用为:匹配关键字
{
	for (int i=0;i<ListNum;i++)
	{
		if (strcmp(keyList[i],key)==0)
			return true;
	}
	return false;
} 
void main()//int argc, char* argv[]
{   	
	printf("运行此程序之前在程序的文件夹下面必须有需要调试的文件\n");
	printf("程序结果为输出来两张文件:\n1.保留字(或关键字)及程序次数,2.非保留字(或关键字).\n");
	char name[128];
	FILE *fSrc = NULL;//定义指针变量并初始化
	while (fSrc==NULL)
	{
		printf("请输入源程序完整的文件名(即文件名+扩展名):");
		scanf("%s",name);
		fSrc = fopen(name,"r");
	}

	FILE *fKey = NULL;//保存关键字
	while (fKey==NULL)
	{
		printf("请输入保存关键字的文件名:");
		scanf("%s",name);
		fKey = fopen(name,"w");
	}

	FILE *fCom= NULL;//保存非关键字
	while (fCom==NULL)
	{
		printf("请输入非关键字的文件名:");
		scanf("%s",name);
		fCom = fopen(name,"w");
	}
    	char buf[128];
	char ch;
	int len;
	while ((ch = fgetc(fSrc))!=EOF)
	{
		if (!isValid(ch))
			continue;
		len = 0;
		while ((ch!=EOF)&&(isValid(ch)))
		{
			buf[len]=ch;
			ch = fgetc(fSrc);
			len++;
		}
		buf[len]=NULL;
		if (find(buf))
			fprintf(fKey,"%s\n",buf);
		else
			fprintf(fCom,"%s\n",buf);
	}

	fclose(fSrc);
	fclose(fCom);
	fclose(fKey);
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -