학교/자료구조

2-2. 다항식

daykim 2021. 9. 27. 17:05

기호 다항식의 조작

  • a : cofficient (계수)
  • e : exponenet (지수)
  • x : variable (변수)
  • degree(차수) : 다항식에서 가장 큰 지수

다항식 표현(1)

degree
(MAX 지수)
coefficient
3 1 10 3 7

 

#define MAX_D 100

typedef struct{
    int degree;
    float coef[MAX_D];
}polynomial;

polynomial a;

a.degree = 3;
// a.coef[i] = a_n-i
a.coef[0] = a_n-0; // 1
a.coef[1] = a_n-1; // 10
a.coef[2] = a_n-2; // 3
a.coef[3] = a_n-3; // 7

 

다항식 표현(2)

* 0인 항이 많을 경우 유용

0 1 2 3 4 index
1 10 3 7   coef
3 2 1 0   expon

 

#define MAX_T 100

typedef struct{
    int degree;
    float coef;
}polynomial2;

polynomial2 term[MAX_T];

 

다항식 표현(3)

#define MAX_T 100

typedef struct{
	int degree;
	float coef;
}polynomial2;

polynomial2 term[MAX_T];
int avail = 0; // 배열 내의 다음 가용 공간

 

다항식 덧셈

void attach(float coefficient, int exponent){
	if(avail >= MAX_T){
    	fprintf(stderr, "다항식에 항이 너무 많다");
        exit(1);
    }
    term[avail].coef = co;
    term[avail].exp = exp;
}

// starta = sa, finisha = fa
void padd(int sa, int fa, int sb, int fb, int *sd, int *fd){
    *sd = avail;
    float co;
    while(sa <= fa && sb <= fb){
    	switch(COMPARE(term[sa].exp, term[sb].exp){
            case -1: attach(term[sb].coef, term[sb].exp);	// a.exp < b.exp
            		 sb++; break;
            case 0: co = term[sa].coef + term[sb].coef;		// a.exp = b.exp
            		if(co) attach(co, term[sa].exp);
                    		sa++; sb++; break;
            case 1: attach(term[sa].coef, term[sa].exp);	// a.exp > b.exp
            		sa++;
        }
	}
    int i;
    for(i=0; i<=fa; sa++) attach(term[sa].coef, term[sa].exp);
    for(i=0; sb<=fb; sb++) attach(term[sb].coef, term[sh].exp);
    *fd = avail-1;
}

 

 

희소행렬

많은 항들이 '0'으로 되어있는 행렬 => 저장공간 낭비

#define MAX_T 100
typedef struct{
    int col;
    int row;
    int val;
}term;
term a[MAX_T];

 

 

전치행렬

void transpose(term a[], term b[]){
    int n, i, j, cb;
    b[0].row = a[0].col;
    b[0].col = a[0].row;
    b[0].val = n;
    if(n>0){
    	cb = 1;
        for(i=0; i<a[0].col; i++){
        	for(j=1; j<=n; j++){
            	if(a[j].col == i){
                	b[cb].row = a[j].col;
                    b[cb].col = a[j].row;
                    b[cb].val = a[j].val;
                    cb++;
                }
            }
        }
    }
}

 

c언어 스트링

#include <string.h>

 

스트링 함수

strcat(s, t)  
strncat(s, t, n)  
strcmp(s, t) s>t : 0보다 큰 값 출력
s=t : 0 출력
s<t : -1 출력
strncmp(s, t, n)  
strcpy(s, t)  
strncpy(s, t, n)  
strlen(s)  
strstr(s, pat) s 중에 pat 패턴을 찾아 pat의 시작지점 반환

 

응용(스트링 삽입)

#define MAX_SIZE 100;
char str1[MAX_SIZE], *s = str1, str2[MAX_SIZE], *t=str2;

char s[] = {"amobile"};
char t[] = {"ato"};

void strin(char *s, char *t, int i){
    char string[MAX_SIZE], *temp = string;	//temp = {"\0"}
    if(!strlen(s)) strcpy(s, t);
    else if(strlen(t)){
    	strncpy(temp, s, i);	// temp = {"a\0"}
        strcat(temp, t);		// temp = {"auto\0"}
        strcat(temp, (s+i));	// temp = {"automobile\0"}
        strcpy(s, temp);		// s = {"automobile\n"}
    }
}

 

'학교 > 자료구조' 카테고리의 다른 글

5. 이중 연결리스트  (0) 2022.03.14
4. 연결 리스트  (0) 2022.03.14
3. 스택과 큐  (0) 2021.09.28
2-1. 배열과 구조  (0) 2021.09.27
1. 기본개념  (0) 2021.09.27