/*----------------------------------------------------------------------------------------- * Project Name : Report04.2 - polynomial expression(Struct) * 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 typedef struct term{ float coef; int expon; }; /*-------------------------------------- calculate ---------------------------------------- /* Function : º¯¼ö xval¿¡µû¸¥ ´ÙÇ×½Ä °è»ê ------------------------------------------------------------------------------------------*/ float calculate(term *poly, int count, float xval){ float result = 0; for (int i = 0; i <= count; i++){ result += poly[i].coef * pow(xval, poly[i].expon); } return result; } /*--------------------------------------- addpoly ----------------------------------------- /* Function : º¯¼ö xval¿¡µû¸¥ ´ÙÇ×½Ä µÎ°³ÀÇ °è»ê ÇÕ ------------------------------------------------------------------------------------------*/ float addpoly(term *poly1, int count1, term *poly2, int count2, float xval){ term poly[MAX] = { 0 }; int result = 0; int count = 0; int i = 0; int j = 0; while (i < count1 && j < count2){ //´ÙÇ×½Ä Áö¼ö°¡ °°À»¶§ °è¼ö¸¦ ´õÇؼ­ ÀúÀåÇÏ°í µÎ°³ÀÇ ´ÙÇ×½Ä index Áõ°¡ if (poly1[i].expon == poly2[j].expon){ poly[count].coef = poly1[i].coef + poly2[j].coef; poly[count].expon = poly2[j].expon; count++; i++; j++; } //´ÙÇ×½Ä Áö¼ö°¡ ´Ù¸¦¶§ Áö¼ö°¡ ÀÛÀºÂÊÀÇ °è¼ö ÀúÀå°ú index Áõ°¡ else if (poly1[i].expon > poly2[j].expon){ poly[count].coef = poly2[j].coef; poly[count].expon = poly2[j].expon; count++; j++; } else{ poly[count].coef = poly1[i].coef; poly[count].expon = poly1[i].expon; count++; i++; } } //µÎ°³ ´ÙÇ×½Ä ºñ±³ÈÄ ÃÖ°íÂ÷°¡ ´õÅ« ´ÙÇ×½ÄÀÇ ³ª¸ÓÁö °è»ê for (; i < count1; i++){ poly[count].coef = poly1[i].coef; poly[count].expon = poly1[i].expon; count++; } for (; j < count2; j++){ poly[count].coef = poly2[j].coef; poly[count].expon = poly2[j].expon; count++; } result = calculate(poly, count, xval); return result; } //Menu void Menu(){ printf("*****Polynomial Expression(Struct)*****\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; term poly1[MAX] = { 0 }; term poly2[MAX] = { 0 }; int count1, count2;// °è¼öÀÇ °³¼ö //*.txt¿¡¼­ °è¼öÀÇ °³¼ö¿Í Áö¼ö¿Í Áö¼ö¿¡ ÇØ´çµÇ´Â °è¼ö¸¦ ¹ÞÀ½ infp1 = fopen("poly1(struct&linkedlist).txt", "r"); if (infp1 == NULL){ printf("ÆÄÀϾøÀ½"); exit(0); } infp2 = fopen("poly2(struct&linkedlist).txt", "r"); if (infp2 == NULL){ printf("ÆÄÀϾøÀ½"); exit(0); } fscanf(infp1, "%d", &count1); fscanf(infp2, "%d", &count2); if (count1 > MAX || count2 > MAX){ printf("°è¼öÀÇ °³¼ö°¡ ³Ê¹« ¸¹½À´Ï´Ù.\n"); exit(0); } for (i = 0; i < count1; i++){ fscanf(infp1, "%f %d", &poly1[i].coef, &poly1[i].expon); } for (i = 0; i < count2; i++){ fscanf(infp2, "%f %d", &poly2[i].coef, &poly2[i].expon); } fclose(infp1); fclose(infp2); while (1){ Menu(); printf("¸Þ´º ¹øÈ£ ÀÔ·Â : "); scanf("%d", &check); switch (check){ case 1: printf("x °ª : "); scanf("%f", &x); result = calculate(poly1, count1, x); printf("Poly1 : %0.1f\n", result); break; case 2: printf("x °ª : "); scanf("%f", &x); result = calculate(poly2, count2, x); printf("Poly2 : %0.1f\n", result); break; case 3: printf("x °ª : "); scanf("%f", &x); result = addpoly(poly1, count1, poly2, count2, x); printf("Poly1 + Poly2 : %0.1f\n", result); break; case 4: exit(0); default: break; } } }