2008.11.15 00:59 Portfolio/C/C++

무한 드래그의 압박이 느껴지실때에는... -_ㅡ+++
그냥,,, 파일 다운 받아서 실행 해보시길 바랍니다...ㅋㅋㅋ
Visual Studio 6.0 C++ 에서 호환됩니다.


 

#pragma warning(disable : 4048)

#pragma warning(disable : 4996)

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#include <math.h>

#define MAX_POLYS 10

#define MAX_TERMS 11

#define MAX_LENGTH 20

#define VALID_TOKENS_COUNT 3

#define NOTFOUND -1

#define END_DATA 'q'

#define ADD '+'

#define SUBTRACT '-'

#define MULTIPLY '*'

#define END_TEST '#'

#define EOS 0

#define END_TEST_MARKER 1

#define OPERATOR 2

#define OPERAND 3

#define ILLEGAL_SYMBOL 4

#define LEXICAL_ERROR 1

#define MISSING_OPERATOR 2

#define MISSING_OPERAND 3

#define TOO_MANY_TOKENS 4

#define UNKNOWN_ERROR 5

#define SUCCESS 100

typedef struct

{

int coefficient;

int exponent;

} TERM;

int load_polynomial_data(char *, TERM[][MAX_POLYS]);

char get_expression(char *, char *);

int tokenize(char *, char *, char *, char *);

int get_token(char *, int *, char *);

int find_operand(char operand, TERM polynomials[][MAX_TERMS], int no_of_polynomials);

void add_poly(TERM [], TERM [], TERM []);

void subtract_poly(TERM [], TERM [], TERM []);

void multiply_poly(TERM [], TERM [], TERM []);

void display_poly(TERM []);

void display_error_message(int);

int main(void)

{

TERM polynomials[MAX_POLYS][MAX_TERMS];

int no_of_polynomials;

int poly1, poly2;

TERM result[MAX_TERMS*MAX_TERMS];

char operator, operand1, operand2;

no_of_polynomials = load_polynomial_data("poly.dat", polynomials);

while(1)

{

operator = get_expression(&operand1, &operand2);

if(operator == END_TEST)

break;

poly1 = find_operand(operand1, polynomials, no_of_polynomials);

if(poly1 == NOTFOUND)

{

fprintf(stderr, "\n\nFirst operand is not registered!! Retry!!");

continue;

}

poly2 = find_operand(operand2, polynomials, no_of_polynomials);

if(poly2 == NOTFOUND)

{

fprintf(stderr, "\n\nSecond operand is not registered!! Retry!!");

continue;

}

 

switch(operator)

{

case ADD:

add_poly(polynomials[poly1], polynomials[poly2], result);

display_poly(result);

break;

case SUBTRACT:

subtract_poly(polynomials[poly1], polynomials[poly2], result);

display_poly(result);

break;

case MULTIPLY:

multiply_poly(polynomials[poly1], polynomials[poly2], result);

display_poly(result);

break;

}

}

return 0;

}

 

int load_polynomial_data(char *file_name, TERM polynomials[][MAX_POLYS])

{

int count;

FILE *fp;

TERM term;

int i;

fp = fopen(file_name, "r");

if(fp == NULL)

{

fprintf(stderr, "Can't open %s!!!", file_name);

exit(1);

}

count = -1;

while(!feof(fp))

{

fscanf(fp, "%d%d", &term.coefficient, &term.exponent);

if(term.coefficient == END_DATA) break;

polynomials[count][0] = term;

count++;

if(count >= MAX_POLYS)

{

fprintf(stderr, "Too many data in %s !!", file_name);

exit(1);

}

for(i=1; i<=polynomials[count][0].exponent; ++i)

{

fscanf(fp, "%d%d", &term.coefficient, &term.exponent);

polynomials[count][i] = term;

}

}

fclose(fp);

return count+1;

}

char get_expression(char *operand1, char *operand2)

{

char expression[MAX_LENGTH];

char operator, opr1, opr2;

int answer;

while(1)

{

printf("\n\nType an expression. (+ p q) ==> ");

scanf("%s", expression);

answer = tokenize(expression, &operator, &opr1, &opr2);

if(answer == SUCCESS)

break;

else

{

display_error_message(answer);

continue;

}

}

*operand1 = opr1;

*operand2 = opr2;

return operator;

}

 

int tokenize(char *expression, char *operator, char *operand1, char *operand2)

{

int current;

int token;

char token_value;

int state;

current = 0;

state = 0;

while(1)

{

token = get_token(expression, &current, &token_value);

if(token == ILLEGAL_SYMBOL)

return LEXICAL_ERROR;

if(token == EOS)

break;

switch(state)

{

case 0:

if(token == END_TEST_MARKER)

{

*operator = token_value;

state = 3;

}

else if(token == OPERATOR)

{

*operator = token_value;

state = 1;

}

else

state = 4;

break;

case 1:

if(token == OPERAND)

{

*operand1 = token_value;

state = 2;

}

else

state = 5;

break;

case 2:

if(token == OPERAND)

{

*operand2 = token_value;

state = 3;

}

else

state = 5;

break;

case 3:

if(token != EOS)

{

state = 6;

break;

}

case 4:

case 5:

case 6:

break;

}

}

if(state == 0)

return MISSING_OPERATOR;

else if(state == 1)

return MISSING_OPERAND;

else if(state == 2)

return MISSING_OPERAND;

else if(state == 3)

return SUCCESS;

else if(state == 4)

return MISSING_OPERATOR;

else if(state == 5)

return MISSING_OPERAND;

else return

TOO_MANY_TOKENS;

}

int get_token(char *s, int *current, char *token_value)

{

char c;

while(1)

{

c = s[*current];

*current ++;

if(c == '\0')

return EOS;

if(isspace(c))

continue;

*token_value = c;

if(c == END_TEST)

return END_TEST_MARKER;

else if(c == ADD)

return OPERATOR;

else if(c == SUBTRACT)

return OPERATOR;

else if(c == MULTIPLY)

return OPERATOR;

else if(isalpha(c))

return OPERAND;

else

return ILLEGAL_SYMBOL;

}

}

void display_error_message(int error_no)

{

static char *error_messages[] =

{

"",

"Lexical Error !!"

"Missing Operator !!",

"Missing Operand !!",

"Too Many Tokens !!",

"Unknown Error !!"

};

fprintf(stderr, "\n\n%s", error_messages[error_no]);

}

 

int find_operand(char operand, TERM polynomials[][MAX_TERMS], int no_of_polynomials)

{

int poly;

for(poly=0; poly<no_of_polynomials; poly++)

{

if(polynomials[poly][0].coefficient == operand)

return poly;

}

return NOTFOUND;

}

 

void add_poly(TERM poly1[], TERM poly2[], TERM result[])

{

int i, j, k;

int coef;

i=1, j=1, k=1;

 

while(i<=poly1[0].exponent && j<=poly2[0].exponent)

{

if(poly1[i].exponent == poly2[j].exponent)

{

coef = poly1[i].coefficient + poly1[i].coefficient;

if(coef != 0)

{

result[k].coefficient = coef;

result[k].exponent = poly1[i].exponent;

k++;

}

i++;

j++;

}

else if(poly1[i].exponent > poly2[j].exponent)

{

result[k].coefficient = poly1[i].coefficient;

result[k].exponent = poly1[i].exponent;

k++;

i++;

}

else

{

result[k].coefficient = poly2[j].coefficient;

result[k].exponent = poly2[j].exponent;

k++;

j++;

}

}

while(i<=poly1[0].exponent)

{

result[k].coefficient = poly1[i].coefficient;

result[k].exponent = poly1[i].exponent;

k++;

i++;

}

while(j<=poly2[0].exponent)

{

result[k].coefficient = poly2[j].coefficient;

result[k].exponent = poly2[j].exponent;

k++;

j++;

}

result[0].exponent = k-1;

}

 

void subtract_poly(TERM poly1[], TERM poly2[], TERM result[])

{

int j;

for(j=1; j<=poly2[0].exponent; j++)

poly2[j].coefficient = -poly2[j].coefficient;

add_poly(poly1, poly2, result);

}

 

void multiply_poly(TERM poly1[], TERM poly2[], TERM result[])

{

TERM temp1[MAX_TERMS];

TERM temp2[MAX_TERMS];

int i, j, k;

result[0].exponent = 0;

for(i=1; i<=poly1[0].exponent; i++)

{

temp1[0].exponent = poly2[0].exponent;

for(j=1; j<=poly2[0].exponent; j++)

{

temp1[j].coefficient = poly1[i].coefficient * poly2[j].coefficient;

temp1[j].exponent = poly1[i].exponent + poly2[j].exponent;

}

add_poly(result, temp1, temp2);

for(k=0; k<=temp2[0].exponent; k++)

{

result[k].coefficient = temp2[k].coefficient;

result[k].exponent = temp2[k].exponent;

}

}

}

 

void display_poly(TERM poly[])

{

int i;

printf("\n\nResult: ");

 

if(poly[0].exponent == 0)

{

printf("There are no terms");

return;

}

else

{

printf("%d", poly[1].coefficient);

printf("x");

printf("%d", poly[1].exponent);

}

 

for(i=2; i<=poly[0].exponent; ++i)

{

printf(" ");

if(poly[i].coefficient>0)

printf("+");

else

printf("-");

printf(" ");

printf("%d", abs(poly[i].coefficient));

printf("x");

printf("%d", poly[1].exponent);

}

}


posted by gomdorz

댓글을 달아 주세요