mirror of
https://github.com/kangjianwei/Data-Structure.git
synced 2026-08-18 01:10:14 +08:00
♻️ 记录m元多项式的元信息
This commit is contained in:
@@ -6,6 +6,6 @@ int main(int argc, char** argv) {
|
||||
char *s = "z((2,y((3,x((10,1),(6,2))),(2,x((5,3))))),(1,y((4,x((4,1),(3,6))),(1,x((0,2))))),(0,5))";
|
||||
|
||||
printf("创建三元多项式...\n");
|
||||
CreateMPList(&P, s, "xyz");
|
||||
CreateMPList(&P, s, "zxy");
|
||||
PrintGraph(P);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
|
||||
#include "MPList.h"
|
||||
|
||||
// 参见头文件中的声明
|
||||
char Var[27];
|
||||
|
||||
/*
|
||||
* 创建
|
||||
*
|
||||
@@ -27,6 +30,9 @@ Status CreateMPList(MPList* P, char* S, char* vars) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
// 初始化元信息
|
||||
strcpy(Var, vars);
|
||||
|
||||
*P = (MPList) malloc(sizeof(MPNode));
|
||||
if(*P == NULL) {
|
||||
exit(OVERFLOW);
|
||||
@@ -107,6 +113,7 @@ static Status Create(MPList* P, char* S) {
|
||||
*/
|
||||
void PrintGraph(MPList P) {
|
||||
if(P == NULL) {
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
Print(P->Node.hp);
|
||||
|
||||
@@ -14,10 +14,19 @@
|
||||
/*
|
||||
* ████ 提示 ████
|
||||
*
|
||||
* 1.对于多元函数,约定第一元位于最内层。
|
||||
* 1.对于多元函数,约定第一元位于最外层。
|
||||
* 2.未知数标记默认为a~z或A~Z这26个字母
|
||||
* 3.约定多项式的每个元其指数是【递减】的,如3、2、1
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* 储存当前多项式的遍历信息,如x、y、z等
|
||||
* 约定var中的字符顺序即位各元的次序,如:
|
||||
* "zyx"指示z为第一元,y为第二元,x为第三元
|
||||
*/
|
||||
extern char Var[27];
|
||||
|
||||
/*
|
||||
* m元多项式结点标记
|
||||
*
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
int main(int argc, char** argv) {
|
||||
MPList P;
|
||||
char *s = "z((2,y((3,x((10,1),(6,2))),(2,x((5,3))))),(1,y((4,x((4,1),(3,6))),(1,x((0,2))))),(0,5))";
|
||||
|
||||
|
||||
printf("创建三元多项式...\n");
|
||||
CreateMPList(&P, s, "xyz");
|
||||
CreateMPList(&P, s, "zxy");
|
||||
PrintGraph(P);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
|
||||
#include "MPList.h"
|
||||
|
||||
// 参见头文件中的声明
|
||||
char Var[27];
|
||||
|
||||
/*
|
||||
* 创建
|
||||
*
|
||||
@@ -18,26 +21,29 @@ Status CreateMPList(MPList* P, char* S, char* vars) {
|
||||
if(P == NULL) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
|
||||
// 清理字符串S中的空白,包括清理不可打印字符和清理空格
|
||||
ClearBlank(&S);
|
||||
|
||||
|
||||
if(strlen(S) == 0) {
|
||||
*P = NULL;
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
|
||||
// 初始化元信息
|
||||
strcpy(Var, vars);
|
||||
|
||||
*P = (MPList) malloc(sizeof(MPNode));
|
||||
if(*P == NULL) {
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
|
||||
|
||||
(*P)->tag = List;
|
||||
(*P)->exp = (int) strlen(vars);
|
||||
(*P)->tp = NULL;
|
||||
|
||||
|
||||
Create(&(*P)->Node.hp, S);
|
||||
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
@@ -50,32 +56,32 @@ static Status Create(MPList* P, char* S) {
|
||||
char* sub;
|
||||
MPList r;
|
||||
float f;
|
||||
|
||||
|
||||
// 获取S的一个副本
|
||||
SubString(&Sc, S, 1, (int) strlen(S));
|
||||
|
||||
|
||||
*P = (MPList) malloc(sizeof(MPNode));
|
||||
if(*P == NULL) {
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
|
||||
|
||||
(*P)->tag = List;
|
||||
(*P)->exp = (int) Sc[0]; // 记下未知数标记,例如x、y、z
|
||||
(*P)->Node.hp = NULL;
|
||||
(*P)->tp = NULL;
|
||||
|
||||
|
||||
StrDelete(&Sc, 1, 1); // 删掉未知数标记
|
||||
SubString(&str, Sc, 2, (int) strlen(Sc) - 2); // 脱去最外层括号
|
||||
|
||||
|
||||
r = *P;
|
||||
|
||||
|
||||
while(!StrEmpty(str)) {
|
||||
// 拆分
|
||||
sever(&hstr, &str);
|
||||
|
||||
|
||||
SubString(&sub, hstr, 2, (int) strlen(hstr) - 2); // 脱去最外层括号
|
||||
sever(&hhstr, &sub);
|
||||
|
||||
|
||||
// 建立子结点
|
||||
r->tp = (MPList) malloc(sizeof(MPNode));
|
||||
if(r->tp == NULL) {
|
||||
@@ -84,7 +90,7 @@ static Status Create(MPList* P, char* S) {
|
||||
GetElem(hhstr, 1, &f);
|
||||
r->tp->exp = (int) f; // 获取指数
|
||||
r->tp->tp = NULL;
|
||||
|
||||
|
||||
if(ElemCount(sub) == 1) {
|
||||
r->tp->tag = Atom;
|
||||
GetElem(sub, 1, &f);
|
||||
@@ -93,10 +99,10 @@ static Status Create(MPList* P, char* S) {
|
||||
r->tp->tag = List;
|
||||
Create(&(r->tp->Node.hp), sub);
|
||||
}
|
||||
|
||||
|
||||
r = r->tp;
|
||||
}
|
||||
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
@@ -107,6 +113,7 @@ static Status Create(MPList* P, char* S) {
|
||||
*/
|
||||
void PrintGraph(MPList P) {
|
||||
if(P == NULL) {
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
Print(P->Node.hp);
|
||||
@@ -118,33 +125,33 @@ void PrintGraph(MPList P) {
|
||||
*/
|
||||
static void Print(MPList head) {
|
||||
MPList p;
|
||||
|
||||
|
||||
if(head == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
printf("%c(", head->exp);
|
||||
|
||||
|
||||
p = head->tp;
|
||||
|
||||
|
||||
while(p != NULL) {
|
||||
printf("(%d,", p->exp);
|
||||
|
||||
|
||||
if(p->tag == List) {
|
||||
Print(p->Node.hp);
|
||||
} else {
|
||||
printf("%.2f", p->Node.coef);
|
||||
}
|
||||
|
||||
|
||||
printf(")");
|
||||
|
||||
|
||||
p = p->tp;
|
||||
|
||||
|
||||
if(p != NULL) {
|
||||
printf(",");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
printf(")");
|
||||
}
|
||||
|
||||
@@ -157,39 +164,39 @@ static void Print(MPList head) {
|
||||
*/
|
||||
static void sever(char** hstr, char** str) {
|
||||
int i, k, n;
|
||||
|
||||
|
||||
char* head, * tail;
|
||||
|
||||
|
||||
// str为空时,hstr也为空
|
||||
if(strlen(*str) == 0) {
|
||||
*hstr = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
n = (int) strlen(*str);
|
||||
|
||||
|
||||
i = 0; // 遍历字符串时的游标
|
||||
k = 0; // 标记遇到的未配对括号数量
|
||||
|
||||
|
||||
do {
|
||||
if((*str)[i] == '(') {
|
||||
++k;
|
||||
}
|
||||
|
||||
|
||||
if((*str)[i] == ')') {
|
||||
--k;
|
||||
}
|
||||
|
||||
|
||||
i++;
|
||||
} while(i < n && ((*str)[i] != ',' || k != 0));
|
||||
|
||||
|
||||
if(i < n) {
|
||||
head = (char*) malloc((i + 1) * sizeof(char));
|
||||
tail = (char*) malloc((n - i - 1 + 1) * sizeof(char));
|
||||
|
||||
|
||||
strncpy(head, *str, i);
|
||||
head[i] = '\0';
|
||||
|
||||
|
||||
strncpy(tail, (*str + i + 1), n - i - 1);
|
||||
tail[n - i - 1] = '\0';
|
||||
} else {
|
||||
@@ -197,7 +204,8 @@ static void sever(char** hstr, char** str) {
|
||||
tail = (char*) malloc(sizeof(char));
|
||||
tail[0] = '\0';
|
||||
}
|
||||
|
||||
|
||||
*hstr = head;
|
||||
*str = tail;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,19 @@
|
||||
/*
|
||||
* ████ 提示 ████
|
||||
*
|
||||
* 1.对于多元函数,约定第一元位于最内层。
|
||||
* 1.对于多元函数,约定第一元位于最外层。
|
||||
* 2.未知数标记默认为a~z或A~Z这26个字母
|
||||
* 3.约定多项式的每个元其指数是【递减】的,如3、2、1
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* 储存当前多项式的遍历信息,如x、y、z等
|
||||
* 约定var中的字符顺序即位各元的次序,如:
|
||||
* "zyx"指示z为第一元,y为第二元,x为第三元
|
||||
*/
|
||||
extern char Var[27];
|
||||
|
||||
/*
|
||||
* m元多项式结点标记
|
||||
*
|
||||
@@ -80,3 +89,4 @@ static void Print(MPList P);
|
||||
static void sever(char** hstr, char** str);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -6,6 +6,6 @@ int main(int argc, char** argv) {
|
||||
char *s = "z((2,y((3,x((10,1),(6,2))),(2,x((5,3))))),(1,y((4,x((4,1),(3,6))),(1,x((0,2))))),(0,5))";
|
||||
|
||||
printf("创建三元多项式...\n");
|
||||
CreateMPList(&P, s, "xyz");
|
||||
CreateMPList(&P, s, "zxy");
|
||||
PrintGraph(P);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
|
||||
#include "MPList.h"
|
||||
|
||||
// 参见头文件中的声明
|
||||
char Var[27];
|
||||
|
||||
/*
|
||||
* 创建
|
||||
*
|
||||
@@ -27,6 +30,9 @@ Status CreateMPList(MPList* P, char* S, char* vars) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
// 初始化元信息
|
||||
strcpy(Var, vars);
|
||||
|
||||
*P = (MPList) malloc(sizeof(MPNode));
|
||||
if(*P == NULL) {
|
||||
exit(OVERFLOW);
|
||||
@@ -107,6 +113,7 @@ static Status Create(MPList* P, char* S) {
|
||||
*/
|
||||
void PrintGraph(MPList P) {
|
||||
if(P == NULL) {
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
Print(P->Node.hp);
|
||||
|
||||
@@ -14,10 +14,19 @@
|
||||
/*
|
||||
* ████ 提示 ████
|
||||
*
|
||||
* 1.对于多元函数,约定第一元位于最内层。
|
||||
* 1.对于多元函数,约定第一元位于最外层。
|
||||
* 2.未知数标记默认为a~z或A~Z这26个字母
|
||||
* 3.约定多项式的每个元其指数是【递减】的,如3、2、1
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* 储存当前多项式的遍历信息,如x、y、z等
|
||||
* 约定var中的字符顺序即位各元的次序,如:
|
||||
* "zyx"指示z为第一元,y为第二元,x为第三元
|
||||
*/
|
||||
extern char Var[27];
|
||||
|
||||
/*
|
||||
* m元多项式结点标记
|
||||
*
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user