• What I benifit from coding 0 - [Saved]

    2007-06-19

    Tag:C

    对应代码请点击:
    Calculate a certain mount of Students' GPA,Average[0]

    对于一组不同长度的字符串可以使用指针数组(pointer array)

    char *book[] = ...{"SORA","NO","KISEKI"};

     这样, 就有三个指针变量,分别指向 book[0] = SORA,  book[1] =NO, book[3] = KISEKI
    注意, book[0]是指向字符串的"指针变量".

     

    以下方法是错误的:

    int main()
    ...{
        
    char line[3= "ab";
        
    char *book[2];
        book[
    0= "This is a test Program";
        strcpy( book[
    1], line);
        printf( 
    "%s,%s", book[0], book[1] );
        
    return 0;
    }

    编译执行时候会出错.

    int main()
    ...{
        
    //char line[3] = "ab";
        char *book[2];
        book[
    0= "This is a test Program";
        strcpy( book[
    1], "ab");
        printf( 
    "%s,%s", book[0], book[1] );
        
    return 0;
    }

    这样也不行.

    int main()
    ...{
        
    char *book[2];
        book[
    0= "This is a test Program";
        book[
    1= "Successful!";
        printf( 
    "%s,%s", book[0], book[1] );
        
    return 0;
    }

    这样就没有问题了
    也就是说, 给指向字符串的字符数组"赋值", 不能用strcpy, 同样的道理

    gets( book[1] );

    也会出错.

     

    gets( cache );
    stu[stuIndex].creditHour[subIndex] 
    = atoi( cache );

    这样的代码是很好的, 先用gets( cache )把输入的内容以string类型读取, 再用atoi(atof,atol)转换成int(flout,long), 避免使用scanf读入数据.

    良好的书写习惯对于程序编写是至关重要的,我现在遵循的习惯是:
    a.函数参数与括号之间有空格, 如gets( name );而不写成gets(name);
    b.运算符与变量/常量间有空格,  如 count  +=  5;而不写成  cou count+=5;
    c.嵌套括号间根据情况加空格, 如 if ( (index==5)&&(index==6) )而不写成if((index==100)&&(index==99)),这样有利于看清括号嵌套层数,减少出错
    d.自定义函数名全部用大写字母  如double AVRGE( int mark[] ) 而不写成 double avrge( int mark[] )
    e.结构体, 或者typedef类型首字母大写, 如 typdef int Num[100];
    f.变量名尽量起有意义的单词, 即使长点也无所谓, subIndex,  markIndex比a, b, c之类的变量写的程序容易看懂的多.
    g.变量名中除第一个单词外,其他单词的首字母大写 如 subIndexSum

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