提交 cc6059f9 编写于 作者: 独孤过's avatar 独孤过

完善代码

上级 3adca89c
......@@ -3,9 +3,9 @@
int8_t endianness()
{
//int8_t temp[] = { 0, 1 };
//return *(int16_t*)temp == 1;
int16_t temp = 0x0100;
//int8_t temp[] = { 0, 0, 0, 0, 0, 0, 0, 1 };
//return *(int64_t*)temp == 1;
int64_t temp = 0x01i64 << (7 << 3);
return *(int8_t*)&temp;
}
......
#include <time.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#if defined(_WIN32) && defined(_MSC_VER)
#define restrict __restrict
......@@ -17,16 +17,16 @@ static inline void swap(int *restrict left, int *restrict right)
*right = temp;
}
void random(int *restrict ptr, int num)
void random(int *restrict base, int num)
{
if (ptr == NULL || num <= 0)
if (base == NULL || num <= 0)
return;
for (int i = 0; i < num; ++i)
ptr[i] = i + 1;
base[i] = i + 1;
for (int i = 0; i < num; ++i)
swap(ptr + i, ptr + rand() % num);
swap(base + i, base + rand() % num);
}
#define N 10
......
......@@ -74,12 +74,12 @@ int main()
//FILE *input = redirectInput(INPUT);
//if (input == NULL)
// return 1;
// return -1;
FILE *output = redirectOutput(OUTPUT);
if (output == NULL)
{
//fclose(input);
return 2;
return -2;
}
// 输出至文件
......@@ -90,10 +90,10 @@ int main()
//if (resetInput() == NULL)
//{
// fclose(output);
// return 3;
// return -3;
//}
if (resetOutput() == NULL)
return 4;
return -4;
// 输出至窗口
puts("freedom and justice");
......
#include <stddef.h>
#include <stdio.h>
#if defined(_WIN32)
#include <io.h>
#elif defined(linux) || defined(__linux) || defined(__linux__)
......@@ -8,6 +6,9 @@
#error "Currently only supports Windows and Linux!"
#endif
#include <stddef.h>
#include <stdio.h>
#if defined(_WIN32) && defined(_MSC_VER)
#define restrict __restrict
#endif
......@@ -31,7 +32,7 @@ FILE *redirectOutput(const char *restrict path)
}
const char * const INPUT = "test.in";
const char * const OUTPUT = "E:/test.out";
const char * const OUTPUT = "test.out";
int main()
{
......@@ -41,25 +42,25 @@ int main()
//if (old_in < 0)
//{
// perror("输入文件描述符拷贝失败");
// return 1;
// return -1;
//}
int std_out = fileno(stdout);
int old_out = dup(std_out);
if (old_out < 0)
{
perror("输出文件描述符拷贝失败");
return 2;
return -2;
}
// 输入输出重定向
//FILE *input = redirectInput(INPUT);
//if (input == NULL)
// return 3;
// return -3;
FILE *output = redirectOutput(OUTPUT);
if (output == NULL)
{
//fclose(input);
return 4;
return -4;
}
// 输出至文件
......@@ -71,12 +72,12 @@ int main()
//{
// perror("默认输入恢复失败");
// fclose(output);
// return 5;
// return -5;
//}
if (dup2(old_out, std_out) < 0)
{
perror("默认输出恢复失败");
return 6;
return -6;
}
// 输出至窗口
......
#include <time.h>
#include <stddef.h>
#include <stdio.h>
#include <time.h>
char *weekday(unsigned day)
{
......
#if !defined(linux) && !defined(__linux) && !defined(__linux__)
#error "Support for Linux!"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Usage: stty [-F DEVICE | --file=DEVICE] [SETTING]...
......
#if !defined(linux) && !defined(__linux) && !defined(__linux__)
#error "Support for Linux!"
#endif
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <unistd.h>
static void alterAttr(struct termios *restrict attribute, int echo)
{
......
#include <string.h>
#include <stdio.h>
#if defined(_WIN32)
#if defined(_WIN32)
#include <conio.h>
#elif defined(linux) || defined(__linux) || defined(__linux__)
#include "conio.h"
#endif
#include <stdio.h>
#include <string.h>
const char * const PASSWORD = "universe";
int main()
......
......@@ -15,16 +15,18 @@ void *bsearch(
size_t num, \
size_t size, \
int(*comp)(const void*, const void*, void*), \
void *context)
void *ctx)
{
if (key == NULL || base == NULL || num*size == 0 || comp == NULL)
if (key == NULL || base == NULL \
|| num == 0 || size == 0 || comp == NULL)
return NULL;
size_t low = 0, high = num - 1, mid;
while (low <= high)
{
mid = low + (high - low >> 1);
int ret = comp(key, address(base, mid, size), context);
int ret = comp(key, \
address(base, mid, size), ctx);
if (ret < 0)
{
if (mid == 0)
......@@ -39,7 +41,10 @@ void *bsearch(
return NULL;
}
int compare(const void *left, const void *right, void *context)
int compare(
const void *left, \
const void *right, \
void *context)
{
int key = *(const int*)left;
int value = *(const int*)right;
......@@ -63,7 +68,8 @@ int main()
printf("输入查找数:");
scanf("%d", &key);
if (bsearch(&key, array, N, sizeof(int), compare, NULL))
if (bsearch(&key, \
array, N, sizeof(int), compare, NULL))
puts("存在查找数");
else
puts("不存在查找数");
......
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32) && defined(_MSC_VER)
#define restrict __restrict
#endif
void *swap(void *restrict left, void *restrict right, size_t size, void *context)
#define ERROR_MEM_ALLOC -1
errno_t swap(
void *restrict left, \
void *restrict right, \
size_t size, \
void **context)
{
if (left == NULL || right == NULL || size == 0)
if (left == NULL \
|| right == NULL || size == 0)
{
free(context);
return NULL;
if (context)
{
free(*context);
*context = NULL;
}
return 0;
}
if (left == right)
return context;
return 0;
if (context && *(size_t*)context < size)
void *buffer;
if (context)
{
free(context);
context = NULL;
}
if (context == NULL)
{
context = malloc(sizeof(size_t) + size);
*(size_t*)context = size;
void *temp = *context;
if (temp && *(size_t*)temp < size)
{
free(temp);
*context = temp = NULL;
}
if (temp == NULL)
{
*context = temp = malloc(sizeof(size_t) + size);
if (temp == NULL)
return ERROR_MEM_ALLOC;
*(size_t*)temp = size;
}
buffer = (void*)((size_t)temp + sizeof(size_t));
}
else
buffer = malloc(size);
void *buffer = (void*)((size_t)context + sizeof(size_t));
memcpy(buffer, left, size);
memcpy(left, right, size);
memcpy(right, buffer, size);
return context;
if (context == NULL)
free(buffer);
return 0;
}
int main()
{
int a = 9, b = 10;
printf("֮ǰ\na=%d,b=%d\n", a, b);
void *context = swap(&a, &b, sizeof(int), NULL);
swap(&a, &b, sizeof(int), NULL);
printf(\na=%d,b=%d\n", a, b);
swap(NULL, NULL, 0, context);
return 0;
}
#include <stdbool.h>
#include "gtsort.h"
void sort(
#include <stdbool.h>
errno_t sort(
void *base, \
size_t num, \
size_t size, \
int(*comp)(const void*, const void*, void*), \
void *ctx)
{
if (base == NULL || num*size == 0 || comp == NULL)
return;
if (base == NULL || num == 0 \
|| size == 0 || comp == NULL)
return 0;
void *context = NULL;
void *left, *right;
bool swapped = true; // 内层循环交换元素标记
/*
* 如果内层循环未交换元素,表明元素已经有序,
* 结束外层循环,减少循环和比较次数
*/
bool swapped = true; // 内层循环交换元素标记
void *left, *right;
void *context = NULL;
errno_t error;
for (size_t i = num - 1, j; swapped && i > 0; --i)
{
swapped = false;
......@@ -26,9 +29,12 @@ void sort(
if (comp(left = address(base, j, size), \
right = address(base, j + 1, size), ctx) > 0)
{
context = swap(left, right, size, context);
error = swap(left, right, size, &context);
if (error != 0)
return error;
swapped = true;
}
}
swap(NULL, NULL, 0, context);
swap(NULL, NULL, 0, &context);
return 0;
}
#ifndef GTSORT_H
#define GTSORT_H
#include <errno.h>
#include <stddef.h>
#define ERROR_MEM_ALLOC -1
#if defined(_WIN32) && defined(_MSC_VER)
#define restrict __restrict
#endif
void *swap(
errno_t swap(
void *restrict left, \
void *restrict right, \
size_t size, \
void *context);
void **context);
static inline void *address(
void *base, \
......@@ -21,7 +24,7 @@ static inline void *address(
return (void*)((size_t)base + num*size);
}
void sort(
errno_t sort(
void *base, \
size_t num, \
size_t size, \
......
#include <string.h>
#include "gtsort.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "gtsort.h"
void sort(
errno_t sort(
void *base, \
size_t num, \
size_t size, \
int(*comp)(const void*, const void*, void*), \
void *ctx)
{
if (base == NULL || num*size == 0 || comp == NULL)
return;
if (base == NULL || num == 0 \
|| size == 0 || comp == NULL)
return 0;
void *key = malloc(size);
if (key == NULL)
return ERROR_MEM_ALLOC;
for (size_t i, j = 1; j < num; ++j)
{
memcpy(key, address(base, j, size), size);
......@@ -38,4 +42,5 @@ void sort(
}
}
free(key);
return 0;
}
#include <string.h>
#include "gtsort.h"
#include <stdbool.h>
#include <stdlib.h>
#include "gtsort.h"
#include <string.h>
static size_t middle(
void *base, \
......@@ -12,7 +12,7 @@ static size_t middle(
int(*comp)(const void*, const void*, void*), \
void *ctx)
{
size_t mid = (low + high) / 2, min, max[2];
size_t mid = low + (high - low >> 1), min, max[2];
if (comp(address(base, low, size), \
address(base, high, size), ctx) < 0)
{
......@@ -35,7 +35,7 @@ static size_t middle(
address(base, max[1], size), ctx) < 0 ? max[0] : max[1];
}
static size_t partition(
static errno_t partition(
void *base, \
size_t low, \
size_t high, \
......@@ -43,7 +43,8 @@ static size_t partition(
int(*comp)(const void*, const void*, void*), \
void *ctx, \
void *key, \
void **context)
void **sctx, \
size_t *axis)
{
/*
* 于左中右三处选择次大的元素作为中轴,
......@@ -51,12 +52,15 @@ static size_t partition(
*/
size_t mid = middle(base, low, high, size, comp, ctx);
memcpy(key, address(base, mid, size), size);
memcpy(address(base, mid, size), \
address(base, low, size), size);
memcpy(address(base, low, size), key, size);
if (mid != low)
{
memcpy(address(base, mid, size), \
address(base, low, size), size);
memcpy(address(base, low, size), key, size);
}
size_t left = low;
size_t right = high;
size_t left = low, right = high;
errno_t error;
while (true)
{
/*
......@@ -78,16 +82,22 @@ static size_t partition(
if (left >= right) break;
// 交换左右无序元素,使之成为有序元素
*context = swap(address(base, left, size), \
address(base, right--, size), size, *context);
error = swap(address(base, left, size), \
address(base, right--, size), size, sctx);
if (error != 0)
return error;
}
// 一次划分之后,将中轴归位
*context = swap(address(base, low, size), \
address(base, right, size), size, *context);
return right;
error = swap(address(base, low, size), \
address(base, right, size), size, sctx);
if (error != 0)
return error;
*axis = right;
return 0;
}
static void recursion(
static errno_t recursion(
void *base, \
size_t low, \
size_t high, \
......@@ -95,41 +105,64 @@ static void recursion(
int(*comp)(const void*, const void*, void*), \
void *ctx, \
void *key, \
void **context)
void **sctx)
{
/*
* 如果左边索引大于或者等于右边索引,
* 意味着已经完成一组划分排序,结束本次递归调用
*/
if (low >= high)
return;
return 0;
// 进行一次划分,寻找中轴位置
size_t axis = partition(base, low, high, \
size, comp, ctx, key, context);
size_t axis;
errno_t error = partition(base, low, high, \
size, comp, ctx, key, sctx, &axis);
if (error != 0)
return error;
// 中轴左边仍有元素之时,进行递归调用,划分左边元素
if (axis > 0)
recursion(base, low, axis - 1, \
size, comp, ctx, key, context);
{
error = recursion(base, low, axis - 1, \
size, comp, ctx, key, sctx);
if (error != 0)
return error;
}
// 进行递归调用,划分右边元素
recursion(base, axis + 1, high, \
size, comp, ctx, key, context);
error = recursion(base, axis + 1, high, \
size, comp, ctx, key, sctx);
if (error != 0)
return error;
return 0;
}
void sort(
errno_t sort(
void *base, \
size_t num, \
size_t size, \
int(*comp)(const void*, const void*, void*), \
void *ctx)
{
if (base == NULL || num*size == 0 || comp == NULL)
return;
if (base == NULL || num == 0 \
|| size == 0 || comp == NULL)
return 0;
void *key = malloc(size);
void *context = NULL;
recursion(base, 0, num - 1, \
size, comp, ctx, key, &context);
swap(NULL, NULL, 0, context);
if (key == NULL)
return ERROR_MEM_ALLOC;
void *sctx = NULL;
errno_t error = recursion(base, 0, num - 1, \
size, comp, ctx, key, &sctx);
if (error != 0)
{
free(key);
return error;
}
swap(NULL, NULL, 0, &sctx);
free(key);
return 0;
}
#include "gtsort.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
static size_t middle(
void *base, \
size_t low, \
size_t high, \
size_t size, \
int(*comp)(const void*, const void*, void*), \
void *ctx)
{
size_t mid = low + (high - low >> 1), min, max[2];
if (comp(address(base, low, size), \
address(base, high, size), ctx) < 0)
{
min = low;
max[0] = high;
}
else
{
min = high;
max[0] = low;
}
if (mid == low)
return min;
max[1] = comp(address(base, min, size), \
address(base, mid, size), ctx) < 0 ? mid : min;
return comp(address(base, max[0], size), \
address(base, max[1], size), ctx) < 0 ? max[0] : max[1];
}
static errno_t partition(
void *base, \
size_t num, \
size_t size, \
int(*comp)(const void*, const void*, void*), \
void *ctx, \
size_t *axis)
{
size_t low = 0, high = num - 1;
void *key = malloc(size);
if (key == NULL)
return ERROR_MEM_ALLOC;
/*
* 于左中右三处选择次大的元素作为中轴,
* 并且与最左边元素交换位置
*/
size_t mid = middle(base, low, high, size, comp, ctx);
memcpy(key, address(base, mid, size), size);
if (mid != low)
{
memcpy(address(base, mid, size), \
address(base, low, size), size);
memcpy(address(base, low, size), key, size);
}
size_t left = low, right = high;
void *sctx = NULL;
errno_t error;
while (true)
{
/*
* 从左往右遍历,在遍历未完的前提之下,
* 寻找一个不小于中轴的元素,即左无序元素
*/
while (left < high \
&& comp(address(base, ++left, size), key, ctx) < 0);
/*
* 从右往左遍历,在遍历未完的前提之下,
* 寻找一个不大于中轴的元素,即右无序元素
*/
while (right > low \
&& comp(address(base, right, size), key, ctx) > 0)
--right;
// 两边遍历重叠之时退出循环
if (left >= right) break;
// 交换左右无序元素,使之成为有序元素
error = swap(address(base, left, size), \
address(base, right--, size), size, &sctx);
if (error != 0)
{
free(key);
return error;
}
}
// 一次划分之后,将中轴归位
error = swap(address(base, low, size), \
address(base, right, size), size, &sctx);
if (error != 0)
{
free(key);
return error;
}
swap(NULL, NULL, 0, &sctx);
free(key);
*axis = right;
return 0;
}
static errno_t recursion(
void *base, \
size_t num, \
size_t size, \
int(*comp)(const void*, const void*, void*), \
void *ctx)
{
/*
* 若元素数量不足两个,无需再次划分,结束本次递归调用
*/
if (num < 2)
return 0;
// 进行一次划分,寻找中轴位置
size_t axis;
errno_t error = partition(base, num, size, comp, ctx, &axis);
if (error != 0)
return error;
// 中轴左边仍有元素之时,进行递归调用,划分左边元素
if (axis > 0)
{
error = recursion(base, axis, size, comp, ctx);
if (error != 0)
return error;
}
// 进行递归调用,划分右边元素
error = recursion(address(base, axis + 1, size), \
num - axis - 1, size, comp, ctx);
if (error != 0)
return error;
return 0;
}
errno_t sort(
void *base, \
size_t num, \
size_t size, \
int(*comp)(const void*, const void*, void*), \
void *ctx)
{
if (base == NULL || num == 0 \
|| size == 0 || comp == NULL)
return 0;
return recursion(base, num, size, comp, ctx);
}
#include "gtsort.h"
void sort(
errno_t sort(
void *base, \
size_t num, \
size_t size, \
int(*comp)(const void*, const void*, void*), \
void *ctx)
{
if (base == NULL || num*size == 0 || comp == NULL)
return;
if (base == NULL || num == 0 \
|| size == 0 || comp == NULL)
return 0;
void *context = NULL;
errno_t error;
for (size_t i = 0, j, k, counter = num - 1; i < counter; ++i)
{
for (j = i + 1, k = i; j < num; ++j)
......@@ -18,8 +20,11 @@ void sort(
address(base, j, size), ctx) > 0)
k = j;
context = swap(address(base, i, size), \
address(base, k, size), size, context);
error = swap(address(base, i, size), \
address(base, k, size), size, &context);
if (error != 0)
return error;
}
swap(NULL, NULL, 0, context);
swap(NULL, NULL, 0, &context);
return 0;
}
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include "gtsort.h"
void *swap(
#include <stdlib.h>
#include <string.h>
errno_t swap(
void *restrict left, \
void *restrict right, \
size_t size, \
void *context)
void **context)
{
if (left == NULL || right == NULL || size == 0)
if (left == NULL \
|| right == NULL || size == 0)
{
free(context);
return NULL;
if (context)
{
free(*context);
*context = NULL;
}
return 0;
}
if (left == right)
return context;
return 0;
if (context && *(size_t*)context < size)
void *buffer;
if (context)
{
free(context);
context = NULL;
}
if (context == NULL)
{
context = malloc(sizeof(size_t) + size);
*(size_t*)context = size;
void *temp = *context;
if (temp && *(size_t*)temp < size)
{
free(temp);
*context = temp = NULL;
}
if (temp == NULL)
{
*context = temp = malloc(sizeof(size_t) + size);
if (temp == NULL)
return ERROR_MEM_ALLOC;
*(size_t*)temp = size;
}
buffer = (void*)((size_t)temp + sizeof(size_t));
}
else
buffer = malloc(size);
void *buffer = (void*)((size_t)context + sizeof(size_t));
memcpy(buffer, left, size);
memcpy(left, right, size);
memcpy(right, buffer, size);
return context;
if (context == NULL)
free(buffer);
return 0;
}
#include <stdio.h>
#include "gtsort.h"
#include "gtsort.h"
#include <stdio.h>
int compare(const void *left, const void *right, void *context)
{
......
......@@ -16,7 +16,7 @@ unsigned greatestCommonDivisor(unsigned a, unsigned b)
// 最小公倍数
unsigned leastCommonMultiple(unsigned a, unsigned b)
{
return a * b / greatestCommonDivisor(a, b);
return a / greatestCommonDivisor(a, b) * b;
}
int main()
......
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32) && defined(_MSC_VER)
#define restrict __restrict
#endif
void *createMatrix(size_t row, size_t column, size_t size)
// 范围有限,溢出隐患
void *createMatrix(
size_t row, \
size_t column, \
size_t size)
{
size_t total = row*column*size;
if (total == 0)
return NULL;
return malloc(total);
return malloc(row*column*size);
}
void destroyMatrix(void **restrict matrix)
......@@ -31,15 +32,24 @@ static inline void *address(
size_t columns, \
size_t size)
{
return (void*)((size_t)pointer + (row*columns + column)*size);
return (void*)((size_t)pointer \
+ (row*columns + column)*size);
}
void* transpose(const void *restrict matrix, size_t row, size_t column, size_t size)
void *transpose(
const void *restrict matrix, \
size_t row, \
size_t column, \
size_t size)
{
if (matrix == NULL || row*column*size == 0)
if (matrix == NULL || row == 0 \
|| column == 0 || size == 0)
return NULL;
void *pointer = createMatrix(column, row, size);
if (pointer == NULL)
return pointer;
for (size_t i = 0, j; i < column; ++i)
for (j = 0; j < row; ++j)
memcpy(address(pointer, i, j, row, size), \
......@@ -47,9 +57,15 @@ void* transpose(const void *restrict matrix, size_t row, size_t column, size_t s
return pointer;
}
void foreach(void *matrix, size_t row, size_t column, size_t size, void(*functor)(void*))
void foreach(
void *matrix, \
size_t row, \
size_t column, \
size_t size, \
void(*functor)(void*))
{
if (matrix == NULL || size == 0 || functor == NULL)
if (matrix == NULL \
|| size == 0 || functor == NULL)
return;
for (size_t i = 0, j; i < row; ++i)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册