Last active
November 25, 2020 19:16
-
-
Save EugW/b8b7bae649588dc414477c3d513ca79c to your computer and use it in GitHub Desktop.
Лаб5/6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
// d) | |
char* getAlphabet() { | |
char* pAlp = malloc(26); //Выделение памяти | |
if (pAlp == NULL) { | |
printf("Cannot allocate memory"); | |
return NULL; | |
} | |
int i = 0; | |
for (unsigned int sym = 'a'; sym <= 'z'; sym++) { | |
pAlp[i] = (char) sym; | |
i++; | |
} | |
char* tmp = realloc(pAlp, 53); //Расширение памяти | |
if (tmp == NULL) | |
printf("Cannot allocate memory"); | |
return NULL; | |
pAlp = tmp; | |
for (unsigned int sym = 'Z'; sym >= 'A'; sym--) { | |
pAlp[i] = (char) sym; | |
i++; | |
} | |
pAlp[i] = '\0'; | |
return pAlp; | |
} | |
// e) | |
void printString(const char* pString) { | |
while (*pString != '\0') { | |
putchar(*pString); | |
pString++; | |
} | |
} | |
// f) Вывод символов из строки от start до end включая | |
void alphabetSubstr(const char* pString, char start, char end) { | |
const char* ptr = strchr(pString, start); | |
while (*ptr != end) { | |
putchar(*ptr); | |
ptr++; | |
} | |
putchar(*ptr); | |
putchar('\n'); | |
} | |
// i) Сравнение памяти, собственная реализация | |
int memecmp(const void* str1, const void* str2, size_t n) { | |
const char* s1 = str1; | |
const char* s2 = str2; | |
while (n > 0) { | |
if (*s1 != *s2) { | |
if (*s1 > *s2) return 1; | |
if (*s2 > *s1) return -1; | |
} | |
s1++; | |
s2++; | |
n--; | |
} | |
return 0; | |
} | |
int main() { | |
// a) Объявление и присваивание переменных | |
int Int = 1; | |
char Char = 'z'; | |
double Double = 1.1; | |
// b) Объявление и присваивание указателей | |
int* pInt = ∬ | |
char* pChar = &Char; | |
double* pDouble = &Double; | |
// c) Вывод разнообразной информации о переменных и указателях | |
printf("----------------------------------------------\n"); | |
printf("Address of int = %p\n", pInt); | |
printf("Value of pointer to int = %d\n", *pInt); | |
printf("Address of pointer to int = %p\n", &pInt); | |
printf("Value of int = %d\n", Int); | |
printf("Size of int = %llu\n", sizeof(Int)); | |
printf("Size of pointer to int = %llu\n", sizeof(pInt)); | |
printf("----------------------------------------------\n"); | |
printf("Address of char = %p\n", pChar); | |
printf("Value of pointer to char = %c\n", *pChar); | |
printf("Address of pointer to char = %p\n", &pChar); | |
printf("Value of char = %c\n", Char); | |
printf("Size of char = %llu\n", sizeof(Char)); | |
printf("Size of pointer to char = %llu\n", sizeof(pChar)); | |
printf("----------------------------------------------\n"); | |
printf("Address of double = %p\n", pDouble); | |
printf("Value of pointer to double = %f\n", *pDouble); | |
printf("Address of pointer to double = %p\n", &pDouble); | |
printf("Value of double = %f\n", Double); | |
printf("Size of double = %llu\n", sizeof(Double)); | |
printf("Size of pointer to double = %llu\n", sizeof(pDouble)); | |
printf("----------------------------------------------\n"); | |
// g) Вывоз функций из main() | |
const char* pStr = getAlphabet(); | |
printf("%s\n", pStr); | |
printString("example\n"); | |
alphabetSubstr(pStr, 's', 'E'); | |
free(pStr); //Освобождение памяти | |
// h) Чтение и вывод строки | |
int strSz; | |
scanf_s("%d", &strSz); | |
char* pH = malloc((size_t)strSz + 1); | |
if (pH != NULL) { //Проверка выделения памяти | |
char* pStart = pH; | |
scanf_s("%s", pH, strSz + 1); | |
for (; *pH != '\0'; pH++) { | |
putchar(*pH); | |
} | |
pH = pStart; | |
free(pH); | |
putchar('\n'); | |
} | |
else { | |
printf("Cannot allocate memory"); | |
} | |
// j) Сравнение результатов собственной и стандртной реализации функции memcmp | |
const char* pStr1 = "ABC"; | |
const char* pStr2 = "AbC"; | |
printf("Strings are equal (my func) vs (std func): %d vs %d\n", | |
memecmp(pStr1, pStr1, 3), | |
memcmp(pStr1, pStr1, 3)); | |
printf("String 2 is greater than String 1 (my func) vs (std func): %d vs %d\n", | |
memecmp(pStr1, pStr2, 3), | |
memcmp(pStr1, pStr2, 3)); | |
printf("String 1 is greater than String 2 (my func) vs (std func): %d vs %d\n", | |
memecmp(pStr2, pStr1, 3), | |
memcmp(pStr2, pStr1, 3)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment