Skip to content
Open

lab3 #200

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
81 changes: 81 additions & 0 deletions lab1/WordCont.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>


int main(int argc, char* argv[]) {


if (argc < 2) {
printf("Usage: WordCount.exe [OPTIONS] filename\n\nFor a list of options use: WordCount.exe -h\n\n\n\n\n\n\n ");
return 1;
}
char* option = argv[1];


FILE* input;
char* name;
name = argv[argc - 1];

input = fopen(name, "r");

if (input == NULL) {
printf("File not be opened");
return 1;
}



for (int l = 1; l <= argc - 2; l++) {
char* options = argv[l];
int ch;

if ((strcmp(options, "-l") == 0) || (strcmp(options, "--lines") == 0)) {
int CountLines = 0;
int word = 0;
while ((ch = fgetc(input)) != EOF) {
if (ch == '\n') {
CountLines++;
}
fseek(input, 0, SEEK_SET);
printf("Opened file contains %d lines\n", CountLines + word);

}

else if ((strcmp(options, "-c") == 0) || (strcmp(options, "--bytes") == 0)) {
int size = -1;
do {
ch = fgetc(input);
size++;
} while (ch != EOF);
fseek(input, 0, SEEK_SET);
printf("File size is %d bytes\n", size);
}


else if ((strcmp(options, "-w") == 0) || (strcmp(options, "--words") == 0)) {
int ch = 0;
int isspace = 0;
int words = 0;
while (ch != EOF) {
ch = fgetc(input);
if ((ch == ' ') || (ch == '\n') || (ch == '\t') || (ch == '\v') || (ch == '\f') || (ch == EOF) || (ch == '.') || (ch == ',')) {
if (words == 1) {
words = 0;
isspace++;
}
}
else {
words = 1;
}
}
fseek(input, 0, SEEK_SET);
printf("The file has %d words\n", isspace);
}
else {
printf("Invalid command.\n");
}
}
fclose(input);
return 0;
}
81 changes: 81 additions & 0 deletions lab1/WordCont1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>


int main(int argc, char* argv[]) {


if (argc < 2) {
printf("Usage: WordCount.exe [OPTIONS] filename\n\nFor a list of options use: WordCount.exe -h\n\n\n\n\n\n\n ");
return 1;
}
char* option = argv[1];


FILE* input;
char* name;
name = argv[argc - 1];

input = fopen(name, "r");

if (input == NULL) {
printf("File not be opened");
return 1;
}



for (int l = 1; l <= argc - 2; l++) {
char* options = argv[l];
int ch;

if ((strcmp(options, "-l") == 0) || (strcmp(options, "--lines") == 0)) {
int CountLines = 0;
int word = 0;
while ((ch = fgetc(input)) != EOF) {
if (ch == '\n') {
CountLines++;
}
fseek(input, 0, SEEK_SET);
printf("Opened file contains %d lines\n", CountLines + word);

}

else if ((strcmp(options, "-c") == 0) || (strcmp(options, "--bytes") == 0)) {
int size = -1;
do {
ch = fgetc(input);
size++;
} while (ch != EOF);
fseek(input, 0, SEEK_SET);
printf("File size is %d bytes\n", size);
}


else if ((strcmp(options, "-w") == 0) || (strcmp(options, "--words") == 0)) {
int ch = 0;
int isspace = 0;
int words = 0;
while (ch != EOF) {
ch = fgetc(input);
if ((ch == ' ') || (ch == '\n') || (ch == '\t') || (ch == '\v') || (ch == '\f') || (ch == EOF) || (ch == '.') || (ch == ',')) {
if (words == 1) {
words = 0;
isspace++;
}
}
else {
words = 1;
}
}
fseek(input, 0, SEEK_SET);
printf("The file has %d words\n", isspace);
}
else {
printf("Invalid command.\n");
}
}
fclose(input);
return 0;
}
130 changes: 130 additions & 0 deletions lab2/lab2.1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
define _CRT_SECURE_NO_WARNINGS

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define Findmax(a,b) (((a) > (b)) ? (a) : (b))
const int cc = 1e9; //radix

typedef struct
{
int32_t* int32;
size_t size;
} uint1024_t;

uint1024_t from_uint(unsigned int x)
{
int size;
if (x >= cc) size = 2;
else size = 1;
uint1024_t t;
t.int32 = malloc(size * sizeof(int32_t));// size * sizeof(int32_t)
t.size = size;
t.int32[0] = x % cc;
if (size > 1) t.int32[1] = x / cc;
return t;
}

void printf_value(uint1024_t x)//вывод
{
int size = x.size;
printf("%d", !size ? 0 : x.int32[size - 1]); //display significant digits without 9 zeros
for (int index = size - 2; index >= 0; --index) //output the result: significant digits are printed at the end of the array
printf("%09d", x.int32[index]);
}

void scanf_value(uint1024_t* x)//заполнение полного массива
{
char str[1024];
scanf("%s", str);
int len = strlen(str);
int size = len % 9 == 0 ? len / 9 : len / 9 + 1;
uint1024_t result;
result.int32 = malloc(size * sizeof(int32_t));
result.size = size;
for (int index = len, j = 0; index > 0; index -= 9, j++)
{
str[index] = '\0'; //убираю нули
result.int32[j] = (atoi(index >= 9 ? str + index - 9 : str)); //I take 9 digits from the end, if the index is less than 9, I go to the rest of the line
}
*x = result;
}


uint1024_t add_op(uint1024_t x, uint1024_t y)//сложение
{
uint1024_t result;
int size = Findmax(x.size, y.size);
result.int32 = calloc(size, sizeof(int32_t));
result.size = size;
int32_t over = 0;
for (int index = 0; index < size || over; index++)
{
if (index == size)
{
result.int32 = realloc(result.int32, (size + 1) * sizeof(int32_t));
result.int32[size] = 0;
result.size++;
}
result.int32[index] = over + (index < x.size ? x.int32[index] : 0) + (index < y.size ? y.int32[index] : 0);
if (result.int32[index] >= cc) over = 1, result.int32[index] -= cc;
else over = 0;
}
return result;
}

uint1024_t subtr_op(uint1024_t x, uint1024_t y)//вычитание
{
uint1024_t result;
int size = Findmax(x.size, y.size);
result.int32 = calloc(size, sizeof(int32_t));
result.size = size;
int over = 0;
for (int index = 0; index < size || over; index++)
{
if (index == size)
break;
result.int32[index] = x.int32[index] - (index < y.size ? y.int32[index] : 0) - over;
if (result.int32[index] < 0) {
over = 1;
result.int32[index] += cc;
}
else over = 0;
}
while (result.int32[result.size - 1] == 0 && result.size > 1)
result.size--;
result.int32 = realloc(result.int32, result.size * sizeof(int32_t));
return result;
}

int main()
{
setlocale(LC_ALL, "Rus");
uint1024_t num1, num2;
char ArrayOfChar[310];
memset(ArrayOfChar, 0, 310);
printf("Введите число\n");
scanf_value(&num1);
printf("Введите число\n");
scanf_value(&num2);
fseek(stdin, 0, SEEK_END);
while (1)
{
printf("Выберете: + - * \n");
scanf("%c", ArrayOfChar);
if (!strcmp(ArrayOfChar, "-")) num1 = subtr_op(num1, num2);
else if (!strcmp(ArrayOfChar, "+")) num1 = add_op(num1, num2);
else break;
printf_value(num1);
fseek(stdin, 0, SEEK_END);
printf("\nВведите число\n");
scanf_value(&num2);
fseek(stdin, 0, SEEK_END);
return 0;
}
}



Loading