设想是每次运行程序随机打印一个数字到txt文件但不能与txt里的数字重复
菜鸟难啊
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int age(int t)
{
int c;
srand((unsigned)time(NULL));
c=rand()%(t+1);
printf("c=%d\n",c);
return(c);
}
int main()
{
FILE *fp=fopen("1.txt","r");
int *a=NULL;
int i,j,t,c,k, flag;
go1:;
printf("你要打印几以内的数字");
scanf("%d",&t);
for(j=1,i=0;!feof(fp);i++,j++)
{
a=(int *)(a?realloc(a,sizeof(int)*(i+1)):malloc(sizeof(int)));
fscanf(fp,"%d",a+i);
}
c=age(t);
flag=0;
for(k=0;k<j-1;k++)
{
if(a[k]==c)
{flag=1;
printf("%d---%d重复了无效\n",c,a[k]);
}}
if(flag==1)
{
goto go1;
}
else
{
fp=fopen("1.txt","at");
fprintf(fp," %d",c); }
printf("flag4=%d\n",flag);
fclose(fp);
system("pause");
return 0;
}
第一次会检查随机数与文本是否相同,有相同会叫你输入第二次,可是第二次没检查就打印了求教原因
求不喷(我中的思路错了改出来了也会出现问题的)
菜鸟难啊
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int age(int t)
{
int c;
srand((unsigned)time(NULL));
c=rand()%(t+1);
printf("c=%d\n",c);
return(c);
}
int main()
{
FILE *fp=fopen("1.txt","r");
int *a=NULL;
int i,j,t,c,k, flag;
go1:;
printf("你要打印几以内的数字");
scanf("%d",&t);
for(j=1,i=0;!feof(fp);i++,j++)
{
a=(int *)(a?realloc(a,sizeof(int)*(i+1)):malloc(sizeof(int)));
fscanf(fp,"%d",a+i);
}
c=age(t);
flag=0;
for(k=0;k<j-1;k++)
{
if(a[k]==c)
{flag=1;
printf("%d---%d重复了无效\n",c,a[k]);
}}
if(flag==1)
{
goto go1;
}
else
{
fp=fopen("1.txt","at");
fprintf(fp," %d",c); }
printf("flag4=%d\n",flag);
fclose(fp);
system("pause");
return 0;
}
第一次会检查随机数与文本是否相同,有相同会叫你输入第二次,可是第二次没检查就打印了求教原因
求不喷(我中的思路错了改出来了也会出现问题的)