C语言 计数器的 C 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27171065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C program for a counter
提问by NewLook
I want to create a counter. Every 10 seconds I want to check the state of a switch. For example if the switch is closed then the 10 sec counter increments. If it is open it goes back to sleep, wakes up again in 10 seconds and checks the state of switch. When the count reaches e.g 100 then do something. How would I go about doing this
我想创建一个计数器。每 10 秒我想检查一次开关的状态。例如,如果开关关闭,则 10 秒计数器递增。如果它是打开的,它会回到睡眠状态,10 秒后再次唤醒并检查开关的状态。当计数达到例如 100 时,请执行某些操作。我将如何去做这件事
My attempt is:
我的尝试是:
for(int i=0;i<100;i++){
if(SW=1) {
i++;
}
else
i=0;
}
回答by Adhiputra Perkasa
I think you need to be more specific on the question. It seems like you want to reset the counter every time the switch is open. Are you sure you want that ?
我认为你需要更具体地回答这个问题。每次开关打开时,您似乎都想重置计数器。你确定你想要那个?
Anyway, here is probably what you want
无论如何,这可能是你想要的
#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;
}
Hope it helps :)
希望能帮助到你 :)
回答by Sourav Ghosh
I'm not very sure if I have understood your problem correctly, but you can check the following code for some ideas. It is self-explaining.
我不太确定我是否正确理解了您的问题,但是您可以查看以下代码以获取一些想法。这是不言自明的。
NOTE : to test the functionality locally, please enable the test codes
注意:要在本地测试功能,请启用测试代码
#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;
}
回答by Santosh A
You can look at this code:
你可以看看这段代码:
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;
}
- Go to sleep for
10seconds - If switch is ON, the increment
num, elsereset num to 0 and go to sleep - Check if
numhas reachedMAXvalue set, if equal toMAXthen do something andbreak. (To continue the cycle comment the break in the code). - Wake up again after 10 seconds and check for the state of the switch -> step 2
- 睡
10几秒 - 如果开关打开,则增量
num,否则reset num to 0 and go to sleep - 检查是否
num已达到MAX设置的值,如果等于MAX则执行某些操作和break。(要继续循环注释代码中的中断)。 - 10 秒后再次唤醒并检查开关状态 -> 步骤 2

