Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Максимова Лабораторная и двумерные массивы #32

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions 13_Maksimova/contr1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "stdio.h"
#include"stdlib.h"
#include"locale.h"
int unic(int* a, int s, int x) {
for (int i = 0; i < s; i++) {
if (a[i] == x) {
return 0;
}
}
return 1;
}int main() {
setlocale(LC_ALL, "Rus");
int* a1, * a2, * ob;
int s1, s2, obs = 0;
printf("������� ������ ������� �������:");
scanf_s("%d", &s1);
a1 = (int*)malloc(s1 * sizeof(int));
printf("������� �������� ������� �������:\n");
for (int i = 0; i < s1; i++) {
scanf_s("%d", &a1[i]);
}
printf("������� ������ ������� �������:");
scanf_s("%d", &s2);
a2 = (int*)malloc(s2 * sizeof(int));
printf("������� �������� ������� �������:\n");
for (int i = 0; i < s2; i++)
{
scanf_s("%d", &a2[i]);
}
ob = (int*)malloc((s1 + 2) * sizeof(int));
for (int i = 0; i < s1; i++) {
if (unic(ob, obs, a1[i]))
{
ob[obs++] = a1[i];
}
}
for (int i = 0; i < s2; i++) {
if (unic(ob, obs, a2[i]))
{
ob[obs++] = a2[i];
}
}
printf("������ �����������:\n");
for (int i = 0; i < obs; i++)
{
printf("%d", ob[i]);
}
printf("\n");
free(a1);
free(a2);
free(ob);
return 0;
}
88 changes: 88 additions & 0 deletions 13_Maksimova/contr2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>


void fillMatrix(double** matrix, int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = (double)(rand() % 100) / 10;
}
}
}

void printMatrix(double** matrix, int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%.2f ", matrix[i][j]);
}
printf("\n");
}
}

double calculateAverage(double** matrix, int m, int n) {
double sum = 0.0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
sum += matrix[i][j];
}
}
return sum / (m * n);
}

int findColumnWithMostAboveAverage(double** matrix, int m, int n, double average, int* columnIndex) {
int maxCount = 0;
*columnIndex = -1;

for (int j = 0; j < n; j++) {
int count = 0;
for (int i = 0; i < m; i++) {
if (matrix[i][j] > average) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
*columnIndex = j;
}
}

return maxCount;
}

int main() {
int m, n;
setlocale(LC_ALL, "Rus");
srand(time(NULL));
printf("������� ���������� �����: ");
scanf_s("%d", &m);
printf("������� ���������� ��������: ");
scanf_s("%d", &n);
double** matrix = (double**)malloc(m * sizeof(double*));
for (int i = 0; i < m; i++) {
matrix[i] = (double*)malloc(n * sizeof(double));
}
fillMatrix(matrix, m, n);
printf("�������:\n");
printMatrix(matrix, m, n);
double average = calculateAverage(matrix, m, n);
printf("������� �������� � �������: %.2f\n", average);
int columnIndex;
int countAboveAverage = findColumnWithMostAboveAverage(matrix, m, n, average, &columnIndex);

if (columnIndex != -1) {
printf("������� � ���������� ����������� �������� ������ ��������: %d\n", columnIndex + 1);
printf("���������� �������� � ���� ������� ������ ��������: %d\n", countAboveAverage);
}
else {
printf("��� �������� ������ ��������.\n");
}

for (int i = 0; i < m; i++) {
free(matrix[i]);
}
free(matrix);

return 0;
}
60 changes: 60 additions & 0 deletions 13_Maksimova/dv14.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <locale.h>

void printMatrix(int matrix[100][100], int a, int b) {
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

void removeColumn(int matrix[100][100], int* a, int* b, int colToRemove) {
for (int i = 0; i < *a; i++) {
for (int j = colToRemove; j < *b - 1; j++) {
matrix[i][j] = matrix[i][j + 1];
}
}
(*b)--;
}

int main() {
int matrix[100][100];
int a, b;
setlocale(LC_ALL, "Rus");
printf("������� ���������� ����� � �������� �������: ");
scanf_s("%d %d", &a, &b);

printf("������� �������� �������:\n");
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
scanf_s("%d", &matrix[i][j]);
}
}
int minElement;
int minColIndex = -1;

minElement = matrix[0][0];
minColIndex = 0;

for (int j = 0; j < b; j++) {
for (int i = 0; i < a; i++) {
if (matrix[i][j] < minElement) {
minElement = matrix[i][j];
minColIndex = j;
}
}
}

if (minColIndex != -1) {
removeColumn(matrix, &a, &b, minColIndex);
printf("\n������� ����� �������� ������� � ����������� ��������� (%d):\n", minElement);
printMatrix(matrix, a, b);
}
else {
printf("�� ������� ����� ����������� �������.\n");
}

return 0;
}
60 changes: 60 additions & 0 deletions 13_Maksimova/dv22.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <locale.h>

// ������� ��� ���������� ������������� ���������� ������ ������ ��������� ����
void findMaxFreeSeats(int train[100][100], int wagons, int seats, int* maxFree, int* wagonNumber) {
*maxFree = 0;
*wagonNumber = -1;

for (int i = 0; i < wagons; i++) {
int currentFree = 0;

for (int j = 0; j < seats; j++) {
if (train[i][j] == 0) {
currentFree++;
}
else {
if (currentFree > *maxFree) {
*maxFree = currentFree;
*wagonNumber = i + 1;
}
currentFree = 0;
}
}

if (currentFree > *maxFree) {
*maxFree = currentFree;
*wagonNumber = i + 1;
}
}
}

int main() {
setlocale(LC_ALL, "Rus");
int train[100][100];
int wagons, seats;

printf("������� ���������� �������: ");
scanf_s("%d", &wagons);
printf("������� ���������� ���� � ������ ������: ");
scanf_s("%d", &seats);

for (int i = 0; i < wagons; i++) {
printf("������� ������ ��� ������ %d (0 - ��������, 1 - ������):\n", i + 1);
for (int j = 0; j < seats; j++) {
scanf_s("%d", &train[i][j]);
}
}
int maxFree, wagonNumber;
findMaxFreeSeats(train, wagons, seats, &maxFree, &wagonNumber);

if (wagonNumber != -1) {
printf("������������ ���������� ������ ������ ��������� ����: %d\n", maxFree);
printf("����� ������ � ���� ����������� ��������� ����: %d\n", wagonNumber);
}
else {
printf("��� ����� ������.\n");
}

return 0;
}
117 changes: 117 additions & 0 deletions 13_Maksimova/kassa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <ctype.h>


typedef struct {
char name[20];
double price_per_kg;
} Product;

// ������� ��� ���������� ������ � ������� ��������
void to_lower_case(char* str) {
for (int i = 0; str[i]; i++) {
str[i] = tolower(str[i]);
}
}

// ������� ��� ������ ������ ������� � �������
void read_purchase_list(char purchases[100][256], int* count) {
setlocale(LC_ALL, "Rus");
printf("������� ������ ������� (������� 'end' ��� ���������� �����):\n");
*count = 0;
while (*count < 100) {
fgets(purchases[*count], 256, stdin);
purchases[*count][strcspn(purchases[*count], "\n")] = 0;
if (strcmp(purchases[*count], "end") == 0) {
break;
}
(*count)++;
}
}

// ������� ��� ������� ������ �� ���������� � �������� ������
void parse_purchase(const char* line, double* quantity, char* name) {
char* token;
char line_copy[256];
strcpy(line_copy, line);

*quantity = 0.0;
name[0] = '\0';

token = strtok(line_copy, " ");
while (token != NULL) {
char* endptr;
double value = strtod(token, &endptr);
if (endptr != token) {
*quantity += value;
}
else {
strcat(name, token);
strcat(name, " ");
}
token = strtok(NULL, " ");
}
name[strlen(name) - 1] = '\0';
}

// ������� ��� ������������ ����
void generate_check(Product stock[], int stock_count, char purchases[100][256], int purchase_count) {
FILE* check_file = fopen("check.txt", "w");
if (check_file == NULL) {
printf("������ �������� ����� ����.\n");
return;
}

double total_sum = 0.0;
fprintf(check_file, "���:\n");
fprintf(check_file, "-----------------------------------\n");
for (int i = 0; i < purchase_count; i++) {
double quantity = 0.0;
char name[256] = "";
parse_purchase(purchases[i], &quantity, name);
to_lower_case(name);

for (int j = 0; j < stock_count; j++) {
char stock_name[256];
strcpy(stock_name, stock[j].name);
to_lower_case(stock_name);

if (strcmp(stock_name, name) == 0) {
double cost = quantity * stock[j].price_per_kg;
fprintf(check_file, "%-15s %2.1f �� * %-10.2f = %.2f\n", stock[j].name, quantity, stock[j].price_per_kg, cost);
total_sum += cost;
break;
}
}
}

fprintf(check_file, "-----------------------------------\n");
fprintf(check_file, "����� �����: %.2f\n", total_sum);
fclose(check_file);
printf("��� ������� ����������� � ���� check.txt\n");
}

int main() {
Product stock[] = {
{"banana", 105.0},
{"tomato", 300.0},
{"kiwi", 400.0},
{"apple", 150.0},
{"cucumber", 200.0},
{"potato", 50.0}
};
int stock_count = sizeof(stock) / sizeof(stock[0]);

char purchases[100][256];
int purchase_count = 0;

read_purchase_list(purchases, &purchase_count);
generate_check(stock, stock_count, purchases, purchase_count);

return 0;
}

Loading