【C语言】两开关控制灯(未经验证仅供参考)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<time.h>
int ScanKey(int A)
{ 

	if (A == 0)//检测按键是否按下
		Sleep(20);//延迟保证按下
	if (A == 0)
	{
		return 0;
	}
	else
	{
		return 1;
	}

}
int ScanKey2(int B)
{
	if (B == 0)
		Sleep(20);
	if (B == 0)
	{
	  return 0;
	}
	else
	  return 1;

}
int main()
{
	time_t start, end;
	start = time(NULL);
		int C = 0;//灯
		int A = 0;
		int B = 0;//两个开关
		int ScanKey(A);//按键状态,表现其输入值
		int ScanKey2(B);//同上
		for (;;){//死循环
			end = time(NULL);
			if ((A == 0) && (B == 0) && (difftime(end, start)==300))//五分钟没按灯灭
			{ 
				C = 0;
			}
			else{
				if ((ScanKey(A) == 0) && (ScanKey2(B) == 0))//开关都没按,灯灭
				{
					C = 0;
				}
				else
					if ((ScanKey(A) == 1) && (ScanKey2(B) == 1))//开关都按,灯灭
					{
					C = 0;
					}
					else
						if ((ScanKey(A) == 1) && (ScanKey2(B) == 0))//灯灭时,按一开关灯亮
						{
					C = 1;
						}
						else
							if ((ScanKey(A) == 0) && (ScanKey2(B) == 1))//同上
							{
					C = 1;
							}
							else
							{
								C = 0;//其他状态,灯灭
							}
			}
	}
	system("pause");
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_43416226/article/details/89406025