Первая лаба

This commit is contained in:
alexey
2025-11-23 23:09:17 +03:00
commit a889e96494
8 changed files with 237 additions and 0 deletions

BIN
fib Executable file

Binary file not shown.

52
fib.c Normal file
View File

@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int fib(int n) {
if (n < 0) {
return -1;
}
if (n == 0) return 0;
if (n == 1) return 1;
int a = 0;
int b = 1;
int c;
for (int i = 2; i <= n; i++) {
if (b > INT_MAX - a) {
return INT_MIN;
}
c = a + b;
a = b;
b = c;
}
return b;
}
int main(void) {
int n;
printf("Введите целое неотрицательное n: ");
if (scanf("%d", &n) != 1) {
fprintf(stderr, "Ошибка ввода: ожидалось целое число.\n");
return EXIT_FAILURE;
}
int result = fib(n);
if (n < 0) {
fprintf(stderr, "Ошибка: n должно быть неотрицательным.\n");
return EXIT_FAILURE;
}
if (result == INT_MIN) {
fprintf(stderr, "Ошибка: переполнение при вычислении fib(%d). "
"Значение фибоначчи слишком велико для типа int.\n", n);
return EXIT_FAILURE;
}
printf("fib(%d) = %d\n", n, result);
return EXIT_SUCCESS;
}

7
hello.c Normal file
View File

@@ -0,0 +1,7 @@
#include <stdio.h>
int main(){
printf("Hello World!\n");
return 0;
}

BIN
myatoi Executable file

Binary file not shown.

51
myatoi.c Normal file
View File

@@ -0,0 +1,51 @@
#include <stdio.h>
#include <ctype.h>
#include <limits.h>
int myatoi(char *instr) {
if (instr == NULL) {
return 0;
}
while (isspace((unsigned char)*instr)) {
instr++;
}
int sign = 1;
if (*instr == '+') {
instr++;
} else if (*instr == '-') {
sign = -1;
instr++;
}
long result = 0;
while (*instr >= '0' && *instr <= '9') {
int digit = *instr - '0';
if (sign > 0) {
if (result > (LONG_MAX - digit) / 10) {
return INT_MAX;
}
} else {
if (-result < (LONG_MIN + digit) / 10) {
return INT_MIN;
}
}
result = result * 10 + digit;
instr++;
}
return (int)(result * sign);
}
int main() {
printf("myatoi(\"1\") = %d\n", myatoi("1"));
printf("myatoi(\"42\") = %d\n", myatoi("42"));
printf("myatoi(\"-105\") = %d\n", myatoi("-105"));
printf("myatoi(\" -12abc34\") = %d\n", myatoi(" -12abc34"));
printf("myatoi(\"9999999999999\") = %d (overflow)\n", myatoi("9999999999999"));
return 0;
}

56
mystr_idx.c Normal file
View File

@@ -0,0 +1,56 @@
#include <stdio.h>
int mystr_idx(char *str, char *substr) {
if (str == NULL || substr == NULL) {
return -1;
}
if (*substr == '\0'){
return 0;
}
int i=0;
while (str[i] != '\0') {
int j = 0;
int temp_i = i;
while (substr[j] != '\0' && str[temp_i] != '\0' && str[temp_i] == substr[j]) {
temp_i++;
j++;
}
if (substr[j] == '\0') {
return i;
}
i++;
}
return -1;
}
void test_mystr_idx() {
printf("mystr_idx(\"helloworld\", \"world\") = %d (ожидается: 5)\n",
mystr_idx("helloworld", "world"));
printf("mystr_idx(\"helloworld\", \"helloworld\") = %d (ожидается: 0)\n",
mystr_idx("helloworld", "helloworld"));
printf("mystr_idx(\"helloworld\", \"foo\") = %d (ожидается: -1)\n",
mystr_idx("helloworld", "foo"));
printf("mystr_idx(\"helloworld\", \"hello\") = %d (ожидается: 0)\n",
mystr_idx("helloworld", "hello"));
printf("mystr_idx(\"helloworld\", \"low\") = %d (ожидается: 3)\n",
mystr_idx("helloworld", "low"));
printf("mystr_idx(\"abcabc\", \"abc\") = %d (ожидается: 0)\n",
mystr_idx("abcabc", "abc"));
printf("mystr_idx(\"\", \"test\") = %d (ожидается: -1)\n",
mystr_idx("", "test"));
printf("mystr_idx(\"test\", \"\") = %d (ожидается: 0)\n",
mystr_idx("test", ""));
}
int main() {
test_mystr_idx();
return 0;
}

24
mystrlen.c Normal file
View File

@@ -0,0 +1,24 @@
#include <stdio.h>
#include <string.h>
int mystrlen(const char *s) {
return strlen(s);
}
int main() {
char str[200];
printf("Введите строку: ");
fgets(str, sizeof(str), stdin);
size_t len = strlen(str);
if (len > 0 && str[len-1] == '\n') {
str[len-1] = '\0';
}
int length = mystrlen(str);
printf("Длина строки: %d\n", length);
return 0;
}

47
sumArray.c Normal file
View File

@@ -0,0 +1,47 @@
#include <stdio.h>
int sumArray(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int myArray[100];
int i;
int count = 0;
printf("Введите числа, для остановки введите число 0:\n");
for(i = 0; i < 100; i++) {
scanf("%d", &myArray[i]);
if(myArray[i] == 0){
break;
}
count++;
}
printf("Введённые числа:\n");
for (i =0; i < count; i++) {
printf("%d ", myArray[i]);
}
printf("\n");
if (count < 100 && myArray[count] == 0) {
printf("Ввод остановлен по команде пользователя (0)\n");
}
printf("Всего введено чисел: %d\n", count);
int total = sumArray(myArray, count);
printf("Сумма элементов массива: %d\n", total);
return 0;
}