#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
/*找出数组最大数模板*/
template<typename T>
T max(T x[],int n)
{
int a;
int m=0;
for(a=0;a<n-1;a++)
{
if(x[m]<x[a]) m=a;
}
return x[m];
}
/*输出一串星号*/
void linestar()
{
printf("*************************************\n");
}
/*主函数*/
int main()
{
int N,b,i=0; /*随机数个数,类型,循环计数量 */
char path[30],path1[30];/*写入路径,和读取路径 */
linestar();
printf("project 1\n");
linestar();
printf("请输入需要产生的随机数个数N=");
scanf("%d",&N);
linestar();
printf("请选择随机数的类型:\n1:整数\n2:浮点数\n3:退出\n");
linestar();
printf("请输入(1,2或3):");
scanf("%d",&b);
linestar();
if (b==1 || b==2){
printf("请输入存储的文件名:");
scanf("%s",path);
FILE *fp=fopen(path,"w");
srand((unsigned)time( NULL ) ); /*使用系统时间初始化随机数种子*/
switch(b)
{
case 1:
for(i=0;i<N;i++)
{
fprintf(fp,"%d ",rand()%(100-10+1)+0); /*文件写入[0,100]的整数*/
};
break;
case 2:
for(i=0;i<N;i++)
{
fprintf(fp,"%f ",0+1.0*rand()/RAND_MAX *(1-0));/*文件写入[0,1]的浮点数*/
};
break;
default: exit(0);break;
}
fclose(fp);
linestar();
printf("请输入读取的文件名:");
scanf("%s",path1);
FILE *fp1=fopen(path1,"r");
if (b==1)
{
int *aa;
aa=new int[N];
for(i=0;i<N;i++)
{
fscanf(fp1,"%d",&aa[i]);
}
linestar();
printf("文件中的最大值是:");
int maxa = max(aa,N);
printf("%d\n",maxa);
delete []aa;
}
else if (b==2)
{
int *bb;
bb=new int[N];
for(i=0;i<N;i++)
{
fscanf(fp1,"%f",&bb[i]);
}
linestar();
printf("文件中的最大值是:");
float maxb = max(bb,N);
printf("%f\n",maxb);
delete []bb;
}
fclose(fp1);
}
return 0;
}
01-09
1014
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)