每位老师的上机作业可能不一样ヾ(@⌒ー⌒@)ノ~
主要给大家一个参考作用,还是自己搞懂最好~
下面的代码有些也参考了其他博主的,侵删~
1、自底向上合并排序(输入10个数字,空格间隔)
#include <stdio.h>
#include <stdlib.h>
void Merge(int A[],int p,int q,int r)
{
int B[r+1];
int s = p, t = q + 1, k = p;
while((s<=q)&&(t<=r)){
if(A[s]<=A[t]){
B[k] = A[s];
s++;
}
else{
B[k] = A[t];
t++;
}
k++;
}
if(s==q+1)
while(t<=r){
B[k] = A[t];
k++;
t++;
}
else
while(s<=q){
B[k] = A[s];
k++;
s++;
}
while(p<=r){
A[p] = B[p];
p++;
}
}
void BottmoUpSort(int A[],int n)
{
int t = 1, s, i;
while(t<n){
s = t;
t = 2 * s;
i = 0;
while(i+t<=n){
Merge(A,i,i+s-1,i+t-1);
i = i + t;
}
if(i+s-1<n) Merge(A,i,i+s-1,n);
}
}
int main()
{
int A[10];
for(int i=0;i<10;i++){
scanf("%d",&A[i]);}
BottmoUpSort(A,9);
for(int i=0;i<10;i++){
printf("%d " ,A[i]);}
return 0;
}
2、快速排序
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int A[] = {23, 32, 27, 18, 45, 11, 22, 63, 12, 19, 16, 25, 52, 14, 18};
int n = sizeof(A) / sizeof(A[0]);
printf("未排序数组:\n");
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");
quickSort(A, 0, n - 1);
printf("排序后的数组:\n");
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");
return 0;
}
3、LSC
#include<stdio.h>
#include<string.h>
int c[200][200]; //用c[i][j]记录X[i]与Y[j] 的LCS 的长度
int b[200][200]; //b[i][j]记录c[i][j]是通过哪一个子问题的值求得的,以决定搜索的方向
char f[200];
/*-----------------------分割线--------------------------------*/
/*取c[i-1][j]和c[i][j-1]的最大值,并记录c[i][j]是通过哪一个子问题的值求得的,以决定搜索的方向*/
int Max(int m,int n,int i,int j)
{
if(m > n)
{
b[i][j] = -1;
return m;
}
else
{
b[i][j] = 1;
return n;
}
}
/*-----------------------分割线--------------------------------*/
/*递归打印LCS的元素内容*/
void print(int i,int j,int s,char x[],char y[])
{
if(b[i][j] == 0)
{
f[s-1] = x[i-1];
i--;j--;s--;
print(i,j,s,x,y);
}
else if(b[i][j] == -1)
{
i--;
print(i,j,s,x,y);
}
else if(b[i][j] == 1)
{
j--;
print(i,j,s,x,y);
}
}
/*-----------------------分割线--------------------------------*/
int LCS(char x[],char y[])
{
int i,j;
int x_len,y_len;
x_len = strlen(x);
y_len = strlen(y);
printf(" ");
for(i = 0;i < y_len;i++)
{
printf("%c ",y[i]);
}
printf("\n");
for(i = 1;i <= x_len;i++)
{
printf("%c ",x[i-1]);
for(j = 1;j <= y_len;j++)
{
if(x[i-1] == y[j-1])
{
c[i][j] = c[i-1][j-1] +1;
b[i][j] = 0;
printf("%d ",c[i][j]);
}
else
{
c[i][j] = Max(c[i-1][j],c[i][j-1],i,j);
printf("%d ",c[i][j]);
}
}
printf("\n");
}
/*-------------------------分割线---------------------------------------*/
//打印X和Y的LCS
printf("X和Y的LCS是:");
print(x_len,y_len,c[x_len][y_len],x,y);
printf("%s",f);
printf("\n");
return c[x_len][y_len];
}
/*------------------------------分割线----------------------------------------*/
void main()
{
char X[200],Y[200];
int i,j,s;
printf("请输入字符串X:");
scanf("%s",X);
printf("请输入字符串Y:");
scanf("%s",Y);
s = LCS(X,Y);
printf("X和Y的LCS: %d \n",s);
}
4、文档压缩(哈夫曼)
需要在当前文件夹下先自己创建一个名为original.txt的文档,里面随便写一些英文。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_SIZE 100
#define MAX_BIT 256
typedef struct HuffmanNode {
char character;
long frequency;
char code[MAX_BIT];
struct HuffmanNode* left;
struct HuffmanNode* right;
struct HuffmanNode* parent;
} HuffmanNode, *HuffmanTree;
typedef struct Dictionary {
HuffmanTree charNodes[128];
} Dictionary;
typedef struct HeapStruct {
HuffmanTree* elements;
int size;
int capacity;
} HeapStruct, *Heap;
HuffmanTree createHuffmanNode();
HuffmanTree createHuffmanTree();
Heap createHeap(int maxSize);
void traverseHeap(Heap heap);
void insertMinHeap(Heap heap, HuffmanTree data);
HuffmanTree deleteMinHeap(Heap heap);
int countCharFrequency(FILE* file);
void generateHuffmanCode(HuffmanTree root);
void encode(FILE* source, FILE* output);
void decode(FILE* source, FILE* output);
Dictionary dictionary;
int main() {
float startTime, endTime;
FILE* originalFile = fopen("original.txt", "rb");
FILE* compressedFile = fopen("compressed.txt", "wb");
if (!(originalFile && compressedFile)) {
printf("Failed to open files!\n");
} else {
startTime = clock();
encode(originalFile, compressedFile);
endTime = clock();
printf("Program execution time: %fs\n\n", (endTime - startTime) / (double)CLOCKS_PER_SEC);
}
fclose(originalFile);
fclose(compressedFile);
return 0;
}
void encode(FILE* source, FILE* output) {
int i, j;
int charTypes = 0;
int textLength = countCharFrequency(source);
HuffmanTree root = createHuffmanTree();
generateHuffmanCode(root);
printf(" *------------> Huffman Code Table <-----------*\n");
printf(" ____________________________________________\n character | frequency | code\n");
printf(" ____________|_____________|_________________\n");
for (i = 0; i < 128; i++)
if (dictionary.charNodes[i]->frequency > 0)
{
if (dictionary.charNodes[i]->character == 10) // 换行符
printf(" \\n\t|%8ld | %s\n", dictionary.charNodes[i]->frequency, dictionary.charNodes[i]->code);
else if (dictionary.charNodes[i]->character == 0) // 结束符
printf(" \\0\t|%8ld | %s\n", dictionary.charNodes[i]->frequency, dictionary.charNodes[i]->code);
else if (dictionary.charNodes[i]->character == 9) // 制表符
printf(" \\t\t|%8ld | %s\n", dictionary.charNodes[i]->frequency, dictionary.charNodes[i]->code);
else if (dictionary.charNodes[i]->character == 13) // 回车
printf(" \\r\t|%8ld | %s\n", dictionary.charNodes[i]->frequency, dictionary.charNodes[i]->code);
else if (dictionary.charNodes[i]->character == 32) // 空格
printf(" space |%8ld | %s\n", dictionary.charNodes[i]->frequency, dictionary.charNodes[i]->code);
else
printf(" %c\t|%8ld | %s\n", dictionary.charNodes[i]->character, dictionary.charNodes[i]->frequency, dictionary.charNodes[i]->code);
charTypes++;
}
printf(" _____________|_____________|_________________\n\n");
fseek(source, 0, SEEK_SET);
fseek(output, 12, SEEK_SET);
int compressedFileLength = 12;
char tempCode[MAX_BIT] = { 0 };
int tempCodeLength = 0;
char ch = fgetc(source);
while (ch != EOF) {
strcat(tempCode, dictionary.charNodes[ch]->code);
tempCodeLength = strlen(tempCode);
ch = 0;
while (tempCodeLength >= 8) {
for (i = 0; i < 8; i++) {
if (tempCode[i] == '1') {
ch = ch << 1;
ch = ch + 1;
} else {
ch = ch << 1;
}
}
fwrite(&ch, sizeof(char), 1, output);
compressedFileLength++;
strcpy(tempCode, tempCode + 8);
tempCodeLength = strlen(tempCode);
}
ch = fgetc(source);
}
if (tempCodeLength > 0) {
ch = 0;
strcat(tempCode, "00000000");
for (i = 0; i < 8; i++) {
if (tempCode[i] == '1') {
ch = ch << 1;
ch = ch + 1;
} else {
ch = ch << 1;
}
}
fwrite(&ch, sizeof(char), 1, output);
compressedFileLength++;
}
fseek(output, 0, SEEK_SET);
fwrite(&textLength, sizeof(int), 1, output);
fwrite(&compressedFileLength, sizeof(int), 1, output);
fwrite(&charTypes, sizeof(int), 1, output);
fseek(output, compressedFileLength, SEEK_SET);
HuffmanTree tempNode;
char codeLengthBit;
for (i = 0; i < 128; i++) {
if (dictionary.charNodes[i]->frequency > 0) {
tempNode = dictionary.charNodes[i];
tempCodeLength = strlen(tempNode->code);
fwrite(&(tempNode->character), 1, 1, output);
compressedFileLength++;
codeLengthBit = tempCodeLength;
fwrite(&codeLengthBit, 1, 1, output);
compressedFileLength++;
while (tempCodeLength % 8 != 0) {
strcat(tempNode->code, "0");
tempCodeLength = strlen(tempNode->code);
}
while (tempNode->code[0] != 0) {
ch = 0;
for (j = 0; j < 8; j++) {
if (tempNode->code[j] == '1') {
ch = ch << 1;
ch += 1;
} else {
ch = ch << 1;
}
}
strcpy(tempNode->code, tempNode->code + 8);
fwrite(&ch, 1, 1, output);
compressedFileLength++;
}
}
}
printf("- original filename: original.txt\n- file length: %d bytes\n", textLength);
printf("- compressed filename: compressed.txt\n- file length: %d bytes\n", compressedFileLength);
printf("\nThe compression has finished! Compression ratio: %.2f%%\n", (float)compressedFileLength / (float)textLength * 100);
}
int countCharFrequency(FILE* file) {
int i;
long length = 0;
char ch;
for (i = 0; i < 128; i++) {
dictionary.charNodes[i] = createHuffmanNode();
}
fseek(file, 0, SEEK_SET);
ch = fgetc(file);
while (ch != EOF) {
length++;
dictionary.charNodes[ch]->character = ch;
dictionary.charNodes[ch]->frequency++;
ch = fgetc(file);
}
return length;
}
void generateHuffmanCode(HuffmanTree root) {
int i, j;
int count = 0;
char tempCode[MAX_BIT];
HuffmanTree currentNode = NULL;
for (i = 0; i < 128; i++) {
if (dictionary.charNodes[i]->frequency > 0) {
currentNode = dictionary.charNodes[i];
while (currentNode->parent) {
if (currentNode->parent->left == currentNode) {
tempCode[count] = '0';
} else {
tempCode[count] = '1';
}
count++;
currentNode = currentNode->parent;
}
for (j = 0; j < count; j++) {
dictionary.charNodes[i]->code[j] = tempCode[count - j - 1];
}
dictionary.charNodes[i]->code[j] = '\0';
count = 0;
}
}
}
HuffmanTree createHuffmanNode() {
HuffmanTree node = (HuffmanTree)malloc(sizeof(HuffmanNode));
node->character = 0;
node->frequency = 0;
node->left = NULL;
node->right = NULL;
node->parent = NULL;
return node;
}
HuffmanTree createHuffmanTree() {
int i;
Heap heap = createHeap(MAX_SIZE);
for (i = 0; i < 128; i++) {
if (dictionary.charNodes[i]->frequency > 0) {
insertMinHeap(heap, dictionary.charNodes[i]);
}
}
while (heap->size > 1) {
HuffmanTree newNode = createHuffmanNode();
HuffmanTree left = deleteMinHeap(heap);
HuffmanTree right = deleteMinHeap(heap);
newNode->frequency = left->frequency + right->frequency;
newNode->left = left;
newNode->right = right;
left->parent = newNode;
right->parent = newNode;
insertMinHeap(heap, newNode);
}
HuffmanTree root = deleteMinHeap(heap);
return root;
}
Heap createHeap(int maxSize) {
Heap heap = (Heap)malloc(sizeof(HeapStruct));
heap->elements = (HuffmanTree*)malloc(sizeof(HuffmanTree) * (maxSize + 1));
heap->size = 0;
heap->capacity = maxSize;
return heap;
}
void traverseHeap(Heap heap) {
int i;
if (heap->size == 0) {
printf("Heap is empty!\n");
return;
}
for (i = 1; i <= heap->size; i++) {
printf("%d ", heap->elements[i]->frequency);
}
printf("\n");
}
void insertMinHeap(Heap heap, HuffmanTree data) {
int i;
if (heap->size == heap->capacity) {
printf("Min heap is full!\n");
return;
}
i = heap->size + 1;
heap->size++;
while (1) {
if (i <= 1) {
break;
}
if (!(heap->elements[i / 2]->frequency > data->frequency)) {
break;
}
heap->elements[i] = heap->elements[i / 2];
i /= 2;
}
heap->elements[i] = data;
}
HuffmanTree deleteMinHeap(Heap heap) {
int parent, child;
HuffmanTree min, temp;
if (heap->size == 0) {
printf("Heap is empty!\n");
return NULL;
}
min = heap->elements[1];
temp = heap->elements[heap->size];
heap->size--;
for (parent = 1; parent * 2 <= heap->size; parent = child) {
child = parent * 2;
if ((child != heap->size) && (heap->elements[child]->frequency > heap->elements[child + 1]->frequency)) {
child++;
}
if (temp->frequency <= heap->elements[child]->frequency) {
break;
} else {
heap->elements[parent] = heap->elements[child];
}
}
heap->elements[parent] = temp;
return min;
}