Created
May 30, 2019 06:14
-
-
Save blood72/d5b23ea282012ba7972efbeaf2cbfbed to your computer and use it in GitHub Desktop.
학창 시절에 만든 C언어 다항식 연산기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h>// pow함수 | |
#pragma warning (disable:4996)// 에러무시 | |
void blankdel(char *poly); | |
double polycalc(char *poly, int x); | |
int main() | |
{ | |
char poly[100]; // 다항식을 저장할 문자열형식의 배열 | |
printf("다항식을 입력하세요 : "); | |
gets(poly); | |
int x = 0; // 미지수 x값을 저장할 변수 | |
printf("미지수 x값을 넣으세요 : "); | |
scanf("%d", &x); | |
// 다항식 훼손 전에 출력 | |
printf("\n\n\n1. 입력받은 다항식 : %s\n", poly); | |
printf("2. 미지수 X의 값 : %d\n", x); | |
// 다항식연산시작 | |
blankdel(poly); // 계산전 공백제거 | |
double sum = polycalc(poly, x); // 계산된 다항식값을 저장할 변수 | |
printf("3. 계산 결과 : %lf\n", sum); | |
printf("4. 정돈된 다항식 : %s\n", poly); | |
system("pause"); | |
return 0; | |
} | |
// 공백제거함수 | |
void blankdel(char *poly) | |
{ | |
int i = 0; | |
int cnt = 0; // 카운터 | |
while (i<100) | |
{ | |
if (poly[i] != ' ') | |
{ | |
poly[cnt] = poly[i]; | |
cnt++; | |
} | |
i++; | |
} | |
} | |
// 다항식연산함수 | |
double polycalc(char *poly, int x) | |
{ | |
int i = 0; // 배열번호를 가리킬 변수 | |
int tmp_coef = 0, tmp_exp = 0; // 임시저장용 계수-지수 변수 | |
double sum = 0; // 다항식의 총합변수 | |
tmp_coef = atoi(poly); //계수 초기값 지정 | |
for (; poly[i] != '\0'; i++) //더이상 식이 없으면 종결 | |
{ | |
// 구할 x값이 더 있는가? | |
if (poly[i] == 'x') | |
{ | |
tmp_exp = atoi(&poly[i + 2]); // ^뒷 숫자를 지수로 지정하고 | |
sum += (tmp_coef*(pow((double)x, tmp_exp)));// 구해놓은 계수와 x,지수를 이용한 합산 | |
} | |
else | |
{ | |
// +나 -가 있을 경우 계수값 변경 | |
if (poly[i] == '+' || poly[i] == '-') | |
{ | |
tmp_coef = atoi(&poly[i]); | |
} | |
} | |
} | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment