代码自动生成工具1.0

2 篇文章 0 订阅

现在可以生成变量 

KeyWords.h

#ifndef _KEYWORDS_H_
#define _KEYWORDS_H_
#include<map>
#include<vector>
const enum WORDINDEX {
	ERROR = -1,
	INT,
	FLOAT,
	DOUBLE,
	CHAR,
	STRING,

	/*******/
	FOR = 100

};
using namespace std;
class KeyWords {
public:
	map<WORDINDEX,string> keyWords();
	KeyWords();
private:
	map<WORDINDEX, string>_keyWords;
};

map<WORDINDEX, string> KeyWords::keyWords() {
	return this->_keyWords;
}

KeyWords::KeyWords() {
	_keyWords = {
		make_pair(WORDINDEX::INT, "int"),
		make_pair(WORDINDEX::FLOAT, "float"),
		make_pair(WORDINDEX::DOUBLE, "double"),
		make_pair(WORDINDEX::CHAR, "char"),
		make_pair(WORDINDEX::STRING, "string")
	
	};
}
#endif // !_KEYWORDS_H_

 util.h

#pragma once
#include"KeyWords.h"
/*
功能: 输出容器的元素
_TYEP_ :输出类型
_DATA_ :容器
INTERVAL:每个数据间隔符号
*/
#define PRINT(_TYPE_, _DATA_, INTERVAL) copy(_DATA_.begin(), _DATA_.end(), std::ostream_iterator<std::_TYPE_>{std::cout, INTERVAL});

/*
功能: 判断字符是否为字母
*/
#define IS_LETTER(_CHAR_) ((_CHAR_ >= 'a' && _CHAR_ <= 'z') || (_CHAR_ >= 'A' && _CHAR_ <= 'Z'))

/*
功能: 判断字符是否为数字
*/
#define IS_DIGITAL(_CHAR_) ((_CHAR_ <= '9' && _CHAR_ >= '0'))


/*
功能: 重写vector的push_back,根据key判断末尾是否加分号
返回值: void
参数: vector<string> &output(储存容器)、string insert(插入数据)、
	  WORDINDEX key(key值)、string insert_(插入数据)
*/
void push(vector<string> &output, string insert, WORDINDEX key,string insert_ = "") {
	if(key < 100)
		output.push_back(insert + " " + insert_ + ";");
	else
		output.push_back(insert + " " + insert_);
}

/*
功能: 判断输入的字符串是否含有关键字,如果有就返回对应的key值,并且将传入的字符串中的关键字去除,并且可以判断变量名是否合法
返回值: WORDINDEX (map的key值,通过这个值获得对应关键字)
参数: string &input(待处理字符串)
*/
WORDINDEX judgeAndCut(string &input) {
	//储存关键字的map
	map<string, WORDINDEX> strToIndex{
		make_pair("整型", WORDINDEX::INT),
		make_pair("单精度浮点型", WORDINDEX::FLOAT),
		make_pair("双精度浮点型", WORDINDEX::DOUBLE),
		make_pair("字符型", WORDINDEX::CHAR),
		make_pair("字符串型", WORDINDEX::STRING)
	};
	//克隆一份待处理字符串
	string input_clone(input);
	//将克隆字符串中的字母、数字、下划线删除,这样只会留下关键字,这时候变量名就没了,所以用克隆来做,防止待处理字符串被破坏
	//由于erase每次只能删除一个所以循环删除字母、数字、下划线
	input_clone.erase(remove_if(begin(input_clone), end(input_clone), [](char c) {
		return (IS_LETTER(c) || IS_DIGITAL(c) || (c == '_'));
	}), input_clone.end());
	//删除过程可视化,将来注释
	cout << "<" << input <<">删除字母、数字、下划线后:"<< input_clone << endl;
	
	//如果iter不是指向strToIndex的结尾就找到了
	auto iter = strToIndex.find(input_clone);
	if (iter != strToIndex.end()) {
		if (iter->second < 100/*key<100就是变量,需要切割变量名,key>=100不需要*/) {
		/*******************************/
		//这里是声明类型需要变量名的情况
		/******************************/

		//由于关键字都是开头字母,所以将从开头 到 找到的关键字的长度 的内容 删除,就留下了变量名
			input.erase(input.begin(), input.begin() + iter->first.size());
			//判断变量名是否符合规范

			KeyWords words;
			map<WORDINDEX, std::string> temp(words.keyWords());//这里不能直接用words.keyWords().begin()/end(),否则会越界(未找到原因)
			auto find_item = find_if(temp.begin(), temp.end(), [input](const map<WORDINDEX, std::string>::value_type item) {
				return item.second == input;
			});
			/*
			//由于是删除字母、下划线、数字后进行判断的,所以包含其他符号的话在“if (iter != strToIndex.end()) {”处程序就会终止,此处代码留用
			for (auto ctemp : input) {
				if (!(IS_LETTER(ctemp) || ctemp == '_' || IS_DIGITAL(ctemp))) {
					cout << "变量名请以字母或下划线开头,字母、下划线、数字组成,请勿使用其他字符" << endl;
					return WORDINDEX::ERROR;
				}
			}
			*/
			if (find_item != temp.end()) {
				cout << "变量名请勿使用系统内置关键字" << endl;
			}
			else if (IS_LETTER(input[0]) || input[0] == '_') {
				return iter->second;
			}
			else {
				cout << "变量名请以字母或下划线开头,字母、下划线、数字组成,请勿使用其他字符" << endl;
			}
			//判断变量名是否符合规范结束
		}
		else {
		/***************************************/
		//这里是循环控制语句等不需要变量名的情况
		/***************************************/
		}
		
	}
	//如果没找到返回错误key
	return WORDINDEX::ERROR;
}

/*
功能: 输出结果
*/
void result(string input) {
	vector<std::string> output{ "output:" };
	KeyWords words;
	WORDINDEX key = judgeAndCut(input);
	if (key >= 0)
		push(output, words.keyWords().at(key), key, input);
	else
		push(output, "error", WORDINDEX::FOR);
	PRINT(string, output, " ");
}

测试 

#include "pch.h"
#include"CppKeyWords.h"
#include"util.h"
using namespace std;
int main()
{
	string input;
	while (true) {
		cin >> input;
		result(input);
		cout << endl  << endl;
	}
	
	return 0;
}

 

输出

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值