计算机网络编程作业

编写一个CRC循环冗余校验码生成器。语言不限,界面清晰。具体内容请查看附件。


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
int main()
{
	int i, j, lenf, leng, k, h,len_ff1;
	char f[100], g[100], comm[10], yu[100] = {0}, ff1[110] = {0};
	while(printf("请输入命令:"),~scanf("%s", comm))//文件EOF结尾 
	{
		if(!strcmp(comm, "f1"))
		{
			printf("请输入要发送的数据比特序列:\n");
			scanf("%s", f);
			printf("请输入生成多项式:\n");
			scanf("%s", g);
			lenf = strlen(f);
			leng = strlen(g); 
			k = leng - 1;
			strcpy(ff1, f);		
			for(i = 0; i < k; i++)
				ff1[i+lenf] = '0';
			ff1[lenf+k] = '\0';
			len_ff1 = strlen(ff1);
			i = 0;
			while(len_ff1 - i >= leng)
			{
				for(j = 0; j < leng; j++, i++)
					ff1[i] = (ff1[i] ^ g[j]) + '0';//^为异或运算 
				for(h = 0; h < len_ff1; h++)
					if(ff1[h] != '0')
					{
						i = h;  
						break;
					} 
			}
			printf("生成的余数是:\n") ;
			for(i = h; i < len_ff1; i++)
				printf("%c",ff1[i]);
			printf("\n");
			printf("生成的接受端的数据比特序列:\n%s", f);
			for(i = h; i < len_ff1; i++)
				printf("%c",ff1[i]);
			printf("\n");
		}
		/*
		else 
		{
			printf("请输入接受段的数据比特序列:\n");
			scanf("%s", f);
			printf("请输入生成多项式:\n");
			scanf("%s", g);
			leng = strlen(g);
			lenf = strlen(f);
			k = leng - 1;
			strcpy(ff1, f);
			for(i = 0; i < k; i++)
				ff1[i+lenf] = '0';
			ff1[lenf+k] = '\0';
			i = 0; 
			while(len_ff1 - i >= leng)
			{
				for(j = 0; j < leng; j++, i++)
					ff1[i] = (ff1[i] ^ g[j]) + '0';
				for(h = 0; h < len_ff1; h++)
					if(ff1[h] != '0')
					{
						i = h;  
						break;
					} 
			}
			printf("生成的余数是:\n");
			printf("发送的数据比特序列是:\n");	
		}*/
		//f2 
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/u013780740/article/details/40429115
今日推荐