Long time no see
This commit is contained in:
+30
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
typedef struct{
|
||||
char ch;
|
||||
struct ste{
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
}st;
|
||||
}STR;
|
||||
|
||||
int main(){
|
||||
|
||||
STR m1;
|
||||
putchar('a'); //1
|
||||
m1.ch = 'A';
|
||||
putchar('g'); //2
|
||||
(&m1)->ch = 'B';
|
||||
putchar('z'); //3
|
||||
putchar('z'); //4
|
||||
|
||||
|
||||
STR* m2;
|
||||
putchar('a');//5
|
||||
m2->st.x = 'A';
|
||||
putchar('g'); //6
|
||||
(&(m2)->st)->x = 'B';
|
||||
putchar('z'); //7
|
||||
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
call putchar@PLT #1
|
||||
movb $65, -32(%rbp)
|
||||
call putchar@PLT #2
|
||||
movb $66, -32(%rbp)
|
||||
call putchar@PLT #3
|
||||
call putchar@PLT #4
|
||||
movl $97, %edi
|
||||
call putchar@PLT #5
|
||||
movq -40(%rbp), %rax
|
||||
movl $65, 4(%rax)
|
||||
call putchar@PLT #6
|
||||
movq -40(%rbp), %rax
|
||||
movl $66, 4(%rax)
|
||||
call putchar@PLT #7
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Executable
+1
@@ -0,0 +1 @@
|
||||
gcc -O0 -Wall ../main.c ../searchFunc.c ../sortFunc.c -o run
|
||||
@@ -0,0 +1,38 @@
|
||||
set terminal pngcairo size 800,1000 font "Arial,12"
|
||||
set output 'result.png'
|
||||
set encoding utf8
|
||||
|
||||
set grid xtics ytics ls 1 lc rgb "#bbbbbb" lw 1 dt 2
|
||||
set xlabel "Количество элементов в массиве, тыс" offset 0,-1
|
||||
set ylabel "Время выполнения, тики процессора" offset 1,0
|
||||
|
||||
set xtics rotate by 90 right
|
||||
|
||||
set multiplot layout 2,1
|
||||
|
||||
# График 1: Все алгоритмы (Log Scale)
|
||||
set title "Общая сложность (Log scale)"
|
||||
set logscale y 10
|
||||
set format y "%.0f"
|
||||
set key top left box opaque
|
||||
|
||||
set xrange [250:5000]
|
||||
set xtics 250
|
||||
|
||||
plot 'table1.dat' using ($1 >= 250000 ? $1/1000 : 1/0):2 with lp pt 9 ps 1 lw 2 lc rgb "red" title "Linear", \
|
||||
'table1.dat' using ($1 >= 250000 ? $1/1000 : 1/0):3 with lp pt 7 ps 1 lw 2 lc rgb "blue" title "Binary", \
|
||||
'table1.dat' using ($1 >= 250000 ? $1/1000 : 1/0):4 with lp pt 5 ps 1 lw 2 lc rgb "#00CC00" title "Exponential"
|
||||
|
||||
# График 2: Binary vs Exponential (Linear Scale)
|
||||
unset logscale y
|
||||
set title "Детальное сравнение: Binary vs Exponential"
|
||||
set format y "%.0f"
|
||||
set key top left box opaque
|
||||
|
||||
set xrange [5:100]
|
||||
set xtics 5
|
||||
|
||||
plot 'table1.dat' using ($1 < 250000 ? $1/1000 : 1/0):3 with lp pt 9 ps 1.5 lw 2 lc rgb "red" title "Binary", \
|
||||
'table1.dat' using ($1 < 250000 ? $1/1000 : 1/0):4 with lp pt 7 ps 1.5 lw 2 lc rgb "blue" title "Exponential"
|
||||
|
||||
unset multiplot
|
||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 86 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 93 KiB |
Executable
BIN
Binary file not shown.
@@ -0,0 +1,21 @@
|
||||
# N Lin Bin Exp
|
||||
250000 823880.2400 198.8200 68.2200
|
||||
500000 1570157.3600 216.7800 69.3600
|
||||
750000 2361166.2800 183.3200 58.1800
|
||||
1000000 1909412.9200 157.9600 49.6400
|
||||
1250000 2337384.9600 169.6600 49.5400
|
||||
1500000 3095655.0200 196.0400 58.1400
|
||||
1750000 4426487.6800 181.7800 53.0800
|
||||
2000000 3904134.1400 159.0000 48.6000
|
||||
2250000 4857206.9400 212.6200 57.4800
|
||||
2500000 6241745.4400 179.8400 49.7000
|
||||
2750000 6862063.1400 161.5600 46.0600
|
||||
3000000 5883265.5000 152.4000 42.4000
|
||||
3250000 6590400.2800 151.8000 42.3600
|
||||
3500000 6930893.2400 153.7000 43.0800
|
||||
3750000 7751974.1400 153.4600 45.6400
|
||||
4000000 7903715.5400 146.8000 40.0600
|
||||
4250000 8012506.1800 164.6200 43.1800
|
||||
4500000 10549555.0400 159.1400 44.7000
|
||||
4750000 8672377.1800 148.4200 40.3200
|
||||
5000000 8648922.8600 134.4800 36.4800
|
||||
@@ -0,0 +1,62 @@
|
||||
# N Lin Bin Exp
|
||||
5000 22792.2892 255.8908 261.4744
|
||||
10000 43178.6448 259.4500 362.3964
|
||||
15000 63184.8404 308.3112 488.9792
|
||||
20000 84523.9740 365.7352 360.4612
|
||||
25000 100447.7676 299.9976 323.6196
|
||||
30000 117121.9308 307.2840 332.2964
|
||||
35000 138012.7064 311.4680 344.4208
|
||||
40000 157729.2220 317.0248 370.1704
|
||||
45000 176898.9820 479.0876 459.0616
|
||||
50000 198925.1288 334.7180 374.3556
|
||||
55000 217621.5508 320.3992 354.5284
|
||||
60000 238092.1940 330.9048 363.8292
|
||||
65000 257089.6136 324.4956 378.4876
|
||||
70000 276850.4564 334.7716 396.1016
|
||||
75000 296776.6076 341.7612 384.4184
|
||||
80000 318149.4224 348.9776 393.2672
|
||||
85000 339139.2752 353.1772 403.8052
|
||||
90000 360221.4100 347.5952 390.2360
|
||||
95000 433178.5312 394.7536 435.9560
|
||||
100000 516536.8316 535.5460 623.7092
|
||||
105000 838486.9860 686.8348 885.3348
|
||||
110000 869442.6728 712.3252 814.9304
|
||||
115000 1093581.5848 942.1464 924.4680
|
||||
120000 1188837.8268 938.8536 1109.6240
|
||||
125000 1215486.4860 906.0784 1037.6880
|
||||
130000 1279109.4168 851.2548 935.3868
|
||||
135000 1332500.4964 905.4932 992.1744
|
||||
140000 1392278.7032 1029.9752 1018.3992
|
||||
145000 1437455.5196 1078.2540 1380.1472
|
||||
150000 1474982.9704 986.5660 1141.7676
|
||||
155000 1502449.9740 896.2012 1024.4216
|
||||
160000 1369750.6928 652.8004 763.5956
|
||||
165000 878368.1192 436.8840 501.2496
|
||||
170000 884453.1924 711.9916 804.5552
|
||||
175000 929062.4524 410.8268 468.9312
|
||||
180000 870936.0876 847.9028 912.8372
|
||||
185000 1338851.9352 881.3660 756.7676
|
||||
190000 1145629.2592 619.9500 832.6808
|
||||
195000 1246022.2124 707.1592 722.3872
|
||||
200000 1367785.1312 839.6192 898.2104
|
||||
|
||||
250000 1865680.0492 590.7676 599.5552
|
||||
500000 2918958.7568 577.7688 660.8012
|
||||
750000 4492213.6780 521.1036 563.6296
|
||||
1000000 4202512.0244 552.7112 589.8260
|
||||
1250000 6587014.2380 717.3448 726.9956
|
||||
1500000 6760774.8748 647.8220 659.6692
|
||||
1750000 7515443.8320 665.7284 676.1792
|
||||
2000000 8801047.4736 695.7312 686.2312
|
||||
2250000 9104591.9784 714.4216 745.4064
|
||||
2500000 11373685.2396 742.7092 803.2212
|
||||
2750000 12049295.3936 780.7432 858.6344
|
||||
3000000 13438895.7052 766.2056 806.7704
|
||||
3250000 15012072.8648 825.1600 1000.3176
|
||||
3500000 17754709.9804 811.4068 826.6624
|
||||
3750000 18787348.3096 2002.7008 2118.2368
|
||||
4000000 18870374.2036 1052.3220 1144.2540
|
||||
4250000 16816836.3428 833.3472 1097.5500
|
||||
4500000 17317340.9908 1073.3676 1377.1500
|
||||
4750000 18436313.1016 829.5996 1089.8952
|
||||
5000000 19069453.1036 866.5192 1103.5064
|
||||
@@ -0,0 +1,22 @@
|
||||
# N K Lin Bin Bin+Sort Sort
|
||||
1000 50 124254661.554 74450.162 790412836.162 790338386.00
|
||||
1000 100 212643210.268 99492.594 790437878.594 790338386.00
|
||||
1000 150 321858886.378 121279.782 790459665.782 790338386.00
|
||||
1000 200 372157001.160 190434.848 790528820.848 790338386.00
|
||||
1000 250 442590573.866 191732.842 790530118.842 790338386.00
|
||||
1000 300 983880552.716 654958.818 790993344.818 790338386.00
|
||||
1000 350 1320350672.720 455389.894 790793775.894 790338386.00
|
||||
1000 400 683558396.704 303878.160 790642264.160 790338386.00
|
||||
1000 450 606651427.380 342184.150 790680570.150 790338386.00
|
||||
1000 500 1131750414.154 1029850.578 791368236.578 790338386.00
|
||||
# N K Lin Bin Bin+Sort Sort
|
||||
5000 200 1589592533.108 262529.994 5428089941.994 5427827412.00
|
||||
5000 400 3070542998.388 557485.020 5428384897.020 5427827412.00
|
||||
5000 600 4142011455.540 532127.878 5428359539.878 5427827412.00
|
||||
5000 800 4417290889.972 1007465.758 5428834877.758 5427827412.00
|
||||
5000 1000 5601565339.514 1085845.428 5428913257.428 5427827412.00
|
||||
5000 1200 7541889444.504 1211935.880 5429039347.880 5427827412.00
|
||||
5000 1400 7214405162.354 1241032.638 5429068444.638 5427827412.00
|
||||
5000 1600 7982626036.362 2265340.880 5430092752.880 5427827412.00
|
||||
5000 1800 9294569988.752 1304890.234 5429132302.234 5427827412.00
|
||||
5000 2000 6834711717.418 1426351.070 5429253763.070 5427827412.00
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef HEAD
|
||||
#define HEAD
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
//#include <sys/time.h>
|
||||
|
||||
#define AVG_TIME_LOOPS 5000
|
||||
#define AVG_RAND_MAX 10000000
|
||||
#define MILLION 1000000
|
||||
|
||||
double wtime();
|
||||
unsigned long long rdtsc();
|
||||
int get_rand(int min, int max);
|
||||
void fill_rand(int *arr, int n);
|
||||
void merge_sort(int *arr, int n);
|
||||
|
||||
int linear_Search(int *arr, int n, int key);
|
||||
int exponential_Search(int *arr, int n, int key);
|
||||
int binary_Search(int *arr, int n, int key);
|
||||
|
||||
void run_exp1();
|
||||
void run_exp2();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
#include "head.h"
|
||||
|
||||
int main(int argc,char **argv) {
|
||||
srand(rdtsc());
|
||||
|
||||
if(argc>1){
|
||||
switch(argv[1][0]){
|
||||
case '1':
|
||||
run_exp1();
|
||||
break;
|
||||
case '2':
|
||||
run_exp2();
|
||||
break;
|
||||
case '3':
|
||||
{
|
||||
if(!(argv[1][1]>='a'&&argv[1][1]<='z')) return 0;
|
||||
int key = 1,n = 100;
|
||||
printf("Write key:");
|
||||
scanf("%d",&key);
|
||||
while(getchar()>'\n');
|
||||
printf("Write n:");
|
||||
scanf("%d",&n);
|
||||
while(getchar()>'\n');
|
||||
int *arr = (int*)malloc(sizeof(int) * n);
|
||||
fill_rand(arr, n);
|
||||
arr[get_rand(0,n)] = key;
|
||||
merge_sort(arr, n);
|
||||
for(int i = 0; i<n;i++)printf("%d ",arr[i]);
|
||||
switch(argv[1][1]){
|
||||
case 'l':
|
||||
printf("\nэлемент N%d\n",linear_Search(arr, n, key));
|
||||
break;
|
||||
case 'b':
|
||||
printf("\nэлемент N%d\n",binary_Search(arr, n, key));
|
||||
break;
|
||||
case 'e':
|
||||
merge_sort(arr, n);
|
||||
printf("\nэлемент N%d\n",exponential_Search(arr, n, key));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
free(arr);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
run_exp1();
|
||||
run_exp2();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void run_exp1() {
|
||||
FILE *f = fopen("table1.dat", "w");
|
||||
if (!f) return;
|
||||
|
||||
int start = 5000, end = 5000000, step = 5000;
|
||||
fprintf(f, "# N Lin Bin Exp\n");
|
||||
|
||||
for (int n = start; n <= end; n += step) {
|
||||
if(n==step*40+step){
|
||||
step=250000;
|
||||
n=250000;
|
||||
fprintf(f,"\n");
|
||||
}
|
||||
|
||||
int *arr = (int*)malloc(sizeof(int) * n);
|
||||
fill_rand(arr, n);
|
||||
|
||||
unsigned long long tLinTotal = 0;
|
||||
for (int i = 0; i < AVG_TIME_LOOPS; i++) {
|
||||
int key = get_rand(0, AVG_RAND_MAX);
|
||||
unsigned long long t = rdtsc();
|
||||
linear_Search(arr, n, key);
|
||||
tLinTotal += (rdtsc() - t);
|
||||
}
|
||||
double tLinRes = (double)tLinTotal / AVG_TIME_LOOPS;
|
||||
|
||||
merge_sort(arr, n);
|
||||
|
||||
unsigned long long tBinTotal = 0;
|
||||
for (int i = 0; i < AVG_TIME_LOOPS; i++) {
|
||||
int key = get_rand(0, AVG_RAND_MAX);
|
||||
unsigned long long t = rdtsc();
|
||||
binary_Search(arr, n, key);
|
||||
tBinTotal += (rdtsc() - t);
|
||||
}
|
||||
double tBinRes = (double)tBinTotal / AVG_TIME_LOOPS;
|
||||
|
||||
unsigned long long tExpTotal = 0;
|
||||
for (int i = 0; i < AVG_TIME_LOOPS; i++) {
|
||||
int key = get_rand(0, AVG_RAND_MAX);
|
||||
unsigned long long t = rdtsc();
|
||||
exponential_Search(arr, n, key);
|
||||
tExpTotal += (rdtsc() - t);
|
||||
}
|
||||
double tExpRes = (double)tExpTotal / AVG_TIME_LOOPS;
|
||||
|
||||
fprintf(f, "%d %.4f %.4f %.4f\n", n, tLinRes, tBinRes, tExpRes);
|
||||
printf("Progress Table 1: %d/%d\r", n, end);
|
||||
fflush(stdout);
|
||||
free(arr);
|
||||
}
|
||||
fclose(f);
|
||||
printf("\nTable 1 done.\n");
|
||||
}
|
||||
|
||||
void run_exp2() {
|
||||
FILE *f = fopen("table2.dat", "w");
|
||||
if (!f) return;
|
||||
|
||||
for(int n = MILLION; n <= MILLION*5;n*=5){
|
||||
int *arr = (int*)malloc(sizeof(int) * n);
|
||||
fill_rand(arr,n);
|
||||
fprintf(f, "# N K Lin Bin Bin+Sort Sort \n");
|
||||
|
||||
unsigned long long t = rdtsc();
|
||||
merge_sort(arr,n);
|
||||
double tSortRes = rdtsc() - t;
|
||||
|
||||
int modif = (n == MILLION) ? 1:2;
|
||||
for(int k = 50*modif;k<=500*modif;k+=50*modif){
|
||||
int kmax = (n == MILLION) ? 500 : 1000;
|
||||
|
||||
unsigned long long tLinTotal = 0;
|
||||
for(int i = 0;i<=k;i++){
|
||||
for (int j = 0; j < AVG_TIME_LOOPS; j++) {
|
||||
int key = get_rand(0, AVG_RAND_MAX);
|
||||
unsigned long long t = rdtsc();
|
||||
linear_Search(arr, n, key);
|
||||
tLinTotal += (rdtsc() - t);
|
||||
}
|
||||
}
|
||||
double tLinRes = (double)tLinTotal / AVG_TIME_LOOPS;
|
||||
|
||||
unsigned long long tBinTotal = 0;
|
||||
for(int i = 0;i<=k;i++){
|
||||
for (int j = 0; j < AVG_TIME_LOOPS; j++) {
|
||||
int key = get_rand(0, AVG_RAND_MAX);
|
||||
unsigned long long t = rdtsc();
|
||||
binary_Search(arr, n, key);
|
||||
tBinTotal += (rdtsc() - t);
|
||||
}
|
||||
}
|
||||
double tBinRes = (double)tBinTotal / AVG_TIME_LOOPS;
|
||||
fprintf(f, "%d %d %.3f %.3f %.3f %.2f\n", n/1000, k*modif, tLinRes, tBinRes, tBinRes+tSortRes,tSortRes);
|
||||
|
||||
printf("Progress Table 2: %d/%d\r", k, kmax);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
free(arr);
|
||||
}//CloseFor
|
||||
fclose(f);
|
||||
printf("\nTable 2 done.\n");
|
||||
}
|
||||
|
||||
int get_rand(int min, int max) {
|
||||
return (double)rand() / (RAND_MAX + 1.0) * (max - min) + min;
|
||||
}
|
||||
|
||||
void fill_rand(int *arr, int n) {
|
||||
for (int i = 0; i < n; i++) arr[i] = get_rand(0, AVG_RAND_MAX);
|
||||
}
|
||||
|
||||
unsigned long long rdtsc() {
|
||||
unsigned int lo, hi;
|
||||
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
|
||||
return ((unsigned long long)hi << 32) | lo;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "head.h"
|
||||
|
||||
int linear_Search(int *arr, int n, int key) {
|
||||
for (int i = 0; i < n; i++)
|
||||
if (arr[i] == key) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int exponential_Search(int *arr, int n, int key) {
|
||||
int a = 1;
|
||||
while (a < n && arr[a - 1] < key)
|
||||
a *= 2;
|
||||
|
||||
int low = a / 2;
|
||||
int high = (a < n) ? a : n;
|
||||
|
||||
while (low <= high) {
|
||||
int mid = low + (high - low) / 2;
|
||||
if (arr[mid] == key) return mid;
|
||||
if (arr[mid] < key) low = mid + 1;
|
||||
else high = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int binary_Search(int *arr, int n, int key) {
|
||||
int low = 0;
|
||||
int high = n - 1;
|
||||
while (low <= high) {
|
||||
int mid = low + (high - low) / 2;
|
||||
if (arr[mid] == key) return mid;
|
||||
if (arr[mid] < key) low = mid + 1;
|
||||
else high = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "head.h"
|
||||
|
||||
void merge(int *arr, int *temp, int left, int mid, int right) {
|
||||
int i = left;
|
||||
int j = mid + 1;
|
||||
int k = left;
|
||||
|
||||
while (i <= mid && j <= right) {
|
||||
if (arr[i] <= arr[j]) {
|
||||
temp[k++] = arr[i++];
|
||||
} else {
|
||||
temp[k++] = arr[j++];
|
||||
}
|
||||
}
|
||||
|
||||
while (i <= mid) temp[k++] = arr[i++];
|
||||
while (j <= right) temp[k++] = arr[j++];
|
||||
|
||||
for (i = left; i <= right; i++) {
|
||||
arr[i] = temp[i];
|
||||
}
|
||||
}
|
||||
|
||||
void merge_sort_recursive(int *arr, int *temp, int left, int right) {
|
||||
if (left < right) {
|
||||
int mid = left + (right - left) / 2;
|
||||
merge_sort_recursive(arr, temp, left, mid);
|
||||
merge_sort_recursive(arr, temp, mid + 1, right);
|
||||
merge(arr, temp, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
void merge_sort(int *arr, int n) {
|
||||
int *temp = (int *)malloc(sizeof(int) * n);
|
||||
if (temp != NULL) {
|
||||
merge_sort_recursive(arr, temp, 0, n - 1);
|
||||
free(temp);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,22 @@
|
||||
#ifndef HEAD
|
||||
#define HEAD
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include <time.h>
|
||||
|
||||
#define AVG_TIME_LOOPS 1
|
||||
#define AVG_RAND_MAX 100000
|
||||
#define MILLION 1000000
|
||||
|
||||
void sort_radix(uint32_t* arr,int n);
|
||||
void sort_heap(uint32_t* arr,int n);
|
||||
void sort_bubble(uint32_t* arr,int n);
|
||||
void sort_merge(uint32_t* arr,int left, int right);
|
||||
|
||||
|
||||
void run_exp();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
#include "head.h"
|
||||
#include <time.h>
|
||||
|
||||
double get_time();
|
||||
void fill_rand(uint32_t *arr, int n);
|
||||
|
||||
int main(int argc,char **argv) {
|
||||
srand(get_time());
|
||||
|
||||
if(argc>1){
|
||||
switch(argv[1][0]){
|
||||
case '1': // Exp what sort is faster
|
||||
run_exp();
|
||||
break;
|
||||
case '2': // Test Sorts
|
||||
{
|
||||
int n;
|
||||
if(!(argv[1][1]>='a'&&argv[1][1]<='z')) return 0;
|
||||
printf("Write n:");
|
||||
scanf("%d",&n);
|
||||
while(getchar()>'\n');
|
||||
uint32_t *arr = (uint32_t*)malloc(sizeof(uint32_t) * n);
|
||||
fill_rand(arr, n);
|
||||
for(int i = 0; i<n;i++)printf("%d ",arr[i]);
|
||||
putchar('\n');
|
||||
|
||||
switch(argv[1][1]){
|
||||
case 'r': // Radix
|
||||
sort_radix(arr,n);
|
||||
for(int i = 0; i<n;i++)printf("%d ",arr[i]);
|
||||
break;
|
||||
case 'h': // Heap
|
||||
sort_heap(arr,n);
|
||||
for(int i = 0; i<n;i++)printf("%d ",arr[i]);
|
||||
break;
|
||||
case 'b': //Bubble
|
||||
sort_bubble(arr,n);
|
||||
for(int i = 0; i<n;i++)printf("%d ",arr[i]);
|
||||
break;
|
||||
case 'm': //Merge
|
||||
sort_merge(arr,0,n-1);
|
||||
for(int i = 0; i<n;i++)printf("%d ",arr[i]);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
case '3':
|
||||
{
|
||||
printf("\n N - %d",(18-1)%24+1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
run_exp();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void run_exp(void) {
|
||||
|
||||
FILE *f = fopen("table.dat", "w");
|
||||
if (!f) return;
|
||||
|
||||
fprintf(f, "# N Radix Heap Bubble \n");
|
||||
|
||||
int n = 50000,end=MILLION,step = n;
|
||||
for (;n<end;n+=step){
|
||||
|
||||
uint32_t *arr = (uint32_t*)malloc(sizeof(uint32_t) * n);
|
||||
|
||||
double tRadTotal = 0;
|
||||
for (int i = 0; i < AVG_TIME_LOOPS; i++) {
|
||||
fill_rand(arr, n);
|
||||
double t = get_time();
|
||||
sort_radix(arr,n);
|
||||
tRadTotal += (get_time() - t);
|
||||
}
|
||||
double tRadRes = tRadTotal / AVG_TIME_LOOPS;
|
||||
|
||||
double tHeapTotal = 0;
|
||||
for (int i = 0; i < AVG_TIME_LOOPS; i++) {
|
||||
fill_rand(arr, n);
|
||||
double t = get_time();
|
||||
sort_heap(arr, n);
|
||||
tHeapTotal += (get_time() - t);
|
||||
}
|
||||
double tHeapRes = tHeapTotal / AVG_TIME_LOOPS;
|
||||
|
||||
double tBubblTotal = 0;
|
||||
for (int i = 0; i < AVG_TIME_LOOPS; i++) {
|
||||
fill_rand(arr, n);
|
||||
double t = get_time();
|
||||
sort_bubble(arr,n);
|
||||
tBubblTotal += (get_time() - t);
|
||||
}
|
||||
double tBubblRes = tBubblTotal / AVG_TIME_LOOPS;
|
||||
|
||||
fprintf(f, "%d %f %f %f\n", n, tRadRes, tHeapRes, tBubblRes);
|
||||
free(arr);
|
||||
printf("Sort: %d/%d \r", n, end);
|
||||
fflush(stdout);
|
||||
}
|
||||
fclose(f);
|
||||
printf("\nTable 1 done.\n");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int get_rand(int min, int max) {
|
||||
return (double)rand() / (RAND_MAX + 1.0) * (max - min) + min;
|
||||
}
|
||||
void fill_rand(uint32_t *arr, int n) {
|
||||
for (int i = 0; i < n; i++) arr[i] = get_rand(0, AVG_RAND_MAX);
|
||||
}
|
||||
|
||||
double get_time() {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return ts.tv_sec + ts.tv_nsec * 1e-9;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
#include "head.h"
|
||||
|
||||
//=== === === RADIX SORT === === ===
|
||||
uint32_t get_max(uint32_t* arr,int n);
|
||||
void counting_sort(uint32_t* arr, int n, int exp);
|
||||
void sort_radix(uint32_t* arr,int n){
|
||||
uint32_t max = get_max(arr, n);
|
||||
|
||||
for(uint32_t exp = 1; max / exp > 0; exp *= 10)
|
||||
counting_sort(arr, n, exp);
|
||||
}
|
||||
uint32_t get_max(uint32_t* arr, int n) {
|
||||
uint32_t max = arr[0];
|
||||
for(int i = 1; i < n; i++)
|
||||
if(arr[i] > max)
|
||||
max = arr[i];
|
||||
return max;
|
||||
}
|
||||
void counting_sort(uint32_t* arr, int n, int exp) {
|
||||
uint32_t output[n];
|
||||
uint32_t count[10] = {0};
|
||||
|
||||
for(int i = 0; i < n; i++)
|
||||
count[(arr[i] / exp) % 10]++;
|
||||
|
||||
for(int i = 1; i < 10; i++)
|
||||
count[i] += count[i - 1];
|
||||
|
||||
for(int i = n - 1; i >= 0; i--) {
|
||||
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
|
||||
count[(arr[i] / exp) % 10]--;
|
||||
}
|
||||
|
||||
for(int i = 0; i < n; i++)
|
||||
arr[i] = output[i];
|
||||
}
|
||||
// === === === HEAP SORT === === ===
|
||||
void heapify(uint32_t* arr, int n, int i);
|
||||
void swap(uint32_t* a, uint32_t *b);
|
||||
void sort_heap(uint32_t* arr,int n){
|
||||
for(int i = n/2 - 1; i >= 0; i--)
|
||||
heapify(arr, n, i);
|
||||
|
||||
for(int i = n - 1; i > 0; i--) {
|
||||
swap(&arr[0], &arr[i]);
|
||||
heapify(arr, i, 0);
|
||||
}
|
||||
}
|
||||
void swap(uint32_t* a, uint32_t *b) {
|
||||
int temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
void heapify(uint32_t* arr, int n, int i) {
|
||||
int largest = i;
|
||||
int left = 2 * i + 1;
|
||||
int right = 2 * i + 2;
|
||||
|
||||
if(left < n && arr[left] > arr[largest])
|
||||
largest = left;
|
||||
|
||||
if(right < n && arr[right] > arr[largest])
|
||||
largest = right;
|
||||
|
||||
if(largest != i) {
|
||||
swap(&arr[i], &arr[largest]);
|
||||
|
||||
heapify(arr, n, largest);
|
||||
}
|
||||
}
|
||||
|
||||
// === === === BUBBLE SORT === === ===
|
||||
void sort_bubble(uint32_t* arr,int n){
|
||||
for(int i = 0; i < n - 1; i++) {
|
||||
int swapped = 0;
|
||||
for(int j = 0; j < n - i - 1; j++) {
|
||||
if(arr[j] > arr[j + 1]) {
|
||||
int temp = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = temp;
|
||||
swapped = 1;
|
||||
}
|
||||
}
|
||||
if(!swapped) break;
|
||||
}
|
||||
}
|
||||
|
||||
// === === === MERGE SORT === === ===
|
||||
void merge(uint32_t* arr, int left, int mid, int right);
|
||||
void sort_merge(uint32_t* arr, int left, int right){
|
||||
if(left < right) {
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
sort_merge(arr, left, mid);
|
||||
sort_merge(arr, mid + 1, right);
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
void merge(uint32_t* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int L[n1], R[n2];
|
||||
|
||||
for(int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for(int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
int i = 0, j = 0, k = left;
|
||||
while(i < n1 && j < n2) {
|
||||
if(L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i < n1) { arr[k] = L[i]; i++; k++;}
|
||||
while(j < n2) { arr[k] = R[j]; j++; k++;}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,28 @@
|
||||
CC = gcc
|
||||
|
||||
CFLAGS = -Wall -Wextra -I../ -g
|
||||
TARGET = sort
|
||||
|
||||
SRC_DIR = ../code
|
||||
OBJ_DIR = ../.o
|
||||
|
||||
SRCS = $(wildcard $(SRC_DIR)/*.c)
|
||||
OBJS = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRCS))
|
||||
|
||||
.PHONY: all clean run
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) $(CFLAGS) $(OBJS) -o $(TARGET)
|
||||
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
|
||||
@mkdir -p $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
run: $(TARGET)
|
||||
./$(TARGET) < input > output
|
||||
@cat output
|
||||
|
||||
clean:
|
||||
rm -rf $(OBJ_DIR) $(TARGET) output
|
||||
Executable
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
double wtime();
|
||||
int getrand(int min, int max);
|
||||
void fill_rand(int *arr, int n);
|
||||
|
||||
int main(void){
|
||||
int sn = 50000;
|
||||
int en = 1000000;
|
||||
int step = 50000;
|
||||
srand(123);
|
||||
|
||||
FILE *f = fopen("res.dat", "w");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
double wtime() {
|
||||
struct timeval t;
|
||||
gettimeofday(&t, NULL);
|
||||
return (double)t.tv_sec + (double)t.tv_usec * 1E-6;
|
||||
}
|
||||
|
||||
int getrand(int min, int max) {
|
||||
return (double)rand() / (RAND_MAX + 1.0) * (max - min) + min;
|
||||
}
|
||||
|
||||
void fill_rand(int *arr, int n) {
|
||||
for (int i = 0; i < n; i++)
|
||||
arr[i] = getrand(1, 1000001);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,48 @@
|
||||
#ifndef Head
|
||||
#define Head
|
||||
|
||||
// INCLUDE
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
// DEFINE
|
||||
#define LLI long long int
|
||||
#define MAX_MATRIX_ALIVE 21
|
||||
#define BUFF_MAX 10 //(char) <= 254
|
||||
#define IN_BUFF_CHARS 10 //(char) <= 254
|
||||
|
||||
//TypeDef
|
||||
// matrix.c
|
||||
typedef struct {
|
||||
int rows;
|
||||
int cols;
|
||||
int** data;
|
||||
} MATRIX2D;
|
||||
|
||||
|
||||
// FUNC
|
||||
// === io.c ===
|
||||
MATRIX2D* create_matrix2d(int rows, int cols);
|
||||
char buff_input(char *inbuff, char *_buff);
|
||||
char cmd_buff(char* _buff,char** _charCommands,char NUM_OF_CHAR_COMMANDS);
|
||||
char get_matrix_num(void);
|
||||
int get_num(void);
|
||||
|
||||
// === matrix.c ===
|
||||
void create_matrix(MATRIX2D** addr, unsigned char n, int rows, int cols);
|
||||
void input_matrix(MATRIX2D** addr, unsigned char n);
|
||||
void get_matrix(MATRIX2D** addr, unsigned char n);
|
||||
void logic_matrix(MATRIX2D** addr, unsigned char n1, unsigned char n2);
|
||||
void edit_matrix(MATRIX2D** addr, unsigned char n);
|
||||
void matrix_random(MATRIX2D** addr, unsigned char n);
|
||||
void copy_matrix(MATRIX2D** addr, unsigned char n1, unsigned char n2);
|
||||
void free_matrix(MATRIX2D** addr, unsigned char n);
|
||||
void get_matrix_col(MATRIX2D** addr, unsigned char n, int c);
|
||||
void get_matrix_row(MATRIX2D** addr, unsigned char n, int r);
|
||||
void transp_matrix(MATRIX2D** addr, unsigned char n);
|
||||
LLI determ_matrix(MATRIX2D** addr, unsigned char n);
|
||||
void obr_matrix(MATRIX2D** addr, unsigned char n);
|
||||
void is_Matrix_Exist(MATRIX2D** addr, unsigned char n);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,108 @@
|
||||
#include "head.h"
|
||||
|
||||
char buff_input(char *inbuff, char *_buff){
|
||||
|
||||
*inbuff = 0;
|
||||
{ // INPUT
|
||||
char ch;
|
||||
while((ch=getchar())>' '&&*inbuff<BUFF_MAX){
|
||||
*(_buff+*inbuff)=(int)ch;
|
||||
(*inbuff)++;
|
||||
}
|
||||
if(ch<= 4)return -1;
|
||||
}
|
||||
|
||||
|
||||
if (_buff[0]>='a' && _buff[0] <= 'z')
|
||||
{ // Is input - command?
|
||||
const char NUM_OF_CHAR_COMMANDS = 15;
|
||||
char * _charCommands[NUM_OF_CHAR_COMMANDS]; // _charCommands[*]["char"]
|
||||
_charCommands[0] = (char[]){"input"};
|
||||
_charCommands[1] = (char[]){"getmatrix"};
|
||||
_charCommands[2] = (char[]){"logic"};
|
||||
_charCommands[3] = (char[]){"edit"};
|
||||
_charCommands[4] = (char[]){"random"};
|
||||
_charCommands[5] = (char[]){"copy"};
|
||||
_charCommands[6] = (char[]){"destruct"};
|
||||
_charCommands[7] = (char[]){"getcolum"};
|
||||
_charCommands[8] = (char[]){"getrow"};
|
||||
_charCommands[9] = (char[]){"transp"};
|
||||
_charCommands[10] = (char[]){"determ"};
|
||||
_charCommands[11] = (char[]){"obr"};
|
||||
_charCommands[12] = (char[]){"create"};
|
||||
_charCommands[13] = (char[]){"n"};
|
||||
_charCommands[14] = (char[]){"exit"};
|
||||
|
||||
|
||||
char cmd = cmd_buff(_buff,_charCommands,NUM_OF_CHAR_COMMANDS);
|
||||
if(cmd==15)return -1; //exit
|
||||
return cmd; //Вернуть комману
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char cmd_buff(char* _buff,char** _charCommands,char NUM_OF_CHAR_COMMANDS){
|
||||
for (int i = 0;i<NUM_OF_CHAR_COMMANDS;i++){
|
||||
char flag = 1;
|
||||
for(int j = 0;*(_charCommands[i]+j)!='\0';j++){
|
||||
if (_buff[j]!=*(_charCommands[i]+j)){
|
||||
flag = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(flag) return i+1;//Вернуть комманду
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char get_matrix_num(void){
|
||||
char inbuff;
|
||||
char _buff[BUFF_MAX];
|
||||
while(buff_input(&inbuff,_buff)>0&&*_buff>' ');
|
||||
|
||||
long int num = 0;
|
||||
char flagIsDec = 1;
|
||||
{ // GetNum
|
||||
int i = 0;
|
||||
if(_buff[0]=='-'){i++;flagIsDec = -1;}
|
||||
|
||||
for(; i<inbuff;i++)
|
||||
if(_buff[i]>= '0' && _buff[i] <= '9')
|
||||
num = (num*10) + (_buff[i]-'0');
|
||||
num *= flagIsDec;
|
||||
}
|
||||
if (num>MAX_MATRIX_ALIVE)num = MAX_MATRIX_ALIVE;
|
||||
if (num<0)num = 0;
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
int get_num(void){
|
||||
char inbuff;
|
||||
char _buff[BUFF_MAX];
|
||||
while(buff_input(&inbuff,_buff)>0&&*_buff>' ');
|
||||
|
||||
long int num = 0;
|
||||
char flagIsDec = 1;
|
||||
{ // GetNum
|
||||
int i = 0;
|
||||
if(_buff[0]=='-'){i++;flagIsDec = -1;}
|
||||
|
||||
for(; i<inbuff;i++)
|
||||
if(_buff[i]>= '0' && _buff[i] <= '9')
|
||||
num = (num*10) + (_buff[i]-'0');
|
||||
num *= flagIsDec;
|
||||
}
|
||||
if (num>INT_MAX)num = INT_MAX;
|
||||
if (num<INT_MIN)num = INT_MIN;
|
||||
return (int)num;
|
||||
}
|
||||
/*
|
||||
void outputInt(int n){
|
||||
|
||||
}
|
||||
|
||||
void outputStr(char* str){
|
||||
|
||||
}*/ // Подрузамевалось подобный io но он это нецелесообразная трата времени
|
||||
@@ -0,0 +1,118 @@
|
||||
#include "head.h"
|
||||
|
||||
int main(void){
|
||||
|
||||
MATRIX2D** __matrixAddr = (MATRIX2D**)calloc(MAX_MATRIX_ALIVE,sizeof(MATRIX2D*));
|
||||
if(__matrixAddr == NULL) {printf("ERROR.__matrixAddr NotExist");return 1;}
|
||||
|
||||
char mode;
|
||||
do{
|
||||
char inbuff;
|
||||
char _buff[BUFF_MAX];
|
||||
char n1,n2;
|
||||
int num1,num2;
|
||||
mode = buff_input(&inbuff,_buff);
|
||||
{
|
||||
switch(mode){
|
||||
case 1:
|
||||
n1 = get_matrix_num();
|
||||
is_Matrix_Exist(__matrixAddr,n1);
|
||||
input_matrix(__matrixAddr,n1);
|
||||
break;
|
||||
|
||||
case 2: // output
|
||||
n1 = get_matrix_num();
|
||||
is_Matrix_Exist(__matrixAddr,n1);
|
||||
get_matrix(__matrixAddr,n1);
|
||||
|
||||
break;
|
||||
|
||||
case 3: // logic
|
||||
n1 = get_matrix_num();
|
||||
n2 = get_matrix_num();
|
||||
is_Matrix_Exist(__matrixAddr,n1);
|
||||
is_Matrix_Exist(__matrixAddr,n2);
|
||||
logic_matrix(__matrixAddr,n1,n2);
|
||||
break;
|
||||
|
||||
case 4: // edit
|
||||
n1 = get_matrix_num();
|
||||
is_Matrix_Exist(__matrixAddr,n1);
|
||||
edit_matrix(__matrixAddr,n1);
|
||||
break;
|
||||
|
||||
case 5: // random
|
||||
n1 = get_matrix_num();
|
||||
is_Matrix_Exist(__matrixAddr,n1);
|
||||
matrix_random(__matrixAddr,n1);
|
||||
break;
|
||||
|
||||
case 6: // copy
|
||||
n1 = get_matrix_num();
|
||||
n2 = get_matrix_num();
|
||||
is_Matrix_Exist(__matrixAddr,n1);
|
||||
free_matrix(__matrixAddr,n2);
|
||||
copy_matrix(__matrixAddr,n1,n2);
|
||||
break;
|
||||
|
||||
case 7: // destroy
|
||||
n1 = get_matrix_num();
|
||||
free_matrix(__matrixAddr,n1);
|
||||
break;
|
||||
|
||||
case 8: // get colum
|
||||
n1 = get_matrix_num();
|
||||
num1 = get_num();
|
||||
is_Matrix_Exist(__matrixAddr,n1);
|
||||
get_matrix_col(__matrixAddr,n1,num1);
|
||||
break;
|
||||
|
||||
case 9: // get row
|
||||
n1 = get_matrix_num();
|
||||
num1 = get_num();
|
||||
is_Matrix_Exist(__matrixAddr,n1);
|
||||
get_matrix_row(__matrixAddr,n1,num1);
|
||||
break;
|
||||
|
||||
case 10: // transp
|
||||
n1 = get_matrix_num();
|
||||
is_Matrix_Exist(__matrixAddr,n1);
|
||||
transp_matrix(__matrixAddr,n1);
|
||||
break;
|
||||
|
||||
case 11: // determ
|
||||
n1 = get_matrix_num();
|
||||
is_Matrix_Exist(__matrixAddr,n1);
|
||||
printf("%lld",determ_matrix(__matrixAddr,n1));
|
||||
|
||||
break;
|
||||
|
||||
case 12: //obr
|
||||
n1 = get_matrix_num();
|
||||
is_Matrix_Exist(__matrixAddr,n1);
|
||||
obr_matrix(__matrixAddr,n1);
|
||||
break;
|
||||
|
||||
case 13:
|
||||
n1 = get_matrix_num();
|
||||
num1 = get_num();
|
||||
num2 = get_num();
|
||||
free_matrix(__matrixAddr,n1);
|
||||
create_matrix(__matrixAddr,n1,num1,num2);
|
||||
break;
|
||||
|
||||
case 15:
|
||||
putchar('\n');
|
||||
putchar('\n');
|
||||
break;
|
||||
|
||||
case 0: // wrong input
|
||||
break;
|
||||
|
||||
case -1: return 0;
|
||||
}
|
||||
}
|
||||
}while(mode>=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,374 @@
|
||||
#include "head.h"
|
||||
|
||||
MATRIX2D* create_matrix2d(int rows,int cols) {
|
||||
MATRIX2D* m = (MATRIX2D*)malloc(sizeof(MATRIX2D));
|
||||
m->rows = rows;
|
||||
m->cols = cols;
|
||||
|
||||
m->data = (int**)malloc(rows * sizeof(int*));
|
||||
|
||||
for (int i = 0; i < rows; i++){
|
||||
m->data[i] = (int*)malloc(cols * sizeof(int));
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void create_matrix(MATRIX2D** __addr,unsigned char n,int rows,int cols){
|
||||
__addr[n] = create_matrix2d(rows,cols);
|
||||
for (int i = 0;i<rows;i++){
|
||||
for(int j = 0;j<cols;j++){
|
||||
__addr[n]->data[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void input_matrix(MATRIX2D** __addr,unsigned char n){
|
||||
int** data = __addr[n]->data;
|
||||
|
||||
for(int rows = 0; rows < __addr[n]->rows;rows++){
|
||||
for(int cols = 0; cols < __addr[n]->cols;cols++){
|
||||
int num = get_num();
|
||||
data[rows][cols] = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void get_matrix(MATRIX2D** __addr,unsigned char n){
|
||||
int rows = __addr[n]->rows;
|
||||
int cols = __addr[n]->cols;
|
||||
|
||||
//printf("\tМассив №%d",n);
|
||||
//printf("Колл-во строк %d\nКолл-во колон %d\n\n",rows,cols);
|
||||
for(int r = 0;r<rows;r++){
|
||||
for(int c = 0;c<cols;c++){
|
||||
printf("%d ",__addr[n]->data[r][c]);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
void logic_matrix(MATRIX2D** __addr,unsigned char n1,unsigned char n2){
|
||||
char inbuff;
|
||||
char _buff[BUFF_MAX];
|
||||
int** data1 = __addr[n1]->data;
|
||||
int** data2 = __addr[n2]->data;
|
||||
|
||||
char cmd = 0;
|
||||
{
|
||||
buff_input(&inbuff,_buff);
|
||||
|
||||
const char NUM_OF_CHAR_COMMANDS = 6;
|
||||
char * _charCommands[NUM_OF_CHAR_COMMANDS]; // _charCommands[*]["char"]
|
||||
_charCommands[0] = (char[]){"=="};
|
||||
_charCommands[1] = (char[]){"!="};
|
||||
_charCommands[2] = (char[]){">="};
|
||||
_charCommands[3] = (char[]){"<="};
|
||||
_charCommands[4] = (char[]){">"};
|
||||
_charCommands[5] = (char[]){"<"};
|
||||
|
||||
cmd = cmd_buff(_buff,_charCommands,NUM_OF_CHAR_COMMANDS);
|
||||
}
|
||||
|
||||
LLI cost1 = 0;
|
||||
{
|
||||
for(int r = 0;r<__addr[n1]->rows;r++)
|
||||
for(int c = 0;c<__addr[n1]->cols;c++)
|
||||
cost1 += data1[r][c];
|
||||
}
|
||||
LLI cost2 = 0;
|
||||
{
|
||||
for(int r = 0;r<__addr[n2]->rows;r++)
|
||||
for(int c = 0;c<__addr[n2]->cols;c++)
|
||||
cost2 += data2[r][c];
|
||||
}
|
||||
|
||||
switch(cmd){
|
||||
case 0: //Null
|
||||
putchar('-');putchar('1');
|
||||
break;
|
||||
case 1: // ==
|
||||
putchar(cost1==cost2?'1':'0');
|
||||
break;
|
||||
case 2: // !=
|
||||
putchar(cost1!=cost2?'1':'0');
|
||||
break;
|
||||
case 3: //>=
|
||||
putchar(cost1>=cost2?'1':'0');
|
||||
break;
|
||||
case 4: //<=
|
||||
putchar(cost1<=cost2?'1':'0');
|
||||
break;
|
||||
case 5: // >
|
||||
putchar(cost1>cost2?'1':'0');
|
||||
break;
|
||||
case 6: // <
|
||||
putchar(cost1<cost2?'1':'0');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void edit_matrix(MATRIX2D** __addr, unsigned char n) {
|
||||
char inbuff;
|
||||
char _buff[BUFF_MAX];
|
||||
int** data = __addr[n]->data;
|
||||
int r = 0, c = 0;
|
||||
char mode = 0;
|
||||
|
||||
char* _locStrings[3];
|
||||
_locStrings[0] = (char[]){"point"};
|
||||
_locStrings[1] = (char[]){"add"};
|
||||
_locStrings[2] = (char[]){"sub"};
|
||||
|
||||
while (r < __addr[n]->rows) {
|
||||
char res = buff_input(&inbuff, _buff);
|
||||
if (res == -1) break;
|
||||
|
||||
if (_buff[0] == 'n' && inbuff == 1) break;
|
||||
|
||||
char loc_cmd = cmd_buff(_buff, _locStrings, 3);
|
||||
|
||||
if (loc_cmd == 1) {
|
||||
buff_input(&inbuff, _buff);
|
||||
long pr = 0, pc = 0;
|
||||
int i = 0;
|
||||
for (; i < inbuff && (_buff[i] != ':' && _buff[i] != ',' && _buff[i] != '.'); i++)
|
||||
if (_buff[i] >= '0' && _buff[i] <= '9') pr = pr * 10 + (_buff[i] - '0');
|
||||
for (i++; i < inbuff; i++)
|
||||
if (_buff[i] >= '0' && _buff[i] <= '9') pc = pc * 10 + (_buff[i] - '0');
|
||||
|
||||
if (pr < __addr[n]->rows && pc < __addr[n]->cols) {
|
||||
r = (int)pr;
|
||||
c = (int)pc;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (loc_cmd == 2) { mode = 1; continue; }
|
||||
if (loc_cmd == 3) { mode = 2; continue; }
|
||||
|
||||
long val = 0;
|
||||
int i = 0, sign = 1;
|
||||
if (inbuff > 0 && _buff[0] == '-') { sign = -1; i++; }
|
||||
for (; i < inbuff; i++) {
|
||||
if (_buff[i] >= '0' && _buff[i] <= '9')
|
||||
val = val * 10 + (_buff[i] - '0');
|
||||
}
|
||||
val *= sign;
|
||||
|
||||
if (mode == 1) {
|
||||
long res_val = (long)data[r][c] + val;
|
||||
data[r][c] = (res_val > INT_MAX) ? INT_MAX : (res_val < INT_MIN) ? INT_MIN : (int)res_val;
|
||||
} else if (mode == 2) {
|
||||
long res_val = (long)data[r][c] - val;
|
||||
data[r][c] = (res_val > INT_MAX) ? INT_MAX : (res_val < INT_MIN) ? INT_MIN : (int)res_val;
|
||||
} else {
|
||||
data[r][c] = (val > INT_MAX) ? INT_MAX : (val < INT_MIN) ? INT_MIN : (int)val;
|
||||
}
|
||||
|
||||
if (++c >= __addr[n]->cols) {
|
||||
c = 0;
|
||||
r++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int get_rand(int min, int max) {
|
||||
return (double)rand() / (RAND_MAX + 1.0) * (max - min) + min;
|
||||
}
|
||||
|
||||
void matrix_random(MATRIX2D** __addr,unsigned char n){
|
||||
unsigned int lo, hi;
|
||||
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
|
||||
srand(((unsigned long long)hi << 32) | lo);
|
||||
|
||||
for (int r = 0; r < __addr[n]->rows; r++){
|
||||
for(int c = 0; c < __addr[n]->cols; c++){
|
||||
__addr[n]->data[r][c] = get_rand(-100, 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void copy_matrix(MATRIX2D** __addr,unsigned char n1, unsigned char n2){
|
||||
// n2 = n1
|
||||
__addr[n2] = create_matrix2d(__addr[n1]->rows,__addr[n1]->cols);
|
||||
|
||||
for(int i = 0; i<__addr[n1]->rows;i++){
|
||||
for(int j = 0; j<__addr[n1]->cols;j++){
|
||||
__addr[n2]->data[i][j] = __addr[n1]->data[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void free_matrix(MATRIX2D** __addr,unsigned char n){
|
||||
if (__addr[n] == NULL) return;
|
||||
|
||||
MATRIX2D* m = __addr[n];
|
||||
for (int i = 0; i < m->rows; i++) {
|
||||
free(m->data[i]);
|
||||
}
|
||||
free(m->data);
|
||||
free(m);
|
||||
|
||||
__addr[n] = NULL;
|
||||
}
|
||||
|
||||
void get_matrix_col(MATRIX2D** __addr,unsigned char n,int c){
|
||||
//printf("colum %d - ",c);
|
||||
if(c >= __addr[n]->cols)c = __addr[n]->cols - 1;
|
||||
if(c<0)c = 0;
|
||||
for(int i = 0;i<__addr[n]->rows;i++){
|
||||
printf("%d ",__addr[n]->data[i][c]);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
void get_matrix_row(MATRIX2D** __addr,unsigned char n,int r){
|
||||
//printf("row %d - ",r);
|
||||
if(r >= __addr[n]->rows)r = __addr[n]->rows - 1;
|
||||
if(r<0)r = 0;
|
||||
for(int i = 0;i<__addr[n]->cols;i++){
|
||||
printf("%d ",__addr[n]->data[r][i]);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
void transp_matrix(MATRIX2D** __addr,unsigned char n){
|
||||
MATRIX2D* tr = create_matrix2d(
|
||||
__addr[n]->cols,__addr[n]->rows);
|
||||
|
||||
|
||||
int** dataDef = __addr[n]->data;
|
||||
int** dataRes = tr->data;
|
||||
|
||||
for(int r = 0;r<__addr[n]->rows;r++)
|
||||
for(int c = 0;c<__addr[n]->cols;c++)
|
||||
dataRes[c][r] = dataDef[r][c];
|
||||
|
||||
free_matrix(__addr,n);
|
||||
|
||||
__addr[n] = tr;
|
||||
}
|
||||
|
||||
LLI determ_matrix(MATRIX2D** __addr, unsigned char n) {
|
||||
int size = __addr[n]->rows;
|
||||
int** data = __addr[n]->data;
|
||||
|
||||
if (size == 1) return data[0][0];
|
||||
if (size == 2) return (LLI)data[0][0] * data[1][1] - (LLI)data[0][1] * data[1][0];
|
||||
|
||||
double det = 1.0;
|
||||
double** temp = (double**)malloc(size * sizeof(double*));
|
||||
for (int i = 0; i < size; i++) {
|
||||
temp[i] = (double*)malloc(size * sizeof(double));
|
||||
for (int j = 0; j < size; j++) temp[i][j] = (double)data[i][j];
|
||||
}
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
int pivot = i;
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
double val1 = temp[j][i] < 0 ? -temp[j][i] : temp[j][i];
|
||||
double val2 = temp[pivot][i] < 0 ? -temp[pivot][i] : temp[pivot][i];
|
||||
if (val1 > val2) pivot = j;
|
||||
}
|
||||
|
||||
if (pivot != i) {
|
||||
double* swap = temp[i];
|
||||
temp[i] = temp[pivot];
|
||||
temp[pivot] = swap;
|
||||
det *= -1;
|
||||
}
|
||||
|
||||
double current_val = temp[i][i] < 0 ? -temp[i][i] : temp[i][i];
|
||||
if (current_val < 1e-9) {
|
||||
for (int k = 0; k < size; k++) free(temp[k]);
|
||||
free(temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
det *= temp[i][i];
|
||||
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
double factor = temp[j][i] / temp[i][i];
|
||||
for (int k = i + 1; k < size; k++) {
|
||||
temp[j][k] -= factor * temp[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LLI final_det;
|
||||
if (det >= 0) final_det = (LLI)(det + 0.5);
|
||||
else final_det = (LLI)(det - 0.5);
|
||||
|
||||
for (int i = 0; i < size; i++) free(temp[i]);
|
||||
free(temp);
|
||||
|
||||
return final_det;
|
||||
}
|
||||
|
||||
void obr_matrix(MATRIX2D** __addr, unsigned char n) {
|
||||
int size = __addr[n]->rows;
|
||||
if (size != __addr[n]->cols) return;
|
||||
|
||||
LLI d = determ_matrix(__addr, n);
|
||||
if (d == 0) return;
|
||||
|
||||
double** temp = (double**)malloc(size * sizeof(double*));
|
||||
for (int i = 0; i < size; i++) {
|
||||
temp[i] = (double*)malloc(2 * size * sizeof(double));
|
||||
for (int j = 0; j < size; j++) {
|
||||
temp[i][j] = (double)__addr[n]->data[i][j];
|
||||
temp[i][j + size] = (i == j ? 1.0 : 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
int pivot = i;
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
double v1 = temp[j][i] < 0 ? -temp[j][i] : temp[j][i];
|
||||
double v2 = temp[pivot][i] < 0 ? -temp[pivot][i] : temp[pivot][i];
|
||||
if (v1 > v2) pivot = j;
|
||||
}
|
||||
|
||||
double* row_ptr = temp[i];
|
||||
temp[i] = temp[pivot];
|
||||
temp[pivot] = row_ptr;
|
||||
|
||||
double divisor = temp[i][i];
|
||||
for (int j = i; j < 2 * size; j++) {
|
||||
temp[i][j] /= divisor;
|
||||
}
|
||||
|
||||
for (int k = 0; k < size; k++) {
|
||||
if (k != i) {
|
||||
double factor = temp[k][i];
|
||||
for (int j = i; j < 2 * size; j++) {
|
||||
temp[k][j] -= factor * temp[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
double val = temp[i][j + size];
|
||||
if (val >= 0) __addr[n]->data[i][j] = (int)(val + 0.5);
|
||||
else __addr[n]->data[i][j] = (int)(val - 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < size; i++) free(temp[i]);
|
||||
free(temp);
|
||||
}
|
||||
|
||||
void is_Matrix_Exist(MATRIX2D** __addr,unsigned char n){
|
||||
if(n > MAX_MATRIX_ALIVE) n = MAX_MATRIX_ALIVE-1;
|
||||
if(__addr[n] == NULL){
|
||||
//printf("Матрица №%d не существует, создана новая матрица 4x4\n",n);
|
||||
__addr[n] = create_matrix2d(4,4);
|
||||
matrix_random(__addr,n);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
## Matrix
|
||||
|
||||
Когда вы запускаете програму она будет ожидать одну из следующих коммнад.
|
||||
Ввод чисел будет проигнорирован но, адм символы приведут к завершению программы.
|
||||
|
||||
При вводе символов весь ввод является строкой.
|
||||
Делителем строк являются - Space Enter EOT
|
||||
Также каждый 10 символ - новая строка
|
||||
При вводе, каждое запрашиваемое значение должно заканчиваться делителем
|
||||
|
||||
###### Пример:
|
||||
|
||||
create
|
||||
0
|
||||
10 20
|
||||
getmatrix
|
||||
exit
|
||||
|
||||
Номер матрицы ограничен их возможным колличеством установленным в header.h
|
||||
По умолчанию = 20
|
||||
|
||||
Если вы пытаетесь манипулировать несуществующей матрицей
|
||||
То будет создана матрица 4 X 4 со случайными значениями от -100 до 100
|
||||
|
||||
##
|
||||
|
||||
Далее будет описыватся только ожидаеммый ввод после команды.
|
||||
Функцианал коммнады понятен из названия.
|
||||
|
||||
#### create
|
||||
|
||||
(Ожидается 3 числа)
|
||||
номер-матрицы
|
||||
коллво-строк
|
||||
коллво-колон
|
||||
|
||||
#### input
|
||||
|
||||
(Ожидается 1 число)
|
||||
номер-матрицы
|
||||
(далее будет ввод матрицы X на Y необходимо ввести X\*Y значений)
|
||||
int1 int2 int3 ... int(x\*y)
|
||||
|
||||
#### getmatrix
|
||||
|
||||
(Ожидается 1 число)
|
||||
номер-матрицы
|
||||
|
||||
#### logic
|
||||
|
||||
(Ожидается 2 числа)
|
||||
номер-матрицы1
|
||||
номер-матрицы2
|
||||
(Ожидается логическая операция > < >= <= != == )
|
||||
Логическа-операция
|
||||
|
||||
#### edit
|
||||
|
||||
(Ожидается 1 число)
|
||||
номер-матрицы
|
||||
(Далее принимает X\*Y чисел и заменяет предыдущие)
|
||||
(Так же может принять 3 буквенных выражения,
|
||||
add - все последующие введёные числа будут добавлены к имеющимся
|
||||
sub - от всех имеющихся чисел будут отняты последующие введёные
|
||||
point - указать на точку в массиве начиная с которой продолжить заполнение, row:col для разделения могут использоваться , . :)
|
||||
|
||||
|
||||
#### random
|
||||
|
||||
(Ожидается 1 число)
|
||||
номер-матрицы
|
||||
|
||||
#### copy
|
||||
|
||||
(Ожидается 2 числа)
|
||||
номер-матрицы-откуда-копировать
|
||||
номер-матрицы-куда-копировать
|
||||
|
||||
#### destruct
|
||||
|
||||
(Ожидается 1 число)
|
||||
номер-матрицы
|
||||
|
||||
#### getcolum
|
||||
|
||||
(Ожидается 2 числа)
|
||||
номер-матрицы
|
||||
номер-столбца
|
||||
|
||||
#### getrow
|
||||
|
||||
(Ожидается 2 числа)
|
||||
номер-матрицы
|
||||
номер-строки
|
||||
|
||||
#### transp
|
||||
|
||||
(Ожидается 1 число)
|
||||
номер-матрицы
|
||||
|
||||
#### determ
|
||||
|
||||
(Ожидается 1 число)
|
||||
номер-матрицы
|
||||
|
||||
#### obr
|
||||
|
||||
(Ожидается 1 число)
|
||||
номер-матрицы
|
||||
|
||||
#### exit
|
||||
@@ -0,0 +1,38 @@
|
||||
create 0 3 3
|
||||
input 0
|
||||
1 2 3
|
||||
4 5 6
|
||||
7 8 9
|
||||
getmatrix 0
|
||||
n n
|
||||
transp 0
|
||||
getmatrix 0
|
||||
n n
|
||||
create 1 3 3
|
||||
random 1
|
||||
getmatrix 1
|
||||
n
|
||||
logic 0 1 >= n
|
||||
logic 0 0 == n
|
||||
edit 0
|
||||
add
|
||||
1 1 1
|
||||
1 1 1
|
||||
1 1 1
|
||||
getmatrix 0
|
||||
n
|
||||
edit 0
|
||||
point 1:1
|
||||
sub
|
||||
10 10
|
||||
getmatrix 0
|
||||
n
|
||||
getcolum 0 1
|
||||
getrow 0 2
|
||||
n
|
||||
copy 0 5
|
||||
getmatrix 5
|
||||
n
|
||||
determ 5
|
||||
destruct 5
|
||||
exit
|
||||
@@ -0,0 +1,28 @@
|
||||
CC = gcc
|
||||
|
||||
CFLAGS = -Wall -Wextra -I../ -g
|
||||
TARGET = matrix
|
||||
|
||||
SRC_DIR = ../code
|
||||
OBJ_DIR = ../.o
|
||||
|
||||
SRCS = $(wildcard $(SRC_DIR)/*.c)
|
||||
OBJS = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRCS))
|
||||
|
||||
.PHONY: all clean run
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) $(CFLAGS) $(OBJS) -o $(TARGET)
|
||||
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
|
||||
@mkdir -p $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
run: $(TARGET)
|
||||
./$(TARGET) < input > output
|
||||
@cat output
|
||||
|
||||
clean:
|
||||
rm -rf $(OBJ_DIR) $(TARGET) output
|
||||
Executable
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,86 @@
|
||||
---
|
||||
Language: Cpp
|
||||
AccessModifierOffset: -4
|
||||
AlignAfterOpenBracket: AlwaysBreak
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveDeclarations: false
|
||||
AlignEscapedNewlines: Left
|
||||
AlignOperands: false
|
||||
AlignTrailingComments: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowShortBlocksOnASingleLine: false
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: true
|
||||
AlwaysBreakTemplateDeclarations: true
|
||||
BinPackArguments: false
|
||||
BinPackParameters: false
|
||||
BraceWrapping:
|
||||
AfterClass: false
|
||||
AfterControlStatement: false
|
||||
AfterEnum: false
|
||||
AfterFunction: true
|
||||
AfterNamespace: false
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
AfterExternBlock: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: true
|
||||
SplitEmptyNamespace: true
|
||||
BreakBeforeBinaryOperators: All
|
||||
BreakBeforeBraces: Custom
|
||||
BreakBeforeInheritanceComma: false
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializersBeforeComma: false
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 80
|
||||
CompactNamespaces: true
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 8
|
||||
Cpp11BracedListStyle: true
|
||||
DerivePointerAlignment: false
|
||||
ExperimentalAutoDetectBinPacking: false
|
||||
FixNamespaceComments: false
|
||||
IncludeBlocks: Preserve
|
||||
IndentCaseLabels: false
|
||||
IndentPPDirectives: None
|
||||
IndentWidth: 4
|
||||
IndentWrappedFunctionNames: false
|
||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
PenaltyBreakAssignment: 2
|
||||
PenaltyBreakBeforeFirstCallParameter: 19
|
||||
PenaltyBreakComment: 300
|
||||
PenaltyBreakFirstLessLess: 120
|
||||
PenaltyBreakString: 1000
|
||||
PenaltyExcessCharacter: 1000000
|
||||
PenaltyReturnTypeOnItsOwnLine: 60
|
||||
PointerAlignment: Left
|
||||
ReflowComments: true
|
||||
SortIncludes: true
|
||||
SortUsingDeclarations: true
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 1
|
||||
SpacesInAngles: false
|
||||
SpacesInContainerLiterals: false
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
Standard: Cpp11
|
||||
TabWidth: 8
|
||||
UseTab: Never
|
||||
...
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,93 @@
|
||||
# lab-rodionov-iv522s18
|
||||
|
||||
|
||||
|
||||
## Getting started
|
||||
|
||||
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
|
||||
|
||||
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
|
||||
|
||||
## Add your files
|
||||
|
||||
* [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
|
||||
* [Add files using the command line](https://docs.gitlab.com/topics/git/add_files/#add-files-to-a-git-repository) or push an existing Git repository with the following command:
|
||||
|
||||
```
|
||||
cd existing_repo
|
||||
git remote add origin https://git.csc.sibsutis.ru/trpo2026/lab-rodionov-iv522s18.git
|
||||
git branch -M master
|
||||
git push -uf origin master
|
||||
```
|
||||
|
||||
## Integrate with your tools
|
||||
|
||||
* [Set up project integrations](https://git.csc.sibsutis.ru/trpo2026/lab-rodionov-iv522s18/-/settings/integrations)
|
||||
|
||||
## Collaborate with your team
|
||||
|
||||
* [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
|
||||
* [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
|
||||
* [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
|
||||
* [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
|
||||
* [Set auto-merge](https://docs.gitlab.com/user/project/merge_requests/auto_merge/)
|
||||
|
||||
## Test and Deploy
|
||||
|
||||
Use the built-in continuous integration in GitLab.
|
||||
|
||||
* [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/)
|
||||
* [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
|
||||
* [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
|
||||
* [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
|
||||
* [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
|
||||
|
||||
***
|
||||
|
||||
# Editing this README
|
||||
|
||||
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
|
||||
|
||||
## Suggestions for a good README
|
||||
|
||||
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
|
||||
|
||||
## Name
|
||||
Choose a self-explaining name for your project.
|
||||
|
||||
## Description
|
||||
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
|
||||
|
||||
## Badges
|
||||
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
|
||||
|
||||
## Visuals
|
||||
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
|
||||
|
||||
## Installation
|
||||
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
|
||||
|
||||
## Usage
|
||||
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
|
||||
|
||||
## Support
|
||||
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
|
||||
|
||||
## Roadmap
|
||||
If you have ideas for releases in the future, it is a good idea to list them in the README.
|
||||
|
||||
## Contributing
|
||||
State if you are open to contributions and what your requirements are for accepting them.
|
||||
|
||||
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
|
||||
|
||||
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
|
||||
|
||||
## Authors and acknowledgment
|
||||
Show your appreciation to those who have contributed to the project.
|
||||
|
||||
## License
|
||||
For open source projects, say how it is licensed.
|
||||
|
||||
## Project status
|
||||
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef HEAD
|
||||
#define HEAD
|
||||
|
||||
// LIBC
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// DEFINE
|
||||
#define CHARS_IN_INPUT_MAX 40
|
||||
|
||||
// TYPEDEF
|
||||
typedef struct{
|
||||
unsigned char currCol;
|
||||
unsigned char currRow;
|
||||
unsigned char nextCol;
|
||||
unsigned char nextRow;
|
||||
unsigned char whosTurn;
|
||||
}__TURN;
|
||||
|
||||
// FUNC
|
||||
// === === io.c === ===
|
||||
char input_(char *_input, char *charsInInput);
|
||||
void print_board(char* _board);
|
||||
|
||||
// === === logic.c === ===
|
||||
char match_turn(char currFigure,char whosTurn);
|
||||
char match_move(char _board[8][8],__TURN* __turn,char currFigure);
|
||||
void move(char _board[8][8],__TURN *__turn);
|
||||
int abs(int n);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,50 @@
|
||||
#include "head.h"
|
||||
|
||||
char input_(char *_input, char *charsInInput){
|
||||
|
||||
// INPUT
|
||||
do{
|
||||
*charsInInput = 0;
|
||||
char ch;
|
||||
while((ch=getchar())>' '&&*charsInInput<CHARS_IN_INPUT_MAX){
|
||||
*(_input+*charsInInput)=ch;
|
||||
(*charsInInput)++;
|
||||
}
|
||||
if(ch<= 4)return -1;
|
||||
if(*_input>= '0' && *_input<='9'){
|
||||
for(int i = 0;i<*charsInInput;i++)
|
||||
putchar(_input[i]);
|
||||
putchar(' ');
|
||||
continue;}
|
||||
if((*_input>= 'A' && *_input<='A'+7) ||
|
||||
(*_input>= 'a' && *_input<='a'+7)||
|
||||
(*_input=='N'||*_input=='Q'||*_input=='R'||*_input=='K'||*_input=='P')){
|
||||
for(int i = 0;i<*charsInInput;i++)
|
||||
putchar(_input[i]);
|
||||
putchar('\n');
|
||||
break;}
|
||||
|
||||
}while(*_input>4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_board(char* _board){
|
||||
for (int i = 0;i<8;i++){
|
||||
putchar('8'-i);
|
||||
putchar(' ');
|
||||
for(int j = 0;j<8;j++){
|
||||
putchar(_board[i*8+j]);
|
||||
putchar(' ');
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
putchar(' ');putchar(' ');
|
||||
for(int i = 0; i<8;i++){
|
||||
putchar('a'+i);
|
||||
putchar(' ');
|
||||
}
|
||||
putchar('\n');
|
||||
putchar('\n');
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
#include "head.h"
|
||||
|
||||
|
||||
char match_turn(char currFigure,char whosTurn){
|
||||
if((currFigure>='a' && currFigure<='z' && whosTurn-1)
|
||||
// a-z white && turn !white
|
||||
||(currFigure>='A' && currFigure<='Z' && whosTurn)){
|
||||
// A-Z black && turn white
|
||||
printf("Err, turn missmatch");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char match_move(char board[8][8], __TURN* turn, char currFigure)
|
||||
{
|
||||
int rowD = turn->nextRow - turn->currRow;
|
||||
int colD = turn->nextCol - turn->currCol;
|
||||
|
||||
if (turn->nextRow < 0 || turn->nextRow > 7 ||
|
||||
turn->nextCol < 0 || turn->nextCol > 7)
|
||||
{
|
||||
printf("Err, move out of board\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
switch (currFigure)
|
||||
{
|
||||
case 'P':
|
||||
case 'p':
|
||||
{
|
||||
int dir = (currFigure == 'P') ? -1 : 1;
|
||||
|
||||
if (colD == 0)
|
||||
{
|
||||
if (board[turn->nextRow][turn->nextCol] != ' ')
|
||||
return 1;
|
||||
|
||||
if (rowD == dir)
|
||||
return 0;
|
||||
|
||||
if ((currFigure == 'P' && turn->currRow == 6 && rowD == 2*dir &&
|
||||
board[turn->currRow + dir][turn->currCol] == ' ') ||
|
||||
(currFigure == 'p' && turn->currRow == 1 && rowD == 2*dir &&
|
||||
board[turn->currRow + dir][turn->currCol] == ' '))
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (abs(colD) == 1 && rowD == dir)
|
||||
{
|
||||
if (board[turn->nextRow][turn->nextCol] != ' ')
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("Err, pawn move mismatch\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
case 'R':
|
||||
case 'r':
|
||||
{
|
||||
if (rowD != 0 && colD != 0)
|
||||
{
|
||||
printf("Err, rook move mismatch\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (rowD == 0)
|
||||
{
|
||||
int step = (colD > 0) ? 1 : -1;
|
||||
for (int c = turn->currCol + step;
|
||||
c != turn->nextCol;
|
||||
c += step)
|
||||
{
|
||||
if (board[turn->currRow][c] != ' ')
|
||||
{
|
||||
printf("Err, rook blocked\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int step = (rowD > 0) ? 1 : -1;
|
||||
for (int r = turn->currRow + step;
|
||||
r != turn->nextRow;
|
||||
r += step)
|
||||
{
|
||||
if (board[r][turn->currCol] != ' ')
|
||||
{
|
||||
printf("Err, rook blocked\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
case 'N':
|
||||
case 'n':
|
||||
{
|
||||
if ((abs(rowD) == 2 && abs(colD) == 1) ||
|
||||
(abs(rowD) == 1 && abs(colD) == 2))
|
||||
return 0;
|
||||
printf("Err, knight move mismatch\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
case 'B':
|
||||
case 'b':
|
||||
{
|
||||
if (abs(rowD) != abs(colD))
|
||||
{
|
||||
printf("Err, bishop move mismatch\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int stepR = (rowD > 0) ? 1 : -1;
|
||||
int stepC = (colD > 0) ? 1 : -1;
|
||||
int r = turn->currRow + stepR;
|
||||
int c = turn->currCol + stepC;
|
||||
|
||||
while (r != turn->nextRow && c != turn->nextCol)
|
||||
{
|
||||
if (board[r][c] != ' ')
|
||||
{
|
||||
printf("Err, bishop blocked\n");
|
||||
return 1;
|
||||
}
|
||||
r += stepR;
|
||||
c += stepC;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
case 'Q':
|
||||
case 'q':
|
||||
{
|
||||
if (abs(rowD) == abs(colD))
|
||||
{
|
||||
int stepR = (rowD > 0) ? 1 : -1;
|
||||
int stepC = (colD > 0) ? 1 : -1;
|
||||
|
||||
int r = turn->currRow + stepR;
|
||||
int c = turn->currCol + stepC;
|
||||
|
||||
while (r != turn->nextRow && c != turn->nextCol)
|
||||
{
|
||||
if (board[r][c] != ' ')
|
||||
{
|
||||
printf("Err, queen blocked\n");
|
||||
return 1;
|
||||
}
|
||||
r += stepR;
|
||||
c += stepC;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (rowD == 0 || colD == 0)
|
||||
{
|
||||
if (rowD == 0)
|
||||
{
|
||||
int step = (colD > 0) ? 1 : -1;
|
||||
for (int c = turn->currCol + step;
|
||||
c != turn->nextCol;
|
||||
c += step)
|
||||
{
|
||||
if (board[turn->currRow][c] != ' ')
|
||||
{
|
||||
printf("Err, queen blocked\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int step = (rowD > 0) ? 1 : -1;
|
||||
for (int r = turn->currRow + step;
|
||||
r != turn->nextRow;
|
||||
r += step)
|
||||
{
|
||||
if (board[r][turn->currCol] != ' ')
|
||||
{
|
||||
printf("Err, queen blocked\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
printf("Err, queen move mismatch\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
case 'K':
|
||||
case 'k':
|
||||
{
|
||||
if (abs(rowD) <= 1 && abs(colD) <= 1)
|
||||
return 0;
|
||||
printf("Err, king move mismatch\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void move(char board[8][8], __TURN *turn)
|
||||
{
|
||||
board[turn->nextRow][turn->nextCol] =
|
||||
board[turn->currRow][turn->currCol];
|
||||
|
||||
board[turn->currRow][turn->currCol] = ' ';
|
||||
|
||||
turn->whosTurn = turn->whosTurn == 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int abs(int n){
|
||||
n = n<0 ? n*-1:n;
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "head.h"
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
char _board[8][8];
|
||||
{ //Set Def desk
|
||||
char _cleanDesk[] = {"rnbqkbnrpppppppp"};
|
||||
for(int i = 0;i<8;i++)
|
||||
for(int j = 0;j<8;j++)
|
||||
_board[i][j] = ' ';
|
||||
for (int i = 0; i < 2;i++)
|
||||
for(int j = 0; j <8;j++)
|
||||
_board[i][j] = _cleanDesk[i*8+j];
|
||||
for (int i = 6; i < 8;i++)
|
||||
for(int j = 0; j < 8;j++)
|
||||
_board[i][j] = i==6? (_cleanDesk[8+j])-32 : (_cleanDesk[j])-32;
|
||||
print_board(*_board);
|
||||
}
|
||||
|
||||
char charsInInput=1;
|
||||
char _input[CHARS_IN_INPUT_MAX];
|
||||
__TURN __turn;
|
||||
__turn.whosTurn = 0; // 0 - white 1 - black
|
||||
while(charsInInput>0){
|
||||
if (input_(_input,&charsInInput)) return 0;
|
||||
unsigned char ptr;
|
||||
{//Turn info
|
||||
ptr = (*_input>='A'&&*_input<='Z')?3:2; // Qe2-e4
|
||||
__turn.currCol = _input[ptr-2]-'a';
|
||||
__turn.currRow = 8-(_input[ptr-1]-'0');
|
||||
__turn.nextCol = _input[ptr+1]-'a';
|
||||
__turn.nextRow = 8-(_input[ptr+2]-'0');
|
||||
}
|
||||
|
||||
{//Rules
|
||||
char currFigure = _board[__turn.currRow][__turn.currCol];
|
||||
{ // 'Q'e2-e5 Missmatch
|
||||
if(ptr==3)
|
||||
if(*_input!=currFigure&&*_input!=currFigure-32){
|
||||
printf("Err, figure missmatch");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(match_turn(currFigure,__turn.whosTurn))return 1;
|
||||
|
||||
{ // TAKE MISMATCH
|
||||
if
|
||||
((_board[__turn.nextRow][__turn.nextCol]!=' ' && _input[ptr]!='x'
|
||||
)||(
|
||||
_board[__turn.nextRow][__turn.nextCol]==' ' && _input[ptr]=='x'))
|
||||
{
|
||||
printf("Err, take missmatch");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(match_move(_board,&__turn,currFigure))return 1;
|
||||
}
|
||||
|
||||
move(_board,&__turn);
|
||||
print_board(*_board);
|
||||
if(_input[charsInInput-1]=='#')break;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,4 @@
|
||||
1. e2-e4 e7-e5
|
||||
2. Bf1-c4 Nb8-c6
|
||||
3. Qd1-h5 Ng8-f6
|
||||
4. Qh5xf7#
|
||||
Binary file not shown.
Submodule
+1
Submodule 1Y-2H/trpo/lab-rodionov-iv522s18 added at 5553e1330f
Reference in New Issue
Block a user