我的状态机(功能automate())遇到了一些问题。似乎枚举始终保持相同的值,而不是根据函数中的不同条件进行更改。 我的输出始终为“ DECREMENT_PARA_1”,谁能告诉我为什么变量etat_courant不能在每次迭代时将值保留在内存中吗?提前致谢 !这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//PARAMETRES
enum Etat {INIT, DECREMENT_PARA_1, INCREMENT_PARA_1, ETAT_INVARIANT_1, DECREMENT_PARA_2, ETAT_INVARIANT_2, INCREMENT_PARA_2, RADAR_SATURE};
//Prototypes
void automate(int tddv_estime, int tab_para_automate[][2], enum Etat *etat_courant, int* para_1_courant, int* para_2_courant);
void fct_test(int scenario[]);
int main()
{
int scenario[15] = {21, 21, 20, 12, 12, 20, 22, 22, 22, 22, 22, 22, 22, 22, 500};
fct_test(scenario);
return 0;
}
/*-----------------------------------------------------------------------
Fonction fct_test
-------------------------------------------------------------------------*/
void fct_test(int scenario[])
{
int increment = 1;
int tddv_estime;
int para_1_courant = 10;
int para_2_courant = 4;
int para_1_min = 5;
int para_1_max = 10;
int para_2_min = 1;
int para_2_max = 4;
int tab_para_automate[2][2] = ;
int tab_para_application[1][3] = ;
tab_para_automate[0][0] = para_1_min;
tab_para_automate[0][1] = para_1_max;
tab_para_automate[1][0] = para_2_min;
tab_para_automate[1][1] = para_2_max;
tab_para_application[0][0] = para_1_courant;
tab_para_application[0][1] = para_2_courant;
tab_para_application[0][2] = increment;
int i;
enum Etat etat_courant = INIT;
for (i=0 ; i<15 ; i++)
{
tddv_estime = scenario[i];
printf("%d\n",scenario[i]);
automate(tddv_estime, tab_para_automate, &etat_courant, ¶_1_courant, ¶_2_courant);
}
//}
}
/*-----------------------------------------------------------------------
Fonction automate
-------------------------------------------------------------------------*/
void automate(int tddv_estime, int tab_para_automate[][2], enum Etat *etat_courant, int* para_1_courant, int* para_2_courant)
{
int evenement;
int tddv_worst = 20;
if (tddv_estime < tddv_worst)
evenement = 1; //Etat initial
if (tddv_estime > tddv_worst)
evenement = 2; //Decrement para1
if (tddv_estime < tddv_worst && *para_1_courant<= tab_para_automate[0][1])
evenement = 3; //Increment para1
if (tddv_estime == tddv_worst)
evenement = 4; //Etat Invariant 1
if (tddv_estime > tddv_worst && *para_1_courant <= tab_para_automate[0][0])
evenement = 5; //Decrement para_2
if (tddv_estime < tddv_worst && *para_2_courant <= tab_para_automate[1][1])
evenement = 6; //Increment para2
if (tddv_estime == tddv_worst)
evenement = 7; //Etat Invariant 2
if (tddv_estime > tddv_worst && *para_1_courant<= tab_para_automate[0][0] && *para_2_courant<= tab_para_automate[1][0])
evenement = 8; //Etat radar sature
switch (*etat_courant) {
case INIT:
switch(evenement)
{
case 1:
*etat_courant = INIT;
printf("ETAT_INITIAL\n");
break;
case 2:
*etat_courant = DECREMENT_PARA_1;
printf("DECREMENT_PARA_1\n");
break;
}
break;
case DECREMENT_PARA_1:
switch(evenement)
{
case 5:
*etat_courant = DECREMENT_PARA_2;
printf("DECREMENT_PARA_2\n");
break;
case 4:
*etat_courant = ETAT_INVARIANT_1;
printf("ETAT_INVARIANT_1\n");
break;
case 3:
*etat_courant = INCREMENT_PARA_1;
printf("INCREMENT_PARA_1\n");
break;
case 2:
*etat_courant = DECREMENT_PARA_1;
printf("DECREMENT_PARA_1\n");
break;
}
break;
case INCREMENT_PARA_1:
switch(evenement)
{
case 4:
*etat_courant = ETAT_INVARIANT_1;
printf("ETAT_INVARIANT_1\n");
break;
case 3:
*etat_courant = INCREMENT_PARA_1;
printf("INCREMENT_PARA_1\n");
break;
case 2:
*etat_courant = DECREMENT_PARA_1;
printf("DECREMENT_PARA_1\n");
break;
}
break;
case ETAT_INVARIANT_1:
switch(evenement)
{
case 2:
*etat_courant = DECREMENT_PARA_1;
printf("DECREMENT_PARA_1\n");
break;
case 4:
*etat_courant = ETAT_INVARIANT_1;
printf("ETAT_INVARIANT_1\n");
break;
case 3:
*etat_courant = INCREMENT_PARA_1;
printf("INCREMENT_PARA_1\n");
break;
}
break;
case DECREMENT_PARA_2:
switch(evenement)
{
case 5:
*etat_courant = DECREMENT_PARA_2;
printf("DECREMENT_PARA_2\n");
break;
case 7:
*etat_courant = ETAT_INVARIANT_2;
printf("ETAT_INVARIANT_2\n");
break;
case 6:
*etat_courant = INCREMENT_PARA_2;
printf("INCREMENT_PARA_2\n");
break;
case 8:
*etat_courant = RADAR_SATURE;
printf("RADAR_SATURE\n");
break;
}
break;
case ETAT_INVARIANT_2:
switch(evenement)
{
case 5:
*etat_courant = DECREMENT_PARA_2;
printf("DECREMENT_PARA_2\n");
break;
case 7:
*etat_courant = ETAT_INVARIANT_2;
printf("ETAT_INVARIANT_2\n");
break;
case 6:
*etat_courant = INCREMENT_PARA_2;
printf("INCREMENT_PARA_2\n");
break;
}
break;
case INCREMENT_PARA_2:
switch(evenement)
{
case 5:
*etat_courant = DECREMENT_PARA_2;
printf("DECREMENT_PARA_2\n");
break;
case 7:
*etat_courant = ETAT_INVARIANT_2;
printf("ETAT_INVARIANT_2\n");
break;
case 6:
*etat_courant = INCREMENT_PARA_2;
printf("INCREMENT_PARA_2\n");
break;
case 3:
*etat_courant = INCREMENT_PARA_1;
printf("INCREMENT_PARA_1\n");
break;
}
break;
default :
exit(1);
break;
}
}
答案 0 :(得分:-2)
有人可以告诉我为什么变量etat_courant不能在每次迭代时将值保留在内存中吗?
它实际上是将值保留在内存中:否则,etat_courant
将保留在内存中,直到fct_test(...)
返回。输出始终为DECREMENT_PARA_1
的原因完全是由于automate()
函数中的逻辑。
例如,您在automate()
函数中有一个多余的比较-这将覆盖evenement
的先前分配。
if (tddv_estime == tddv_worst)
evenement = 4; //Etat Invariant 1
// ...
if (tddv_estime == tddv_worst)
evenement = 7; //Etat Invariant 2
如果-这仅仅是假设(是否正确,这取决于您要实现的目标)-删除第二个比较,输出将至少变化一次。
顺便说一句……将数组的大小传递给fct_test(...)
函数更安全-如
int main(void) {
int scenario[] = {21, 21, 20, 12, 12, 20, 22, 22,
22, 22, 22, 22, 22, 22, 500};
fct_test(scenario, sizeof(scenario)/sizeof(scenario[0]));
return 0;
}
void fct_test(int *scenario, size_t array_len) {
// Etc. (ommitted for brevity )
for (i = 0; i < array_len; i++) {
tddv_estime = scenario[i];
// etc.
}
}
(请注意,我还在您的return 0
中添加了一个main()
-您丢失了。)