程序改写如下,已编译通过:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * f1 = fopen("dd.txt","r")/* 改为实际文件名 */
FILE * f2 = fopen("bb.txt","r")
char c1 = fgetc(f1)
char c2 = fgetc(f2)
while(!feof(f1) &&!feof(f2)){
if(c1 != c2){printf("NO"磨碰)system("pause")return 0}
c1 = fgetc(f1)
c2 = fgetc(f2)
}
if(c1==EOF&&c2==EOF) /* 判断两个文件是否都到瞎升谈结尾 */
printf("YES")
else
printf("NO")
printf("\n")
fclose(f1)
fclose(f2)
system("pause")
return 0
}
先读取第一个文件, 将所有人的名单存在一个字符串数组中。
然后读哗桥碰另一个文件,到第一个数组中去查找,如乱谈果文件没错消乱的话,那么每个都应该找到。
删掉找到的。
最后输出剩下的。
最长公共子序列方法比较两个文件的相似性。输入两个文件的名字,输出一个文件枣宏,不同的地方用红色标出。#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 = 0i <= len1i++) {
midLCS[i] = (int *)malloc(sizeof(int) * (len2 + 1))
}
for (i = 0i <猜庆= len1i++) {
midLCS[i][0] = 0
}
for (i = 0i <= len2i++) {
midLCS[0][i] = 0
}
for (i = 1i <= len1i++)
{
for (j = 1j <= len2j++)
{
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 = len2i >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 = 0i <= len1i++) {
free(midLCS[i])
}
free(midLCS)
return same
}
void show_compare(char *str, char *same, int len, FILE *fout)
{
int i, flag = 0
for (i = 0i <leni++)
{
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)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)