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
+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);
}
// Домашка: сортировка списка и вычисление длины списка(по желанию запись в файл и чтение с файла)
// Ханойские башни игра написать