Archive for 十一月 28th, 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: , ,