init
Declaration of variables with identifiers generated in the program text.
Appeal:
var_by_txt("init", string string_with_varnames, string vartype, string local_or_global, void preset_value);
Здесь:
string_with_varnames is a string containing one or a list of variable identifiers separated by commas ("var1" or "var1,var2,var3").
vartype is the type of the variable being declared - "string", "int", "double", "bool".
local_or_global - in which visibility level to create variables - "global" or "local".
preset_value - the value assigned to variables by default.
Global variables can be created in a loop.
Local variables cannot be created in a loop or in other cases using curly brackets, because after leaving the lowering area {} they are deleted.
This is how we would write:
if(...)
{
// declaring a local variable k in a reduced scope
int k = 1;
}
// here, outside of curly brackets, the local variable k already does not exist
To perform a mass declaration of local variables, first form a string with identifiers separated by commas, and then execute the var_by_txt("init"...) function.
Re-declaring global variables may result in an error.
To avoid this, use var_by_txt("is_exist",...).
Example.
//************************************************************
// Declare variables with identificators, that creating from text string
// function var_by_txt()
// LABPP 2021
//************************************************************
int main()
{
test_global_variables();
test_local_variables();
}
// Form name for variable
string make_varname(string base, int ii, int jj)
{
return base + "_" + itoa(ii) + "_" + itoa(jj)
}
// Test for global variables
int test_global_variables()
{
int i,j;
string basename = "glob_var";
cout << "---------------------------\n";
cout << "test_global_variables\n";
// if early not created variable with name "glob_var_0_0", then create all other variables from "glob_var_0_0" to "glob_var_9_9"
if (var_by_txt("is_exist",make_varname(basename,0,0))==false)
{
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
cout << "declare variable " << make_varname(basename,i,j) << "\n";
var_by_txt("init", make_varname(basename, i, j), "string", "global", "glob value " + itoa(i) + "_" + itoa(j));
}
}
}
string s = glob_var_7_7;
coutvar << s;
glob_var_7_7 = "new value for 7_7";
cout << "All values from all global variables:\n";
string varname;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
varname = make_varname(basename, i, j);
cout << varname + "="<< var_by_txt("get", varname) << "\n";
}
}
return 0;
}
// Test for local variables
// unlike global variables, local variables should be created in the function body no deeper than other curly brackets
// { }
// otherwise it lowers the scope.
// Therefore, for local variables, first form a string with the names of variables separated by commas.
// This technique is also possible for global variables.
int test_local_variables()
{
int i, j;
string basename = "local_var";
cout << "---------------------------\n";
cout << "test_local_variables\n";
string string_with_varnames;
cout << "forming a list of variable names in a comma-separated string\n";
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
string_with_varnames += make_varname(basename, i, j) + ",";
}
}
coutvar << string_with_varnames;
// create variables from "local_var_0_0" to "local_var_9_9", initiate with an empty string (you can use another value)
var_by_txt("init", string_with_varnames, "string", "local", "");
// присвоение значений
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
var_by_txt("set", make_varname(basename, i, j), "local value " + itoa(i) + "_" + itoa(j));
}
}
// direct access to the variable
string s = local_var_7_7;
coutvar << s;
local_var_7_7 = "new value for 7_7";
cout << "All values of created local variables:\n";
string varname;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
varname = make_varname(basename, i, j);
cout << varname + "=" << var_by_txt("get", varname) << "\n";
}
}
return 0;
}