global rename

This commit is contained in:
ada-dmitry
2023-05-10 21:55:33 +03:00
parent 350c719381
commit 0cf222ed89
21 changed files with 9 additions and 7 deletions
+103
View File
@@ -0,0 +1,103 @@
#include <stdio.h>
#include <stdlib.h>
#define N 20
void selectionSort(int arr[], int n)
{
int i, j, tmp;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[i])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
void bubbleSort(int arr[], int n)
{
int i, j, tmp;
for (i = 0; i < n; i++)
{
for (j = 0; j < n - 1; j++)
{
if (arr[j] > arr[j + 1])
{
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
void insertionSort(int arr[], int n)
{
int i, j, tmp;
for (i = 1; i < n; i++)
{
for (j = 0; j < i; j++)
{
if (arr[i] < arr[j])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
int main(int argc, char *argv[])
{
srand(time(0));
int *arr1 = malloc(sizeof(int) * N);
int *arr2 = malloc(sizeof(int) * N);
int *arr3 = malloc(sizeof(int) * N);
if (!arr1 || !arr2 || !arr3)
{
printf("could not malloc\n");
exit(1);
}
int i;
for (i = 0; i < N; i++)
{
int d = rand() % 100;
arr1[i] = d;
arr2[i] = d;
arr3[i] = d;
}
selectionSort(arr1, N);
insertionSort(arr2, N);
bubbleSort(arr3, N);
printf("Selection sort: ");
for (i = 0; i < N; i++)
printf("%d ", arr1[i]);
printf("\nInsertion sort: ");
for (i = 0; i < N; i++)
printf("%d ", arr2[i]);
printf("\nBubble sort: ");
for (i = 0; i < N; i++)
printf("%d ", arr3[i]);
printf("\n");
free(arr1);
free(arr2);
free(arr3);
return 0;
}
Binary file not shown.
Binary file not shown.
+206
View File
@@ -0,0 +1,206 @@
#include "head.h"
void linked_print(LinkedList *l)
{
while (l != NULL)
{
printf("%d %s\n", l->x, l->name);
l = l->next;
}
}
LinkedList *add_tail(LinkedList *l, int x, char name[])
{
if (l->x == 0 && !strcmp(l->name, "\0"))
{
l->x = x;
memcpy(l->name, name, STR_LEN);
l->next = NULL;
l->prev = NULL;
return l;
}
LinkedList *res = l;
while (l->next != NULL)
l = l->next;
LinkedList *tmp = malloc(sizeof(LinkedList));
tmp->x = x;
memcpy(tmp->name, name, STR_LEN);
tmp->prev = l;
l->next = tmp;
return res;
}
LinkedList *add_head(LinkedList *l, int x, char name[])
{
if (l->x == 0 && !strcmp(l->name, "\0"))
{
l->x = x;
memcpy(l->name, name, STR_LEN);
l->next = NULL;
l->prev = NULL;
return l;
}
LinkedList *tmp = malloc(sizeof(LinkedList));
if (tmp == NULL)
exit(1);
tmp->next = l;
l->prev = tmp;
tmp->prev = NULL;
tmp->x = x;
memcpy(tmp->name, name, STR_LEN);
return tmp;
}
LinkedList *del_head(LinkedList *l)
{
if (l == NULL)
return NULL;
else if (l->next == NULL)
{
free(l);
l = NULL;
return NULL;
}
LinkedList *tmp = l->next;
tmp->prev = NULL;
free(l);
l = NULL;
return tmp;
}
void del_tail(LinkedList *l)
{
if (l == NULL)
return;
else if (l->next == NULL)
{
free(l);
l = NULL;
return;
}
while (l->next != NULL)
l = l->next;
LinkedList *tmp = l->prev;
l->prev = NULL;
free(l);
tmp->next = NULL;
}
int len(LinkedList *l)
{
int res = 0;
while (l != NULL)
{
res++;
l = l->next;
}
return res;
}
void swap(LinkedList *l, int first, int second)
{
if (first >= len(l) || second >= len(l))
exit(3);
int i, min, max;
min = (first <= second) ? first : second;
max = first + second - min;
for (i = 0; i < min; i++)
{
if (l != NULL)
l = l->next;
else
exit(2);
}
LinkedList *tmp = l;
for (i = 0; i < max - min; i++)
{
if (l != NULL)
l = l->next;
else
exit(2);
}
LinkedList temp = *l;
l->x = tmp->x;
tmp->x = temp.x;
memcpy(l->name, tmp->name, STR_LEN);
memcpy(tmp->name, temp.name, STR_LEN);
}
LinkedList get(LinkedList *l, int n)
{
if (n > len(l))
exit(3);
int i;
for (i = 0; i < n; i++)
{
l = l->next;
}
return *l;
}
void select_sort(LinkedList *l)
{
if (l == NULL || l->next == NULL)
return;
int n = len(l), i, j;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (get(l, j).x < get(l, i).x)
swap(l, i, j);
}
}
}
void list_fprintf(LinkedList *l, char path[])
{
FILE *f = fopen(path, "w");
if (f == NULL)
exit(4);
int i, n = len(l);
for (i = 0; i < n; i++)
{
fprintf(f, "%d %s\n", l->x, l->name);
l = l->next;
}
fclose(f);
}
LinkedList *list_fscanf(char path[])
{
FILE *f = fopen(path, "r");
if (f == NULL)
exit(5);
LinkedList *tmp = malloc(sizeof(LinkedList));
tmp->prev = NULL;
tmp->next = NULL;
int tmp_x, i;
char s[STR_LEN] = {'\0'};
for (i = 0; fscanf(f, "%d %s\n", &tmp_x, s) != EOF; i++)
{
if (i == 0)
{
tmp->x = tmp_x;
memcpy(tmp->name, s, STR_LEN);
}
else
add_tail(tmp, tmp_x, s);
}
fclose(f);
return tmp;
}
+52
View File
@@ -0,0 +1,52 @@
#include "head.h"
int main(int argc, char *argv[])
{
srand(time(0));
LinkedList *l = malloc(sizeof(LinkedList));
if (l == NULL)
exit(1);
int i, j;
for (i = 0; i < N; i++)
{
char s[STR_LEN];
for (j = 0; j < STR_LEN - 1; j++)
s[j] = 'a' + rand() % 25;
s[j + 1] = '\0';
add_tail(l, rand() % 100, s);
}
printf("\nAfter add_tail(%d):\n", len(l));
linked_print(l);
l = add_head(l, 5, "head");
printf("\nAfter add_head(%d):\n", len(l));
linked_print(l);
l = del_head(l);
printf("\nAfter del_head(%d):\n", len(l));
linked_print(l);
swap(l, 0, 1);
printf("\nAfter swap first and second el:\n");
linked_print(l);
del_tail(l);
printf("\nAfter del_tail(%d):\n", len(l));
linked_print(l);
select_sort(l);
printf("\nAfter sort:\n");
linked_print(l);
printf("\nWriting to file test.txt\n");
list_fprintf(l, "test.txt");
printf("\nRead from file test.txt:\n");
LinkedList *l2 = list_fscanf("test.txt");
linked_print(l2);
free(l);
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 5
#define STR_LEN 8
typedef struct LinkedList
{
int x;
struct LinkedList *next;
struct LinkedList *prev;
char name[STR_LEN];
} LinkedList;
void linked_print(LinkedList *l);
LinkedList *add_tail(LinkedList *l, int x, char name[]);
LinkedList *add_head(LinkedList *l, int x, char name[]);
LinkedList *del_head(LinkedList *l);
void del_tail(LinkedList *l);
int len(LinkedList *l);
LinkedList get(LinkedList *l, int n);
void swap(LinkedList *l1, int i, int j);
void select_sort(LinkedList *l);
void list_fprintf(LinkedList *l, char path[]);
LinkedList *list_fscanf(char path[]);
+171
View File
@@ -0,0 +1,171 @@
#include "head.h"
void linked_print(Node* l) {
while (l != NULL) {
printf("%d %s\n", l->x, l->name);
l = l->next;
}
}
Node* add_tail(Node* l, int x, char name[]) {
if (l->x == 0 && !strcmp(l->name, "\0")) {
l->x = x;
memcpy(l->name, name, STR_LEN);
l->next = NULL;
l->prev = NULL;
return l;
}
Node* res = l;
while (l->next != NULL) l = l->next;
Node* tmp = malloc(sizeof(Node));
tmp->x = x;
memcpy(tmp->name, name, STR_LEN);
tmp->prev = l;
l->next = tmp;
return res;
}
Node* add_head(Node* l, int x, char name[]) {
if (l->x == 0 && !strcmp(l->name, "\0")) {
l->x = x;
memcpy(l->name, name, STR_LEN);
l->next = NULL;
l->prev = NULL;
return l;
}
Node* tmp = malloc(sizeof(Node));
if (tmp == NULL)
exit(1);
tmp->next = l;
l->prev = tmp;
tmp->prev = NULL;
tmp->x = x;
memcpy(tmp->name, name, STR_LEN);
return tmp;
}
Node* del_head(Node* l) {
if (l == NULL) return NULL;
else if (l->next == NULL) {
free(l);
l = NULL;
return NULL;
}
Node* tmp = l->next;
tmp->prev = NULL;
free(l);
l = NULL;
return tmp;
}
void del_tail(Node* l) {
if (l == NULL) return;
else if (l->next == NULL) {
free(l);
l = NULL;
return;
}
while (l->next != NULL) l = l->next;
Node* tmp = l->prev;
l->prev = NULL;
free(l);
tmp->next = NULL;
}
int len(Node* l) {
int res = 0;
while (l != NULL) {
res++;
l = l->next;
}
return res;
}
void swap(Node* l, int first, int second) {
if (first >= len(l) || second >= len(l)) exit(3);
int i, min, max;
min = (first <= second) ? first : second;
max = first + second - min;
for (i = 0; i < min; i++) {
if (l != NULL) l = l->next;
else exit(2);
}
Node* tmp = l;
for (i = 0; i < max - min; i++) {
if (l != NULL) l = l->next;
else exit(2);
}
Node temp = *l;
l->x = tmp->x;
tmp->x = temp.x;
memcpy(l->name, tmp->name, STR_LEN);
memcpy(tmp->name, temp.name, STR_LEN);
}
Node get(Node* l, int n) {
if (n > len(l)) exit(3);
int i;
for (i = 0; i < n; i++) {
l = l->next;
}
return *l;
}
void select_sort(Node* l) {
if (l == NULL || l->next == NULL) return;
int n = len(l), i, j;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (get(l, j).x < get(l, i).x)
swap(l, i, j);
}
}
}
void list_fprintf(Node* l, char path[]) {
FILE* f = fopen(path, "w");
if (f == NULL)
exit(4);
int i, n = len(l);
for (i = 0; i < n; i++) {
fprintf(f, "%d %s\n", l->x, l->name);
l = l->next;
}
fclose(f);
}
Node* list_fscanf(char path[]) {
int tmp_x, i;
char s[STR_LEN] = {'\0'};
FILE* f = fopen(path, "r");
if (f == NULL)
exit(5);
Node* tmp = malloc(sizeof(Node)); //Дописать проверку на NULL
tmp->prev = NULL;
tmp->next = NULL;
for (i = 0; fscanf(f, "%d %s\n", &tmp_x, s) != EOF; i++) {
if (i == 0) {
tmp->x = tmp_x;
memcpy(tmp->name, s, STR_LEN);
} // Перенести в a_t
else add_tail(tmp, tmp_x, s);
}
fclose(f);
return tmp;
}
+11
View File
@@ -0,0 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct Node {
int x;
struct Node* next;
struct Node* prev;
char name[STR_LEN];
} Node;
+6
View File
@@ -0,0 +1,6 @@
#include "func.c"
int main(){
}
+51
View File
@@ -0,0 +1,51 @@
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define E 1.0E-5
#define A1 -2.0
#define B1 -1.5
#define A2 -1.0
#define B2 0.0
char *sf1 = "[-3:1] sin(2*x)-exp(x)\"";
char *sf2 = "[-3:1] sin(2*x)+exp(x)\"";
char *gpl = "gnuplot -p -e \"plot ";
#define DBG
//gcc filename.c -l m -o filename
double f1(double x) {
return sin(2*x)-exp(x);
}
double f2(double x) {
return sin(2*x)+exp(x);
}
double dih(double f(), double a, double b);
int main(){
char cmd[128] = "\0";
strcat(cmd,gpl);
strcat(cmd,sf1);
printf("%s\n", cmd);
system(cmd);
printf("X1 = %.10F\n",dih(f1,A1,B1));
printf("X2 = %F\n",dih(f2,A2,B2));
return 0;
}
double dih(double f(), double a, double b){
double c;
while( fabs(b-a) > E ) {
c = (a+b)/2;
#ifdef DBG
printf ("%F \n", c );
#endif
if(f(a)*f(c) < 0 )
b = c;
else
a = c;
}
return c;
}
Binary file not shown.
@@ -0,0 +1,84 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "header.h"
void pr(node *tmp){
int i;
if(tmp == NULL){printf("[%p]\n", tmp); return;}
while(tmp){
printf("[%14p]<-[%14p]->[%14p] ", tmp->prev, tmp, tmp->next);
printf("data: [%4ld] s[" , tmp->data);
for(i=0;i<COUN; i++) printf("%c", tmp->s[i]);
printf("]\n");
tmp = tmp->next;
}
printf("---------------------\n");
}
node *add_head(node *head, int hh){
node *tmp=NULL;
int i;
if((tmp=malloc(sizeof(node)))==NULL){
perror("malloc: NULL");
exit(2);
}
tmp->prev=NULL;
tmp->data=hh;
for(i=0;i<COUN;i++) tmp->s[i]=((char)(65+rand()%25));
if(head==NULL) tmp->next=NULL;
else{
tmp->next=head;
head->prev=tmp;
}
return tmp;
}
node *add_tail(node *head, int hh){
node *tmp=NULL;
node *t = NULL;
int i;
if((tmp=malloc(sizeof(node)))==NULL){
perror("malloc: NULL");
exit(3);
}
tmp->data=hh;
for(i=0;i<COUN;i++) tmp->s[i]=((char)(65+rand()%25));
tmp->next=NULL;
if(head==NULL) tmp->prev=NULL;
else{
t = head;
while(t->next!=NULL) t = t->next;
t->next=tmp;
tmp->prev=head;
}
return head;
}
node *del_head(node *head){
node *tmp = head;
if(head==NULL) return NULL;
if(head -> next == NULL){
free(head);
return NULL;
}
head = head->next;
head -> prev = NULL;
free(tmp);
return head;
}
node *del_tail(node *list){
node *tail = NULL;
if(list==NULL) return NULL;
if(list -> next == NULL){
free(list);
return NULL;
}
tail = list;
while(tail->next->next) tail = tail->next;
free(tail->next);
tail -> next=NULL;
return list;
}
@@ -0,0 +1,19 @@
#define COUN 40
typedef struct Node
{
long int data;
struct Node *next;
struct Node *prev;
char s[COUN];
} node;
void pr(node *tmp);
node *add_head(node *head, int hh);
node *add_tail(node *head, int hh);
node *del_head(node *head);
node *del_tail(node *list);
// node *sort(node *head);
// int lenghth(node *list);
// void writefile(const char *path, node *head);
// node *readfile(const char *path);
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"
#include <windows.h>
int main(int args, char *argv[])
{
node *head = NULL;
int r;
srand(time(NULL));
while ((r = (rand() % 100)) < 90)
head = add_head(head, r);
pr(head);
head = del_head(head);
pr(head);
head = add_tail(head, 5);
pr(head);
head = del_tail(head);
pr(head);
}
// Домашка: сортировка списка и вычисление длины списка(по желанию запись в файл и чтение с файла)
// Ханойские башни игра написать