The C language code below uses a function that generates random numbers, and a delay function. Please carefully observe its display effect.
From the following code, we can draw an important conclusion: when the above two kinds of functions are put into the loop, we should make some changes. Also focus on the definition of its parameters (outside of the child function?). Inside the child function? Global variable? Local variables? )。
Also note: scanf should use%LF when typing a double type, but only use%f when printf.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
Double T_delay;
time_t start,end; Start,end the modified position
int Createornot ()
{
int flag;
Srand (NULL);//srand () Original position
flag = (int) ((2*rand ()/rand_max) +1);
Return Flag;//return 1 or 2
1 create,2 not create.
}
void Delay ()
{
time_t Start,end;//end the original position
Double dif;
Time (&start);
while ((dif= difftime (End,start)) <5000.0)//notice here
{
Time (&end);
};
}
void delay (time_t end)//end modified location
{
time_t start;//,end;
Double dif;
Time (&start);
while ((dif= difftime (End,start)) <2.0)//notice here
{
Time (&end);
};
}
void Delay (time_t end,double time_delay)//change ...
{
time_t Start;//,end;//change ...
Double dif;
Time (&start);
Time (&end);
while ((dif= difftime (End,start)) < Time_delay)
{
Time (&end);
};
}
void delay (time_t start,time_t end)//change ...
{
time_t Start;//,end;//change ...
Double dif;
Time (&start);
Time (&end);
while ((dif= difftime (End,start)) < T_delay)
{
Time (&end);
};
}
int main ()
{
int I,flag = 1,times = 10;
time_t start,end; Start,end the modified position
Srand (Time (NULL)), and/or corrected, Srand () where it should be.
for (i=0;i<times;i++)
{
Flag= Createornot ();
printf ("%d\n", flag);
Delay ();
Delay (end);
}
printf ("\ n \ nthe");
for (i=0;i<times;i++)
{
Flag= Createornot ();
printf ("%d\n", flag);
Delay ();
Delay (end);
}
printf ("t_delay:\n");
scanf ("%lf", &t_delay);/////////////////////////////////////
printf ("t_delay:%f\n", T_delay);
for (i=0;i<times;i++)
{
Flag= Createornot ();
printf ("%d\n", flag);
Delay ();
Delay (End,t_delay);
}
printf ("t_delay:\n");
scanf ("%lf", &t_delay);
for (i=0;i<times;i++)
{
Flag= Createornot ();
printf ("%d\n", flag);
Delay ();
Delay (start,end);
}
}