我想编写一个打开以前创建的文件“out.dat”的c ++程序。并计算agaatacaccaacgacta中字母'c'的相对频率...请帮助我填补空白点。
问题描述:
#include<.........>
#include <fstream>
#include<........>
int main()
{
........a;
........count=0;
........myFile("out.dat");
while(myFile >> a)
{
if(a ............'C')
Count=count.....;
}
cout<< ........ <<"\n";
getchar();
return 0;
}
我的尝试:
我试过Cpp.sh ......无法运行它..因为我不明白如何完成程序......我发现计数很难理解
What I have tried:
I tried Cpp.sh ... Couldn't run it .. Because I didn't understand how to complete the program... I find counting is hard to understand
答
这是纯粹的家庭作业。请参阅您的课程笔记或询问您的老师。这是一个非常简单的练习。所以试试吧。计数并不困难。不要告诉自己,我不能这样做!这将使其更加困难。学习你的课程笔记,这将相对容易。
This is pure homework. Please refer to your course notes or ask your teacher. This is a really easy exercise. So give it a try. Counting is not difficult at all. Don't tell yourself, "I can't do this"! This will make it even more difficult. Study your course notes and it will be relatively easy.
看看std :: count
,std :: iostream_iterator
和std :: ifstream
。使用标准库的这三个块,您可以在两行代码中执行所需的操作 - 如果您希望在文件无法打开时抛出异常,则可以执行三行。
Have a look atstd::count
,std::iostream_iterator
andstd::ifstream
. Using these three chunks of the standard library you can do what you want in two lines of code - three if you want to have an exception thrown if the file failed to open.