C, Student GPA, AVERGAE

#include  " stdio.h "
#include 
" stdlib.h "
#include 
" string.h "
#define  stuNum 1
#define  subNum 6
#define  nameSize 256
#define  cacheSize 256

// using of typedef, structure
typedef  struct  tagStudent
{
    
char    name[nameSize];
    
char    sex;
    
char    *subject[subNum];
    
int        mark[subNum],creditHour[subNum];
    
double    gpa,average;
}
 Student;

// pointer array
char   * basicSubject[subNum] = "C/C++ Programming"
                             
"Calculus""Physics""Philosophy""English""Automatic Control Theory" }
;

int  main()
{
    
    Student    stu[stuNum];
    
char    cache[cacheSize];
    
int        stuIndex = 0, subIndex = 0, SubIndex = 0;
    
    
double    ARVGE( Student Stu );
    
double    CAL_GPA( Student Stu );

    
    
for ( stuIndex = 0; stuIndex < stuNum; stuIndex++ )
    
{
        
//input name
        printf( " input the name of the student. " );
        gets( stu[stuIndex].name );

        
//input sex
        printf( " input the sex of the student. " );
        
for ( ; ; )
        
{
            stu[stuIndex].sex
=getchar();
            fflush(stdin);
            
if ( (stu[stuIndex].sex == 'm')||(stu[stuIndex].sex == 'f') ) break;
            
if ( (stu[stuIndex].sex == 'M')||(stu[stuIndex].sex == 'F') ) break;
            
else printf( "ONLY m,f,M,F Accpetable " );
        }


        
//initialize subjects
        for (subIndex=0; subIndex < subNum; subIndex++)
        
{
            stu[stuIndex].subject[subIndex] 
= basicSubject[subIndex];
        }


        
//input marks
        printf( " input the marks. " );
        
for ( subIndex = 0; subIndex < subNum; subIndex++ )
        
{
            printf( 
"%s = ", stu[stuIndex].subject[subIndex] );
            gets( cache );
            stu[stuIndex].mark[subIndex] 
= atoi( cache );
        }


        
//input credit hour
        printf( " input the credit hour of each subject " );
        
for ( subIndex = 0; subIndex < subNum; subIndex++ )
        
{
            printf( 
"Credit hour of %s = ", stu[stuIndex].subject[subIndex] );
            gets( cache );
            stu[stuIndex].creditHour[subIndex] 
= atoi( cache );
        }


        
//calculate average
        stu[stuIndex].average = ARVGE( stu[stuIndex] );
        

        
//calcualte GPA
        stu[stuIndex].gpa = CAL_GPA( stu[stuIndex] );
        
        printf( 
"Name : %s Sex : %c Average : %g GPA :%g "
                stu[stuIndex].name, stu[stuIndex].sex, stu[stuIndex].average, stu[stuIndex].gpa );
        
    }

    
return 0;
}


// external functions
double  ARVGE( Student Stu )
{
    
double    average = 0;
    
int        markIndex = 0;
    
    
for ( markIndex = 0; markIndex < subNum; markIndex++ )
    
{
        average 
+= Stu.mark[markIndex];
    }

    average 
= (double)average / (double)subNum;

    
return ( average );
}


double  CAL_GPA( Student Stu )
{
    
double    gp[subNum];
    
double    gpa = 0;
    
int        gpIndex = 0, crdtHourSum = 0;
    
for ( gpIndex = 0; gpIndex < subNum; gpIndex++ )
    
{
        
if        ( (Stu.mark[gpIndex] <= 100)&&(Stu.mark[gpIndex] >= 90) ) gp[gpIndex] = 4;
        
else if ( (Stu.mark[gpIndex] <= 89)&&(Stu.mark[gpIndex] >= 80) ) gp[gpIndex] = 3;
        
else if ( (Stu.mark[gpIndex] <= 79)&&(Stu.mark[gpIndex] >= 70) ) gp[gpIndex] = 2;
        
else if ( (Stu.mark[gpIndex] <= 69)&&(Stu.mark[gpIndex] >= 60) ) gp[gpIndex] = 1;
        
else    gp[gpIndex] = 0;
    }

    
    
for ( gpIndex = 0; gpIndex < subNum; gpIndex++ )
    
{
        crdtHourSum 
+= Stu.creditHour[gpIndex];
    }


    
for ( gpIndex = 0; gpIndex < subNum; gpIndex++)
    
{
        gpa 
+= ( gp[gpIndex]*Stu.creditHour[gpIndex] );
    }

    gpa 
= gpa / (double)crdtHourSum;

    
return (gpa);
}






    
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值