Experimental content

1. Experimental task 1

#include<math.h> #include<stdio.h> int main(){ float a,b,c,x1,x2; float delta,real,image; printf("Enter a,b,c: "); while(scanf("%f,%f,%f",&a,&b,&c)!=EOF){ if(a==0) printf("not quadratic equation.\n\n"); else{ delta=b*b-4*a*c; if(delta>=0){ x1=(-b+sqrt(delta))/(2*a); x2=(-b-sqrt(delta))/(2*a); printf("x1=%.2f,x2=%.2f\n",x1,x2); } else{ real=-b/(2*a); image=sqrt(-delta)/(2*a); printf("x1=%.2f+%.2fi,x2=%2f-%2fi\n\n",real,image,real,image); } } printf("Enter a,b,c: "); } return 0; }

Operation results

But I happened to find that if I input some numbers first and then press Ctrl+Z, there will be infinite output

Later learned that this is because

Maybe this is the so-called "loophole".

PS: the value of EOF is generally - 1

When we use input functions such as scanf, in fact, the user's input in cmd is actually stored in the buffer. When the user types enter, the previously entered data will be saved. Here, whether it is a single character or a string, we all know that the return value of scanf represents the number of objects successfully received. If there are special circumstances, For example, if the buffer file is full, how will scanf handle it? The answer is to return - 1! Here is not only scanf, but also the function with the number of return values. When the file stream is full, most of them will return - 1, so this - 1 is used more, so stdio H simply defines a macro to express it. Take the first three letters of end of file to form EOF, so there is #define EOF (-1)!

2. Experimental task 2

#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 5 int main(){ int x,n; srand(time(0)); n=0; do{ n++; x=rand()%10; printf("%3d",x); }while(n<N); printf("\n"); return 0; }

Operation results

PS:

Pseudo random number:

Pseudo random number is a sequence of random numbers uniformly distributed from [0,1] calculated by deterministic algorithm. It is not really random, but it has statistical characteristics similar to random numbers, such as uniformity, independence and so on. When calculating pseudo-random number, if the initial value (seed) used is unchanged, the number order of pseudo-random number is also unchanged. Pseudorandom numbers can be generated in large quantities by computer. In order to improve the simulation efficiency, pseudorandom numbers are generally used to replace real random numbers. Generally, the pseudo-random number with extremely long cycle period and passing the random number test is used in the simulation to ensure the randomness of the calculation results.

so~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3. Experimental task 3

#include<stdio.h> #include<math.h> int main() { int m,k,n; k=0; for(n=101;n<=200;n++) { for(m=2;m<=sqrt(n);++m) if(n%m==0)break; if(m>sqrt(n)) { printf("%d\t",n); k++; if(k%5==0) printf("\n"); } } printf("101~200"); printf("101^200 Shared between%d Prime number",k); return 0;}

Operation result (normal)

And before that, there were some bug s

and......

4. Experimental task 4

#include<stdio.h> int main() { int a,s,t,m; m = 1; t = 0; printf("Enter a number: "); while (scanf("%d", &s) != EOF) { while (s >= 1) { a = s % 10; s = s / 10; if (a % 2 != 0) { t = t + a * m; m = m * 10; } } if (s % 2 == 1) t = t + m * s; printf("new number is %d\n", t); printf("Enter a number: "); t = 0; m = 1; } return 0; }

Operation results (it must be normal, don't read it ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

But it's not. I found that the program was running abnormally

In fact, it's the range of data types~~

PS: algorithm idea

In fact, it is to input a number first, then obtain the numbers on each digit by using the circular statement of taking the remainder of 10, and then take the remainder of these numbers to 2. If it is 1, it will be determined as an odd number. Again, use the circular statement to output these data in the original order, but the output is a real number, which depends on the output variable multiplied by the corresponding digits and the result of the previous cycle.

5. Experimental task 5

#include<stdio.h> int main(){ int n,k,i; float s,x; s=0; x=1; i=1; k=1; printf("Enter n(0^10):"); while(scanf("%d",&n)!=EOF){ for(; k<=n; k++) { i=(-1)*i*k; s=s-x/i;} printf("n=%d,s=%f\n\n",i,s); printf("Enter n(0^10):"); } return 0; }

Just because a quotation mark made me do it for a long time, I thought the computer should be changed (>) 3: <)

while(scanf("%d,&n")!=EOF){

emmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

and hard won operational achievements:

6. Experimental task 6

#include<stdlib.h> #include<time.h> int main(){ int n,i,t; srand(time(0)); t=rand()%30+1; i=0; printf("Guess which day will be yours in 2020 luck day\n"); printf ("Here we go. You have three chances. Guess (1)^31): "); scanf("%d",&n) ; while(i<=3){ if(n>t){ printf("You guessed it was late, luck day Sneak to the front\n"); printf("Guess again:"); scanf("%d",&n) ; i++;} else if(n<t){ printf("You guessed it was early, luck day Not yet\n"); printf("Guess again;"); scanf("%d",&n); i++;} else printf("You guessed right, nice!") ; i++; } printf("We've run out of times. Secretly tell you: December, your luck day yes%d number",t); return 0; }

The operation results are normal

Good luck~

Guess 100 million times again~~

Experimental summary

This experiment is very difficult, and there are many new concepts~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

for instance:

1. Pseudo random numbers rand () and srand() pseudo random numbers are sequences of random numbers uniformly distributed from [0,1] calculated by deterministic algorithm. It is not really random, but it has statistical characteristics similar to random numbers, such as uniformity, independence and so on. When calculating pseudo-random number, if the initial value (seed) used is unchanged, the number order of pseudo-random number is also unchanged. Pseudorandom numbers can be generated in large quantities by computer. In order to improve the simulation efficiency, pseudorandom numbers are generally used to replace real random numbers. Generally, the pseudo-random number with extremely long cycle period and passing the random number test is used in the simulation to ensure the randomness of the calculation results.In the standard C library, the function rand() can generate 0 ~ Rand_ A random number between Max, where RAND_MAX is stdlib An integer defined in H, which is related to the system.

The rand() function has no input parameters and is directly referenced by the expression rand(); For example, you can print two random numbers with the following statement:

printf("Random numbers are: %i %i\n",rand(),rand());

Because the rand() function generates integers in the specified order, the same two values are printed every time the above statement is executed. Therefore, the randomness of C language is not really random, sometimes it is also called pseudo-random number.

In order to generate a new sequence of random values every time the program executes, we usually provide a new random seed for the random number generator. The function srand() (from stdlib.h) can seed the random number generator. As long as the seeds are different, the rand() function will produce different random number sequences. srand() is called the initializer of the random number generator.

2. Header file time H and time (x)

Function name: localtime

Function prototype: struct tm *localtime(const time_t *timer)

Function function: return a machine time information expressed in tm structure

Function return: time expressed in tm structure

timer - the machine time obtained using the time() function

Of course, there is the correct opening method!

1. Nested use of circular statements

*Pay special attention to the use of "" and parentheses! Two seemingly identical programs may be vastly different because of a small oversight!

2. Introduction of variables

*Don't confuse variables!

3. Transfer and application between experiments

*An experiment may be included in another experiment. Understanding and using basic experiments can handle more complex experiments!

4. Learn to simplify procedures

*Try to simplify the problem!

Thanks again to everyone who saw the last!