[問題] 如何用POW做未知數的運算
開發平台(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
09/24 23:51, 1F
→
09/24 23:59, , 2F
09/24 23:59, 2F
→
09/25 00:09, , 3F
09/25 00:09, 3F
→
09/25 00:10, , 4F
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
09/25 00:18, 7F
→
09/25 00:24, , 8F
09/25 00:24, 8F
→
09/25 00:34, , 9F
09/25 00:34, 9F
→
09/25 00:47, , 10F
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
09/25 02:07, 13F
推
09/25 22:33, , 14F
09/25 22:33, 14F
→
09/25 22:37, , 15F
09/25 22:37, 15F
→
09/25 22:38, , 16F
09/25 22:38, 16F
→
09/25 22:40, , 17F
09/25 22:40, 17F
→
09/25 22:58, , 18F
09/25 22:58, 18F
→
09/25 23:00, , 19F
09/25 23:00, 19F
→
09/28 20:39, , 20F
09/28 20:39, 20F