문자열, 구조체, 버블정렬
        



Ex_bubble.txt



#include<stdio.h>


#define smallNumber 96 // 소문자 필터

#define bigNumber 64 // 대문자 필터

#define line 10 // 행

#define column 20 // 열


// 문자열정보 구조체

struct string {

char str[column]; // 문자열

int point; // 문자열에 대한 포인트

};


int length(char *string); // 문자열 길이 계산

void swap(struct string *s1, struct string *s2); // 구조체 정렬


main()

{

struct string s[line];


int i, j;

int point;

int count = 0;


// 문자열 입력

printf("단어를 입력하십시오.\n");

for(i=0; i<line; i++) {

scanf("%s", s[i].str);

}


printf("\n- 입력된 문자열 -\n");

// 문자열 Point 계산

for(i=0; i<line; i++){


point=0;


for(j=0; j<length(s[i].str); j++) {


if(s[i].str[j] >= 65 && s[i].str[j] <= 90)

point += (s[i].str[j]-bigNumber);


else if (s[i].str[j] >= 97 && s[i].str[j] <= 122)

point += (s[i].str[j]-smallNumber);


}


// 문자열 point 저장

printf("%s(%d)\n", s[i].str, point);

s[i].point = point;

}


printf("\n- Point 내림차순 정렬 -\n");


// 버블 정렬

for(i=0; i<line; i++)

for(j=0; j<line-1; j++)

if(s[i].point > s[j].point)

swap(&s[i], &s[j]);


for(i=0; i<line; i++) {

printf("%s(%d)\n", s[i].str, s[i].point);

}


printf("\n");


}


// 문자열 길이

int length(char *string)

{

    int i, count=0;

    for(i=0; string[i]!='\0'; i++)   // null 까지

    {   

// 공백 . , 있을 때 때 count++

        if(string[i]!= ' ' && string[i]!= '.' && string[i]!= ',') 

            count++;

    }

    return count;  

}


// 구조제 버블 정렬

void swap(struct string *s1, struct string *s2)

{

struct string temp;


temp = *s1;

*s1 = *s2;

*s2 = temp;

}





posted by 쪼재