计数器的C程序
我想创建一个计数器。每10秒我要检查一次开关的状态。例如,如果开关闭合,则10秒计数器递增。如果开关打开,它将返回睡眠状态,10秒钟后再次醒来并检查开关的状态。当计数达到例如100时,然后做一些事情。我该怎么做呢 我的尝试是:计数器的C程序,c,counter,pic,C,Counter,Pic,我想创建一个计数器。每10秒我要检查一次开关的状态。例如,如果开关闭合,则10秒计数器递增。如果开关打开,它将返回睡眠状态,10秒钟后再次醒来并检查开关的状态。当计数达到例如100时,然后做一些事情。我该怎么做呢 我的尝试是: for(int i=0;i<100;i++){ if(SW=1) { i++; } else i=0; } for(int i=0;i您可以查看以下代码: int sw = 0; #define MAX 100 #define gotosleep
for(int i=0;i<100;i++){
if(SW=1) {
i++;
}
else
i=0;
}
for(int i=0;i您可以查看以下代码:
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;
}
进入睡眠状态10秒钟
如果开关打开,则增量num
,否则将num重置为0并进入睡眠状态
检查num
是否已达到MAX
设置的值,如果等于MAX
则采取措施并break
(继续循环注释代码中的中断)
10秒后再次醒来,检查开关的状态->步骤2
我认为你需要更具体地回答这个问题。似乎你想在每次开关打开时重置计数器。你确定要这样做吗
无论如何,这可能是你想要的
#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;
}
#包括
#包括
struct-tm;
启动时间,enxd;
双秒;
整数计数器;
int main(){
int开关计数器=0;
int校验开关;
checkSwitch=1;//我之所以这么说是有目的的,因为我不知道您将如何读取该开关。
//因此,假设开关始终闭合。
while(开关计数器!=100){
//1.等待10秒钟
秒=0;
时间(&开始);
而(秒=10){
++计数器;
时间(&enxd);
秒=离散时间(enxd,开始);
}
//2.在此处读取开关的状态。
// ..............
//3.如果不是这样,那就简单
如果(checkSwitch==1){//开关关闭
开关计数器++;
printf(“计数器递增。当前=%i\n”,开关计数器);
}
否则//如果开关打开
{
switchCounter=0;//我在这里很困惑,这是你想要的吗?
printf(“开关打开\n”);
}
}
//4.最后当你的计数器达到100时,你想在这里做点什么
// ............................
返回0;
}
希望有帮助:)我不确定是否正确理解了您的问题,但您可以查看以下代码以了解一些想法。这是自我解释
注意:要在本地测试功能,请启用测试代码
#包括
#包括
#包括
#定义MAXVAL 100
#定义睡眠时间10
//外部-内部开关组//如果在其他文件中定义
int开关组;
int main()
{
int计数器=0;
int-toSleep=0;
#如果0
//用于测试
开关组=最大值;
#恩迪夫
而(1)
{
toSleep=switchSet?睡眠时间:0;//检查开关状态,0=open,1=closed
如果(睡觉)
{
printf(“要睡眠%d秒\n”,睡眠时间);
睡眠(toSleep);
}
其他的
{
计数器++;
printf(“无休眠,计数器为%d\n”,计数器);
}
如果(计数器==最大值)
打破
#如果0
//用于测试
开关组--;
如果(开关集<0)开关集=0;
#恩迪夫
}
printf(“做点什么……做了,做了!!\n”);
返回0;
}
您需要一个文本编辑器和一个编译器。实际上,您已经告诉我们了伪代码,现在应该很容易实现了。试试看。那么你是从循环内修改循环变量的吗?为了澄清urzeit的观点:不要在循环内修改循环变量!请使用另一个变量。@networkedspline在某些情况下,这是非常有用的。如果你知道自己在做什么,并能找到这样做的理由。(在OP的情况下,我会说他不能)但是“不要那样做”,我不能完全受屈
#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;
}