請問如何開啟兩個檔,並讓兩個檔案裡面的數值做運算?用visual c++的形式 |
尚未結案
|
56angela
一般會員 發表:12 回覆:13 積分:5 註冊:2005-10-01 發送簡訊給我 |
假設"已經"將數值分別寫入兩個txt檔,檔名poly_a跟檔名poly_b
數值是:
a檔案
coef expon
-4 15
8 7
13 0 b檔案
coef expon
17 15
-16 3
5 1 做多項式的相加 a檔案資料代表 -4X^15 + 8X^7 + 13X^0 b檔案資料代表 17X^15 - 16X^3 + 5X^1 印出:13X^15 + 8X^7 - 16X^3 + 5X^1 + 13X^0 將兩個多項式相加,想請問該如何用"fscanf"讀取兩個檔案的資料, (1)需要將檔案b的內容複製到檔案a再讀取嗎 ? 還是可以兩個檔一起開並做運算
(2)該怎麼分別將 coef 跟 expon 的值代入 padd副函式 做運算? (3) printf("%.0fX^%d%c",terms[i].coef,terms[i].expon,i==b?'':'+');
這行又是什麼意思呢? 想麻煩大家幫我解答~謝謝
void main() { typedef struct { float coef; int expon; }polynomial; polynomial terms[MAX_TERMS]; int avail=0; int a,b,i,j,x,y; FILE *fptr1,*fptr2; char ch1,ch2; if ((fptr1 = fopen("d:\\資料結構\\poly_a.txt","r")) &&(fptr2 = fopen("d:\\資料結構\\poly_b.txt","r"))!= NULL) { while ((ch1=getc(fptr1)) && (ch2=getc(fptr2)) != EOF) { fscanf(fptr1,"%d%d",&x,&a); fscanf(fptr2,"%d%d",&y,&b); printf("%d%d",x,y,z,w); padd(0,2,3,5,a,b); for(i=a;i<=b;i ) printf("%.0fX^%d%c",terms[i].coef,terms[i].expon,i==b?'':' '); } } }以上的主程式我寫不出來...寫的有點亂七八糟...|||...可以教我應該該如何改嗎?謝謝(主要是想請教主程式的寫法與讀檔的問題) 以下是padd副程式(做多項式的相加)與attach副程式 void padd(int starta,int finisha,int startb,int finishb,int *startd,int *finishd) { float coefficient; *startd = avail; while ( starta <= finisha && startb <= finishb) switch(COMPARE(terms[startb].expon,terms[starta].expon)) { case -1: attach(terms[startb].coef,terms[startb].expon); startb ; break; case 0: coefficient = terms[starta].coef terms[startb].coef; if(coefficient) attach(coefficient,terms[starta].expon); starta ; startb ; break; case 1: attach(terms[starta].coef,terms[starta].expon); starta ; } for(; starta <= finisha ; starta ) attach(terms[starta].coef,terms[starta].expon); for(; startb <= finishb ; startb ) attach(terms[startb].coef,terms[startb].expon); *finishd = avail-1; } void attach(float coefficient,int exponent) { if(avail >= MAX_TERMS) { fprintf(stderr,"Too many terms in the polynomial\n"); exit(1); } terms[avail].coef = coefficient; terms[avail ].expon = exponent; }發表人 - 56angela 於 2005/11/12 11:16:12 發表人 - 56angela 於 2005/11/12 11:18:53 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |