【问题标题】:C program for a counter计数器的 C 程序
【发布时间】:2014-11-27 12:40:58
【问题描述】:

我想创建一个计数器。每 10 秒我想检查一次开关的状态。例如,如果开关闭合,则 10 秒计数器递增。如果它是打开的,它会重新进入睡眠状态,在 10 秒内再次唤醒并检查开关状态。当计数达到例如 100 时,请执行某些操作。我该怎么做呢

我的尝试是:

for(int i=0;i<100;i++){
if(SW=1) {
    i++;
}
else
    i=0;
}

【问题讨论】:

  • 您需要一个文本编辑器和一个编译器。
  • 实际上您已经告诉我们伪代码,现在实现应该很容易。试试看。
  • 所以你从循环内部修改循环变量?
  • 澄清urzeit的观点:不要修改循环内的循环变量!使用另一个变量。
  • @ReticulatedSpline 在某些情况下这完全有用。如果您知道自己在做什么,并且可以找到这样做的理由。 (如果是 OP,我会说他不能)但是“不要那样做”我完全不能同意

标签: c counter pic


【解决方案1】:

我认为您需要更具体地回答这个问题。每次打开开关时,您似乎都想重置计数器。你确定要那个吗?

不管怎样,这可能就是你想要的

#include <stdio.h>
#include <time.h>
struct tm tm;   
time_t start,enxd;
double sec;
int counter;
int main(){
    int switchCounter = 0;
    int checkSwitch;

    checkSwitch = 1; // I put this in purpose since I have no idea how you're going to read the switch. 
                     // Thus, this assumes the switch is always closed.

    while(switchCounter != 100){
        // 1. Wait for 10 seconds
        sec = 0;
        time(&start);

        while(sec !=10){
            ++counter;
            time(&enxd);
            sec=difftime(enxd,start);
        }

        // 2. Read the state of the switch here.
        // ..............

        // 3. Simple if-else
        if (checkSwitch == 1){ //switch is closed
            switchCounter++;
            printf("Counter incremented. Current = %i \n", switchCounter);
        }
        else //if switch is open
        {
            switchCounter = 0 ;// Iam confused here, is this what you want ?
            printf("Switch is open \n");
        }
    }
    // 4. Finally when your counter reaches 100, you wanna do something here
    // ............................

    return 0;
}

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    你可以看看这段代码:

    int sw = 0;
    #define MAX 100
    #define gotosleep sleep(10)
    
    int main(void)
    {
        int num = 0;
        while(1) {
            gotosleep;
            if(sw)
                num++;
            else
                num = 0;
    
            if(num == MAX) {
                //do something
                printf("Done\n");
                num = 0;
                break;
            }
        }
    
        return 0;
    }
    
    1. 进入睡眠状态10
    2. 如果开关打开,增量num,否则reset num to 0 and go to sleep
    3. 检查num 是否已达到MAX 设置的值,如果等于MAX 则做一些事情并break。 (要继续循环注释代码中的中断)。
    4. 10 秒后再次唤醒并检查开关的状态 -> 步骤 2

    【讨论】:

      【解决方案3】:

      我不太确定我是否正确理解了您的问题,但您可以查看以下代码以获得一些想法。这是不言自明的。

      注意:要在本地测试功能,请启用测试代码

      #include <stdio.h>
      #include <stdlib.h>
      #include <unistd.h>
      
      #define MAXVAL 100
      #define SLEEPTIME 10
      
      //extern int switchSet; //if defined in some other file
      int switchSet;
      
      int main()
      {
          int counter = 0;
          int toSleep = 0;
      #if 0
              //for testing
          switchSet = MAXVAL; 
      #endif
      
          while (1)
          {
              toSleep = switchSet? SLEEPTIME:0;   //check for the switch state, 0 = open, 1 = closed
              if (toSleep)
              {
                  printf("Going to sleep for %d sec\n", SLEEPTIME); 
                  sleep(toSleep);
              }
              else
              {
                  counter++;
                  printf ("No sleeping, counter is %d\n", counter);
              }
              if (counter == MAXVAL)
              break;
      #if 0
              //for testing 
              switchSet--;
              if (switchSet < 0) switchSet = 0;
      #endif
          }
      
          printf("Do Something... Did, Done !!\n");
      
          return 0;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-03
        • 2021-04-23
        • 2012-04-23
        • 1970-01-01
        • 1970-01-01
        • 2015-04-06
        • 1970-01-01
        相关资源
        最近更新 更多