2 Star 5 Fork 2

koifishly/function_generator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
functiongenerator.h 5.67 KB
一键复制 编辑 原始数据 按行查看 历史
koifishly 提交于 4年前 . init
#ifndef B30540203B62422CA161D73980D50D95
#define B30540203B62422CA161D73980D50D95
#include <stdlib.h>
#include <time.h>
#include <list>
#include <string>
#include "shared/shared.h"
#define FLAG_SENTENCE_DEFAULT 0
#define NUM_SUPPORTED_FUNCTION 2
using std::list;
using std::string;
class CFunctionGenerator
{
string _function_declare;
list < CFunctionGenerator* > _lst_functions;
list<char*> _lst_sentence;
list<int> _lst_sentence_flag;
int get_random_int(int max/*not included*/);
int get_random_int(int min/*included*/, int max/*not included*/);
void get_random_str(char* buffer, int str_length, bool is_add_zero);
void get_random_var_name(char* buffer, int str_length, bool is_add_zero);
int push_sentence(char* p_sentence, int flag)
{
_lst_sentence_flag.push_back(FLAG_SENTENCE_DEFAULT);
if (flag == FLAG_SENTENCE_DEFAULT)
{
_lst_sentence.push_back(p_sentence);
}
return _lst_sentence.size();
}
void function_add_iai_ia(const char* p_int_ary_var_name, int size_of_ary) {
int random_num = get_random_int(70);
// 随机变量名
// 为了防止后面可能出现的变量名重复的问题
char buf_loop_name[16];
get_random_var_name(buf_loop_name, 15, true);
// 代码格式
char code_format[] =
"for (int %s = 0; %s < %d; %s++){%s[%s] = %s[%s] + %d;}";
char cbuf[1024];
sprintf(cbuf, code_format,
buf_loop_name, buf_loop_name, size_of_ary, buf_loop_name,
p_int_ary_var_name, buf_loop_name, p_int_ary_var_name, buf_loop_name, random_num);
// 保存到对象中
int code_length = strlen(cbuf);
char* buf_saved = new char[code_length + 1];
strcpy(buf_saved, cbuf);
this->push_sentence(buf_saved, FLAG_SENTENCE_DEFAULT);
}
void function_sub_iai_ia(const char* p_int_ary_var_name, int size_of_ary) {
int random_num = get_random_int(70);
// 随机变量名
// 为了防止后面可能出现的变量名重复的问题
char buf_loop_name[16];
get_random_var_name(buf_loop_name, 15, true);
// 代码格式
char code_format[] =
"for (int %s = 0; %s < %d; %s++){%s[%s] = %s[%s] - %d;}";
char cbuf[1024];
sprintf(cbuf, code_format,
buf_loop_name, buf_loop_name, size_of_ary, buf_loop_name,
p_int_ary_var_name, buf_loop_name, p_int_ary_var_name, buf_loop_name, random_num);
// 保存到对象中
int code_length = strlen(cbuf);
char* buf_saved = new char[code_length + 1];
strcpy(buf_saved, cbuf);
this->push_sentence(buf_saved, FLAG_SENTENCE_DEFAULT);
}
public:
/*
* @func_declare the function declare eg. void get_string(int num)
*/
CFunctionGenerator() {
// init random seed
srand(time(0));
}
~CFunctionGenerator()
{
list< char*>::iterator it_bg_s = _lst_sentence.begin();
list< char*>::iterator it_ed_s = _lst_sentence.end();
list< CFunctionGenerator*>::iterator it_bg_fc = _lst_functions.begin();
list< CFunctionGenerator*>::iterator it_ed_fc = _lst_functions.end();
for (; it_bg_s != it_ed_s; it_bg_s++)
{
char* p_sentence = *it_bg_s;
if (p_sentence != nullptr)
{
delete[] p_sentence;
}
}
for (; it_bg_fc != it_ed_fc; it_bg_fc++)
{
CFunctionGenerator* p_function = *it_bg_fc;
if (p_function != nullptr)
{
delete p_function;
}
}
}
bool gen_string2intAry(const char* func_name, const char* str_var_name, int str_len, int num, const char* p_int_ary_var_name, int size_of_ary)
{
// init function declare
char buf_funcname[] = "void %s (const char* %s, int* %s)";
char buffer[512];
sprintf(buffer, buf_funcname,
func_name, str_var_name, p_int_ary_var_name);
_function_declare.append(buffer);
// init
char buf_loop_name[16];
get_random_var_name(buf_loop_name, 15, true);
char code_format[] =
"for (int %s = 0; %s < %d; %s++){%s[%s] = %s%s%s%s;}";
char cbuf[1024];
sprintf(cbuf, code_format,
buf_loop_name, buf_loop_name, str_len, buf_loop_name,
p_int_ary_var_name, buf_loop_name, str_var_name, "[", buf_loop_name, "]");
int code_length = strlen(cbuf);
char* buf_saved = new char[code_length + 1];
strcpy(buf_saved, cbuf);
this->push_sentence(buf_saved, FLAG_SENTENCE_DEFAULT);
int* pi_id_funcs = new int[num];
for (int i = 0; i < num; i++)
{
switch (get_random_int(NUM_SUPPORTED_FUNCTION))
{
case 0:
function_add_iai_ia(p_int_ary_var_name, size_of_ary);
break;
case 1:
function_sub_iai_ia(p_int_ary_var_name, size_of_ary);
break;
}
}
return true;
}
void log_print()
{
printf("%s \r\n{\r\n",_function_declare.c_str());
list< char*>::iterator it_bg_s = _lst_sentence.begin();
list< char*>::iterator it_ed_s = _lst_sentence.end();
list< int>::iterator it_bg_f = _lst_sentence_flag.begin();
list< int>::iterator it_ed_f = _lst_sentence_flag.end();
for (; it_bg_s != it_ed_s; it_bg_s++, it_bg_f++)
{
char* p_sentence = *it_bg_s;
int flag = *it_bg_f;
if (flag == FLAG_SENTENCE_DEFAULT)
{
printf(" %s\r\n", p_sentence);
}
}
printf("}");
}
};
#endif // !B30540203B62422CA161D73980D50D95
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/koifishly/function_generator.git
git@gitee.com:koifishly/function_generator.git
koifishly
function_generator
function_generator
master

搜索帮助