滑县人民检察院网站:帮忙修改程序C++语言~~ (输入完一组数据后,这样才能不跳出,能继续输入第二组数据)

来源:百度文库 编辑:高校问答 时间:2024/05/07 17:01:00
//数据处理//
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<math.h>
int panduan(char ch) //判断是否为数字。
{
return (ch>='0'&&ch<='9');
}
int sushu(int a) //判断是否为素数。
{
if (a==0) return 0;
if (a==1) return 0;
if (a==2) return 1;
for(int i=2;i<=sqrt(a);i++)
{
if (a%i==0) return 0;
}
return 1;
}

void work()
{ofstream outf("old.dat"); //创建文件old.dat。
if(!outf)
{
cout<<"Can not open file.\n";
//return 1;
}
char a[100];
cout<<"请输入任意一串字符:\n";
cin>>a;
outf<<a<<endl ; //将字符串存入文件。
outf.close();
ofstream outf1("new1.dat");
ofstream outf2("new2.dat");
if(!outf1)
{
cout<<"Can not open the file new1.dat.\n";
//return 1;
}
if(!outf2)
{
cout<<"Can not open the file new2.dat.\n";
//return 1;
}
int Y[20]; //定义一字符存放素数。
int N[20]; //定义一字符存放非素数。
int p1=0;
int p2=0;
int n=strlen(a);
int i=0;
while(i<n)
{
while(i<n&&!panduan(a[i])) i++;
if(panduan(a[i]))
{
int num=0;
while(i<n&&panduan(a[i]))
{
num=num*10+a[i]-'0';
i++;
}
if(sushu(num))
{Y[p1]=num;
cout<<"素数:"<<Y[p1]; //显示素数。
outf1<<Y[p1]<<endl; //将素数存入文件new1.dat。
p1++;}
else
{N[p2]=num;
cout<<"非素数:"<<N[p2]; //显示非素数。
outf2<<N[p2]<<endl; //将非素数存入文件new2.dat。
p2++;}
}
}
}
main()
{work();
return 0;
}

//数据处理//
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<math.h>
int panduan(char ch) //判断是否为数字。
{
return (ch>='0'&&ch<='9');
}
int sushu(int a) //判断是否为素数。
{
if (a==0) return 0;
if (a==1) return 0;
if (a==2) return 1;
for(int i=2;i<=sqrt(a);i++)
{
if (a%i==0) return 0;
}
return 1;
}

int work()
{
char a[100];
cout<<"请输入任意一串字符,输入 0 退出:\n";
cin>>a;
if (strcmp(a,"0")==0)
{
return -1;
}

ofstream outf("old.dat"); //创建文件old.dat。
if(!outf)
{
cout<<"Can not open file.\n";
//return 1;
}

outf<<a<<endl ; //将字符串存入文件。
outf.close();
ofstream outf1("new1.dat");
ofstream outf2("new2.dat");
if(!outf1)
{
cout<<"Can not open the file new1.dat.\n";
//return 1;
}
if(!outf2)
{
cout<<"Can not open the file new2.dat.\n";
//return 1;
}
int Y[20]; //定义一字符存放素数。
int N[20]; //定义一字符存放非素数。
int p1=0;
int p2=0;
int n=strlen(a);
int i=0;
while(i<n)
{
while(i<n&&!panduan(a[i])) i++;
if(panduan(a[i]))
{
int num=0;
while(i<n&&panduan(a[i]))
{
num=num*10+a[i]-'0';
i++;
}
if(sushu(num))
{Y[p1]=num;
cout<<"素数:"<<Y[p1]<<endl; //显示素数。
outf1<<Y[p1]<<endl; //将素数存入文件new1.dat。
p1++;}
else
{N[p2]=num;
cout<<"非素数:"<<N[p2]<<endl; //显示非素数。
outf2<<N[p2]<<endl; //将非素数存入文件new2.dat。
p2++;}
}
}
return 0;
}
main()
{
while(work()!=-1)
;
return 0;
}

把cin>>a;改为(cin>>a).get();

//数据处理//
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<math.h>
int panduan(char ch) //判断是否为数字。
{
return (ch>='0'&&ch<='9');
}
int sushu(int a) //判断是否为素数。
{
if (a==0) return 0;
if (a==1) return 0;
if (a==2) return 1;
for(int i=2;i<=sqrt(a);i++)
{
if (a%i==0) return 0;
}
return 1;
}

int work()
{
char a[100];
cout<<"请输入任意一串字符,输入 0 退出:\n";
cin>>a;
if (strcmp(a,"0")==0)
{
return -1;
}

ofstream outf("old.dat"); //创建文件old.dat。
if(!outf)
{
cout<<"Can not open file.\n";
//return 1;
}

outf<<a<<endl ; //将字符串存入文件。
outf.close();
ofstream outf1("new1.dat");
ofstream outf2("new2.dat");
if(!outf1)
{
cout<<"Can not open the file new1.dat.\n";
//return 1;
}
if(!outf2)
{
cout<<"Can not open the file new2.dat.\n";
//return 1;
}
int Y[20]; //定义一字符存放素数。
int N[20]; //定义一字符存放非素数。
int p1=0;
int p2=0;
int n=strlen(a);
int i=0;
while(i<n)
{
while(i<n&&!panduan(a[i])) i++;
if(panduan(a[i]))
{
int num=0;
while(i<n&&panduan(a[i]))
{
num=num*10+a[i]-'0';
i++;
}
if(sushu(num))
{Y[p1]=num;
cout<<"素数:"<<Y[p1]<<endl; //显示素数。
outf1<<Y[p1]<<endl; //将素数存入文件new1.dat。
p1++;}
else
{N[p2]=num;
cout<<"非素数:"<<N[p2]<<endl; //显示非素数。
outf2<<N[p2]<<endl; //将非素数存入文件new2.dat。
p2++;}
}
}
return 0;
}
main()
{
while(work()!=-1)
;
return 0;
}