Posts Tagged ‘C程式’

24
十二月

C程式設計:C版本的大樂透彩單程式

   Posted by: admin    in 98(上)C程式設計

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*LottoGen:產生大樂透彩單*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void LottoNumGen(int List[]);
 
int main( int argc, char *argv[ ])
{
	int i,j, nStakeNum=1;
	int NumList[6];
	if (argc>=2)
	{
		nStakeNum=atoi(argv[1]);
		if (nStakeNum<=0||nStakeNum>5)
		{
			printf("1~5 stakes one time\n");
			return;
		}
	}
	srand( (unsigned)time( NULL ) );
 
	for (i=0; i<nStakeNum; i++)
	{
		LottoNumGen(NumList);
		for (j=0; j< 6; j++)
		    printf("%2d ", NumList[j]);
        printf("\n");
	}
	return 0;
}
void LottoNumGen(int List[])
{
    int i, j;
    int number[49], highest, which;
 
    for (i=0; i<49; i++)
    { 
        number[i]=rand();
    }
    for (i=0; i<6; i++)
    {
       highest = 0; which = -1;
       /*每次選最高分*/
       for (j=0; j< 49; j++)
       {
           if (number[j] > highest)
           {
               highest = number[j]; which=j;
           }
       }
       List[i]=which+1; number[which]=0;
    }
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

10
十二月

C程式設計12/10:示範樂透號碼產生程式

   Posted by: admin    in 98(上)C程式設計

void LottoNumGen(int List[])
{
    int i, j;
    int number[49], highest, which;
 
    for (i=0; i<49; i++)
    { 
        number[i]=rand();
    }
    for (i=0; i<6; i++)
    {
       highest = 0; which = -1;
       /*每次選最高分*/
       for (j=0; j< 49; j++)
       {
           if (number[j] > highest)
           {
               highest = number[j]; which=j;
           }
       }
       List[i]=which+1; number[which]=0;
    }
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: ,

10
十二月

C程式設計:練習九樣板

   Posted by: admin    in 98(上)C程式設計

49選6大樂透是從01~49中任選6個不重複號碼進行投注。

請寫一個程式產生一次最多可以有5注的投注單來。

/*LottoGen:產生大樂透彩單*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void LottoNumGen(int List[]);
 
int main( int argc, char *argv[ ])
{
	int i,j, nStakeNum=1;
	int NumList[6];
	if (argc>=2)
	{
		nStakeNum=atoi(argv[1]);
		if (nStakeNum<=0||nStakeNum>5)
		{
			printf("1~5 stakes one time\n");
			return;
		}
	}
	srand( (unsigned)time( NULL ) );
 
	for (i=0; i<nStakeNum; i++)
	{
		LottoNumGen(NumList);
		for (j=0; j< 6; j++)
		    printf("%2d ", NumList[j]);
        printf("\n");
	}
	return 0;
}
 
void LottoNumGen(int List[])
{
	/*在以下寫程式*/
 
	/*寫到這裡*/
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: ,

#include <stdio.h>
#include <stdlib.h>
 
typedef struct _SCORE
{
   char ID[10];
   int SS[2];
   double Avg;
   int Num;
} SCORE;
 
main()
{
   int ret, i, j, k, count, who, highest;
   SCORE *pScore;//int score[100];
   FILE * stream;
   //dynamic memory allocation 100 Scores
   pScore = (SCORE *) calloc(100, sizeof(SCORE));
 
   char filename[20]="score2.txt";
   if( (stream  = fopen( filename, "r" )) == NULL )
   {
      printf( "The file %s was not opened\n", filename );
      return -1;
   }
   i=0; count=0;
   ret = fscanf(stream, "%8c %d %d\n", 
         &pScore[i].ID,&pScore[i].SS[0], &pScore[i].SS[1]);
 
   while (ret !=  EOF)
   {
         pScore[i].Avg = 0.5*(pScore[i].SS[0]+pScore[i].SS[1]);
         pScore[i].Num = 0;
         i = i+1;
         ret = fscanf(stream, "%8c %d %d\n", 
               &pScore[i].ID,&pScore[i].SS[0], &pScore[i].SS[1]);
   }
   fclose(stream);
   count = i;
   /*算有幾個人比你分數高*/
   for (j=0; j<count; j++)
   {
       k = 0;
       for (i=0; i< count; i++)
       {
           if (pScore[i].Avg > pScore[k].Avg)
           {
               k++;
           }
       }
       pScore[j]= k + 1;
   }
 
   for (i=0; i< count; i++)
   {
       pScore[i].Avg = 0.5*(pScore[i].SS[0]+pScore[i].SS[1]);
       printf("%s %2d %2d %4.2f %2d\n", 
            pScore[i].ID, pScore[i].SS[0],pScore[i].SS[1], pScore[i].Avg, pScore[i].Num);
   }
   free(pScore);
   return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: ,

3
十二月

C程式設計12/03上課:成績排序範例

   Posted by: admin    in 98(上)C程式設計

#include <stdio.h>
#include <stdlib.h>
typedef struct _Score{
        char ID[10];
        int SS[3];
        double Avg;
} SCORE;
int compare( const void *arg1, const void *arg2 );
main()
{
   int ret, i, ss, sum, average, count;
   SCORE *pScore;
   FILE * stream;
   char filename[20]="score3.txt";
 
   pScore = (SCORE*) calloc(100, sizeof(SCORE));
 
   if( (stream  = fopen( filename, "r" )) == NULL )
   {
      printf( "The file %s was not opened\n", filename );
      return -1;
   }
   i=0; count=0;
   ret = fscanf(stream, "%8c %d %d %d\n", &pScore[i].ID, &pScore[i].SS[0], &pScore[i].SS[1], &pScore[i].SS[2]);
 
   while (ret !=  EOF)
   {
         pScore[i].Avg = 0.33333333333*(pScore[i].SS[0]+pScore[i].SS[1]+pScore[i].SS[2]);
         i++;
         ret = fscanf(stream, "%8c %d %d %d\n", &pScore[i].ID, &pScore[i].SS[0], &pScore[i].SS[1], &pScore[i].SS[2]);
   }
   fclose(stream);
   count = i;
 
   qsort(pScore, count, sizeof(SCORE), compare);
 
   for (i=0; i< count; i++)
   {
         printf("%2d %8s %2d %2d %2d %6.2f \n", i+1, pScore[i].ID, pScore[i].SS[0], pScore[i].SS[1], pScore[i].SS[2], pScore[i].Avg);
   }
 
   return 0;
}
int compare( const void *arg1, const void *arg2 )
{
    double diff = (*((SCORE*)arg1)).Avg - (*((SCORE*)arg2)).Avg;
    if (diff > 0)
       return -1;
    else
       return 1;
    return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: ,