mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 01:00:15 +00:00
dir.main added
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
#include "head.h"
|
#include "head.h"
|
||||||
|
|
||||||
|
|
||||||
void linked_print(LinkedList* l) {
|
void linked_print(LinkList* l) {
|
||||||
while (l != NULL) {
|
while (l != NULL) {
|
||||||
printf("%d %s\n", l->x, l->name);
|
printf("%d %s\n", l->x, l->name);
|
||||||
l = l->next;
|
l = l->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LinkedList* add_tail(LinkedList* l, LinkedList node) {
|
LinkList* add_tail(LinkList* l, LinkList node) {
|
||||||
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
||||||
l->x = node.x;
|
l->x = node.x;
|
||||||
memcpy(l->name, node.name, LEN);
|
memcpy(l->name, node.name, LEN);
|
||||||
@@ -17,10 +17,10 @@ LinkedList* add_tail(LinkedList* l, LinkedList node) {
|
|||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
LinkedList* res = l;
|
LinkList* res = l;
|
||||||
while (l->next != NULL) l = l->next;
|
while (l->next != NULL) l = l->next;
|
||||||
|
|
||||||
LinkedList* tmp = malloc(sizeof(LinkedList));
|
LinkList* tmp = malloc(sizeof(LinkList));
|
||||||
tmp->x = node.x;
|
tmp->x = node.x;
|
||||||
memcpy(tmp->name, node.name, LEN);
|
memcpy(tmp->name, node.name, LEN);
|
||||||
tmp->prev = l;
|
tmp->prev = l;
|
||||||
@@ -28,7 +28,7 @@ LinkedList* add_tail(LinkedList* l, LinkedList node) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
LinkedList* add_head(LinkedList* l, LinkedList node) {
|
LinkList* add_head(LinkList* l, LinkList node) {
|
||||||
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
||||||
l->x = node.x;
|
l->x = node.x;
|
||||||
memcpy(l->name, node.name, LEN);
|
memcpy(l->name, node.name, LEN);
|
||||||
@@ -37,7 +37,7 @@ LinkedList* add_head(LinkedList* l, LinkedList node) {
|
|||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
LinkedList* tmp = malloc(sizeof(LinkedList));
|
LinkList* tmp = malloc(sizeof(LinkList));
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
||||||
@@ -49,19 +49,19 @@ LinkedList* add_head(LinkedList* l, LinkedList node) {
|
|||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
LinkedList* del_head(LinkedList* l) {
|
LinkList* del_head(LinkList* l) {
|
||||||
if (l == NULL) return NULL;
|
if (l == NULL) return NULL;
|
||||||
if (l->next == NULL) {
|
if (l->next == NULL) {
|
||||||
free(l);
|
free(l);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
LinkedList* tmp = l->next;
|
LinkList* tmp = l->next;
|
||||||
tmp->prev = NULL;
|
tmp->prev = NULL;
|
||||||
free(l);
|
free(l);
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void del_tail(LinkedList* l) {
|
void del_tail(LinkList* l) {
|
||||||
if (l == NULL) return;
|
if (l == NULL) return;
|
||||||
else if (l->next == NULL) {
|
else if (l->next == NULL) {
|
||||||
free(l);
|
free(l);
|
||||||
@@ -69,13 +69,13 @@ void del_tail(LinkedList* l) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
while (l->next != NULL) l = l->next;
|
while (l->next != NULL) l = l->next;
|
||||||
LinkedList* tmp = l->prev;
|
LinkList* tmp = l->prev;
|
||||||
l->prev = NULL;
|
l->prev = NULL;
|
||||||
free(l);
|
free(l);
|
||||||
tmp->next = NULL;
|
tmp->next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int len(LinkedList* l) {
|
int len(LinkList* l) {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
if (l->x == 0 && !strcmp(l->name, "\0"))
|
if (l->x == 0 && !strcmp(l->name, "\0"))
|
||||||
return 0;
|
return 0;
|
||||||
@@ -87,7 +87,7 @@ int len(LinkedList* l) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
LinkedList get(LinkedList* l, int n) {
|
LinkList get(LinkList* l, int n) {
|
||||||
if (n > len(l)) exit(3);
|
if (n > len(l)) exit(3);
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
@@ -96,7 +96,7 @@ LinkedList get(LinkedList* l, int n) {
|
|||||||
return *l;
|
return *l;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tower_of_hanoi(int n, LinkedList* start, LinkedList* finish, LinkedList* tmp) {
|
void tower_of_hanoi(int n, LinkList* start, LinkList* finish, LinkList* tmp) {
|
||||||
if (n == 1) {
|
if (n == 1) {
|
||||||
finish = add_head(finish, get(start, 0));
|
finish = add_head(finish, get(start, 0));
|
||||||
start = del_head(start);
|
start = del_head(start);
|
||||||
@@ -118,11 +118,11 @@ void tower_of_hanoi(int n, LinkedList* start, LinkedList* finish, LinkedList* tm
|
|||||||
linked_print(tmp);
|
linked_print(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void linked_free(LinkedList* l) {
|
void linked_free(LinkList* l) {
|
||||||
while (l->next != NULL) l = l->next;
|
while (l->next != NULL) l = l->next;
|
||||||
|
|
||||||
while (l->prev != NULL) {
|
while (l->prev != NULL) {
|
||||||
LinkedList* tmp = l;
|
LinkList* tmp = l;
|
||||||
l = l->prev;
|
l = l->prev;
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
srand(time(0));
|
srand(time(0));
|
||||||
LinkedList* l1 = malloc(sizeof(LinkedList));
|
LinkList* l1 = malloc(sizeof(LinkList));
|
||||||
LinkedList* l2 = malloc(sizeof(LinkedList));
|
LinkList* l2 = malloc(sizeof(LinkList));
|
||||||
LinkedList* l3 = malloc(sizeof(LinkedList));
|
LinkList* l3 = malloc(sizeof(LinkList));
|
||||||
|
|
||||||
if (l1 == NULL || l2 == NULL || l3 == NULL)
|
if (l1 == NULL || l2 == NULL || l3 == NULL)
|
||||||
exit(1);
|
exit(1);
|
||||||
@@ -17,7 +17,7 @@ int main(int argc, char* argv[]) {
|
|||||||
s[j] = 'a' + rand() % 25;
|
s[j] = 'a' + rand() % 25;
|
||||||
s[j + 1] = '\0';
|
s[j + 1] = '\0';
|
||||||
|
|
||||||
LinkedList l;
|
LinkList l;
|
||||||
l.x = i;
|
l.x = i;
|
||||||
memcpy(l.name, s, LEN);
|
memcpy(l.name, s, LEN);
|
||||||
l.next = NULL;
|
l.next = NULL;
|
||||||
|
|||||||
@@ -6,19 +6,19 @@
|
|||||||
#define N 3
|
#define N 3
|
||||||
#define LEN 8
|
#define LEN 8
|
||||||
|
|
||||||
typedef struct LinkedList {
|
typedef struct LinkList {
|
||||||
int x;
|
int x;
|
||||||
char name[LEN];
|
char name[LEN];
|
||||||
struct LinkedList* next;
|
struct LinkList* next;
|
||||||
struct LinkedList* prev;
|
struct LinkList* prev;
|
||||||
} LinkedList;
|
} LinkList;
|
||||||
|
|
||||||
void linked_print(LinkedList* l);
|
void linked_print(LinkList* l);
|
||||||
LinkedList* add_tail(LinkedList* l, LinkedList node);
|
LinkList* add_tail(LinkList* l, LinkList node);
|
||||||
LinkedList* add_head(LinkedList* l, LinkedList node);
|
LinkList* add_head(LinkList* l, LinkList node);
|
||||||
LinkedList* del_head(LinkedList* l);
|
LinkList* del_head(LinkList* l);
|
||||||
void del_tail(LinkedList* l);
|
void del_tail(LinkList* l);
|
||||||
int len(LinkedList* l);
|
int len(LinkList* l);
|
||||||
LinkedList get(LinkedList* l, int n);
|
LinkList get(LinkList* l, int n);
|
||||||
void tower_of_hanoi(int n, LinkedList* start, LinkedList* finish, LinkedList* tmp);
|
void tower_of_hanoi(int n, LinkList* start, LinkList* finish, LinkList* tmp);
|
||||||
void linked_free(LinkedList* l);
|
void linked_free(LinkList* l);
|
||||||
Binary file not shown.
@@ -0,0 +1,76 @@
|
|||||||
|
#include "header.h"
|
||||||
|
|
||||||
|
void linked_print(LinkedList *l)
|
||||||
|
{
|
||||||
|
while (l != NULL)
|
||||||
|
{
|
||||||
|
printf("%lF\n", l->x);
|
||||||
|
l = l->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LinkedList *add_head(LinkedList *l, double x)
|
||||||
|
{
|
||||||
|
if (l == NULL)
|
||||||
|
{
|
||||||
|
l = malloc(sizeof(LinkedList));
|
||||||
|
if (l == NULL)
|
||||||
|
exit(1);
|
||||||
|
l->x = x;
|
||||||
|
l->next = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (l->x == 0.0)
|
||||||
|
{
|
||||||
|
l->x = x;
|
||||||
|
l->next = NULL;
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
LinkedList *tmp = malloc(sizeof(LinkedList));
|
||||||
|
if (tmp == NULL)
|
||||||
|
exit(1);
|
||||||
|
|
||||||
|
tmp->next = l;
|
||||||
|
tmp->x = x;
|
||||||
|
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;
|
||||||
|
free(l);
|
||||||
|
l = NULL;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
int len(LinkedList *l)
|
||||||
|
{
|
||||||
|
int res = 0;
|
||||||
|
while (l != NULL)
|
||||||
|
{
|
||||||
|
res++;
|
||||||
|
l = l->next;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
#include "header.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
srand(time(0));
|
||||||
|
LinkedList *numbers = malloc(sizeof(LinkedList));
|
||||||
|
if (numbers == NULL)
|
||||||
|
exit(1);
|
||||||
|
|
||||||
|
char c;
|
||||||
|
char flag = 'n';
|
||||||
|
|
||||||
|
while ((c = getchar()) != EOF)
|
||||||
|
{
|
||||||
|
if (c == '\n')
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (c == ' ')
|
||||||
|
{
|
||||||
|
flag = 's';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (c)
|
||||||
|
{
|
||||||
|
case '0' ... '9':
|
||||||
|
if (flag == 's')
|
||||||
|
{
|
||||||
|
numbers = add_head(numbers, (double)(c - '0'));
|
||||||
|
flag = 'n';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
double a = get(numbers, 0).x;
|
||||||
|
numbers = del_head(numbers);
|
||||||
|
a = a * 10 + c - '0';
|
||||||
|
numbers = add_head(numbers, a);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '+':
|
||||||
|
case '-':
|
||||||
|
case '*':
|
||||||
|
case '/':
|
||||||
|
if (len(numbers) < 2)
|
||||||
|
{
|
||||||
|
printf("Invalid expression\n");
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
double a = get(numbers, 0).x;
|
||||||
|
numbers = del_head(numbers);
|
||||||
|
double b = get(numbers, 0).x;
|
||||||
|
numbers = del_head(numbers);
|
||||||
|
|
||||||
|
b = (c == '+') ? b + a : b;
|
||||||
|
b = (c == '-') ? b - a : b;
|
||||||
|
b = (c == '*') ? b * a : b;
|
||||||
|
b = (c == '/') ? b / a : b;
|
||||||
|
|
||||||
|
numbers = add_head(numbers, b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("res: %lF", get(numbers, 0).x);
|
||||||
|
|
||||||
|
free(numbers);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#define N 5
|
||||||
|
|
||||||
|
typedef struct LinkedList
|
||||||
|
{
|
||||||
|
double x;
|
||||||
|
struct LinkedList *next;
|
||||||
|
} LinkedList;
|
||||||
|
|
||||||
|
void linked_print(LinkedList *l);
|
||||||
|
LinkedList *add_head(LinkedList *l, double x);
|
||||||
|
LinkedList *del_head(LinkedList *l);
|
||||||
|
int len(LinkedList *l);
|
||||||
|
LinkedList get(LinkedList *l, int n);
|
||||||
Binary file not shown.
@@ -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.
+43
-25
@@ -1,16 +1,20 @@
|
|||||||
#include "header.h"
|
#include "header.h"
|
||||||
|
|
||||||
|
void node_print(Node *n)
|
||||||
void node_print(Node* n) {
|
{
|
||||||
while (n != NULL) {
|
while (n != NULL)
|
||||||
|
{
|
||||||
printf("%s\n", n->name);
|
printf("%s\n", n->name);
|
||||||
n = n->next;
|
n = n->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* add_tail(Node* n, char s[]) {
|
Node *add_tail(Node *n, char s[])
|
||||||
if (n == NULL) {
|
{
|
||||||
if ((n = malloc(sizeof(Node))) == NULL) {
|
if (n == NULL)
|
||||||
|
{
|
||||||
|
if ((n = malloc(sizeof(Node))) == NULL)
|
||||||
|
{
|
||||||
printf("could not malloc\n");
|
printf("could not malloc\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@@ -19,51 +23,62 @@ Node* add_tail(Node* n, char s[]) {
|
|||||||
memcpy(n->name, s, LEN);
|
memcpy(n->name, s, LEN);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
if (!strcmp(n->name, "\0")) {
|
if (!strcmp(n->name, "\0"))
|
||||||
|
{
|
||||||
memcpy(n->name, s, LEN);
|
memcpy(n->name, s, LEN);
|
||||||
n->next = NULL;
|
n->next = NULL;
|
||||||
n->prev = NULL;
|
n->prev = NULL;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
Node *res = n;
|
||||||
Node* res = n;
|
while (n->next != NULL){
|
||||||
while (n->next != NULL) n = n->next;
|
printf("%p\n", n);
|
||||||
|
n = n->next;
|
||||||
Node* tmp = malloc(sizeof(Node));
|
}
|
||||||
|
Node *tmp = malloc(sizeof(Node));
|
||||||
memcpy(tmp->name, s, LEN);
|
memcpy(tmp->name, s, LEN);
|
||||||
tmp->prev = n;
|
tmp->prev = n;
|
||||||
n->next = tmp;
|
n->next = tmp;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node get(Node* node, int n) {
|
Node get(Node *node, int n)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
node = node->next;
|
node = node->next;
|
||||||
}
|
}
|
||||||
return *node;
|
return *node;
|
||||||
}
|
}
|
||||||
|
|
||||||
int len(Node* n) {
|
int len(Node *n)
|
||||||
|
{
|
||||||
int res = 0;
|
int res = 0;
|
||||||
while (n != NULL) {
|
while (n != NULL)
|
||||||
|
{
|
||||||
res++;
|
res++;
|
||||||
n = n->next;
|
n = n->next;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* xor_find(Node* n1, Node* n2) {
|
Node *xor_find(Node *n1, Node *n2)
|
||||||
|
{
|
||||||
int i, j, f = 0;
|
int i, j, f = 0;
|
||||||
Node* res = NULL;
|
Node *res = NULL;
|
||||||
if ((res = malloc(sizeof(Node))) == NULL) {
|
if ((res = malloc(sizeof(Node))) == NULL)
|
||||||
|
{
|
||||||
printf("could not malloc\n");
|
printf("could not malloc\n");
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < len(n1); i++) {
|
for (i = 0; i < len(n1); i++)
|
||||||
for (j = 0; j < len(n2); j++) {
|
{
|
||||||
if (strcmp(get(n1, i).name, get(n2, j).name) == 0) {
|
for (j = 0; j < len(n2); j++)
|
||||||
|
{
|
||||||
|
if (strcmp(get(n1, i).name, get(n2, j).name) == 0)
|
||||||
|
{
|
||||||
f = 1;
|
f = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,9 +87,12 @@ Node* xor_find(Node* n1, Node* n2) {
|
|||||||
f = 0;
|
f = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < len(n2); i++) {
|
for (i = 0; i < len(n2); i++)
|
||||||
for (j = 0; j < len(n1); j++) {
|
{
|
||||||
if (strcmp(get(n2, i).name, get(n1, j).name) == 0) {
|
for (j = 0; j < len(n1); j++)
|
||||||
|
{
|
||||||
|
if (strcmp(get(n2, i).name, get(n1, j).name) == 0)
|
||||||
|
{
|
||||||
f = 1;
|
f = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-12
@@ -1,29 +1,37 @@
|
|||||||
#include "header.h"
|
#include "header.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
int main(int argc, char* argv[]) {
|
{
|
||||||
|
printf("Cto-Nibud\n");
|
||||||
srand(time(0));
|
srand(time(0));
|
||||||
Node* n1 = NULL;
|
Node *n1 = NULL;
|
||||||
Node* n2 = NULL;
|
Node *n2 = NULL;
|
||||||
Node* n3 = NULL;
|
Node *n3 = NULL;
|
||||||
if ((n1 = malloc(sizeof(Node))) == NULL || (n2 = malloc(sizeof(Node))) == NULL || (n3 = malloc(sizeof(Node))) == NULL) {
|
|
||||||
|
if ((n1 = malloc(sizeof(Node))) == NULL || (n2 = malloc(sizeof(Node))) == NULL || (n3 = malloc(sizeof(Node))) == NULL)
|
||||||
|
{
|
||||||
printf("could not malloc\n");
|
printf("could not malloc\n");
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
printf("Error here 1");
|
n1 = add_tail(n1, "bebebe");
|
||||||
|
|
||||||
|
printf("Error here 1\n");
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < COUNT; i++) {
|
for (i = 0; i < COUNT; i++)
|
||||||
|
{
|
||||||
char s[LEN];
|
char s[LEN];
|
||||||
int j;
|
int j;
|
||||||
|
printf("sss\n");
|
||||||
for (j = 0; j < LEN - 1; j++)
|
for (j = 0; j < LEN - 1; j++)
|
||||||
s[j] = 'a' + rand() % 2;
|
s[j] = 'a' + rand() % 2;
|
||||||
s[LEN - 1] = '\0';
|
s[LEN - 1] = '\0';
|
||||||
|
|
||||||
n1 = add_tail(n1, s);
|
n1 = add_tail(n1, s);
|
||||||
|
printf("11");
|
||||||
for (j = 0; j < LEN - 1; j++)
|
for (j = 0; j < LEN - 1; j++){
|
||||||
s[j]= 'a' + rand() % 2;
|
s[j] = 'a' + rand() % 2;
|
||||||
|
printf("22");
|
||||||
|
}
|
||||||
s[LEN - 1] = '\0';
|
s[LEN - 1] = '\0';
|
||||||
n2 = add_tail(n2, s);
|
n2 = add_tail(n2, s);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,14 +6,15 @@
|
|||||||
#define COUNT 3
|
#define COUNT 3
|
||||||
#define LEN 3
|
#define LEN 3
|
||||||
|
|
||||||
typedef struct Node {
|
typedef struct Node
|
||||||
|
{
|
||||||
char name[LEN];
|
char name[LEN];
|
||||||
struct Node* next;
|
struct Node *next;
|
||||||
struct Node* prev;
|
struct Node *prev;
|
||||||
} Node;
|
} Node;
|
||||||
|
|
||||||
void node_print(Node* n);
|
void node_print(Node *n);
|
||||||
Node* add_tail(Node* n, char s[]);
|
Node *add_tail(Node *n, char s[]);
|
||||||
Node get(Node* node, int n);
|
Node get(Node *node, int n);
|
||||||
Node* xor_find(Node* n1, Node* n2);
|
Node *xor_find(Node *n1, Node *n2);
|
||||||
int len(Node* n);
|
int len(Node *n);
|
||||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,6 @@
|
|||||||
|
1 fdxyqwb3
|
||||||
|
1 icotltwo
|
||||||
|
2 ysharmos
|
||||||
|
2 xhlbvpd3
|
||||||
|
3 owvischE
|
||||||
|
3 bmmuvuhM
|
||||||
@@ -0,0 +1,166 @@
|
|||||||
|
#include "head.h"
|
||||||
|
|
||||||
|
|
||||||
|
void st_rand(Book books[], int n) {
|
||||||
|
int i, j;
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
// books[i].len = rand() % 99 + 1;
|
||||||
|
books[i].len = rand() % 3 + 1;
|
||||||
|
for (j = 0; j < STR_LEN - 1; j++)
|
||||||
|
books[i].name[j] = 'a' + rand() % 25;
|
||||||
|
books[i].name[j + 1] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void st_print(Book books[], int n) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
printf("len: %3d; name: %s\n", books[i].len, books[i].name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(Book* b1, Book* b2) {
|
||||||
|
Book* tmp = malloc(sizeof(Book));
|
||||||
|
|
||||||
|
if (tmp == NULL) {
|
||||||
|
printf("could not malloc\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(tmp, b1, sizeof(Book));
|
||||||
|
memcpy(b1, b2, sizeof(Book));
|
||||||
|
memcpy(b2, tmp, sizeof(Book));
|
||||||
|
free(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void selection_sort(Book books[], int n) {
|
||||||
|
int i, j;
|
||||||
|
for (i = 0; i < n - 1; i++) {
|
||||||
|
for (j = i + 1; j < n; j++) {
|
||||||
|
if (books[j].len < books[i].len)
|
||||||
|
swap(books + i, books + j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Book* bin_search(Book books[], int n, int key) {
|
||||||
|
Book* res = NULL;
|
||||||
|
int low = 0, high = n - 1;
|
||||||
|
|
||||||
|
while (low <= high) {
|
||||||
|
int mid = (high + low) / 2;
|
||||||
|
|
||||||
|
if (books[mid].len == key) {
|
||||||
|
res = &books[mid];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
low = (books[mid].len < key) ? mid + 1: low;
|
||||||
|
high = (books[mid].len > key) ? mid - 1: high;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pst(Book books[], int size, int n) {
|
||||||
|
if (n >= size) {
|
||||||
|
printf("index bigger then length of the array\n");
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("len: %3d; name: %s\n", books[n].len, books[n].name);
|
||||||
|
}
|
||||||
|
|
||||||
|
int matches_number(Book b[], int n, int match) {
|
||||||
|
int res = 0, i;
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
if (b[i].len == match) res++;
|
||||||
|
else if (b[i].len > match) break;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int find_pairs_easy(Book b1[], int n1, Book b2[], int n2) {
|
||||||
|
int res = 0, i;
|
||||||
|
for (i = 0; i < n1; i++) {
|
||||||
|
Book* b_res = bin_search(b2, n2, b1[i].len);
|
||||||
|
if (b_res == NULL) continue;
|
||||||
|
res++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int find_pairs_advanced(Book b1[], int n1, Book b2[], int n2) {
|
||||||
|
int res = 0, i;
|
||||||
|
for (i = 0; i < n1; i++) {
|
||||||
|
Book* b_res = bin_search(b2, n2, b1[i].len);
|
||||||
|
if (b_res == NULL) continue;
|
||||||
|
res += matches_number(b2, n2, b1[i].len);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void st_fprintf(Book books[], int n, char path[], char access[]) {
|
||||||
|
int i;
|
||||||
|
FILE* f = fopen(path, access);
|
||||||
|
if (f == NULL) {
|
||||||
|
printf("Could not open file %s\n", path);
|
||||||
|
exit(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
fprintf(f, "%d %s\n", books[i].len, books[i].name);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
int lines_numf(char path[]) {
|
||||||
|
int res = 0;
|
||||||
|
char c;
|
||||||
|
FILE* f = fopen(path, "r");
|
||||||
|
if (f == NULL) {
|
||||||
|
printf("Could not open file %s\n", path);
|
||||||
|
exit(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((c = fgetc(f)) != EOF) {
|
||||||
|
if (c == '\n') res++;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lines_num(char path[]) {
|
||||||
|
int res = 0, flag = 0;
|
||||||
|
char c;
|
||||||
|
FILE* f = fopen(path, "r");
|
||||||
|
if (f == NULL) {
|
||||||
|
printf("Could not open file %s\n", path);
|
||||||
|
exit(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((c = fgetc(f)) != EOF) {
|
||||||
|
if (c != '\n') flag = 1;
|
||||||
|
if (c == '\n' && flag) {
|
||||||
|
res++;
|
||||||
|
flag = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
return res + flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
void st_fscanf(Book b[], int n, char path[]) {
|
||||||
|
int i;
|
||||||
|
FILE* f = fopen(path, "r");
|
||||||
|
if (f == NULL) {
|
||||||
|
printf("Could not open file %s\n", path);
|
||||||
|
exit(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
fscanf(f, "%d %s\n", &b[i].len, b[i].name);
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
#include "head.h"
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
srand(time(0));
|
||||||
|
|
||||||
|
Book* books1 = malloc(sizeof(Book) * N);
|
||||||
|
Book* books2 = malloc(sizeof(Book) * N);
|
||||||
|
|
||||||
|
if (books1 == NULL || books2 == NULL) {
|
||||||
|
printf("could not malloc\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
st_rand(books1, N);
|
||||||
|
st_rand(books2, N);
|
||||||
|
|
||||||
|
printf("First array:\n");
|
||||||
|
st_print(books1, N);
|
||||||
|
printf("\nSecond array:\n");
|
||||||
|
st_print(books2, N);
|
||||||
|
|
||||||
|
selection_sort(books1, N);
|
||||||
|
selection_sort(books2, N);
|
||||||
|
|
||||||
|
printf("\nFirst array after sort:\n");
|
||||||
|
st_print(books1, N);
|
||||||
|
printf("\nSecond array after sort:\n");
|
||||||
|
st_print(books2, N);
|
||||||
|
|
||||||
|
Book* res = bin_search(books1, N, 15);
|
||||||
|
|
||||||
|
if (res == NULL)
|
||||||
|
printf("\nNo such book\n");
|
||||||
|
else
|
||||||
|
printf("\nLength: %2d; name: %s\n", res->len, res->name);
|
||||||
|
|
||||||
|
printf("\nElement in first array: ");
|
||||||
|
pst(books1, N, (int)(N * 0.7));
|
||||||
|
printf("Element in second array: ");
|
||||||
|
pst(books2, N, (int)(N * 0.3));
|
||||||
|
|
||||||
|
printf("\nNumber of pairs (easy realization): %d\n", find_pairs_easy(books1, N, books2, N));
|
||||||
|
printf("Number of pairs (advanced realization): %d\n", find_pairs_advanced(books1, N, books2, N));
|
||||||
|
|
||||||
|
st_fprintf(books1, N, "books.txt", "w");
|
||||||
|
st_fprintf(books2, N, "books.txt", "a");
|
||||||
|
|
||||||
|
int line_number = lines_numf("books.txt");
|
||||||
|
printf("\nNumber of lines in books.txt is %d\n", line_number);
|
||||||
|
|
||||||
|
Book* books3 = malloc(sizeof(Book) * line_number);
|
||||||
|
|
||||||
|
if (books3 == NULL) {
|
||||||
|
printf("could not malloc\n");
|
||||||
|
free(books1);
|
||||||
|
free(books2);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
st_fscanf(books3, line_number, "books.txt");
|
||||||
|
selection_sort(books3, line_number);
|
||||||
|
|
||||||
|
printf("\nThird array is:\n");
|
||||||
|
st_print(books3, line_number);
|
||||||
|
|
||||||
|
free(books1);
|
||||||
|
free(books2);
|
||||||
|
free(books3);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#define N 3
|
||||||
|
#define STR_LEN 8
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct Book {
|
||||||
|
int len;
|
||||||
|
char name[STR_LEN];
|
||||||
|
} Book;
|
||||||
|
|
||||||
|
void st_rand(Book books[], int n);
|
||||||
|
void st_print(Book books[], int n);
|
||||||
|
void swap(Book* b1, Book* b2);
|
||||||
|
void selection_sort(Book books[], int n);
|
||||||
|
Book* bin_search(Book books[], int n, int key);
|
||||||
|
void pst(Book books[], int size, int n);
|
||||||
|
int matches_number(Book b[], int n, int match);
|
||||||
|
int find_pairs_easy(Book b1[], int n1, Book b2[], int n2);
|
||||||
|
int find_pairs_advanced(Book b1[], int n1, Book b2[], int n2);
|
||||||
|
void st_fprintf(Book books[], int n, char path[], char access[]);
|
||||||
|
int lines_numf(char path[]);
|
||||||
|
int lines_num(char path[]);
|
||||||
|
void st_fscanf(Book b[], int n, char path[]);
|
||||||
Binary file not shown.
@@ -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;
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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[]);
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
double f(double x) { return exp(x) + sin(2.0 * x); }
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
srand(time(0));
|
||||||
|
|
||||||
|
double start = -4.0, finish = -3.0, e = 1e-6, mid = (finish + start) / 2.0;
|
||||||
|
|
||||||
|
if (f(start) * f(finish) > 0) {
|
||||||
|
printf("Could not find\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (fabs(f(mid)) >= e) {
|
||||||
|
mid = (finish + start) / 2.0;
|
||||||
|
if (f(mid) > 0) {
|
||||||
|
start = (f(start) > f(finish)) ? mid : start;
|
||||||
|
finish = (f(start) < f(finish)) ? mid : finish;
|
||||||
|
} else {
|
||||||
|
start = (f(start) < f(finish)) ? mid : start;
|
||||||
|
finish = (f(start) > f(finish)) ? mid : finish;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Found: %lF\n", start);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -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.
@@ -1,50 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#define N 5
|
|
||||||
void sort_Select(int x, int *arr){
|
|
||||||
int ind, temp = 0;
|
|
||||||
int i,j;
|
|
||||||
for(i=0;i<(x-1);i++){
|
|
||||||
ind = i;
|
|
||||||
for(j=i+1;j<x;j++){
|
|
||||||
if(arr[ind]>arr[j]){
|
|
||||||
ind = j;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
temp = arr[i];
|
|
||||||
arr[i] = arr[ind];
|
|
||||||
arr[ind] = temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
//meow
|
|
||||||
void sort_insert(int l, int *arr){
|
|
||||||
int i, j, x;
|
|
||||||
for(i=0;i<l;i++){
|
|
||||||
j = i-1;
|
|
||||||
x = arr[i];
|
|
||||||
while((arr[j]>x)&&(j>=0)){
|
|
||||||
arr[j+1] = arr[j];
|
|
||||||
j--;
|
|
||||||
arr[j+1] = x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
int main(int argc, char** argv) {
|
|
||||||
int l,i;
|
|
||||||
//printf("Vvedite razmer massiva: ");
|
|
||||||
//scanf("%d", l);
|
|
||||||
int arr[N] = {4,5,2,3,1};
|
|
||||||
int arra[N] = {3,5,2,4,1};
|
|
||||||
sort_Select(N, arr);
|
|
||||||
sort_insert(N, arra);
|
|
||||||
for(i=0;i<N;i++){
|
|
||||||
printf("%d", arr[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
for(i=0;i<N;i++){
|
|
||||||
printf("%d", arra[i]);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#include "st_func.h"
|
|
||||||
#define N 5
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
|
||||||
struct st1 q[N];
|
|
||||||
|
|
||||||
st_rand(q,N);
|
|
||||||
st_print(q,N);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
struct st1{
|
|
||||||
int i;
|
|
||||||
char s[128];
|
|
||||||
};
|
|
||||||
|
|
||||||
void st_rand(struct st1 temp[], int n);
|
|
||||||
void st_print(struct st1 temp[], int n);
|
|
||||||
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<stdlib.h>
|
|
||||||
#include "st_func.h"
|
|
||||||
|
|
||||||
void st_rand(struct st1 temp[], int n){
|
|
||||||
int k;
|
|
||||||
int j;
|
|
||||||
|
|
||||||
srand(time(NULL));
|
|
||||||
for(k=0; k<n; k++){
|
|
||||||
(temp+k)->i = rand()%100;
|
|
||||||
for(j=0;j<127;j++){
|
|
||||||
(temp+k)->s[j]=((char)(65+rand()%25));
|
|
||||||
}
|
|
||||||
(temp+k)->s[127]='\0';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void st_print(struct st1 temp[], int n){
|
|
||||||
int k;
|
|
||||||
for(k=0;k<n;k++){
|
|
||||||
printf("%d->[%2d][%s]\n", k, temp[k].i, (temp+k)->s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
struct st1{
|
|
||||||
int i;
|
|
||||||
char s[128];
|
|
||||||
};
|
|
||||||
|
|
||||||
void st_rand(struct st1 temp[], int n);
|
|
||||||
void st_print(struct st1 temp[], int n);
|
|
||||||
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<stdlib.h>
|
|
||||||
|
|
||||||
struct list {
|
|
||||||
char name[20];
|
|
||||||
int age;
|
|
||||||
float salary;
|
|
||||||
};
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
struct list *person;
|
|
||||||
int i, n = 2;
|
|
||||||
person = malloc(n*sizeof(struct list));
|
|
||||||
|
|
||||||
for(i = 0; i<n; i++){
|
|
||||||
printf("Enter name %d: ", i+1);
|
|
||||||
scanf("%s", &(person+i)->name);
|
|
||||||
|
|
||||||
printf("Enter age %d: ", i+1);
|
|
||||||
scanf("%d", &(person+i)->age);
|
|
||||||
|
|
||||||
printf("Enter salary %d: ", i+1);
|
|
||||||
scanf("%f", &(person+i)->salary);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(i=0; i<n; i++){
|
|
||||||
printf("Name: %s\n Age: %d\n Salary: %.2f\n", (person+i)->name, (person+i)->age, (person+i)->salary);
|
|
||||||
|
|
||||||
}
|
|
||||||
free(person);
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<stdlib.h>
|
|
||||||
#include<time.h>
|
|
||||||
|
|
||||||
void swap(int a[], int c, int d, int n){
|
|
||||||
int temp;
|
|
||||||
if((c>=n) || (d>=n)) return;
|
|
||||||
temp = a[c];
|
|
||||||
a[c] = a[d];
|
|
||||||
a[d] = temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
void pa(int a[], int n){
|
|
||||||
int i;
|
|
||||||
for(i=0;i<n;i++){
|
|
||||||
printf("a[%d] = %2d ", i, a[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void init(int *a, int n){
|
|
||||||
srand(time(NULL));
|
|
||||||
while(n--){
|
|
||||||
*(a++)=rand()%100;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main2(int n){
|
|
||||||
int a[n];
|
|
||||||
|
|
||||||
init(a,n);
|
|
||||||
pa(a,n);
|
|
||||||
swap(a,1,4,n);
|
|
||||||
pa(a,n);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
int alenght;
|
|
||||||
printf("input n = ");
|
|
||||||
scanf("%d", &alenght);
|
|
||||||
return main2(alenght);
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<stdlib.h>
|
|
||||||
#include<time.h>
|
|
||||||
#include<error.h>
|
|
||||||
struct st1{
|
|
||||||
int i;
|
|
||||||
char s[128];
|
|
||||||
};
|
|
||||||
/*
|
|
||||||
void sort_Select(int x, struct st1 ){
|
|
||||||
struct st1 temp;
|
|
||||||
int i,j;
|
|
||||||
for(i=0;i<(x-1);i++){
|
|
||||||
ind = i;
|
|
||||||
for(j=i+1;j<x;j++){
|
|
||||||
if(arr[ind]>arr[j]){
|
|
||||||
ind = j;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
temp = arr[i];
|
|
||||||
arr[i] = arr[ind];
|
|
||||||
arr[ind] = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
void st_rand(struct st1 temp[], int n){
|
|
||||||
int k;
|
|
||||||
int j;
|
|
||||||
|
|
||||||
|
|
||||||
for(k=0; k<n; k++){
|
|
||||||
(temp+k)->i = rand()%100;
|
|
||||||
for(j=0;j<127;j++){
|
|
||||||
(temp+k)->s[j]=((char)(65+rand()%25));
|
|
||||||
}
|
|
||||||
(temp+k)->s[127]='\0';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*void swap(struct st1 temp[], int c, int d, int n){
|
|
||||||
Дописать функцию!
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
void st_print(struct st1 temp[], int n){
|
|
||||||
int k;
|
|
||||||
for(k=0;k<n;k++){
|
|
||||||
printf("%d->[%2d][%s]\n", k, temp[k].i, (temp+k)->s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
int main(int argc, char *argv[]){
|
|
||||||
int alenght;
|
|
||||||
printf("input n = ");
|
|
||||||
scanf("%d", &alenght);
|
|
||||||
struct st1 *pq=NULL;
|
|
||||||
srand(time(NULL));
|
|
||||||
|
|
||||||
if((pq=malloc(alenght*sizeof(struct st1))) == NULL){
|
|
||||||
printf("malloc: NULL\n");
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
st_rand(pq,alenght);
|
|
||||||
st_print(pq,alenght);
|
|
||||||
free(pq);
|
|
||||||
pq = NULL;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user