问题描述
我在 C++ 编码方面的经验并不丰富,但我边学边学。但是,我无法正确查询如何执行此操作,可能使用了错误的术语或未充分表达我的愿望。情况是这样的。
我在一个结构下设置了很多变量(3x12):
struct Tracking
{
String Title;
BoolArray n24hr;
bool State;
unsigned char Days,Weeks;
uint16_t Minutes,TotalMinutes,Daily,Weekly,Monthly,n7d[7],n4w[4];
} Components[3];
我也有代码在不同的“级别”上执行基本相同的事情 3 次,例如每天、每周、每月。它跟踪这些时间段的状态、填充数组、查找总数和占空比等。它将分钟填充为天,当达到一周时,它将总数放入周格式,并重复直到达到每月水平。所以基本上,我让它做类似的事情:
在我的主循环中:
//calls status formula
StatusFormula();
//status formula defined
void StatusFormula()
{
// for each element of Components:
//determine current status
//for daily
//add it to the correct spot in the array
//perform calculations on it
//when it reaches a week:
//add it to the correct spot in the next array
//perform calculations on it
//when it reaches a month:
//add it to the correct spot in the next array
//perform calculations on it
}
这些计算基本相同,唯一的区别是结构成员名称和计算常量(即 MinsADay、DaysAWk 等)。
我可以让它以这种方式工作,它只是意味着更多的行,如果我想更改某些内容,我必须重复 3 次。我想要的是这样的:
在我的主循环中:
//calls status formula
StatusFormula("Daily"); //sends the status formula information to decide which level (daily,weekly,monthly),it supposed to work on
if (Components[i].Minutes == MinsADay)
{
StatusFormula("Weekly"); //sends the status formula information to decide which level (daily,it supposed to work on
if (Components[i].Daily == DaysAWk)
{
StatusFormula("Monthly");
}
}
//status formula defined
void StatusFormula()
{
//determine which level & variables to use (I would probably use case for this),then
//add it to the correct spot in the correct array
//perform calculations on it
}
我尝试使用字符串传递关卡,但没有奏效:
在我的主循环中:
StatusFormula(i,"Daily"); //sending data to formula,where i is value 0 to 2 for the Components array & defined earlier in the for loop.
//formula defined as:
void StatusFormula(uint8_t counter,string level)
{Components[counter].level -= //etc... performing calculations as desired.
//so I thought this should evaluate to "Components[i].Daily -=" (& i would be a value 0 to 2) & treat it like the structure,but it doesn't work that way apparently.
我尝试传递结构和变量本身,但这也不起作用:
在我的主循环中:
StatusFormula(i,Components[i].Daily); //sending data to formula
//formula defined as:
void StatusFormula(uint8_t counter,Tracking& level)
{level -= //etc... //(level should be Components[i].Daily -=" (& i would be a value 0 to 2)) this didn't work either.
我找不到任何谷歌搜索来帮助我,我试错了很多方法,但我无法弄清楚如何在 C++ 中做到这一点,更不用说在 Arduino 平台上了。在 Excel VBA 中,我只会将变量作为字符串传递给公式,该公式将替换单词,然后将其视为变量,但我无法在 C++ 中实现这一点。另请注意,我将尝试将其定义为单独的文件/选项卡,以便我的大量代码文件更易于阅读/编辑,以防万一。我会直接粘贴我的代码,但它很长而且超级混乱。
我想我要问的是如何将结构和/或结构成员传递给公式,即相当于:
case 1: //"Daily"
//use Components[i].Daily & Components[i].Minutes & MinsaDay
break;
case 2: //"Weekly"
//use Components[i].Weekly & Components[i].Days & DaysaWk
break;
//etc.
我觉得应该有一种方法,我只是缺少一个重要的小部分。评论中有几个人建议使用枚举,经过研究,这可能是我想要的,但我目前无法对其进行可视化,需要做更多的研究和示例。关于如何将适当的结构和成员发送到要在其中进行修改的公式的任何建议或示例?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)