Skip to content
Open

Kp 6 #431

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions kp6/convert_to_binary.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include "schedule.h"
// gcc convert_to_binary.c
// ./a.exe output.bin data.txt
int main(int argc, char *argv[]) {
schedule t;
FILE *file_out = NULL;
FILE *file_in = NULL;

if (argc != 3) {
printf("Usage: %s output.bin data.txt\n", argv[0]);
return 1;
}

file_out = fopen(argv[1], "wb");
if (file_out == NULL) {
printf("Error with %s\n", argv[1]);
return 1;
}
file_in = fopen(argv[2], "r");
if (file_in == NULL) {
printf("Error with %s\n", argv[2]);
return 1;
}

while (!feof(file_in)) {
fscanf(file_in, "%d %s %d %s %d %d",
&t.group_number,
t.day,
&t.lesson_number,
t.subject,
&t.room_number,
&t.week
);
fwrite(&t, sizeof(t), 1, file_out);
}
fclose(file_out);
fclose(file_in);
return 0;
}
74 changes: 74 additions & 0 deletions kp6/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "schedule.h"

// gcc main.c
// ./a.exe output.bin -f
// ./a.exe output.bin -p 100
int main(int argc, char *argv[])
{
schedule t;
FILE *file = NULL;

if (argc < 3)
{
printf("Usage: %s <file> <flag>\nFlags:\n-f - show all schedule\n-p - show schedule in the particular classroom\n", argv[0]);
return 1;
}

file = fopen(argv[1], "rb");

if (file == NULL)
{
printf("Error with %s\n", argv[1]);

return 1;
}

if (!strcmp(argv[2], "-f"))
{
printf(" ____________________________________________________________________________________________________________________\n");
printf("| Группа | день недели | номер пары | предмет | номер аудитории | неделя (чет/нечет) |\n");
printf("`--------------`-------------------`------------------`-----------------`-----------------------`--------------------`\n");

while (fread(&t, sizeof(t), 1, file) == 1)
{
printf("|%14d|%19s|%18d|%17s|%23d|%20d|\n",
t.group_number,
t.day,
t.lesson_number,
t.subject,
t.room_number,
t.week
);

printf("`--------------`-------------------`------------------`-----------------`-----------------------`--------------------`\n");
}
} else if (!strcmp(argv[2], "-p")) {
int requiredRoomNumber = atoi(argv[3]);
printf(" ____________________________________________________________________________________________________________________\n");
printf("| Группа | день недели | номер пары | предмет | номер аудитории | неделя (чет/нечет) |\n");
printf("`--------------`-------------------`------------------`-----------------`-----------------------`--------------------`\n");

while (fread(&t, sizeof(t), 1, file) == 1) {
if (t.room_number == requiredRoomNumber ) {
printf("|%14d|%19s|%18d|%17s|%23d|%20d|\n",
t.group_number,
t.day,
t.lesson_number,
t.subject,
t.room_number,
t.week
);
printf("`--------------`-------------------`------------------`-----------------`-----------------------`--------------------`\n");
}
}
} else {
printf("Usage: %s <file> <flag>\nFlags:\n-f - show all data base\n-p - show teams with proven research-doc\n", argv[0]);
return 0;
}
fclose(file);

return 0;
}
14 changes: 14 additions & 0 deletions kp6/schedule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __SCHEDULE__
#define __SCHEDULE__
#include <stdbool.h>


typedef struct schedule {
char day[30];
char subject[30];
int group_number;
int lesson_number;
int room_number;
int week;
} schedule;
#endif
1 change: 1 addition & 0 deletions lab1/lab1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
listing kbkbkb
Binary file added lab6
Binary file not shown.
Binary file added lab6.jdt
Binary file not shown.
12 changes: 12 additions & 0 deletions laba25-26/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
lab26: main.o stack.o test.o sort.o
gcc -o lab26 main.o stack.o test.o sort.o
main.o: main.c stack.h test.h sort.h
gcc -c main.c
test.o: test.c test.h stack.h sort.h
gcc -c test.c
sort.o: sort.c sort.h stack.h
gcc -c sort.c
stack.o: stack.c stack.h
gcc -c stack.c


9 changes: 9 additions & 0 deletions laba25-26/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include "test.h"
#include "sort.h"

int main() {
test();
}
26 changes: 26 additions & 0 deletions laba25-26/sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include "stack.h"
#include "sort.h"

void Sort(Stack** stack) {
if (Size(*stack) <= 1) {
return;
}

Stack* sortedStack = InitStack();
Stack* tempStack = InitStack();

while (!IsEmpty(*stack)) {
T maxItem = ExtractMax(*stack, tempStack);
Push(sortedStack, maxItem);


while (!IsEmpty(tempStack)) {
Push(*stack, Pop(tempStack));
}
}

DelStack(stack);
*stack = sortedStack;
DelStack(&tempStack);
}
7 changes: 7 additions & 0 deletions laba25-26/sort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef __SORT__
#define __SORT__
#include "stack.h"

void Sort(Stack** stack);

#endif
99 changes: 99 additions & 0 deletions laba25-26/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"

#define LEN 10

Stack* InitStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->array = (T*)malloc(sizeof(T) * LEN);
stack->maxLen = LEN;
stack->index = -1;
return stack;
}

int IsEmpty(Stack* stack) {
if (stack == NULL) {
return 1;
}
if (stack->index == -1) {
return 1;
} else {
return 0;
}
}

void Push(Stack* stack, T value) {
if (stack == NULL) {
return;
}
stack->index++;
if (stack->index >= stack->maxLen) {
stack->array = realloc(stack->array, sizeof(T) * stack->maxLen * 2);
stack->maxLen = stack->maxLen * 2;
}
(stack->array)[stack->index] = value;
}


int Size(Stack* stack) {
if (stack == NULL) {
return 0;
}
return stack->index + 1;
}

void DelStack(Stack** stack) {
if (*stack == NULL) {
return;
}
free((*stack)->array);
free(*stack);
*stack = NULL;
}


void PrintStack(Stack* stack) {
if (stack == NULL) {
return;
}
printf("start\n");
if (stack->index == -1) {
printf("empty\n");
} else {
for(int i = stack->index; i != -1; i--) {
printf("%d\n", (stack->array)[i]);
}
}
printf("end\n");
}

T Pop(Stack* stack) {
if (IsEmpty(stack)) {
printf("Error");
return 0;
}
stack->index--;
return (stack->array)[stack->index + 1];
}

T ExtractMax(Stack* sourceStack, Stack* tempStack) {
if (IsEmpty(sourceStack)) {
printf("Error");
return 0;
}

T maxItem = Pop(sourceStack);

while (!IsEmpty(sourceStack)) {
T item = Pop(sourceStack);
if (item > maxItem) {
Push(tempStack, maxItem);
maxItem = item;
} else {
Push(tempStack, item);
}
}

return maxItem;
}
22 changes: 22 additions & 0 deletions laba25-26/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef __STACK__
#define __STACK__

typedef int T;

typedef struct Stack {
T* array;
int index;
int maxLen;
} Stack;

Stack* InitStack();
int IsEmpty(Stack* stack);
void Push(Stack* stack, T value);
T Pop(Stack* stack);
int Size(Stack* stack);
void DelStack(Stack** stack);
void PrintStack(Stack* stack);
T ExtractMax(Stack* sourceStack, Stack* tempStack);


#endif
60 changes: 60 additions & 0 deletions laba25-26/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include "sort.h"
#include "test.h"

void test () {
char command = ' ';
int ind;
int arg;
Stack* stacks[5] = {NULL, NULL, NULL, NULL, NULL};
printf("i - init\n");
printf("u - push\n");
printf("o - pop\n");
printf("s - stop\n");
printf("d - del stack\n");
printf("l - line sort\n");
while (command != 's') {
scanf("%c", &command);
switch (command) {
case 'i': {
scanf("%d", &ind);
stacks[ind] = InitStack();
break;
}
case 'u': {
scanf("%d %d", &ind, &arg);
Push(stacks[ind], arg);
PrintStack(stacks[ind]);
break;
}
case 'o': {
scanf("%d", &ind);
printf("pop %d\n", Pop(stacks[ind]));
PrintStack(stacks[ind]);
break;
}
case 's': {
for (int i = 0; i < 5; i++) {
if (stacks[i] != NULL) DelStack(&(stacks[i]));
}
break;
}

case 'd': {
scanf("%d", &ind);
DelStack(&(stacks[ind]));
break;
}

case 'l': {
scanf("%d", &ind);
Sort(&(stacks[ind]));
PrintStack(stacks[ind]);
break;
}
}
}
return;
}
8 changes: 8 additions & 0 deletions laba25-26/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __TEST__
#define __TEST__
#include "stack.h"
#include "sort.h"

void test();

#endif
Binary file added photolab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.