Archive for 一月, 2014

7
一月

程式設計一程式 01/10: quiz2a.c

   Posted by: admin    in 102(上)程式設計(一)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
    int i, value, N;
    FILE * stream;
    srand( (unsigned) time( NULL ) );
	//讀入一個整數
    N = atoi(argv[1]);
    stream=fopen("sim.txt", "w");//開啟檔案
    for (i=0; i< N; i++)
    {
		value = rand()%60;
		printf("%d\n", value);
		fprintf(stream, "%d\n", value);//列印一行到檔案
	}
    fclose(stream);//關閉檔案
    return 0; 
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

7
一月

程式設計一程式 01/10: quiz2b.c

   Posted by: admin    in 102(上)程式設計(一)

#include <stdio.h>
int main()
{
   int i, ret, value;
   int count=0;
   FILE * stream;
 
   stream=fopen("sim.txt", "r");//開啟檔案
   ret=fscanf(stream,"%d\n", &value);
   while (ret > 0)
   {  
		if (value <= 9)//設定條件
			count = count +1;
		ret=fscanf(stream,"%d\n", &value);//從檔案讀入一行
   }
	printf("The number of (1<=n<=9) = %d", count);
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,