本文介绍了有人可以告诉我为什么这段代码什么都不打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个比较三个文本文件的代码,我不知道为什么我不会打印任何东西:



I have to write a code that compares three text files and i cant for the life of me find out why this wont print anything:

#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;

int main (int argc, char *argv[])
{
	ifstream mousefile;
	mousefile.open(argv[1]);
	string mouse_dna;
	getline(mousefile, mouse_dna);

	ifstream humanfile;
	humanfile.open(argv[2]);
	string human_dna;
	getline(humanfile, human_dna);

	ifstream unknownfile;
	unknownfile.open(argv[3]);
	string unknown_dna;
	getline(unknownfile, unknown_dna);

	int len = mouse_dna.size();
	int mouseDistance = 0, humanDistance = 0;

	for(int i=0; i<len; i++)
		if(mouse_dna[i] != unknown_dna[i])
			mouseDistance++;
			return mouseDistance;
	for(int i=0; i<len; i++)
		if(human_dna[i] != unknown_dna[i])
			humanDistance++;
			return humanDistance;


	double similarity_scoreH = (len - humanDistance) / len;
	double similarity_scoreM = (len - mouseDistance) / len;
	cout << "MouseCompare = " << similarity_scoreM << endl;
	cout << "HumanCompare = " << similarity_scoreH << endl;

	if (similarity_scoreH == similarity_scoreM)
		cout << "identity cannot be determined" << endl;
	else if (similarity_scoreH > similarity_scoreM)
		cout << "human" << endl;
	else if (similarity_scoreM > similarity_scoreH)
		cout << "mouse" << endl;


}



它编译正确,并没有出现任何错误,但是当我把它作为:

./DNA mouseDNA.txt humanDNA.txt unknownDNA.txt

它仍然无效。

我感谢任何帮助。谢谢!

推荐答案



double similarity_scoreH = (len - humanDistance) / len;



由于len和humanDistance都是int类型,因此除法将以整数运算完成,最有可能产生0的结果。 br />


As len and humanDistance are both of type int, the division will be done in integer arithmetic and most probably yield the result of 0. Use instead:

double similarity_scoreH = 1.0;
if (len > 0)
    similarity_scoreH = ((double) len - humanDistance) / (double) len;



similarity_scoreM 做同样的事情。



我只能同意其他两个解决方案中的要点:习惯于在调试器中运行程序。如果在调试器中运行它,您将在几秒钟内检测到问题。并做更多检查输入数据。例如,如果 unknownfile 的长度短于其他两个文件的长度怎么办?



As对于比较算法:逐个比较可能不是比较DNA序列的最佳方法。如果流中只缺少一个元素,则所有剩余元素将报告为偏差。有意义的比较比你在这里做的要复杂得多。


And do the same for similarity_scoreM.

I can only agree with the points made in the two other solutions: Get used to run your program in a debugger. You would have detected the problem within a few seconds had you run it in the debugger. And do a lot more checking of the input data. For example, what if the length of the unknownfile is shorter than that of the other two files?

As for the comparison algorithm: A one-by-one comparison is probably not the best way of comparing DNA sequences. If just one element is missing in a stream, all the remaining elements will be reported as deviations. A meaningful comparison is much more complex than what you do here.


这篇关于有人可以告诉我为什么这段代码什么都不打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 04:53
查看更多