求大虾帮忙写个简单的数字文件比较程序(C++编写)
求牛人帮忙写一小程序,小弟从来没有接触过位比较的问题,实在是写不出来了~
要求:从两个TXT文件中读取数字(数字间有对应的空格和小数点),将数字按位计较,如发现不同的位,输出是第几位,计算比较的时间,并把第几位开始不同和程序的运行时间写到第三个个TXT文件中。
我朋友帮忙写了个JAVA的程序,代码如下,希望有所帮助。
import java.io.*;
import java.util.regex.*;
public class FileCompare {
public static void main(String[] args) {
//读取两个文件内容到字符数组中
char[] c1 = ReadFile.read("F://1.txt");
char[] c2 = ReadFile.read("F://2.txt");
int a = c1.length > c2.length ? c2.length : c1.length;
//b为第一个不同处的位置
int b = 0;
for(int i=0; i<a; i++) {
if(c1[0] == c1[0]) {
if(c1[i] != c2[i]) {
System.out.println("小数点后第" + i + "位不相同");
}
} else {
System.out.println("第1位不相同!");
}
}
}
}
class ReadFile {
public static char[] read(String filename) {
File f = new File(filename);
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
String str = new String();
try {
br = new BufferedReader(new FileReader(f));
} catch(FileNotFoundException e) {
System.out.println("File not found!");
System.exit(-1);
}
try {
while((str = br.readLine()) != null) {
sb.append(str);
}
} catch(IOException e) {
e.printStackTrace();
}
str = trim(sb.toString());
//System.out.println(str);
return str.toCharArray();
}
public static String trim(String str) {
str = str.replace(".", "")
str = str.replaceAll(" ", "");
return str;
}
}