帮忙修改程序C++语言~~ (输入完一组数据后,这样才能不跳出,能继续输入第二组数据)

2025-05-23 12:46:48
推荐回答(1个)
回答1:

//数据处理//
#include
#include
#include
#include
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< 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 {
while(i if(panduan(a[i]))
{
int num=0;
while(i {
num=num*10+a[i]-'0';
i++;
}
if(sushu(num))
{Y[p1]=num;
cout<<"素数:"< outf1< p1++;}
else
{N[p2]=num;
cout<<"非素数:"< outf2< p2++;}
}
}
return 0;
}
main()
{
while(work()!=-1)
;
return 0;
}