mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 09:10:14 +00:00
resort
This commit is contained in:
Binary file not shown.
@@ -1,70 +0,0 @@
|
||||
#include "funcHeader.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
srand(time(0));
|
||||
People *professions1 = malloc(sizeof(People) * COUN);
|
||||
People *professions2 = malloc(sizeof(People) * COUN);
|
||||
|
||||
if (professions1 == NULL || professions2 == NULL)
|
||||
{
|
||||
printf("Mesta net\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
st_rand(professions1, COUN);
|
||||
st_rand(professions2, COUN);
|
||||
|
||||
printf("Perviy massiv:\n");
|
||||
print_struc(professions1, COUN);
|
||||
printf("\nVtoroi massiv:\n");
|
||||
print_struc(professions2, COUN);
|
||||
|
||||
sort_sel(professions1, COUN);
|
||||
sort_sel(professions2, COUN);
|
||||
|
||||
printf("\nPerviy massiv posle sort:\n");
|
||||
print_struc(professions1, COUN);
|
||||
printf("\nVtoroi massiv posle sort:\n");
|
||||
print_struc(professions2, COUN);
|
||||
|
||||
People *ans = binSearch(professions1, COUN, 15);
|
||||
|
||||
if (ans == NULL)
|
||||
printf("\nPeoples net\n");
|
||||
else
|
||||
printf("\nLength: %2d; name: %s\n", ans->count, ans->name);
|
||||
|
||||
printf("\nElement in perviy massiv: ");
|
||||
pst(professions1, COUN, (int)(COUN * 0.7));
|
||||
printf("Element in vtoroi massiv: ");
|
||||
pst(professions2, COUN, (int)(COUN * 0.3));
|
||||
|
||||
printf("\nNumber of para : %d\n", find_para(professions1, COUN, professions2, COUN));
|
||||
|
||||
st_fprintf(professions1, COUN, "professions.txt", "w");
|
||||
st_fprintf(professions2, COUN, "professions.txt", "a");
|
||||
|
||||
int line_number = lines_numf("professions.txt");
|
||||
printf("\nNumber of lines in professions.txt is %d\n", line_number);
|
||||
|
||||
People *professions3 = malloc(sizeof(People) * line_number);
|
||||
|
||||
if (professions3 == NULL)
|
||||
{
|
||||
printf("Mesta net\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
st_fscanf(professions3, line_number, "professions.txt");
|
||||
sort_sel(professions3, line_number);
|
||||
|
||||
Sleep(50000);
|
||||
|
||||
printf("\nTTretiy massiv is:\n");
|
||||
print_struc(professions3, line_number);
|
||||
free(professions1);
|
||||
free(professions2);
|
||||
free(professions3);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
#define COUN 4
|
||||
#define LEN 8
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <windows.h>
|
||||
|
||||
typedef struct People
|
||||
{
|
||||
int count;
|
||||
char name[LEN];
|
||||
} People;
|
||||
|
||||
void print_struc(People professions[], int n);
|
||||
void sort_sel(People professions[], int n);
|
||||
void st_rand(People professions[], int n);
|
||||
People *binSearch(People professions[], int n, int k);
|
||||
void pst(People professions[], int s, int n);
|
||||
int find_para(People t1[], int n1, People t2[], int n2);
|
||||
void st_fprintf(People professions[], int n, char path[], char access[]);
|
||||
int lines_numf(char path[]);
|
||||
void st_fscanf(People t[], int n, char path[]);
|
||||
void swap(People *t1, People *t2);
|
||||
@@ -1,149 +0,0 @@
|
||||
#include "funcHeader.h"
|
||||
|
||||
void print_struc(People professions[], int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
printf("count: %3d; name: %s\n", professions[i].count, professions[i].name);
|
||||
}
|
||||
|
||||
void sort_sel(People professions[], int n)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < n - 1; i++)
|
||||
{
|
||||
for (j = i + 1; j < n; j++)
|
||||
{
|
||||
if (professions[j].count < professions[i].count)
|
||||
swap(professions + i, professions + j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void st_rand(People professions[], int n)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
professions[i].count = rand() % 3 + 1;
|
||||
for (j = 0; j < LEN - 1; j++)
|
||||
professions[i].name[j] = 'a' + rand() % 25;
|
||||
professions[i].name[j + 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
People *binSearch(People professions[], int n, int k)
|
||||
{
|
||||
People *ans = NULL;
|
||||
int right = 0, left = n - 1;
|
||||
|
||||
while (right <= left)
|
||||
{
|
||||
int mid = (left + right) / 2;
|
||||
|
||||
if (professions[mid].count == k)
|
||||
{
|
||||
ans = &professions[mid];
|
||||
break;
|
||||
}
|
||||
right = (professions[mid].count < k) ? mid + 1 : right;
|
||||
left = (professions[mid].count > k) ? mid - 1 : left;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
void pst(People professions[], int s, int n)
|
||||
{
|
||||
if (n >= s)
|
||||
{
|
||||
printf("Out of range\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
printf("count: %2d; name: %s\n", professions[n].count, professions[n].name);
|
||||
}
|
||||
|
||||
int find_para(People t1[], int n1, People t2[], int n2)
|
||||
{
|
||||
int ans = 0, i;
|
||||
for (i = 0; i < n1; i++)
|
||||
{
|
||||
People *tans = binSearch(t2, n2, t1[i].count);
|
||||
if (tans == NULL)
|
||||
continue;
|
||||
ans++;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
void st_fprintf(People professions[], 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", professions[i].count, professions[i].name);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
int lines_numf(char path[])
|
||||
{
|
||||
int ans = 0;
|
||||
char c;
|
||||
FILE *f = fopen(path, "r");
|
||||
if (f == NULL)
|
||||
{
|
||||
printf("Faila %s net...\n", path);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
while ((c = fgetc(f)) != EOF)
|
||||
{
|
||||
if (c == '\n')
|
||||
ans++;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
return ans;
|
||||
}
|
||||
|
||||
void st_fscanf(People t[], int n, char path[])
|
||||
{
|
||||
int i;
|
||||
FILE *f = fopen(path, "r");
|
||||
if (f == NULL)
|
||||
{
|
||||
printf("Faila %s net...\n", path);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
fscanf(f, "%d %s\n", &t[i].count, t[i].name);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
void swap(People *t1, People *t2)
|
||||
{
|
||||
People *tmp = malloc(sizeof(People));
|
||||
|
||||
if (tmp == NULL)
|
||||
{
|
||||
printf("could not malloc\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memcpy(tmp, t1, sizeof(People));
|
||||
memcpy(t1, t2, sizeof(People));
|
||||
memcpy(t2, tmp, sizeof(People));
|
||||
free(tmp);
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
1 pkaeoew
|
||||
1 ujqrkpqP
|
||||
2 nllvevc=
|
||||
2 wrqqkwf(
|
||||
1 krxtlgme
|
||||
1 srgyisre
|
||||
3 tiflqbd(
|
||||
3 opukfbci
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,140 +0,0 @@
|
||||
#include "head.h"
|
||||
|
||||
|
||||
void linked_print(LinkList* l) {
|
||||
while (l != NULL) {
|
||||
printf("%d %s\n", l->x, l->name);
|
||||
l = l->next;
|
||||
}
|
||||
}
|
||||
|
||||
LinkList* add_tail(LinkList* l, LinkList node) {
|
||||
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
||||
l->x = node.x;
|
||||
memcpy(l->name, node.name, LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
LinkList* res = l;
|
||||
while (l->next != NULL) l = l->next;
|
||||
|
||||
LinkList* tmp = malloc(sizeof(LinkList));
|
||||
tmp->x = node.x;
|
||||
memcpy(tmp->name, node.name, LEN);
|
||||
tmp->prev = l;
|
||||
l->next = tmp;
|
||||
return res;
|
||||
}
|
||||
|
||||
LinkList* add_head(LinkList* l, LinkList node) {
|
||||
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
||||
l->x = node.x;
|
||||
memcpy(l->name, node.name, LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
LinkList* tmp = malloc(sizeof(LinkList));
|
||||
if (tmp == NULL)
|
||||
exit(1);
|
||||
|
||||
tmp->next = l;
|
||||
l->prev = tmp;
|
||||
tmp->prev = NULL;
|
||||
tmp->x = node.x;
|
||||
memcpy(tmp->name, node.name, LEN);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
LinkList* del_head(LinkList* l) {
|
||||
if (l == NULL) return NULL;
|
||||
if (l->next == NULL) {
|
||||
free(l);
|
||||
return NULL;
|
||||
}
|
||||
LinkList* tmp = l->next;
|
||||
tmp->prev = NULL;
|
||||
free(l);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void del_tail(LinkList* l) {
|
||||
if (l == NULL) return;
|
||||
else if (l->next == NULL) {
|
||||
free(l);
|
||||
l = NULL;
|
||||
return;
|
||||
}
|
||||
while (l->next != NULL) l = l->next;
|
||||
LinkList* tmp = l->prev;
|
||||
l->prev = NULL;
|
||||
free(l);
|
||||
tmp->next = NULL;
|
||||
}
|
||||
|
||||
int len(LinkList* l) {
|
||||
int res = 0;
|
||||
if (l->x == 0 && !strcmp(l->name, "\0"))
|
||||
return 0;
|
||||
|
||||
while (l != NULL) {
|
||||
res++;
|
||||
l = l->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
LinkList get(LinkList* l, int n) {
|
||||
if (n > len(l)) exit(3);
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
l = l->next;
|
||||
}
|
||||
return *l;
|
||||
}
|
||||
|
||||
void tower_of_hanoi(int n, LinkList* start, LinkList* finish, LinkList* tmp) {
|
||||
if (n == 1) {
|
||||
finish = add_head(finish, get(start, 0));
|
||||
start = del_head(start);
|
||||
return;
|
||||
}
|
||||
|
||||
tower_of_hanoi(n - 1, start, tmp, finish);
|
||||
|
||||
finish = add_head(finish, get(start, 0));
|
||||
start = del_head(start);
|
||||
|
||||
tower_of_hanoi(n - 1, tmp, finish, start);
|
||||
|
||||
printf("\nPosition:\nStart:\n");
|
||||
linked_print(start);
|
||||
printf("Finish:\n");
|
||||
linked_print(finish);
|
||||
printf("Tmp:\n");
|
||||
linked_print(tmp);
|
||||
}
|
||||
|
||||
void linked_free(LinkList* l) {
|
||||
while (l->next != NULL) l = l->next;
|
||||
|
||||
while (l->prev != NULL) {
|
||||
LinkList* tmp = l;
|
||||
l = l->prev;
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
#include "head.h"
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
srand(time(0));
|
||||
LinkList* l1 = malloc(sizeof(LinkList));
|
||||
LinkList* l2 = malloc(sizeof(LinkList));
|
||||
LinkList* l3 = malloc(sizeof(LinkList));
|
||||
|
||||
if (l1 == NULL || l2 == NULL || l3 == NULL)
|
||||
exit(1);
|
||||
|
||||
int i, j;
|
||||
char s[LEN];
|
||||
for (i = 0; i < N; i++) {
|
||||
for (j = 0; j < LEN - 1; j++)
|
||||
s[j] = 'a' + rand() % 25;
|
||||
s[j + 1] = '\0';
|
||||
|
||||
LinkList l;
|
||||
l.x = i;
|
||||
memcpy(l.name, s, LEN);
|
||||
l.next = NULL;
|
||||
l.prev = NULL;
|
||||
|
||||
l1 = add_tail(l1, l);
|
||||
}
|
||||
|
||||
tower_of_hanoi(N, l1, l3, l2);
|
||||
|
||||
// printf("Done:\n");
|
||||
|
||||
linked_free(l1);
|
||||
linked_free(l2);
|
||||
linked_free(l3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define N 3
|
||||
#define LEN 8
|
||||
|
||||
typedef struct LinkList {
|
||||
int x;
|
||||
char name[LEN];
|
||||
struct LinkList* next;
|
||||
struct LinkList* prev;
|
||||
} LinkList;
|
||||
|
||||
void linked_print(LinkList* l);
|
||||
LinkList* add_tail(LinkList* l, LinkList node);
|
||||
LinkList* add_head(LinkList* l, LinkList node);
|
||||
LinkList* del_head(LinkList* l);
|
||||
void del_tail(LinkList* l);
|
||||
int len(LinkList* l);
|
||||
LinkList get(LinkList* l, int n);
|
||||
void tower_of_hanoi(int n, LinkList* start, LinkList* finish, LinkList* tmp);
|
||||
void linked_free(LinkList* l);
|
||||
Binary file not shown.
@@ -1,76 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
#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.
@@ -1,17 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
typedef struct node {
|
||||
int x;
|
||||
struct node* next;
|
||||
}Plate;
|
||||
|
||||
void print_plates(Plate* plates);
|
||||
Plate* push(Plate* plates, int x);
|
||||
Plate* remove_plate(Plate* plates);
|
||||
int pop(Plate** plates);
|
||||
int len(Plate* plates);
|
||||
int isnm(char *str);
|
||||
@@ -1,74 +0,0 @@
|
||||
#include "head.h"
|
||||
|
||||
void print_plates(Plate *plates)
|
||||
{
|
||||
if (plates == NULL)
|
||||
{
|
||||
printf("[%p]\n", plates);
|
||||
return;
|
||||
}
|
||||
while(plates){
|
||||
printf("[%14p]->[%14p] ", plates, plates->next);
|
||||
printf("data: [%4d]\n", plates->x);
|
||||
plates = plates->next;
|
||||
}
|
||||
printf("-------------\n");
|
||||
}
|
||||
|
||||
Plate* push(Plate* plates, int d){
|
||||
Plate *temp = NULL;
|
||||
if((temp=malloc(sizeof(Plate)))==NULL){
|
||||
perror("malloc:NULL");
|
||||
exit(2);
|
||||
}
|
||||
temp->x = d;
|
||||
if(plates==NULL) temp->next=NULL;
|
||||
else{
|
||||
temp->next=plates;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
Plate* del_head(Plate* plates){
|
||||
Plate *temp=plates;
|
||||
if(plates==NULL) return NULL;
|
||||
if(plates->next==NULL){
|
||||
free(plates);
|
||||
return NULL;
|
||||
}
|
||||
plates = plates->next;
|
||||
free(temp);
|
||||
return plates;
|
||||
}
|
||||
|
||||
int len(Plate* plates){
|
||||
int ans = 0;
|
||||
Plate *temp = plates;
|
||||
if(plates==NULL) return 0;
|
||||
while(temp){
|
||||
ans++;
|
||||
temp=temp->next;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
int pop(Plate** plates){
|
||||
int i;
|
||||
if(*plates==NULL){
|
||||
printf("Error");
|
||||
exit(4);
|
||||
}
|
||||
i = (*plates)->x;
|
||||
*plates = del_head(*plates);
|
||||
return i;
|
||||
}
|
||||
|
||||
int isnm(char *str){
|
||||
int i = 0, n = strlen(str);
|
||||
for(i=0; i<n; i++){
|
||||
if(!isdigit(str[i])){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
#include "head.h"
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
Plate *plates = NULL;
|
||||
char c;
|
||||
int i,j,k, ans = 0;
|
||||
for (k=1; k < argc; k++){
|
||||
if (isnm(argv[k])){
|
||||
plates = push(plates, atoi(argv[k]));
|
||||
}
|
||||
else if (strlen(argv[k]) == 1 || !strcmp(argv[k], "*m")){
|
||||
if (len(plates)<2){
|
||||
printf("Not enough numbers");
|
||||
exit(2);
|
||||
}
|
||||
if (!strcmp(argv[k], "*m")) c = '*';
|
||||
else c = *argv[k];
|
||||
i = pop(&plates);
|
||||
j = pop(&plates);
|
||||
switch(c){
|
||||
case '+':
|
||||
plates = push(plates, j + i);
|
||||
break;
|
||||
case '-':
|
||||
plates = push(plates, j - i);
|
||||
break;
|
||||
case '*':
|
||||
plates = push(plates, j * i);
|
||||
break;
|
||||
case '/':
|
||||
plates = push(plates, j / i);
|
||||
break;
|
||||
default:
|
||||
printf("You wrote something wrong");
|
||||
exit(3);
|
||||
}
|
||||
}
|
||||
else{
|
||||
printf("You wrote something wrong");
|
||||
exit(7);
|
||||
}
|
||||
}
|
||||
if (len(plates)!=1){
|
||||
printf("Not enough operations");
|
||||
exit(4);
|
||||
}
|
||||
ans = pop(&plates);
|
||||
printf("Result of line is %d\n", ans);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -1,105 +0,0 @@
|
||||
#include "header.h"
|
||||
|
||||
void node_print(Node *n)
|
||||
{
|
||||
while (n != NULL)
|
||||
{
|
||||
printf("%s\n", n->name);
|
||||
n = n->next;
|
||||
}
|
||||
}
|
||||
|
||||
Node *add_tail(Node *n, char s[])
|
||||
{
|
||||
if (n == NULL)
|
||||
{
|
||||
if ((n = malloc(sizeof(Node))) == NULL)
|
||||
{
|
||||
printf("could not malloc\n");
|
||||
exit(1);
|
||||
}
|
||||
n->next = NULL;
|
||||
n->prev = NULL;
|
||||
memcpy(n->name, s, LEN);
|
||||
return n;
|
||||
}
|
||||
if (!strcmp(n->name, "\0"))
|
||||
{
|
||||
memcpy(n->name, s, LEN);
|
||||
n->next = NULL;
|
||||
n->prev = NULL;
|
||||
return n;
|
||||
}
|
||||
Node *res = n;
|
||||
while (n->next != NULL){
|
||||
printf("%p\n", n);
|
||||
n = n->next;
|
||||
}
|
||||
Node *tmp = malloc(sizeof(Node));
|
||||
memcpy(tmp->name, s, LEN);
|
||||
tmp->prev = n;
|
||||
n->next = tmp;
|
||||
return res;
|
||||
}
|
||||
|
||||
Node get(Node *node, int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
node = node->next;
|
||||
}
|
||||
return *node;
|
||||
}
|
||||
|
||||
int len(Node *n)
|
||||
{
|
||||
int res = 0;
|
||||
while (n != NULL)
|
||||
{
|
||||
res++;
|
||||
n = n->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
Node *xor_find(Node *n1, Node *n2)
|
||||
{
|
||||
int i, j, f = 0;
|
||||
Node *res = NULL;
|
||||
if ((res = malloc(sizeof(Node))) == NULL)
|
||||
{
|
||||
printf("could not malloc\n");
|
||||
exit(3);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
f = 1;
|
||||
}
|
||||
}
|
||||
if (f == 0)
|
||||
res = add_tail(res, get(n1, i).name);
|
||||
f = 0;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
f = 1;
|
||||
}
|
||||
}
|
||||
if (f == 0)
|
||||
res = add_tail(res, get(n2, i).name);
|
||||
f = 0;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
#include "header.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
printf("Cto-Nibud\n");
|
||||
srand(time(0));
|
||||
Node *n1 = NULL;
|
||||
Node *n2 = NULL;
|
||||
Node *n3 = NULL;
|
||||
|
||||
if ((n1 = malloc(sizeof(Node))) == NULL || (n2 = malloc(sizeof(Node))) == NULL || (n3 = malloc(sizeof(Node))) == NULL)
|
||||
{
|
||||
printf("could not malloc\n");
|
||||
exit(2);
|
||||
}
|
||||
n1 = add_tail(n1, "bebebe");
|
||||
|
||||
printf("Error here 1\n");
|
||||
int i;
|
||||
for (i = 0; i < COUNT; i++)
|
||||
{
|
||||
char s[LEN];
|
||||
int j;
|
||||
printf("sss\n");
|
||||
for (j = 0; j < LEN - 1; j++)
|
||||
s[j] = 'a' + rand() % 2;
|
||||
s[LEN - 1] = '\0';
|
||||
|
||||
n1 = add_tail(n1, s);
|
||||
printf("11");
|
||||
for (j = 0; j < LEN - 1; j++){
|
||||
s[j] = 'a' + rand() % 2;
|
||||
printf("22");
|
||||
}
|
||||
s[LEN - 1] = '\0';
|
||||
n2 = add_tail(n2, s);
|
||||
}
|
||||
|
||||
printf("Source arrays:\n");
|
||||
node_print(n1);
|
||||
printf("\n");
|
||||
node_print(n2);
|
||||
|
||||
n3 = xor_find(n1, n2);
|
||||
|
||||
printf("\nFinal array:\n");
|
||||
node_print(n3);
|
||||
|
||||
free(n1);
|
||||
free(n2);
|
||||
free(n3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define COUNT 3
|
||||
#define LEN 3
|
||||
|
||||
typedef struct Node
|
||||
{
|
||||
char name[LEN];
|
||||
struct Node *next;
|
||||
struct Node *prev;
|
||||
} Node;
|
||||
|
||||
void node_print(Node *n);
|
||||
Node *add_tail(Node *n, char s[]);
|
||||
Node get(Node *node, int n);
|
||||
Node *xor_find(Node *n1, Node *n2);
|
||||
int len(Node *n);
|
||||
@@ -1,56 +0,0 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct Node {
|
||||
int data;
|
||||
struct Node* next;
|
||||
};
|
||||
|
||||
struct Node* top = NULL; // инициализация вершины стека как пустой
|
||||
|
||||
/* функция для добавления элемента в стек */
|
||||
void push(int data) {
|
||||
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
|
||||
new_node->data = data;
|
||||
new_node->next = top;
|
||||
top = new_node;
|
||||
}
|
||||
|
||||
/* функция для удаления элемента из стека */
|
||||
int pop() {
|
||||
if (top == NULL) { // проверка на пустой стек
|
||||
printf("Stack Underflow!");
|
||||
return -1;
|
||||
}
|
||||
int data = top->data;
|
||||
struct Node* temp = top;
|
||||
top = top->next; // Спросить по поводу отсутствия эл-та
|
||||
free(temp);
|
||||
return data;
|
||||
}
|
||||
|
||||
/* функция для просмотра последнего элемента стека */
|
||||
int peek() {
|
||||
if (top == NULL) { // проверка на пустой стек
|
||||
printf("Stack is Empty!");
|
||||
return -1;
|
||||
}
|
||||
return top->data;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("%d\n", peek());
|
||||
push(1);
|
||||
printf("%d\n", peek());
|
||||
push(2);
|
||||
printf("%d\n", peek());
|
||||
push(3);
|
||||
|
||||
printf("%d\n", pop()); // выведет 3
|
||||
printf("%d\n", peek()); // выведет 2
|
||||
printf("%d", pop()); // выведет 2
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
#include "header.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;
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define N 5
|
||||
#define STR_LEN 8
|
||||
|
||||
typedef struct Node {
|
||||
int x;
|
||||
struct Node* next;
|
||||
struct Node* prev;
|
||||
char name[STR_LEN];
|
||||
} Node;
|
||||
|
||||
void linked_print(Node* l);
|
||||
Node* add_tail(Node* l, int x, char name[]);
|
||||
Node* add_head(Node* l, int x, char name[]);
|
||||
Node* del_head(Node* l);
|
||||
void del_tail(Node* l);
|
||||
int len(Node* l);
|
||||
Node get(Node* l, int n);
|
||||
|
||||
void swap(Node* l1, int i, int j);
|
||||
void select_sort(Node* l);
|
||||
void list_fprintf(Node* l, char path[]);
|
||||
Node* list_fscanf(char path[]);
|
||||
@@ -1,50 +0,0 @@
|
||||
#include "function.c"
|
||||
|
||||
int main() {
|
||||
srand(time(0));
|
||||
Node* l = malloc(sizeof(Node));
|
||||
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");
|
||||
Node* l2 = list_fscanf("test.txt");
|
||||
linked_print(l2);
|
||||
|
||||
free(l);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,4 +0,0 @@
|
||||
2 qjrjqtg
|
||||
7 upivrdd
|
||||
49 bokdbfv
|
||||
74 hlchgxy
|
||||
@@ -1,32 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
/* function to solve the Tower of Hanoi puzzle */
|
||||
void tower_of_hanoi(int n, char from, char to, char aux)
|
||||
{
|
||||
/* base case - when there's only one disk present */
|
||||
if (n == 1)
|
||||
{
|
||||
printf("Move disk 1 from rod %c to rod %c\n", from, to);
|
||||
return;
|
||||
}
|
||||
/* move n-1 disks from the source to auxiliary rod */
|
||||
tower_of_hanoi(n - 1, from, aux, to);
|
||||
/* move the remaining one disk from the source to destination rod */
|
||||
printf("Move disk %d from rod %c to rod %c\n", n, from, to);
|
||||
/* move n-1 disks from the auxiliary to destination rod */
|
||||
tower_of_hanoi(n - 1, aux, to, from);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
/* ask user for the number of disks */
|
||||
printf("Enter the number of disks: ");
|
||||
scanf("%d", &n);
|
||||
/* call the tower_of_hanoi function to solve the puzzle */
|
||||
tower_of_hanoi(n, 'A', 'C', 'B');
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,6 +0,0 @@
|
||||
1 fdxyqwb3
|
||||
1 icotltwo
|
||||
2 ysharmos
|
||||
2 xhlbvpd3
|
||||
3 owvischE
|
||||
3 bmmuvuhM
|
||||
@@ -1,166 +0,0 @@
|
||||
#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);
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
#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[]);
|
||||
@@ -1,32 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user