Archive for 十一月, 2012

28
十一月

程式設計一程式 11/28: 練習7 (ex7b.c)

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

#include <stdio.h>
#include <stdlib.h>
int compare( const int *arg1, const int *arg2 );
int main(int argc, char *argv[])
{
    int i, j;
    int ret;
    int a[6], count[7];
    int bingo[6], cc;
    FILE* stream;
 
    for (i = 6; i >= 0; i--)
        count[i] = 0;
 
    printf("Bingo numbers:");
    scanf("%2d %2d %2d %2d %2d %2d", &bingo[0], &bingo[1], &bingo[2], &bingo[3], &bingo[4], &bingo[5])
    qsort(bingo, 6, sizeof(int), compare);
 
    stream=fopen("lotto.txt", "r");
    ret=fscanf(stream,"%2d %2d %2d %2d %2d %2d\n", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5])
 
    while (ret == 6)
    {     
        qsort(a, 6, sizeof(int), compare);
        printf("%2d %2d %2d %2d %2d %2d\n",a[0], a[1], a[2], a[3], a[4], a[5])
        //compare two array
        cc = 0;
        for (i=0, j=0; i<6 && j <6; )
        {
            if (bingo[i] a[j])
            {
               cc++;
               i++; j++;    
            }
            else if (bingo[i]   a[j])
                j++;
            else
                i++;       
        }
        count[cc]++;
        ret=fscanf(stream,"%2d %2d %2d %2d %2d %2d\n",
                &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
    }
    fclose(stream);
    for (i = 6; i >= 0; i--)
        printf("Match %d numbers: %d \n", i, count[i]);
    return 0;
}
int compare( const int *arg1, const int *arg2 )
{
    return *arg1-*arg2;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

28
十一月

程式設計一程式 11/28: 練習7 (ex7a.c)

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

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int i, j;
    int ret;
    int a[6], count[7];
    int bingo[6], cc;
    FILE* stream;
 
    for (i = 6; i >= 0; i--)
        count[i] = 0;
 
    printf("Bingo numbers:");
    scanf("%2d %2d %2d %2d %2d %2d",
                &bingo[0], &bingo[1], &bingo[2], &bingo[3], &bingo[4], &bingo[5]);
 
    stream=fopen("lotto.txt", "r");
    ret=fscanf(stream,"%2d %2d %2d %2d %2d %2d\n",
                &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
 
    while (ret == 6)
    {      
        printf("%2d %2d %2d %2d %2d %2d\n",
                a[0], a[1], a[2], a[3], a[4], a[5]);
        //compare two array
        cc = 0;
        for (i=0; i<6; i++)
        {
            for (j=0; j<6;j++)
            {
                if (bingo[i]==a[j])
                   cc++;
            }
        }
        count[cc]++;
        ret=fscanf(stream,"%2d %2d %2d %2d %2d %2d\n",
                &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
    }
    fclose(stream);
    for (i = 6; i >= 0; i--)
        printf("Match %d numbers: %d \n", i, count[i]);
 
    return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

22
十一月

程式設計一上課 11/21: 檔案讀寫

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

20121122Prog

程式2種類及3步驟

程式2種類及3步驟

程式碼5要素

程式碼5要素

寫入資料到檔案的函數

寫入資料到檔案的函數

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

21
十一月

程式設計一程式 11/21: 練習6 (ex6.c)

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

#
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void numgen(int num[6]);
int main(int argc, char *argv[])
{
    int i, N;
    int a[6];
    FILE* stream;
 
    //N = atoi(argv[1]);
    //srand( (unsigned) time( NULL ) );
 
    stream=fopen("", "");
    for (i=0; i< N; i++)
    {
        numgen(a);
        printf("%2d %2d %2d %2d %2d %2d\n"
                a[0], a[1], a[2], a[3], a[4], a[5]);
        fprintf(,""
                a[0], a[1], a[2], a[3], a[4], a[5]);
    }
    fclose(stream);
    return 0;
}
void numgen(int num[6])
{
     int i, j, min;
     int select;
     int balls[49];
     for (i=0; i< 49; i++)
     {
         balls[i]=rand();
     }
     for (j=0; j<6; j++)
     {
         select = -1;
         min = RAND_MAX + 1;
         for (i=0; i< 49; i++)
         {
             if (balls[i] < min)
             {
                min    = balls[i];
                select = i;        
             }
         }
         num[j] = select + 1;
         balls[select] = RAND_MAX + 1;
     }
}
 
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

20121114Prog

隨機亂數rand()的使用

隨機亂數rand()的使用

選不重覆的幾個數字,是從一缸有編號的球中選出幾個球

選不重覆的幾個數字,是從一缸有編號的球中選出幾個球

陣列(array)變數的宣告及變數參數的傳遞。

陣列(array)變數的宣告及變數參數的傳遞。

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,