프로그래밍[Univ]/데이터구조

[C언어]데이터구조_ 배열리스트

Cloud Travel 2009. 3. 16. 17:10

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100

typedef struct list{
        char name[15];
        int eng;
        int mat;
}list_stu;

int  count = 0;

void insert(list_stu list_t[],char a[],int b,int c,int position){
        if ( (position < 0 )||(position > count)){
                printf("ERRor\n");
                return ;
        }else if ( position <= count ) {
                for ( i = position ; i <= count ; i++ ) {
                        temp = count-1;
                        list_t[temp+1] = list_t[temp];
                        temp--;
                }
        }

        strcpy(list_t[position].name,a);
        list_t[position].eng = b;
        list_t[position].mat = c;
        count++;
}
void delete(list_stu list_t[],int position){
        int i;
        for ( i = position ; i < count ; i++ ){
                list_t[i] = list_t[i+1];
        }
        count--;
}

void display(list_stu list_t[]){
        int i;
        int total_eng, total_mat;
        total_eng = total_mat = 0;
        for ( i = 0 ; i < count ; i++ ){
                total_eng = total_eng + list_t[i].eng;
                total_mat = total_mat + list_t[i].mat;
                printf("Name : %s\tenglish : %d\tmath : %d\n",list_t[i].name,list_t[i].eng,list_t[i].mat);
        }
        printf("total eng average = %f\ttotal math average = %f\n",(float)total_eng/(count),(float)total_mat/(count));
}


main(){
        list_stu student[MAX];
        char name[15];
        int eng, mat;
        int select, pos;
        printf("1.»ðÀÔ\t2.»èÁ¦\t3.Ãâ·Â\n");
        while( scanf("%d",&select) == 1){
                switch(select){
                        case 1 : printf("Name :");
                                 scanf("%s",name);
                                 printf("Math :");
                                 scanf("%d",&mat);
                                 printf("English :");
                                 scanf("%d",&eng);
                                 printf("Position :");
                                 scanf("%d",&pos);
                                 insert(student,name,eng,mat,pos);
                                 break;
                        case 2 : printf("Delete Position :");
                                 scanf("%d",&pos);
                                 delete(student,pos);
                                 break;
                        case 3 : display(student);
                                 break;
                        default : break;
                }
                printf("1.»ðÀÔ\t2.»èÁ¦\t3.Ãâ·Â\n");
        }
}