永发信息网

把这段C++转换成C语言

答案:1  悬赏:70  手机版
解决时间 2021-06-08 21:50
  • 提问者网友:我们很暧昧
  • 2021-06-08 12:37

#include <fstream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
using namespace std;

int threholdinp()
{
long maxnum=0;
char tmpch='\0';
cout << "I can show the prime numbers lower than a threhold." << '\n';
cout<<"Just tell me the upper threhold you want to calculate bellow:"<<endl;
cin>>maxnum;
while(tmpch=cin.get()){if (tmpch='\n') break;}
if (maxnum<1){cerr<<"Threhold not appropriate."<<endl;return threholdinp();}
return maxnum;
}

int primevectorcalc(int targetnum,bool *prmvct,int *count)
{
bool *tmp;tmp=prmvct;
int i=0,j=0,prmcnt=0;
for (i=0;i<targetnum;i++) *(tmp+i)=true;
for (i=2;i<targetnum;i++)
{
if (*(tmp+i)==true)
{
prmcnt++;
if (i+i<targetnum)
{
for(j=i+i;j<targetnum+1;j=j+i) *(tmp+j)=false;
}
}
}
*count=prmcnt;
return 0;
}

int primevectortime(int targetnum, int *prmcnt,bool *tmpprime)
{
clock_t start,finish;double duration;
start=clock();
int ctrl=0;
ctrl=primevectorcalc(targetnum,tmpprime,prmcnt);
if (ctrl<0) return -1;
finish=clock();
duration=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<"Work done in "<<duration<<" seconds."<<'\n';
return 0;
}

int fileopt(int maxnum,int primecount, bool *prime)
{
ofstream outfile;
outfile.open("PrimeNum.txt",ios::out);
if (!outfile){ cerr<<"Can not open file."<<'\n'; return -1;};
outfile<<"There are "<<primecount<<" prime numbers less than "<<maxnum<<" ."<<'\n';
outfile<<"Here are the prime numbers less than threhold "<<maxnum<<'\n';
bool *tmp;tmp=prime;
primecount=0;
for (int i=2;i<maxnum;i++)
{
if (*(tmp+i)==true)
{
outfile<<i<<'\t';
}
}
outfile<<endl;
outfile.close();
return 0;
}

int primetable1()
{
long maxnum=0;
int prmcnt;
maxnum=threholdinp();
bool *prime; int ctrl=0;
prmcnt=0;
prime= (bool *)malloc(sizeof(bool)*(maxnum+1));
if (prime==NULL ) {cerr<<"Memory not enough to caculate!"<<endl;return -1;}
ctrl=primevectortime(maxnum,&prmcnt,prime);
if (ctrl<0) return -1;
fileopt(maxnum,prmcnt,prime);
return 0;
}

int main()
{
primetable1();
system("pause");
return 0;

}

最佳答案
  • 五星知识达人网友:山有枢
  • 2021-06-08 13:29
是源程序吗?我有c++转C#的工具.但是是演示版的.要吗?
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯