Posts Tagged ‘C程式’

qsort(pArray, count, sizeof(Score), compare);
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: ,

26
十一月

C程式設計:練習七

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

#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, 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;
   /*算名次,每次挑出第j名的人*/
   for (j=1; j<= count; j++)
   {
       highest = .0; who = -1;
       for (i=0; i< count; i++)
       {
           if (pScore[i].Avg > highest)
           {
               highest = pScore[i].Avg; who=i;
           }
       }
       pScore[who].Num = j; pScore[who].Avg = -1.0;
   }
   for (j=1; j<= count; j++)
   {
       highest = .0; who = -1;
       for (i=0; i< count; i++)
       {
           if (pScore[i].Avg > highest)
           {
               highest = pScore[i].Avg; who=i;
           }
       }
       pScore[who].Num = j; pScore[i].Avg = -1.0;
   }
 
   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: ,

#include <stdio.h>
 
main()
{
   int ret, i, ss, count, sum, price;
   int distance[100];
   FILE * stream;
   char filename[20]="distance.txt";
   if( (stream  = fopen( filename, "r" )) == NULL )
   {
      printf( "The file %s was not opened\n", filename );
      return -1;
   }
   i=0;
   ret = fscanf(stream, "%d\n", &ss);
   while (ret !=  EOF)
   {
         distance[i++]=ss;
         ret = fscanf(stream, "%d\n", &ss);
   }
   count = i;
   fclose(stream);
 
   sum=0;
   for (i=0; i<count; i++)
   {
   	  sum=sum+distance[i];
   	  price = 20+5*((sum+2)/4);
   	  printf("The price from station 0 to %d is %d\n", i+1, price);
   }
 
   return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: ,

19
十一月

C程式設計11/12範例: 簡化的稅金計算

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

#include <stdio.h>
main()
{
   int income, tax;
   printf("Please input net income:", income);
   scanf("%d", &income);
   if (income<370000)
      tax = income *6 /100;
   else
      tax = (370000 *6 /100)+(income - 370000)*13/100;
   printf("Income=%d Tax=%d", income, tax);
   return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: ,

29
十月

Ch程式設計:練習五

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

#include <stdio.h>
 
main()
{
   int ret, i, ss, sum, average, count;
   int score[100];
   FILE * stream;
   char filename[20]="score.txt";
   if( (stream  = fopen( filename, "r" )) == NULL )
   {
      printf( "The file %s was not opened\n", filename );
      return -1;
   }
   i=0; sum=0; count=0;
   ret = fscanf(stream, "%d\n", &ss);
 
   while (ret !=  EOF)
   {
         score[i++]=ss;
         sum +=ss;
         ret = fscanf(stream, "%d\n", &ss);
   }
   fclose(stream);
 
   count=i;
   if (count >0)
      average = sum/count;
   else
       average = -1;
 
   for (i=0; i< count; i++)
   {
       if (score[i] >=average)
          printf("%d\n", score[i]);
   }
 
   return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: ,