您可以尝试类似的方法(注意:CLK 每隔 5 毫秒的奇数组更改一次,并且取决于调用函数的时间,输出会有所不同,所以除了查看 CLK 的实时表示外,不要这样做)。
#include <iostream>
#include <ctime>
#define PASSED_MS (int(float(now)/CLOCKS_PER_SEC*1000))
using namespace std;
class DFF
{
public:
DFF(bool D):D(D),CLK(0),Q(0),QN(1),start(clock()),now(start){}
~DFF();
bool DFF_Q();
void DFF_ALL(bool& clk,bool& q,bool& qn); //To return all Parameters at once
private:
bool D,CLK,Q,QN;
const clock_t start;
clock_t now;
};
DFF::~DFF()
{
delete &D;
delete &CLK;
delete &Q;
delete &QN;
delete &start;
delete &now;
}
bool DFF::DFF_Q()
{
now=clock()-start;
if((PASSED_MS/5)%2!=0 && PASSED_MS!=0) //if an odd period of 5ms passed change else keep it as its
{
CLK=CLK?0:1;
if(CLK)
{
Q=D;
QN=D?0:1;
}
}
return Q;
}
void DFF::DFF_ALL(bool& clk,bool& q,bool& qn)
{
DFF_Q();
clk=CLK;
q=Q;
qn=QN;
}
int main()
{
DFF a(1);
bool clk,q,qn;
int c=30;
cout<<"CLK "<<"DFF-Q "<<"DFF-QN "<<endl;
while(c!=0)
{
c--;
a.DFF_ALL(clk,q,qn);
cout<<clk<<" "<<q<<" "<<qn<<endl<<endl;
}
return 0;
}