This commit is contained in:
ada-dmitry
2023-04-02 21:02:59 +03:00
parent 1124209d64
commit c5e31ada93
49 changed files with 0 additions and 0 deletions
Binary file not shown.
+70
View File
@@ -0,0 +1,70 @@
#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;
}
+25
View File
@@ -0,0 +1,25 @@
#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);
+149
View File
@@ -0,0 +1,149 @@
#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);
}
+8
View File
@@ -0,0 +1,8 @@
1 pkaeoew
1 ujqrkpqP
2 nllvevc=
2 wrqqkwf(
1 krxtlgme
1 srgyisre
3 tiflqbd(
3 opukfbci
Binary file not shown.
+32
View File
@@ -0,0 +1,32 @@
#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;
}
@@ -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);
}
// Домашка: сортировка списка и вычисление длины списка(по желанию запись в файл и чтение с файла)
// Ханойские башни игра написать
Binary file not shown.
+50
View File
@@ -0,0 +1,50 @@
#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;
}
+33
View File
@@ -0,0 +1,33 @@
#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;
}
+43
View File
@@ -0,0 +1,43 @@
#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);
}
+65
View File
@@ -0,0 +1,65 @@
#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;
}
+11
View File
@@ -0,0 +1,11 @@
#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;
}
Binary file not shown.
+8
View File
@@ -0,0 +1,8 @@
struct st1{
int i;
char s[128];
};
void st_rand(struct st1 temp[], int n);
void st_print(struct st1 temp[], int n);
+24
View File
@@ -0,0 +1,24 @@
#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);
}
}
+8
View File
@@ -0,0 +1,8 @@
struct st1{
int i;
char s[128];
};
void st_rand(struct st1 temp[], int n);
void st_print(struct st1 temp[], int n);