This commit is contained in:
ada-dmitry
2023-04-01 22:43:00 +03:00
parent fa03214161
commit 60d07bfca5
5 changed files with 97 additions and 71 deletions
+6 -4
View File
@@ -1,12 +1,13 @@
#include "funcHeader.h" #include "funcHeader.h"
int main()
int main() { {
srand(time(0)); srand(time(0));
People *professions1 = malloc(sizeof(People) * COUN); People *professions1 = malloc(sizeof(People) * COUN);
People *professions2 = malloc(sizeof(People) * COUN); People *professions2 = malloc(sizeof(People) * COUN);
if (professions1 == NULL || professions2 == NULL) { if (professions1 == NULL || professions2 == NULL)
{
printf("Mesta net\n"); printf("Mesta net\n");
exit(1); exit(1);
} }
@@ -49,7 +50,8 @@ int main() {
People *professions3 = malloc(sizeof(People) * line_number); People *professions3 = malloc(sizeof(People) * line_number);
if (professions3 == NULL) { if (professions3 == NULL)
{
printf("Mesta net\n"); printf("Mesta net\n");
exit(1); exit(1);
} }
+2 -4
View File
@@ -7,10 +7,8 @@
#include <string.h> #include <string.h>
#include <windows.h> #include <windows.h>
typedef struct People
{
typedef struct People {
int count; int count;
char name[LEN]; char name[LEN];
} People; } People;
+52 -28
View File
@@ -1,24 +1,30 @@
#include "funcHeader.h" #include "funcHeader.h"
void print_struc(People professions[], int n) { void print_struc(People professions[], int n)
{
int i; int i;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
printf("count: %3d; name: %s\n", professions[i].count, professions[i].name); printf("count: %3d; name: %s\n", professions[i].count, professions[i].name);
} }
void sort_sel(People professions[], int n) { void sort_sel(People professions[], int n)
{
int i, j; int i, j;
for (i = 0; i < n - 1; i++) { for (i = 0; i < n - 1; i++)
for (j = i + 1; j < n; j++) { {
for (j = i + 1; j < n; j++)
{
if (professions[j].count < professions[i].count) if (professions[j].count < professions[i].count)
swap(professions + i, professions + j); swap(professions + i, professions + j);
} }
} }
} }
void st_rand(People professions[], int n) { void st_rand(People professions[], int n)
{
int i, j; int i, j;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++)
{
professions[i].count = rand() % 3 + 1; professions[i].count = rand() % 3 + 1;
for (j = 0; j < LEN - 1; j++) for (j = 0; j < LEN - 1; j++)
professions[i].name[j] = 'a' + rand() % 25; professions[i].name[j] = 'a' + rand() % 25;
@@ -26,14 +32,17 @@ void st_rand(People professions[], int n) {
} }
} }
People* binSearch(People professions[], int n, int k) { People *binSearch(People professions[], int n, int k)
{
People *ans = NULL; People *ans = NULL;
int right = 0, left = n - 1; int right = 0, left = n - 1;
while (right <= left) { while (right <= left)
{
int mid = (left + right) / 2; int mid = (left + right) / 2;
if (professions[mid].count == k) { if (professions[mid].count == k)
{
ans = &professions[mid]; ans = &professions[mid];
break; break;
} }
@@ -43,8 +52,10 @@ People* binSearch(People professions[], int n, int k) {
return ans; return ans;
} }
void pst(People professions[], int s, int n) { void pst(People professions[], int s, int n)
if (n >= s) { {
if (n >= s)
{
printf("Out of range\n"); printf("Out of range\n");
exit(2); exit(2);
} }
@@ -52,68 +63,81 @@ void pst(People professions[], int s, int n) {
printf("count: %2d; name: %s\n", professions[n].count, professions[n].name); printf("count: %2d; name: %s\n", professions[n].count, professions[n].name);
} }
int find_para(People t1[], int n1, People t2[], int n2)
int find_para(People t1[], int n1, People t2[], int n2) { {
int ans = 0, i; int ans = 0, i;
for (i = 0; i < n1; i++) { for (i = 0; i < n1; i++)
{
People *tans = binSearch(t2, n2, t1[i].count); People *tans = binSearch(t2, n2, t1[i].count);
if (tans == NULL) continue; if (tans == NULL)
continue;
ans++; ans++;
} }
return ans; return ans;
} }
void st_fprintf(People professions[], int n, char path[], char access[])
void st_fprintf(People professions[], int n, char path[], char access[]) { {
int i; int i;
FILE *f = fopen(path, access); FILE *f = fopen(path, access);
if (f == NULL) { if (f == NULL)
{
printf("Could not open file %s\n", path); printf("Could not open file %s\n", path);
exit(3); exit(3);
} }
for (i = 0; i < n; i++) { for (i = 0; i < n; i++)
{
fprintf(f, "%d %s\n", professions[i].count, professions[i].name); fprintf(f, "%d %s\n", professions[i].count, professions[i].name);
} }
fclose(f); fclose(f);
} }
int lines_numf(char path[]) { int lines_numf(char path[])
{
int ans = 0; int ans = 0;
char c; char c;
FILE *f = fopen(path, "r"); FILE *f = fopen(path, "r");
if (f == NULL) { if (f == NULL)
{
printf("Faila %s net...\n", path); printf("Faila %s net...\n", path);
exit(3); exit(3);
} }
while ((c = fgetc(f)) != EOF) { while ((c = fgetc(f)) != EOF)
if (c == '\n') ans++; {
if (c == '\n')
ans++;
} }
fclose(f); fclose(f);
return ans; return ans;
} }
void st_fscanf(People t[], int n, char path[]) { void st_fscanf(People t[], int n, char path[])
{
int i; int i;
FILE *f = fopen(path, "r"); FILE *f = fopen(path, "r");
if (f == NULL) { if (f == NULL)
{
printf("Faila %s net...\n", path); printf("Faila %s net...\n", path);
exit(3); exit(3);
} }
for (i = 0; i < n; i++) { for (i = 0; i < n; i++)
{
fscanf(f, "%d %s\n", &t[i].count, t[i].name); fscanf(f, "%d %s\n", &t[i].count, t[i].name);
} }
fclose(f); fclose(f);
} }
void swap(People* t1, People* t2) { void swap(People *t1, People *t2)
{
People *tmp = malloc(sizeof(People)); People *tmp = malloc(sizeof(People));
if (tmp == NULL) { if (tmp == NULL)
{
printf("could not malloc\n"); printf("could not malloc\n");
exit(1); exit(1);
} }
@@ -1,7 +1,7 @@
#define COUN 40 #define COUN 40
typedef struct Node
typedef struct Node { {
long int data; long int data;
struct Node *next; struct Node *next;
struct Node *prev; struct Node *prev;
+4 -2
View File
@@ -4,12 +4,14 @@
#include "header.h" #include "header.h"
#include <windows.h> #include <windows.h>
int main(int args, char *argv[]){ int main(int args, char *argv[])
{
node *head = NULL; node *head = NULL;
int r; int r;
srand(time(NULL)); srand(time(NULL));
while((r=(rand()%100))<90) head = add_head(head,r); while ((r = (rand() % 100)) < 90)
head = add_head(head, r);
pr(head); pr(head);
head = del_head(head); head = del_head(head);
pr(head); pr(head);