您的位置:首页 > 文旅 > 旅游 > 乌鲁木齐疫情最新消息_上海新闻坊_手机免费建站app_鹤壁seo

乌鲁木齐疫情最新消息_上海新闻坊_手机免费建站app_鹤壁seo

2024/10/13 12:25:10 来源:https://blog.csdn.net/release_lonely/article/details/142889380  浏览:    关键词:乌鲁木齐疫情最新消息_上海新闻坊_手机免费建站app_鹤壁seo
乌鲁木齐疫情最新消息_上海新闻坊_手机免费建站app_鹤壁seo

改进了一下,加入了能够输出错误提示(注意:我们是用char接收的,但是遇到中文的时候因为码值的原因,一个中文字占三个char所以按照我们判断流程会输出三个错误提示,你们要是见意的话可以自行修改)

非常详细(包括跳过注释部分),不多说直接上代码(结合代码讲解)

#include<bits/stdc++.h>using namespace std;#define ARRAY_LENGTH(arr) (sizeof(arr) / sizeof(arr[0]))//关键词集合
string KEY_WORD[] = {"int","char","string","bool","float","double","true","false","return","if","else","while","for","default","do","public","static","switch","case","private","protected"};
//界符集合,我将'/'也加到界符表中用于判断是否是注释
char BOUND_CHAR[] = {',', ';', '(', ')', '{', '}', '[', ']', '\'', '\"', '/'};
int pos = 0;//当前读到的数的下标
short in_annotation = 0;//当前是否在注释的范围,1表示在并且是"//"这种类型,2表示在并且是"/**/"这种注释类型,默认不在(因为两种注释的退出条件不同)// 关键字,标识符,运算符,界符和常量
enum WordTypeKind{KEYWORD, IDENTIFIER, CONSTANT, OPERATOR, DELIMITER, ANNOTATION, ERROR, STOP
};//定义词
struct WORD {WordTypeKind wordType;string value;
};//读取文件中的内容
string openFile(string fileName) {ifstream readFile(fileName);//文件是否打开if(!readFile.is_open()) {cerr << "无法打开文件!" << '\n';return "";}string content, temp;while(getline(readFile, temp)) {// cout << content << endl;content += temp;content += '\n';}//关闭资源readFile.close();return content;
}//将字符串写入文件
bool writeFile(string fileName, string content) {ofstream writeFile(fileName);if(!writeFile.is_open()) {cerr << "无法打开文件!" << '\n';return false;}writeFile << content << endl;writeFile.close();return true;
}//判断一个单词是不是关键字,是返回true
bool isKeyWord(string word) {for(int i = 0; i < ARRAY_LENGTH(KEY_WORD); i++) {if(word == KEY_WORD[i]) return true;}return false;
}//分词器,将字符串分成最小单位(关键字,标识符,运算符,界符和常量),关键字我们可以自己根据使用的高级语句自定义
WORD getNextWord(string str) {string tempStr = "";//暂存这个单词WORD newWord;//返回的词newWord.wordType = STOP;//方便后续退出循环while (pos < str.length() && std::isspace(str[pos])) {if(in_annotation == 1 && str[pos] == '\n') in_annotation = 0;//退出注释状态++pos; // 跳过空白字符}if (pos >= str.length()) {return newWord; // 结束}if(pos < str.length()) {//不越界char c = str[pos++];tempStr += c;//标识符的命名规范:只能以字母或'_'开头if(isalpha(c) || c == '_') {//这个词是关键词或标识符// isalnum(str[pos])这个函数用来检查传递给它的字符是否是字母(isalpha)或者是数字(isdigit)while(pos < str.length() && (isalnum(str[pos]) || str[pos] == '_')) {tempStr += str[pos++];}//判断这个单词是标识符or关键字if(isKeyWord(tempStr)) {//是关键字newWord.wordType = KEYWORD;} else {//标识符newWord.wordType = IDENTIFIER;}} else if(isdigit(c)) {//数字开头只可能是常数,我们把所有数字读完while(pos < str.length() && isdigit(str[pos])) {tempStr += str[pos++];}newWord.wordType = CONSTANT;} else if(c >= 0 && c <= 127) {//只可能是运算符或界符for(int i = 0; i < ARRAY_LENGTH(BOUND_CHAR); i++) {//是不是界符if(c == BOUND_CHAR[i]) {//遇到'/'判断是不是注释和注释类型if(c == '/' && pos < str.length() && str[pos] == '/') {//是'//'类型in_annotation = 1; newWord.wordType = ANNOTATION;tempStr += str[pos++];newWord.value = tempStr;return newWord;} else if(c == '/' && pos < str.length() && str[pos] == '*') {//是'/**/'类型in_annotation = 2;newWord.wordType = ANNOTATION;tempStr += str[pos++];newWord.value = tempStr;return newWord;}newWord.wordType = DELIMITER;break;}}//是运算符,注意双目运算符(三目运算符我们就不考虑了)if(pos < str.length()) {//注意不要越界newWord.wordType = OPERATOR;char nextChar = str[pos];//特判一下'*/'的情况,因为这是第二种注释的退出标识if(c == '*' && nextChar == '/') {//退出注释状态,下面也不用看了in_annotation = 0;tempStr += nextChar;newWord.wordType = ANNOTATION;pos++;newWord.value = tempStr;return newWord;}//考虑一下所有的双目运算符if((c == '+' || c == '-' || c == '*' || c == '/' || c == '!' || c == '^' || c == '%' || c == '=' || c == '<' || c == '>') && nextChar == '=') {tempStr += nextChar;//更新一下nextCharpos++;}if(c == '+' && nextChar == '+') {tempStr += nextChar;//更新一下nextCharpos++;}if(c == '-' && nextChar == '-') {tempStr += nextChar;//更新一下nextCharpos++;}if(c == '&' && nextChar == '&') {tempStr += nextChar;//更新一下nextCharpos++;}if(c == '|' && nextChar == '|') {tempStr += nextChar;//更新一下nextCharpos++;}if(c == '<' && nextChar == '<') {tempStr += nextChar;//更新一下nextCharpos++;}if(c == '>' && nextChar == '>') {tempStr += nextChar;//更新一下nextCharpos++;}}} else {newWord.wordType = ERROR;}newWord.value = tempStr;}return newWord;
}//换行符占一个长度!!!int main() {//先读取txt文件string fileName = "E:\\program\\bianyiyuanli\\1.txt";string str = openFile(fileName);WORD word;while((word = getNextWord(str)).wordType != STOP) {if(word.wordType == ERROR && in_annotation == 0) cout << "出现ERROR!!!" << '\n';else if(word.wordType != ANNOTATION && in_annotation == 0) {//我们只有在词语不是注释类型和不在注释状态才输出它std::cout << "[" << word.wordType << ",   " << word.value << "]" << std::endl;}}return 1;
}

txt文件内容:

/*我好帅*/
int main() {
//你好
    int x = 10;
    好
    return x;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com