#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);
}
#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);
}