2007
04.25

資料結構作業第一題

編寫函數 readpoly 和 printpoly ,他們可以建立和印出多項式。

多項式輸入,輸出及相加,課本 P.65 EX.1(read poly and print poly)及多項式相加 (add poly),加法的演算法(algorithm)可以參考課本 P.62 program 2.4 或 P.64 program 2.5,課本:資料結構-使用C語言 松崗電腦圖書資料股份有限公司

#include <stdio.h>
#define MAX_TERMS 100

typedef struct {
    float coef; //每項的係數
    int expon;  //每項的指數
} polynomial;

polynomial terms[MAX_TERMS];  // 僅能儲存一百個項數和係數

int starta, finisha,  startb, finishb, startd, finishd;
int avail = 0;

/*
	starta 代表第一個多項式開始的位置
	finisha 代表第一個多項式結束的位置

	startb 代表第二個多項式開始的位置
	finishb 代表第二個多項式結束的位置

	startd 代表儲存結果的多項式開始的位置
	finishd 代表儲存結果的多項式結束的位置

	avail 代表可以使用的索引
*/

void readpoly();
void printpoly();
int compare(int i, int j);
void attach(float coefficient, int exponent);
void padd();
void presult();

int main(void)
{
	printf("\nSTEP 1. 讀取多項式...\n");
	readpoly();
	printf("\nSTEP 2. 印出剛剛輸入的兩個項式...\n");
	printpoly();
	padd();
	printf("\nSTEP 3. 兩個多項式相加的結果\n");
	presult();
    return 0;
}

void readpoly()
{

    int i,j;

	printf("本程式可以處理兩個多項式相加,請依序輸入第一和第二個多項式..\n");

	printf("請輸入您的第一個多項式的項次有幾項: ");
    scanf("%d", &i);

	starta = 0; //第一個多項式的起點一定是 0

    for(j = starta ; j < i ; j++)
	{

		terms[j].coef = 2;
		terms[j].expon = 3;
		/*
		printf("請輸入 %d 項的係數: ", j + 1);
        scanf("%f", &terms[j].coef);
		printf("請輸入 %d 項的指數: ", j + 1);
		scanf("%d", &terms[j].expon);
		*/
	}

	finisha = i - 1;

    printf("請輸入您的第二個多項式不為零的項次有幾項: ");

    scanf("%d", &j);

	startb = finisha + 1;

	finishb = startb + j;

    for(i = startb ; i <= finishb  ; i++)
	{
		terms[i].coef = 4;
		terms[i].expon = 5;
		/*
		printf("請輸入 %d 項的係數: ", j + 1);
        scanf("%f", &terms[j].coef);
		printf("請輸入 %d 項的指數: ", j + 1);
		scanf("%d", &terms[j].expon);
		*/
	}

	avail = finishb+1;

}

void printpoly()
{
	printf("印出第一個多項式:");
	for(int i = starta ; i <= finisha ; i++)
	{
        printf("%1.0fX^%d", terms[i].coef,terms[i].expon);	//	印出多項式
		if( i != finisha)
			printf("+"); // 印出多項式的加號
	}

	printf("\n印出第二個多項式:");
	for(i = startb ; i <= finishb ; i++)
	{
        printf("%1.0fX^%d", terms[i].coef,terms[i].expon);	//	印出多項式
		if( i != finishb)
			printf("+"); // 印出多項式的加號
	}

	printf("\n");
}

void padd()
{   // add A(X) and B(X) to obtain D(X)

	int t_starta = starta;
	int t_finisha = finisha;
	int t_startb = startb;
	int t_finishb = finishb;

	// 用區域變數取代全域變數,以免運算時影響全域變數的值

	float coefficient;

	printf("avail1. %d \n",avail);
    startd = avail;
    while(t_starta <=  t_finisha && t_startb <= t_finishb)
        switch (compare(terms[t_starta].expon, terms[t_startb].expon))
        {
            case -1: // a expon < b expon
				attach(terms[t_startb].coef, terms[t_startb].expon);
				t_startb++;
				break;

            case 0: // equal exponments
				coefficient = terms[t_starta].coef + terms[t_startb].coef;
				if (coefficient)
					attach(coefficient, terms[t_starta].expon);
				t_starta++;
				t_startb++;
				break;

            case 1: // a expon > b expon
				attach(terms[t_starta].coef, terms[t_starta].expon);
                t_starta++;
        }

    // add in remaining terms of A(X)

    for (; t_starta <= t_finisha ; t_starta++)
        attach(terms[t_starta].coef, terms[t_starta].expon);
    // add in remaining terms of B(X)

    for (; t_startb <= t_finishb ; t_startb++)
		attach(terms[t_startb].coef, terms[t_startb].expon);

	// avail 的值在 attach

    finishd = avail - 1;
}

void attach(float coefficient, int exponent)
{
    /* add a new term to the polynomial */

    if (avail >= MAX_TERMS)
    {
        printf("Too many terms in the polynomial. \n");
    }

    terms[avail].coef = coefficient;
    terms[avail++].expon = exponent;

}

int compare(int i, int j)
{
    int result;
    if( i < j )
        result = -1;
    else if( i == j)
        result = 0;
    else if( i > j )
        result = 1;
     return result;
}

void presult()
{
	printf("印出多項式相加的結果: ");
	for(int i = startd ; i <= finishd ; i++)
	{
        printf("%1.0fX^%d", terms[i].coef,terms[i].expon);	//	印出多項式
		if( i != finishd)
			printf("+"); // 印出多項式的加號
	}

	printf("\n");
}

6 comments so far

Add Your Comment
  1. 你現在在當助教 (or 講師)?

  2. 沒有,帥哥,我只是想造福學弟妹而已 :lol:

  3. 學長你好
    感謝你提供程式碼
    但是好像complier不過

    for(i=startb;i<=finishb;i++) 這行
    他顯示 In function `void printpoly()’:

    name lookup of `i’changed for new ISO `for’scoping

    for(int i=starta;i<=finisha;i++)這行
    他顯示 using obsolete binding at `i’

    我看不太懂錯誤在哪
    可不可以麻煩學長解答
    感謝你囉^^

  4. 楼上的把int i换成 int j别的变量 ;
    或者把这些变量在函数前部申明.

  5. 請問61,62行為何要這樣設定
    terms[j].coef = 2;
    terms[j].expon = 3;

    • 年代久遠,沒有仔細回去看程式,不過當時這樣寫,看來只是單純為了省略手動輸入而已。

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>