目录
一、实现题目
二、send模块
三、receive模块
四、运行截图
一、实现题目
二、send模块
#include <stdio.h>
#include <string.h>// 执行模2除法,并计算出余数(CRC校验码)
//dividend被除, divisor除数
void divide(char *dividend, const char *divisor, char *remainder) {int len_divisor = strlen(divisor);int len_dividend = strlen(dividend);strncpy(remainder, dividend, len_divisor); //将dividend前四位赋值给remainderremainder[len_divisor] = '\0';// 与运算 for (int i = 0; i <= len_dividend - len_divisor; ++i) {if (remainder[0] == '1') {for (int j = 1; j < len_divisor; ++j) {remainder[j] = ((remainder[j] == divisor[j]) ? '0' : '1');}}// 将余数左移 1 位并降低被除数的下一位memmove(remainder, remainder + 1, len_divisor - 1);remainder[len_divisor - 1] = dividend[i + len_divisor];remainder[len_divisor] = '\0';}
}int main() {char original_message[1024]; // 原始信息char generator[1024]; // 生成多项式G(x)printf("请输入待发送的信息: ");scanf("%s", original_message);printf("请输入生成多项式: ");scanf("%s", generator);int message_len = strlen(original_message);int generator_len = strlen(generator);// 扩展信息长度以放置CRC校验码char extended_message[message_len + generator_len];strcpy(extended_message, original_message); //将extended_message=original_messagememset(extended_message + message_len, '0', generator_len - 1);//扩充三个零(针对题目来说)extended_message[message_len + generator_len - 1] = '\0';//字符串以/0结尾char crc[generator_len];// crc余数的长度divide(extended_message, generator, crc);// 将CRC校验码附加到原始信息后面char final_message[message_len + generator_len];strcpy(final_message, original_message);strcat(final_message, crc);printf("待发送的信息: %s\n", original_message);printf("生成多项式: %s\n", generator);printf("CRC校验码: %s\n", crc);printf("要发送的信息: %s\n", final_message);// 打开文件并写入要发送的信息FILE *file = fopen("D:\\上机程序\\计网\\CRC\\CRCcode.txt", "w");if (file == NULL) {perror("无法打开文件");return 1;}fprintf(file, "%s", final_message);fclose(file);printf("信息已经写入到 D:\\上机程序\\计网\\CRC\\CRCcode.txt\n");return 0;
}
三、receive模块
#include <stdio.h>
#include <stdlib.h>
#include <string.h>void divide(char *dividend, const char *divisor, char *remainder) {int len_divisor = strlen(divisor);int len_dividend = strlen(dividend);strncpy(remainder, dividend, len_divisor);remainder[len_divisor] = '\0';for (int i = 0; i <= len_dividend - len_divisor; ++i) {if (remainder[0] == '1') {for (int j = 1; j < len_divisor; ++j) {remainder[j] = ((remainder[j] == divisor[j]) ? '0' : '1');}}memmove(remainder, remainder + 1, len_divisor - 1);remainder[len_divisor - 1] = i + len_divisor < len_dividend ? dividend[i + len_divisor] : '0';remainder[len_divisor] = '\0';}
}int main() {const char *filepath = "D:\\Z上机程序\\计网\\CRC\\CRCcode.txt";char received_message[1024];char generator[1024];FILE *file = fopen(filepath, "r");if (file == NULL) {perror("无法打开文件");return 1;}printf("请输入生成多项式: ");scanf("%s", generator);if (fgets(received_message, sizeof(received_message), file) == NULL) {perror("读取文件失败");fclose(file);return 1;}fclose(file);printf("接收到的信息:%s",received_message);int generator_len = strlen(generator);char crc[generator_len];divide(received_message, generator, crc);// 检查CRC校验码是否为0int error_found = 0;for (int i = 0; i < generator_len - 1; ++i) {if (crc[i] != '0') {error_found = 1;break;}}if (error_found) {printf("检测到误码。\n");} else {printf("未检测到误码。\n");}printf("产生的余数(CRC校验码): %s\n", crc);return 0;
}
四、运行截图