Take advantage of all the knowledge you've learned before.
Randomly generate 10 numbers (1-1000), one line at a, and coexist in 1.txt.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int F_gettimestamp () {
time_t L_now;
Time (&l_now);
return (int) L_now;
}
void f_randomize (int p_v1) {
Srand (P_V1);
}
int F_rand (int p_min, int p_max) {
Return rand ()% (P_max-p_min + 1) + p_min;
}
void Main () {
int l_time = F_gettimestamp ();
F_randomize (L_time);
int l_length = 10; Sets the number of random numbers that are generated.
int *p = (int *) calloc (l_length, sizeof (int));
for (size_t i = 0; i < l_length; i++) {
P[i] = F_rand (1, 1000);
}
FILE * L_fp_write = fopen ("1.txt", "w");
if (l_fp_write! = NULL) {
Char l_out[20] = {0};
for (size_t i = 0; i < l_length; i++) {
sprintf (L_out, "%d\n", P[i]);
Fputs (L_out, l_fp_write);
}
}
Fclose (L_fp_write);
Free (p);
System ("pause");
}
Then read the data in 1.txt, use the bubbling algorithm to sort, and then re-save to 1.txt after sorting.
84 bubbling sorted Text data