注册 登录
编程论坛 C++教室

程序的最后多出了一次循环。不知道哪里有问题

laogao3232 发布于 2007-09-24 15:12, 456 次点击

这是我编写的从文件中读入数据,然后判断,如果是素数输出排序,否则输出0。
不知道 为什么最后一个数回被判断2次。
#include<iostream>
#include<fstream.h>
#include<cmath>
int main(){
int i,j,k,m,n=0,num=0,h=0,l=0;
fstream _open_file;
_open_file.open("d:\\abc.txt",ios::nocreate|ios::in);
if (!_open_file)
{ cerr<<"error opening the file"<<endl;
return;
}
while(!_open_file.eof())
{
_open_file>>num;
_open_file.ignore();
if(num==2)
cout<<"1"<<endl;
else if(num==3)
{cout<<"2"<<endl;}
k=2;m=0;
if(num>=4)
{for (j=2;j<=sqrt(num);j++)
{if(num%j==0)
{m++;}
};
if(m!=0) cout<<"0"<<endl;
else
{ for(i=4;i<num;i++)
{for(n=2;n<i;n++)
{if(i%n!=0)
h++;}
if(h==i-2)
{k++;}
h=0;}
cout<<k<<endl;}}
}
return;
}

3 回复
#2
laogao32322007-09-24 15:15
应该是读文件的问题,
while(!_open_file.eof())
{
_open_file>>num;
_open_file.ignore();这个具体是什么进行的解释一下,谢谢。
#3
雨中飞燕2007-09-24 15:21
while(!_open_file.eof())
{
_open_file>>num;

while(_open_file>>num)
{



by 雨中飞燕 QQ:78803110 C/C++讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=162918]C++编写的Windows界面游戏[/url]
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge):[/url] http://yzfy.org/
#4
laogao32322007-09-27 08:28

能解释一下为什么吗?谢谢。

1