2013-03-27 52 views
0

首先这个被触发:返回程序预触发状态

if ((temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit) | (temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit)) 
    activateAlarm(channelID); 

激活警报被触发,然后从那里:

void activateAlarm(int channelID); 
{ while (temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit || temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit) 
    {  
    logSubsystem(temperatureChannel[channelID].currentTemperature); 
    }  
} 

然后报警画面被触发与下面的情况:

int logSubsystem(int currentTemperature) 

case 'F': //if user input is 'F' 
case 'f': //if user input is 'f'   

      currentTemperature--; 
      printf("your current exceeded temp is %i\n \n", currentTemperature);  
      if (currentTemperature <= 100 || currentTemperature >= 50); 
        compareLimit(); 
       break; //exits loop 

如何设置此功能,以便用户使用F递减并获取当前温度t o低于极限(< 100,或> 50),那么它将返回到compareLimit函数,并且对上限/下限触发状态的要求将为FALSE,将程序返回到其原始的预警状态?

+1

没有足够的上下文。根据你的问题,你可能需要:1.一个'while'循环,2.'longjmp()',3。 – 2013-03-27 15:22:07

+0

我还能告诉你什么?我只是不想给你带来不必要的代码 – 2013-03-27 15:26:34

+0

这也没关系。特别是,我会很喜欢'activateAlarm()'函数。 – 2013-03-27 15:27:36

回答

1

我想你会从很多关于你的程序如何流动的问题中受益颇多。现在,我可以推断出你的程序流程是:

  • 你有一个外循环,检查温度,在至少一个通道ID。在那个循环中,你有第一次给我们看的if声明。
  • 然后激活报警做一些其他的东西,但循环,直到温度下降,呼吁logSubsystem
  • logSubsystem然后可能会得到某种用户输入,并从那里,你想它调用你的初始函数,可能被称为准备限制。

问题是这些功能都没有完成。他们都会互相调用,最终会导致堆栈溢出。很好,因为这是这个网站的名字,但不是你想要的东西。

你基本上需要的是一台状态机。你需要一些跟踪值的东西,看看这些值,并调用返回的函数,这些函数对这些值进行操作。应该只有一个循环,并且它应该根据这些值发生什么来控制所发生的情况。好消息是,你已经拥有了所有这些。 temperatureChannel正在跟踪你的价值,并且你有很多循环。

让我给你我的方式建议,我建议你的程序应该流向:

bool checkTemperatureValuesOutOfRange(int channelID) { 
    // this is just a convenience, to make the state machine flow easier. 
    return (temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit) || // note the || not just one | 
      (temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit); 
} 

void actOnUserInput() { 
    char input = // ... something that gets a user input. It should check if any is available, otherwise return. 
    switch (input) { 
     case 'F': 
     case 'f': 
      temperatureChannel[channelID].currentTemperature--; 
      break; // This doesn't exit the loop - it gets you out of the switch statement 
} 

void activateAlarm(int channelID) { 
    // presumably this does something other than call logSubsystem? 
    // if that's all it does, just call it directly 
    // note - no loop here 
    logSubsystem(channelID); 
} 

void logSubsystem(int channelID) { // Not the current temperature - that's a local value, and you want to set the observed value 
    // I really think actOnUserInput should be (an early) part of the while loop below. 
    // It's just another input for the state machine, but I'll leave it here per your design 
    // Presumably actually logs things, too, otherwise it's an unnecessary function 
    actOnUserInput(); 
} 

while (TRUE) { // this is the main loop of your function, and shouldn't exit unless the program does 
    // do anything else you need to - check other stuff 
    // maybe have a for loop going through different channelIDs? 
    if (checkTemperatureValuesOutOfRange(channelID)) { 
     activateAlarm(channelId); 
    // do anything else you need to 
} 

我敢肯定,你可以看到很多的代码和我之间的差异。这里有一些关键的事情要考虑:

  • 现在所有的功能返回。主while循环调用检查状态的函数,并调用改变状态的函数。
  • 我强烈建议作为主while循环的一部分在用户输入上进行操作。这只是状态机的另一个输入。得到它,采取行动,然后检查你的状态。你大概需要从用户那里获得一些信息,否则你将永远不会处于糟糕的状态。
  • 现在,每次都会发生警报。用你显示的代码,这很好 - 因为logSubsystem就是所有被调用的。如果您只想让闹钟振铃一次,请在temperatureChannel [channelId]内保留一个布尔型跟踪器,该值告诉您闹钟是否响铃,并在activateAlarm中将其设置为true,然后根据checkTemperatureValuesOutOfRange的返回值将其重置为false。
  • 您不必将自己置于activateAlarm/logSubsystem区域,而是每次都会返回,并且每次检查您的值以查看是否仍然存在。这是关键 - 你的功能应该很快,而不是垄断你的处理器。让每个函数只做一种事情,并让所有的控制来自主循环。

我对你的代码做了很多改变,我不知道你是否被允许创建所有的代码,但是你需要类似的东西。它更加强大,并为您提供四处扩展的空间。

+0

斯科特...非常感谢你的深思熟虑的回应......我不知道这种情况是堆栈溢出,但这是我学到的东西:)然而,我在想,我需要停止callng函数之后功能,并专注于从我的主和其他任何地方调用它们。非常感谢您的回复... – 2013-03-27 20:20:53

+0

没问题。每次添加函数时,都会将它推送到函数堆栈中。这是分配所有包含可执行语句的内存和包含本地变量的所有内存的地方。 (当你退出函数时,它会弹出堆栈。)每个操作系统都有一定的限制,你可以放在堆栈上(这可能是“所有可用的内存”)。特别是在一个看起来像是要永远存在的系统中,不断添加函数最终会让你溢出这个限制。 – 2013-03-27 21:05:16