如果有帮助到学弟学妹,可以评论下,让我开心开心。
虽然有现成代码,但是还是建议好好分析一下这个代码,实在不行用GPT辅助理解,这样期末考试的题思路会很清晰,有助于写大题。
如果有乱码问题,考虑是不是编码问题(utf-8与gbk)
.txt等文件我为了方便大家知道对应文件名所以第一行加了//注释,到时候记得删,不然读取文件的格式不对


#include<bits/stdc++.h>
void Print(const std::string& temp, const std::map<std::string, int>& keywordMap) {
if (!temp.empty()) {
int flag = -1;
// 判断 temp 是否为符号关键字
if (!((temp[0] >= 'a' && temp[0] <= 'z') || (temp[0] >= 'A' && temp[0] <= 'Z') || (temp[0] >= '0' && temp[0] <= '9'))) {
flag = keywordMap.count(temp) ? keywordMap.at(temp) : -1;
if (flag != -1) {
std::cout << "{TypeNum(syn): " << flag << ", TokenLiteral(token): " << temp << ", Num: -1" << "}\n";
return;
}else {
if(temp[0] == '_'){
int underflag = 0;
for (int i = 0; i < int(temp.length());i++){
if(temp[i]!='_'){
underflag = 1;
}
}
if(underflag==1){
std::cout << "{TypeNum(syn): " << 46 << ", TokenLiteral(token): " << temp << ", Num: -1" << "}\n"; // 非关键字标识符
}else{
std::cout << temp << ", error!!!\n"; // 非法内容
}
}else{
std::cout << temp << ", error!!!\n"; // 非法内容
}
}
} else {
// 判断是否为数字
if (isdigit(temp[0])) {
bool isNumber = true;
for (char ch : temp) {
if (!isdigit(ch)) {
isNumber = false;
break;
}
}
if (isNumber) {
std::cout << "{TypeNum(syn): " << 45 << ", TokenLiteral(token): " << temp << ", Num: " << temp << "}\n"; // 数字标识符
} else {
std::cout << temp << ", error!!!\n"; // 非法内容
}
} else {
// 检查是否为关键字
flag = keywordMap.count(temp) ? keywordMap.at(temp) : -1;
if (flag == -1) {
std::cout << "{TypeNum(syn): " << 46 << ", TokenLiteral(token): " << temp << ", Num: -1" << "}\n"; // 参数名
} else {
std::cout << "{TypeNum(syn): " << flag << ", TokenLiteral(token): " << temp << ", Num: -1" << "}\n";
}
}
}
}
}
int main() {
// 打开输入文件 text.txt 和输出文件 text_new.txt
std::ifstream fp("text.txt");
std::ofstream fp2("text_new.txt");
if (!fp.is_open()) { // 检查文件是否成功打开
std::cout << "File not found\n";
return 1;
}
// 使用 std::vector<char> 存储文件内容
std::vector<char> ch1;
char c;
// 逐个字符读取文件内容并存入 ch1
while (fp.get(c)) {
ch1.push_back(c);
}
std::string ch2; // 保存删除注释后的内容
// 去除注释
for (size_t i = 0; i < ch1.size(); i++) {
if (ch1[i] == '/' && ch1[i + 1] == '/') { // 单行注释
while (ch1[i] != '\n' && i < ch1.size()) {
i++;
}
}
else if (ch1[i] == '/' && ch1[i + 1] == '*') { // 多行注释
i += 2;
while (!(ch1[i] == '*' && ch1[i + 1] == '/') && i < ch1.size()) {
i++;
}
i++;
}
else if (ch1[i] == '\n'){ // 换行符
if(ch2.back() != ' '){
ch2 += ' ';
}
}
else if (ch1[i] == '\t'){ // 制表符
if(ch2.back() != ' '){
ch2 += ' ';
}
}
else if (ch1[i] == ' ' && ch2.back() == ' '){ // 多个空格
}
else { // 非注释内容添加到 ch2
ch2 += ch1[i];
}
}
std::string clean_str;
for (char c : ch2) {
clean_str += c;
fp2 << c; // 将字符写入新文件
}
fp.close(); // 关闭输入文件
fp2.close(); // 关闭输出文件
// 关键字表使用 std::map
std::map<std::string, int> keywordMap = {
{"main", 1}, {"if", 2}, {"then", 3}, {"while", 4}, {"do", 5},
{"static", 6}, {"int", 7}, {"double", 8}, {"struct", 9}, {"else", 10},
{"long", 11}, {"switch", 12}, {"case", 13}, {"typedef", 14}, {"char", 15},
{"return", 16}, {"const", 17}, {"float", 18}, {"break", 19}, {"short", 20},
{"!=", 21}, {"for", 22}, {"void", 23}, {"continue", 24}, {"#", 25},
{"<=", 26}, {">", 27}, {">=", 28}, {"=", 29}, {"[", 30}, {"]", 31},
{";", 32}, {"(", 33}, {")", 34}, {"+", 35}, {"-", 36}, {"*", 37},
{"/", 38}, {"**", 39}, {"==", 40}, {"<", 41}, {"<>", 42}, {"{", 43},
{"}", 44}, {"NUM", 45}, {"ID", 46}, {"++", 47}, {"--", 48}
};
// 使用 vector 存储关键字对
std::vector<std::pair<std::string, int>> keywordVec(keywordMap.begin(), keywordMap.end());
// 按照值排序
std::sort(keywordVec.begin(), keywordVec.end(), [](const auto& a, const auto& b) {
return a.second < b.second; // 根据值排序
});
// 打印 map 的内容,按列格式
int columnCount = 3; // 列数
int rowCount = (keywordVec.size() + columnCount - 1) / columnCount; // 计算行数
for (int row = 0; row < rowCount; ++row) {
for (int col = 0; col < columnCount; ++col) {
std::vector<std::pair<std::string, int>>::size_type index = row + col * rowCount; // 使用 size_type
if (index < keywordVec.size()) { // 确保索引在范围内
std::cout << "(" << std::left << std::setw(10) << keywordVec[index].first
<< ", " << std::setw(2) << keywordVec[index].second << ") ";
}
}
std::cout << std::endl; // 每行结束后换行
}
// 逐个单词分析内容并输出
std::string temp;
for (size_t i = 0; i < clean_str.size(); i++) {
if (clean_str[i] == ' '){ // 忽略空格
} else if (isalnum(clean_str[i]) || clean_str[i] == '_') { // 识别字母和数字,拼接为 temp
temp += clean_str[i];
if (i + 1 == clean_str.size() || (!isalnum(clean_str[i + 1]) && clean_str[i + 1] != '_')) { // 若下一个字符非字母数字则输出
Print(temp, keywordMap);
temp.clear();
}
} else { // 符号
temp += clean_str[i];
switch (clean_str[i])
{
case '=':
if(clean_str[i+1] == '='){
i++;
temp += clean_str[i];
}
break;
case '+':
if(clean_str[i+1] == '+'){
i++;
temp += clean_str[i];
}
break;
case '-':
if(clean_str[i+1] == '-'){
i++;
temp += clean_str[i];
}
break;
case '>':
if(clean_str[i+1] == '='){
i++;
temp += clean_str[i];
}
break;
case '<':
if(clean_str[i+1] == '='){
i++;
temp += clean_str[i];
}
break;
case '!':
if(clean_str[i+1] == '='){
i++;
temp += clean_str[i];
}
break;
default:
break;
}
Print(temp, keywordMap);
temp.clear();
}
}
return 0;
}
// test.txt
int main(){
int abc = 45a;
short __a_bq = 9;
int ___ = 2;
// notes
if (int i=1;i<2;i++){
/*
123
*/
a = 90;
}
return 0;
}
// 生成文件text_new.txt
int main(){ int abc = 45a; short __a_bq = 9; int ___ = 2; if (int i=1;i<2;i++){ a = 90; } return 0; }
部分结果:

298




被折叠的 条评论
为什么被折叠?



