⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 variable.c

📁 可以在嵌入式应用中
💻 C
字号:
#include <CsAgb.h>
#include <rbasic.h>
#include <tree.h>
#include <rb_string.h>
#define Error_color RGB(0,30,0)
extern void exp_adj(char *name);
u8 check_var_name(char *name)//检查变量名是否合法(出错返回1)
{
   u8 i=0;
   u8 len;
   exp_adj(name);
   len=str_len(name);
   if (len==0) return 1;//空的变量名
   if (!is_letter(name[0])) return 1;//变量名不以字母开头
   while (name[i]!='\0')
   {
      if (!(is_letter(name[i]) || name[i]=='_' || is_number(name[i]))) return 1;
      i++;
   }
   str_up(name);
   if (is_keyword(rb_key,name)||is_keyword(rb_math_key,name)) return 1;//变量名是关健字
   name[len]='$';
   name[len+1]='\0';
   if (is_keyword(rb_str_key,name)) {name[len]='\0';return 1;}//变量为字符串函数关健字
   name[len]='\0';
   return 0;//OK
}
struct tre_child //结点子树
{
	struct tre_node *first; //结点首子树
	struct tre_child *next; //其它子树
};
struct tre_node //树结点
{
	char name; //结点名
	struct tre_child *ch;//结点子树
};
void rb_say_error(char *mes)
{
   endl();
   Rprint(Error_color,mes);
}
void rb_int_register(char *name,int value)//整形变量注册
{
   if (rb_int_count>=v_max_count)
   {
      rb_error=3;
      return;
   }
   else if(check_var_name(name))
   {
      rb_error=22;
      return;
   }
   else
   {
      ins_node(ins_word(rb_int,name),rb_int_count);
      v_int[rb_int_count]=(int *) malloc(sizeof(int));
      if (v_int[rb_int_count]==NULL)//分配失败
      {
         rb_say_error("OUT OF MEMORY.");
         rb_error=1;
         return;
      }
      *v_int[rb_int_count]=value;//初始化
      rb_int_count++;
   }
}
int rb_get_int(char *name)//取整形变量的值
{
   struct tre_node *loc;
   str_up(name);
   loc=word_is_exists(rb_int,name);
   if (loc==NULL)//变量尚未注册
   {
      rb_int_register(name,0);//注册变量
      return 0;
   }
   return *v_int[((loc->ch)->first)->name];//返回变量值
}
void rb_give_int(char *name,int value)//整型变量赋值
{
   struct tre_node *loc;
   str_up(name);
   loc=word_is_exists(rb_int,name);
   if (loc==NULL) rb_int_register(name,value);//注册并赋值
   else if (v_int[((loc->ch)->first)->name]!=NULL)
      *v_int[((loc->ch)->first)->name]=value;
}
void str_copy(char *str,char *value)
{
   u8 i=0;
   while (*(value+i)!='\0' && i<str_max_len-1)
   {
      *(str+i)=*(value+i);
      i++;
   }
   *(str+i)='\0';
}

void rb_str_register(char *name,char *value)//字符串变量注册
{
   if (rb_str_count>=v_max_count)
   {
      rb_error=3;
      return;
   }
   else if(check_var_name(name))
   {
      rb_error=22;
      return;
   }
   else
   {
      ins_node(ins_word(rb_str,name),rb_str_count);
      v_str[rb_str_count]=(char *) malloc(sizeof(char)*str_max_len);
      if (v_str[rb_str_count]==NULL)//分配失败
      {
         rb_error=1;
         return;
      }
      str_copy(v_str[rb_str_count],value);//初始化
      rb_str_count++;
   }
}
void rb_give_str(char *name,char *value)//字符串变量赋值
{
   struct tre_node *loc;
   str_up(name);
   loc=word_is_exists(rb_str,name);
   if (loc==NULL) rb_str_register(name,value);//注册并赋值
   else if (v_str[((loc->ch)->first)->name]!=NULL)
      str_copy(v_str[((loc->ch)->first)->name],value);
}
char * rb_get_str(char *name)
{
   struct tre_node *loc;
   str_up(name);
   loc=word_is_exists(rb_str,name);
   if (loc==NULL)//变量尚未注册
   {
      rb_str_register(name,"");//注册变量
      loc=word_is_exists(rb_str,name);
   }
   return v_str[((loc->ch)->first)->name];//返回变量值
}
void rb_float_register(char *name,double value)//浮点数变量注册
{
   if (rb_float_count>=v_max_count)
   {
      rb_error=3;
      return;
   }
   else if(check_var_name(name))
   {
      rb_error=22;
      return;
   }
   else
   {
      ins_node(ins_word(rb_float,name),rb_float_count);
      v_float[rb_float_count]=(double *) malloc(sizeof(double));
      if (v_float[rb_float_count]==NULL)//分配失败
      {
         rb_error=1;
         return;
      }
      *v_float[rb_float_count]=value;//初始化
      rb_float_count++;
   }
}
double rb_get_float(char *name)//取浮点数变量的值
{
   struct tre_node *loc;
   str_up(name);
   loc=word_is_exists(rb_float,name);
   if (loc==NULL)//变量尚未注册
   {
      rb_float_register(name,0.0);//注册变量
      return 0.0;
   }
   return *v_float[((loc->ch)->first)->name];//返回变量值
}

void rb_give_float(char *name,double value)//浮点数变量赋值
{
   struct tre_node *loc;
   str_up(name);
   loc=word_is_exists(rb_float,name);
   if (loc==NULL) rb_float_register(name,value);//注册并赋值
   else if (v_float[((loc->ch)->first)->name]!=NULL)
      *v_float[((loc->ch)->first)->name]=value;
}

void rb_int_dim_register(char *name,u8 num)//整形数组变量注册
{
   struct tre_node *loc;
   if (check_var_name(name))
   {
      rb_error=26;
      return;
   }
   loc=word_is_exists(rb_int_dim,name);
   if (loc)//重复定义
   {
      rb_error=2;
      return;
   }
   if (num>v_max_dim_count || rb_int_dim_count>=v_max_count || num<1)
   {
      rb_error=3;
      return;
   }
   ins_node(ins_word(rb_int_dim,name),rb_int_dim_count);
   v_int_dim[rb_int_dim_count]=(int *) malloc(sizeof(int)*num);
   if (v_int_dim[rb_int_dim_count]==NULL)//分配失败
   {
      rb_error=1;
      return;
   }
   int_dim[rb_int_dim_count]=num;
   rb_int_dim_count++;
}
int rb_get_int_dim(char *name,u8 num)//取整形数组变量的值
{
   struct tre_node *loc;
   str_up(name);
   loc=word_is_exists(rb_int_dim,name);
   if (loc==NULL)//变量尚未注册
   {
      rb_error=4;
      return 0;
   }
   if (num<1 || num>int_dim[((loc->ch)->first)->name])//检查下标
   {
      rb_error=3;
      return 0;
   }
   return *(v_int_dim[((loc->ch)->first)->name]+num-1);//返回变量值
}
void rb_give_int_dim(char *name,int value,u8 num)//整型数组变量赋值
{
   struct tre_node *loc;
   str_up(name);
   loc=word_is_exists(rb_int_dim,name);
   if (loc==NULL)//变量尚未注册
   {
      rb_error=4;
      return;
   }
   if (num<1 || num>int_dim[((loc->ch)->first)->name])//检查下标
   {
      rb_error=3;
      return;
   }
   *(v_int_dim[((loc->ch)->first)->name]+num-1)=value;
}
void rb_float_dim_register(char *name,u8 num)//浮点数组变量注册
{
   struct tre_node *loc;
   if (check_var_name(name))
   {
      rb_error=22;
      return;
   }
   loc=word_is_exists(rb_float_dim,name);
   if (loc)//重复定义
   {
      rb_error=2;
      return;
   }
   if (num>v_max_dim_count || rb_float_dim_count>=v_max_count || num<1)
   {
      rb_error=3;
      return;
   }
   ins_node(ins_word(rb_float_dim,name),rb_float_dim_count);
   v_float_dim[rb_float_dim_count]=(double *) malloc(sizeof(double)*num);
   if (v_float_dim[rb_float_dim_count]==NULL)//分配失败
   {
      rb_error=1;
      return;
   }
   float_dim[rb_float_dim_count]=num;
   rb_float_dim_count++;
}
double rb_get_float_dim(char *name,u8 num)//取浮点数组变量的值
{
   struct tre_node *loc;
   str_up(name);
   loc=word_is_exists(rb_float_dim,name);
   if (loc==NULL)//变量尚未注册
   {
      rb_error=4;
      return 0;
   }
   if (num<1 || num>int_dim[((loc->ch)->first)->name])//检查下标
   {
      rb_error=3;
      return 0.0;
   }
   return *(v_float_dim[((loc->ch)->first)->name]+num-1);//返回变量值
}
void rb_give_float_dim(char *name,double value,u8 num)//浮点数组变量赋值
{
   struct tre_node *loc;
   str_up(name);
   loc=word_is_exists(rb_float_dim,name);
   if (loc==NULL)//变量尚未注册
   {
      rb_error=4;
      return;
   }
   if (num<1 || num>float_dim[((loc->ch)->first)->name])//检查下标
   {
      rb_error=3;
      return;
   }
   *(v_float_dim[((loc->ch)->first)->name]+num-1)=value;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -