/*----------------------------------------------------------------------------------------- * Project Name : Report04.1 - polynomial expression(Array) * Programmed by : Young Min Jo * Student Number : 2008060021 * Date : Sunday 6 April 2014 * (c) Copyright by Department of Computer Education at Chungbuk * National University, Chungbuk, Republic of Korea ------------------------------------------------------------------------------------------*/ #include "stdafx.h" #include #include #include #define MAX 100 /*-------------------------------------- calculate ---------------------------------------- /* Function : º¯¼ö xval¿¡µû¸¥ ´ÙÇ×½Ä °è»ê ------------------------------------------------------------------------------------------*/ float calculate(float *poly, int degree, float xval){ float result = 0; for (int i = 0; i <= degree; i++){ result += poly[i] * pow(xval, i); } return result; } /*--------------------------------------- addpoly ----------------------------------------- /* Function : º¯¼ö xval¿¡µû¸¥ ´ÙÇ×½Ä µÎ°³ÀÇ °è»ê ÇÕ ------------------------------------------------------------------------------------------*/ float addpoly(float *poly1, int degree1, float *poly2, int degree2, float x){ float poly[MAX] = {} ; int result = 0; int degree; if (degree1 > degree2){ degree = degree1; } else{ degree = degree2; } for (int i = 0; i <= degree; i++){ poly[i] = poly1[i] + poly2[i]; } result = calculate(poly, degree, x); return result; } //Menu void Menu(){ printf("*****Polynomial Expression(Array)*****\n"); printf("1. Poly1\n"); printf("2. Poly2\n"); printf("3. Poly1 + Poly2\n"); printf("4. EXIT\n"); } void main(){ FILE *infp1, *infp2; int i; int check; float x, result; float poly1[MAX] = {}; int degree1; float poly2[MAX] = {}; int degree2; //*.txt¿¡¼­ ÃÖ°íÂ÷¿Í °¢ Ç×ÀÇ °è¼ö¸¦ ¹ÞÀ½ infp1 = fopen("poly1(array).txt", "r"); if (infp1 == NULL){ printf("ÆÄÀϾøÀ½"); exit(0); } infp2 = fopen("poly2(array).txt", "r"); if (infp2 == NULL){ printf("ÆÄÀϾøÀ½"); exit(0); } fscanf(infp1, "%d", °ree1); fscanf(infp2, "%d", °ree2); if (degree1 > MAX || degree2 > MAX){ printf("Áö¼öÀÇ Å©±â°¡ ³Ê¹« Å®´Ï´Ù.\n"); exit(0); } for (i = 0; i <= degree1; i++){ fscanf(infp1, "%f", &poly1[i]); } for (i = 0; i <= degree2; i++){ fscanf(infp2, "%f", &poly2[i]); } fclose(infp1); fclose(infp2); while (1){ Menu(); printf("¸Þ´º ¹øÈ£ ÀÔ·Â : "); scanf("%d", &check); switch (check){ case 1: printf("x °ª : "); scanf("%f", &x); result = calculate(poly1, degree1, x); printf("Poly1 : %0.1f\n", result); break; case 2: printf("x °ª : "); scanf("%f", &x); result = calculate(poly2, degree2, x); printf("Poly2 : %0.1f\n", result); break; case 3: printf("x °ª : "); scanf("%f", &x); result = addpoly(poly1, degree1, poly2, degree2, x); printf("Poly1 + Poly2 : %0.1f\n", result); break; case 4: exit(0); default: break; } } }