Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include <time.h>
- int main(void)//主程式
- {
- int i,max=0,min=10;
- int num, no[10];
- int count=0;
- srand(time(NULL));
- //////////////陣列歸零///////////////////////////
- for (i=1; i<=10; i++)
- no[i]=0;
- //////////////隨機產生不重複的數字//////////////////////////
- while (count<5)
- {
- num=rand()%10;
- if (no[num]==0)
- {
- no[num]=1;
- count = count + 1;
- }
- }
- //////////////////////若陣列中的值為1則寫出///////////////
- FILE *written;
- written = fopen("random.txt","w");
- for (i=1; i<=10; i++)
- {
- if (no[i]==1)
- {
- fprintf(written,"%d ",i);
- }
- }
- fclose(written);
- printf("寫檔完成\n");
- printf("=====================\n\n");
- /////////////////////讀檔找出最大值與最小值/////////////////////////////////////
- printf("**讀檔**\n");
- FILE *read;
- int b;
- read = fopen("random.txt","r");
- printf("開出的號碼有:\n");
- while((fscanf(read,"%d",&b))!=EOF)
- {
- if (b>max)
- {
- max = b;
- }
- if (b<min)
- {
- min = b;
- }
- printf("%d\n",b);
- }
- printf("=====================\n\n");
- printf("最大的號碼是:%d\n", max);
- printf("最小的號碼是:%d\n", min);
- printf("=====================\n\n");
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement