[問題] 如何用POW做未知數的運算

看板C_and_CPP作者 (vitamindg)時間12年前 (2013/09/24 15:48), 編輯推噓1(1019)
留言20則, 7人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) vc 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): POW對未知數的運算方法該如何運用 餵入的資料(Input): 陣列: 2 2 1 1 0 0 輸入X值: 5 預期的正確結果(Expected Output): 2x^2+1x^1 2x^2+1x^1=55 錯誤結果(Wrong Output): 只顯示2X^2+1X^1 程式碼:*[m // HW2.cpp : 定義主控台應用程式的進入點。 // //#include "stdafx.h" #include "stdio.h" #include "math.h" #include "memory.h" #include <iostream> #include <string> using namespace std; #define MAX_degree 20 /*MAX degree of polynomial+1*/ typedef struct{ int degree; int coef [MAX_degree]; } polynomial; #define MAX_TERMS 20 /*size of terms array*/ typedef struct{ int coef; int expo; } polynomial_term; polynomial_term terms [MAX_TERMS]; int avail = 0; void input(string sMessage, string &sInputString) { cout << sMessage << endl; cin >> sInputString; return; } void output(string sMessage) { cout << endl << sMessage << endl; return; } polynomial Zero(polynomial p) { memset(&p, 0, sizeof(p)); return p; } int IsZero(polynomial p) { if (p.degree < 0) return 1; if (p.degree == 0 && p.coef[0] == 0) return 1; return 0; } int Lead_Exp(polynomial p) { return p.degree; } int COMPARE(int i, int j) { if (i < j) return -1; if (i > j) return 1; return 0; } int Coef(polynomial p, int expo) { return p.coef[expo]; } polynomial Attach(polynomial p, int coef, int expo) { p.coef[expo] = coef; if (coef > 0 && p.degree < expo) p.degree = expo; return p; } polynomial Remove(polynomial p, int expo) { p.degree--; return p; } void attach(int coef, int expo) { if (avail >= MAX_TERMS) return; terms[avail].coef = coef; terms[avail++].expo = expo; } void PrintPoly1(polynomial p) { for (int i = p.degree; i >= 0; i--) { if (p.coef[i] != 0 ) { if (p.degree == i) { if (i == 0 ) printf("%d", p.coef[i]); else printf("%dX^%d", p.coef[i], i); } else { if ( i == 0 ) printf(" + %d", p.coef[i]); else printf(" + %dX^%d", p.coef[i], i); } } } } void PrintPoly2(polynomial_term t[], int start, int finish) { for (int i = start; i <= finish; i++) { if (t[i].coef != 0) { if ( i == start ) { if (t[i].expo == 0) printf("%d", t[i].coef); else printf("%dX^%d", t[i].coef, t[i].expo); } else { if (t[i].expo == 0) printf(" + %d", t[i].coef); else printf(" + %dX^%d", t[i].coef, t[i].expo); } } } } polynomial padd1(polynomial p1, polynomial p2) { polynomial p3; int sum; p3.degree = 0; p3 = Zero(p3); while ( ! IsZero(p1) && ! IsZero(p2) ) { switch ( COMPARE( Lead_Exp(p1), Lead_Exp(p2) ) ) { case -1: p3 = Attach(p3, Coef(p2, Lead_Exp(p2)), Lead_Exp(p2)); p2 = Remove(p2, Lead_Exp(p2)); break; case 0: sum = Coef(p1, Lead_Exp (p1)) + Coef( p2, Lead_Exp(p2)); if (sum) p3 = Attach (p3, sum, Lead_Exp(p1)); p1 = Remove(p1 , Lead_Exp(p1)); p2 = Remove(p2 , Lead_Exp(p2)); break; case 1: p3 = Attach(p3, Coef (p1, Lead_Exp(p1)), Lead_Exp(p1)); p1 = Remove(p1, Lead_Exp(p1)); } } while (!IsZero(p1) ) { p3 = Attach(p3, Coef (p1, Lead_Exp(p1)), Lead_Exp(p1)); p1 = Remove(p1, Lead_Exp(p1)); } while (!IsZero(p2) ) { p3 = Attach(p3, Coef (p2, Lead_Exp(p2)), Lead_Exp(p2)); p2 = Remove(p2, Lead_Exp(p2)); } return p3; } void padd2(int starta, int finisha, int startb, int finishb, int* startd, int* finishd) { int sum; *startd = avail; while (starta <= finisha && startb <= finishb) { switch (COMPARE( terms[starta].expo, terms[startb].expo) ) { case -1: attach(terms[startb].coef, terms[startb].expo); startb++; break; case 0: sum = terms[starta].coef + terms[startb].coef; if (sum) attach(sum, terms[starta].expo); starta++; startb++; break; case 1: attach(terms[starta].coef, terms[starta].expo); starta++; } } for (; starta <= finisha; starta++) attach(terms[starta].coef, terms[starta].expo); for (; startb <= finishb; startb++) attach(terms[startb].coef, terms[startb].expo); *finishd = avail - 1; } int main() { string sCommand, sName, Num, sMessage; polynomial a, b, d; int coef, expo, sum, value; int starta = 0, finisha, startb, finishb, startd, finishd; memset(terms, 0, sizeof(terms)); a.degree = b.degree = d.degree = 0; a = Zero(a); b = Zero(b); d = Zero(d); while (1) { input( "Enter command: " "\n I to input polynomials a and b, C to compute a + b, " "\n S to show polynomials, X to Exit", sCommand ); if ( sCommand == "I" || sCommand == "i" ) { // add your code here while (1) { printf("\ninput a's coefficient and exponent: "); scanf("%d %d", &coef, &expo); if (expo == 0 && coef == 0) break; a.coef[expo] = coef; if (a.degree < expo) a.degree = expo; terms[avail].coef = coef; terms[avail].expo = expo; avail++; } finisha = avail - 1; startb = avail; printf("\n\na = "); PrintPoly1(a); printf("\na = "); PrintPoly2(terms, starta, finisha); printf("\n"); while (1) { printf("\ninput b's coefficient and exponent: "); scanf("%d %d", &coef, &expo); if (expo == 0 && coef == 0) break; b.coef[expo] = coef; if (b.degree < expo) b.degree = expo; terms[avail].coef = coef; terms[avail].expo = expo; avail++; } finishb = avail - 1; printf("\n\nb = "); PrintPoly1(b); printf("\nb = "); PrintPoly2(terms, startb, finishb); } if ( sCommand == "S" || sCommand == "s" ) { // add your code here finisha = avail - 1; startb = avail; printf("\na = "); PrintPoly1(a); printf("\na = "); PrintPoly1(a); finishb = avail - 1; printf("\n\nb = "); PrintPoly1(b); printf("\nb = "); PrintPoly1(b); } if ( sCommand == "C" || sCommand == "c" ) { printf("\nInput the value of X: "); scanf("%d", &value); // add your code here d = padd1(a, b); printf("\n"); PrintPoly1(d); //在這邊做POW的運算 將X的質帶進去 求結果 } if ( sCommand == "X" || sCommand == "x" ) sMessage = "Unknown command!"; output ( sMessage ); } system("PAUSE"); return 0; } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.162.99.124

09/24 23:51, , 1F
都開學了這麼久才HW2啊...
09/24 23:51, 1F

09/25 00:09, , 3F
貼程式碼都不看置底的喔...
09/25 00:09, 3F

09/25 00:10, , 4F
應該是遲交吧 XD
09/25 00:10, 4F

09/25 00:13, , 5F
想求解 想了很久
09/25 00:13, 5F

09/25 00:14, , 6F
解答就在連結中
09/25 00:14, 6F

09/25 00:18, , 7F
s大大 我看了 但我要加總 做總和 X是未知數...這樣不知
09/25 00:18, 7F

09/25 00:24, , 8F
就把 x 和 expo 一起傳進 pow() 計算, 算完再乘 coef ...
09/25 00:24, 8F

09/25 00:34, , 9F
我這樣寫pow(expo, value); 他說POW錯誤
09/25 00:34, 9F

09/25 00:47, , 10F
pow要用浮點數
09/25 00:47, 10F

09/25 00:50, , 11F
我輸入的直都是正整數
09/25 00:50, 11F

09/25 01:31, , 12F
強制轉型
09/25 01:31, 12F

09/25 02:07, , 13F
int轉double不是可以無痛自動轉型嗎? 到底error是什麼?
09/25 02:07, 13F

09/25 22:33, , 14F
code直接end了,看推文應該是把正整數丟進pow了吧
09/25 22:33, 14F

09/25 22:37, , 15F
pow吃float和double,他不知道int該轉哪個
09/25 22:37, 15F

09/25 22:38, , 16F
吃float的是powf()吧,pow()是double版,還有加長版powl()
09/25 22:38, 16F

09/25 22:40, , 17F
c++全部都是pow的overload
09/25 22:40, 17F

09/25 22:58, , 18F
沒看見是C++... Orz
09/25 22:58, 18F

09/25 23:00, , 19F
幹嘛把 C 寫的 code 附檔名改成 cpp 然後加個 cout 啊?
09/25 23:00, 19F

09/28 20:39, , 20F
樓上...他是cout/printf混著用
09/28 20:39, 20F
文章代碼(AID): #1IGRH4uQ (C_and_CPP)