永发信息网

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

答案:1  悬赏:70  手机版
解决时间 2021-06-08 21:50

#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;

}

最佳答案
是源程序吗?我有c++转C#的工具.但是是演示版的.要吗?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
爱情可以永恒吗?前提是另一方背叛过你。
长沙到南昌的动车时间或者晚上长沙到南昌的火
美艳如花的女人诗词,第一个形容女人如花的人
小型方便面价格多少啊?有几千块钱的吗
红旗区新乡欧思嘉艺术培训中心地址在什么地方
植物大战僵尸中怎么最快收集齐所有植物
国庆节去日本人多吗,大连什么地方居住的日本
求几本好看的写网游的小说!谢谢
好似梦一场的上一句,……像……一样……仿佛
表示声好听的词语,形容乐曲美妙的词语
鬼泣4详细图文全攻略
为什么我QQ炫舞手机紫钻,游戏中却显示非紫钻
怎样学好c++语言
新邵县邵阳卤一味先地址是什么,有没有知道的
关于午休的唯美句子,描写午休的句子
推荐资讯
自考考试保留时间
龙凤区大庆可诺丹婷美容美体在哪里啊,我有事
为什么支付宝里的钱提现不了?
有邀请游戏人生资格的进
怎样研究抛体运动 知识点
大家里面谈话费用户口才能力量
为什么玩反恐精英Online老是会断线?
从杜家坎到昌平蓝郡怎么去
本人姓朱,爱人姓林,2009年11月20日(农历10
领导让你完成一项任务,但有关部门的主管却不
浅析2009年中国经济形势
DNF41加什么点好啊.
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?