#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); }
Archive for the ‘102(上)程式設計(一)’ Category
#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; }
11
十二月
程式設計一程式 12/11: ex12.c
using System; using System.Collections.Generic; using System.IO; using System.Text; namespace Ex12 { class Program { static void Main(string[] args) { int i; int num, pos; //從命令列讀入一個參數 //num = atoi(argv[1]); num = int.Parse(args[0]); //設定亂數的啟始點 //srand( (unsigned) time( NULL ) ); //建立亂數的物件 Random rnd = new Random(); //out = fopen("number.txt", "w"); StreamWriter sw = new StreamWriter("number.txt"); //產生1000個亂數 for (i=0; i<num; i++) { //pos = rand();////產生1個亂數 pos = rnd.Next(); //printf("%d\n", pos); Console.WriteLine(pos); //fprintf(out, "%d\n", pos); sw.WriteLine(pos); } //fclose(out); sw.Close(); } } }