100分 c语言对比两个文件
要求用C语言读取两个文件(文件格式不限例如.txt,.c等),然后对比两个文件的不同(忽略字母大小写、空格、换行的处理),最后输出比较结果(完全相同语句提示或不同文件名及...
要求用C语言读取两个文件(文件格式不限例如.txt,.c等),然后对比两个文件的不同(忽略字母大小写、空格、换行的处理),最后输出比较结果(完全相同语句提示或不同文件名及其内容所在的行号)。谢谢了,希望给出注释,答好可追加分数。
十分感谢两位的回答,问题已解决 展开
十分感谢两位的回答,问题已解决 展开
展开全部
最长公共子序列方法比较两个文件的相似性。输入两个文件的名字,输出一个文件,不同的地方用红色标出。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max(x,y) (((x)>(y))?(x):(y))
int LCS(char *str1, char *same1, int len1, char *str2, char *same2, int len2)
{
int same, i, j;
int **midLCS;
midLCS = (int **)malloc(sizeof(int *) * (len1 + 1));
for (i = 0; i <= len1; i++) {
midLCS[i] = (int *)malloc(sizeof(int) * (len2 + 1));
}
for (i = 0; i <= len1; i++) {
midLCS[i][0] = 0;
}
for (i = 0; i <= len2; i++) {
midLCS[0][i] = 0;
}
for (i = 1; i <= len1; i++)
{
for (j = 1; j <= len2; j++)
{
if (str1[i - 1] == str2[j - 1]) {
midLCS[i][j] = midLCS[i - 1][j - 1] + 1;
}
else {
midLCS[i][j] = max(midLCS[i - 1][j], midLCS[i][j - 1]);
}
}
}
for (i = len1, j = len2; i > 0 && j > 0; )
{
if (str1[i - 1] == str2[j - 1]) {
same1[i - 1] = 1;
same2[j - 1] = 1;
--i;
--j;
}
else if (midLCS[i - 1][j] > midLCS[i][j - 1]) {
--i;
}
else {
--j;
}
}
same = midLCS[len1][len2];
for (i = 0; i <= len1; i++) {
free(midLCS[i]);
}
free(midLCS);
return same;
}
void show_compare(char *str, char *same, int len, FILE *fout)
{
int i, flag = 0;
for (i = 0; i < len; i++)
{
if (str[i] == '\n') {
fprintf(fout, " <br> ");
}
else if (str[i] == '\t') {
fprintf(fout, " ");
}
else {
if (same[i] == 1) {
if (flag == 1) {
fprintf(fout, " </font> ");
flag = 0;
}
fputc(str[i], fout);
}
else {
if (flag == 0) {
fprintf(fout, " <font color=red> ");
flag = 1;
}
fputc(str[i], fout);
}
}
}
if (flag == 1)
fprintf(fout, " </font> ");
}
int main()
{
int i, len1, len2;
FILE *fin1, *fin2, *fout;
char buf[1024];
char *str1, *str2, *same1, *same2;
printf("file1: ");
scanf("%s", buf);
fin1 = fopen(buf, "rb");
if (fin1 == NULL) {
printf("%s not exist!\n", buf);
return 0;
}
printf("file2: ");
scanf("%s", buf);
fin2 = fopen(buf, "rb");
if (fin2 == NULL) {
printf("%s not exist!\n", buf);
return 0;
}
fout = fopen("compare.html", "w+");
fseek(fin1, 0, SEEK_END);
len1 = (int)ftell(fin1);
fseek(fin1, 0, SEEK_SET);
fseek(fin2, 0, SEEK_END);
len2 = (int)ftell(fin2);
fseek(fin2, 0, SEEK_SET);
str1 = (char *)malloc(sizeof(char) * len1);
str2 = (char *)malloc(sizeof(char) * len2);
same1 = (char *)malloc(sizeof(char) * len1);
same2 = (char *)malloc(sizeof(char) * len2);
fread(str1, 1, len1, fin1);
fread(str2, 1, len2, fin2);
LCS(str1, same1, len1, str2, same2, len2);
show_compare(str1, same1, len1, fout);
fprintf(fout, " ----------------------------------------------------------------<br> ");
show_compare(str2, same2, len2, fout);
free(str1);
free(str2);
free(same1);
free(same2);
fclose(fin1);
fclose(fin2);
fclose(fout);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max(x,y) (((x)>(y))?(x):(y))
int LCS(char *str1, char *same1, int len1, char *str2, char *same2, int len2)
{
int same, i, j;
int **midLCS;
midLCS = (int **)malloc(sizeof(int *) * (len1 + 1));
for (i = 0; i <= len1; i++) {
midLCS[i] = (int *)malloc(sizeof(int) * (len2 + 1));
}
for (i = 0; i <= len1; i++) {
midLCS[i][0] = 0;
}
for (i = 0; i <= len2; i++) {
midLCS[0][i] = 0;
}
for (i = 1; i <= len1; i++)
{
for (j = 1; j <= len2; j++)
{
if (str1[i - 1] == str2[j - 1]) {
midLCS[i][j] = midLCS[i - 1][j - 1] + 1;
}
else {
midLCS[i][j] = max(midLCS[i - 1][j], midLCS[i][j - 1]);
}
}
}
for (i = len1, j = len2; i > 0 && j > 0; )
{
if (str1[i - 1] == str2[j - 1]) {
same1[i - 1] = 1;
same2[j - 1] = 1;
--i;
--j;
}
else if (midLCS[i - 1][j] > midLCS[i][j - 1]) {
--i;
}
else {
--j;
}
}
same = midLCS[len1][len2];
for (i = 0; i <= len1; i++) {
free(midLCS[i]);
}
free(midLCS);
return same;
}
void show_compare(char *str, char *same, int len, FILE *fout)
{
int i, flag = 0;
for (i = 0; i < len; i++)
{
if (str[i] == '\n') {
fprintf(fout, " <br> ");
}
else if (str[i] == '\t') {
fprintf(fout, " ");
}
else {
if (same[i] == 1) {
if (flag == 1) {
fprintf(fout, " </font> ");
flag = 0;
}
fputc(str[i], fout);
}
else {
if (flag == 0) {
fprintf(fout, " <font color=red> ");
flag = 1;
}
fputc(str[i], fout);
}
}
}
if (flag == 1)
fprintf(fout, " </font> ");
}
int main()
{
int i, len1, len2;
FILE *fin1, *fin2, *fout;
char buf[1024];
char *str1, *str2, *same1, *same2;
printf("file1: ");
scanf("%s", buf);
fin1 = fopen(buf, "rb");
if (fin1 == NULL) {
printf("%s not exist!\n", buf);
return 0;
}
printf("file2: ");
scanf("%s", buf);
fin2 = fopen(buf, "rb");
if (fin2 == NULL) {
printf("%s not exist!\n", buf);
return 0;
}
fout = fopen("compare.html", "w+");
fseek(fin1, 0, SEEK_END);
len1 = (int)ftell(fin1);
fseek(fin1, 0, SEEK_SET);
fseek(fin2, 0, SEEK_END);
len2 = (int)ftell(fin2);
fseek(fin2, 0, SEEK_SET);
str1 = (char *)malloc(sizeof(char) * len1);
str2 = (char *)malloc(sizeof(char) * len2);
same1 = (char *)malloc(sizeof(char) * len1);
same2 = (char *)malloc(sizeof(char) * len2);
fread(str1, 1, len1, fin1);
fread(str2, 1, len2, fin2);
LCS(str1, same1, len1, str2, same2, len2);
show_compare(str1, same1, len1, fout);
fprintf(fout, " ----------------------------------------------------------------<br> ");
show_compare(str2, same2, len2, fout);
free(str1);
free(str2);
free(same1);
free(same2);
fclose(fin1);
fclose(fin2);
fclose(fout);
}
展开全部
//奇怪啊,我试了是可以的啊~
//非常简单的程序,对比a.dat和b.dat是否相同
#include<stdio.h>
void main()
{
FILE *fp1,*fp2;
fp1 = fopen("a.dat","rb");
fp2 = fopen("b.dat","rb");
int n=0;
unsigned a,b;
while( (!feof(fp1)) && (!feof(fp2)) )
{
fread(&a,1,1,fp1);
fread(&b,1,1,fp2);
if(a != b)
{
n=1;
break;
}
}
if(n==1)
printf("no same!\n");
else
printf("same!\n");
fclose(fp1);
fclose(fp2);
}
//非常简单的程序,对比a.dat和b.dat是否相同
#include<stdio.h>
void main()
{
FILE *fp1,*fp2;
fp1 = fopen("a.dat","rb");
fp2 = fopen("b.dat","rb");
int n=0;
unsigned a,b;
while( (!feof(fp1)) && (!feof(fp2)) )
{
fread(&a,1,1,fp1);
fread(&b,1,1,fp2);
if(a != b)
{
n=1;
break;
}
}
if(n==1)
printf("no same!\n");
else
printf("same!\n");
fclose(fp1);
fclose(fp2);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询