代码拉取完成,页面将自动刷新
#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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。