真的是純純的小白,然后現在要求用c++完成多項式運算(含括號)的那種,但是程式寫出來就是有問題!在Calculate中創建堆疊以后會莫名其妙地呼叫getpop方法,然后就會報錯,求大佬救救小白哇!
#include <stdio.h>
#include<math.h>
#include<stdlib.h>
#include <malloc.h>
#include <iostream>
using namespace std;
typedef int ElemType;
typedef int Status;
//定義符號鏈式堆疊
typedef struct SNode* Stack;
struct SNode{
char data;
struct SNode* next;
};
//初始化
Stack InitList(){
Stack L;
L = (Stack)malloc(sizeof(struct SNode));
L->next = NULL;//創建頭結點
return L;
}
//入堆疊
void push(Stack s, ElemType x){
Stack TmpCell;
TmpCell = (Stack)malloc(sizeof(struct SNode));
TmpCell->data = x;
TmpCell->next = s->next;
s->next = TmpCell;
}
//出堆疊
char pop(Stack s){
Stack FirstCell;
ElemType temp;
FirstCell = s->next;
s->next = FirstCell->next;
temp = FirstCell->data;
free(FirstCell); return temp;
}
//定義堆疊頂元素優先級
Status isp(char ch){
switch(ch){
case'#': return 0;
case'(': return 1;
case'+': return 3;
case'-': return 3;
case'*': return 5;
case'/': return 5;
case'%': return 5;
case'^': return 7;
case')': return 8;
}
}
//獲得堆疊頂元素
char getpop(Stack s){
char temp;
temp = s->next->data;
return temp;
}
//定義當前元素優先級
Status osp(char ch){
switch(ch){
case'#': return 1;
case'(': return 8;
case'+': return 2;
case'-': return 2;
case'*': return 4;
case'/': return 4;
case'%': return 4;
case'^': return 6;
case')': return 1;
}
}
//定義計算方法
void count(Stack nuper, char topc){
int result = 0;
int number1, number2;
number2 = pop(nuper)-48;
number1 = pop(nuper)-48;
switch(topc){
case'+': {result = number1 + number2; break;}
case'-': {result = number1 - number2; break;}
case'*': {result = number1 * number2; break;}
case'/': {result = number1 / number2; break;}
case'%': {result = number1 % number2; break;}
case'^': {result = pow(number1, number2); break;}
}
push(nuper, result);
}
//計算程序
char Calculate(char* e){
char c,topc;
Stack nuper = InitList(); //創建數字堆疊
Stack oper = InitList(); //創建符號堆疊
int i;
push(oper,'#');
i = 0; c = e[0];
while(c != '#'){
if(c >= 48 && c <= 57)
push(nuper, c); //將c輸入數字堆疊
else
{
if(c == 41)
{
topc = pop(oper);
while(c != 40){
count(nuper, topc); //拋出數字堆疊最后兩個數字并進行操作,將結果再入堆疊
topc = pop(oper);
}
pop(oper); //拋棄堆疊中的'('
}
else{
topc = getpop(oper);
while(osp(c) < isp(topc)){ //比較當前符號和堆疊中符號的優先級
topc = pop(oper);
count(nuper, topc); //拋出數字堆疊最后兩個數字并進行操作,將結果再入堆疊
topc = getpop(oper);
}
}push(oper, c);
}c = e[++i];
}topc = pop(oper); while(topc != '#') {count(nuper, topc); topc = pop(oper);}
return (pop(nuper));
}
int main(){
cout<<"輸入多項式:";
char e[256];
cin>>e;
char total = Calculate(e);
cout<<"最終結果為:"<<total;
return 0;
}
uj5u.com熱心網友回復:
僅供參考://鏈表實作一元多項式的加法減法乘法
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
float coef; //系數
int expn; //指數
struct node *next;
}
PolyNode; //多項式節點 polynomial node
typedef PolyNode* Polynomial;
Polynomial createPolynomial() { //創建多項式
PolyNode *p, *q, *head = (PolyNode *)malloc(sizeof(PolyNode)); //頭節點
head->next = NULL;
float coef;
int expn;
printf("輸入該多項式每一項的系數和指數,每項一行,輸入0 0結束!\n");
while (scanf("%f %d", &coef, &expn) && coef) { // 默認,按指數遞減排列
if (head->next) {
p = head;
while (p->next && expn < p->next->expn)
p = p->next;
if (p->next) {
if (expn == p->next->expn) { //有相同指數的直接把系數加到原多項式
p->next->coef += coef;
if (p->next->coef > -0.000001 && p->next->coef < 0.000001) { //若是相加后系數為0,則舍棄該節點
q = p->next;
p->next = q->next;
free(q);
}
} else {
q = (PolyNode*)malloc(sizeof(PolyNode));
q->coef = coef;
q->expn = expn;
q->next = p->next;
p->next = q;
}
} else {
p->next = (PolyNode*)malloc(sizeof(PolyNode));
p = p->next;
p->coef = coef;
p->expn = expn;
p->next = NULL;
}
} else {
head->next = (PolyNode*)malloc(sizeof(PolyNode));
head->next->coef = coef;
head->next->expn = expn;
head->next->next = NULL;
}
}
return head;
}
Polynomial multiply(Polynomial poly, float coef, int expn) { //多項式與指定單項式相乘,該單項式為 coefx^expn
PolyNode *p, *q, *Poly = (PolyNode*)malloc(sizeof(PolyNode));
p = Poly;
q = poly->next;
while (q) {
p->next = (PolyNode*)malloc(sizeof(PolyNode));
p = p->next;
p->coef = (q->coef*coef);
p->expn = (q->expn + expn);
q = q->next;
}
p->next = NULL;
return Poly;
}
void add(Polynomial poly1, Polynomial poly2) { //把 poly2 加到 poly1 上
PolyNode *p, *q, *r;
r = poly1;
p = poly1->next; //指向第一個節點
q = poly2->next;
poly2->next = NULL;
while (p && q) {
if (p->expn > q->expn) {
r->next = p;
p = p->next;
r = r->next;
} else if (p->expn < q->expn) {
r->next = q;
q = q->next;
r = r->next;
} else {
PolyNode *t;
p->coef += q->coef;
if (!(p->coef > -0.000001 && p->coef < 0.000001)) //系數不為0
{
r->next = p;
r = r->next;
p = p->next;
} else {
t = p;
p = p->next;
free(t);
}
t = q;
q = q->next;
free(t);
}
}
if (p)
r->next = p;
if (q)
r->next = q;
}
Polynomial polySubtract(Polynomial poly1, Polynomial poly2) { //多項式減法 poly1-poly2形成一個新的多項式
//把poly2的系數取相反數,形成一個新的多項式
Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode)); //構造頭節點
PolyNode *p, *q;
p = poly;
q = poly2->next;
while (q) {
p->next = (PolyNode*)malloc(sizeof(PolyNode));
p = p->next;
p->coef = -(q->coef); //系數取反
p->expn = q->expn;
q = q->next;
}
p->next = NULL;
add(poly, poly1); //利用加法
return poly;
}
Polynomial polyAdd(Polynomial poly1, Polynomial poly2) { //多項式相加 poly1+poly2形成一個新的多項式
Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode)); //和多項式的頭節點
poly->next = NULL;
PolyNode *p, *q, *r;
r = poly;
p = poly1->next;
q = poly2->next;
while (p&&q) {
if (p->expn > q->expn) {
r->next = (PolyNode*)malloc(sizeof(PolyNode));
r = r->next;
r->coef = p->coef;
r->expn = p->expn;
p = p->next;
} else if (p->expn < q->expn) {
r->next = (PolyNode*)malloc(sizeof(PolyNode));
r = r->next;
r->coef = q->coef;
r->expn = q->expn;
q = q->next;
} else {
float m = p->coef + q->coef;
if (!(m > -0.000001 && m < 0.000001)) {
r->next = (PolyNode*)malloc(sizeof(PolyNode));
r = r->next;
r->coef = m;
r->expn = p->expn;
}
q = q->next;
p = p->next;
}
}
while (p) {
r->next = (PolyNode*)malloc(sizeof(PolyNode));
r = r->next;
r->coef = p->coef;
r->expn = p->expn;
p = p->next;
}
while (q) {
r->next = (PolyNode*)malloc(sizeof(PolyNode));
r = r->next;
r->coef = q->coef;
r->expn = q->expn;
q = q->next;
}
r->next = NULL;
return poly;
}
Polynomial polyMultiply(Polynomial poly1, Polynomial poly2) { //多項式相乘
Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode)); //創建多項式和的頭節點
poly->next = NULL;
PolyNode *p;
p = poly2->next;
while (p) {
add(poly, multiply(poly1, p->coef, p->expn));
p = p->next;
}
return poly;
}
void printPoly(Polynomial poly) { //列印多項式
if (poly && poly->next) {
PolyNode *p = poly->next; //p指向第一個節點
while (p->next) {
printf("%gx^%d", p->coef, p->expn);
p = p->next;
if (p && (p->coef > 0))
printf("+");
}
if (p->expn == 0)
printf("%g", p->coef); //列印常數項
else
printf("%gx^%d", p->coef, p->expn);
printf("\n");
}
}
void freePoly(Polynomial poly) { //釋放記憶體
if (poly && poly->next) {
PolyNode *p, *q;
p = poly;
while (p) {
q = p->next;
free(p);
p = q;
}
}
poly = NULL;
}
int main() {
printf("用鏈表實作多項式的加減法\n");
Polynomial poly1, poly2, poly3;
printf("創建多項式一\n");
poly1 = createPolynomial();
printf("多項式一:\n");
printPoly(poly1);
printf("創建多項式二\n");
poly2 = createPolynomial();
printf("多項式二:\n");
printPoly(poly2);
printf("兩多項式相加,和為:\n");
poly3 = polyAdd(poly1, poly2);
printPoly(poly3);
freePoly(poly3);
printf("兩個多項式相乘,積為:\n");
poly3 = polyMultiply(poly1, poly2);
printPoly(poly3);
freePoly(poly3);
printf("兩多項式相減,差為:\n");
poly3 = polySubtract(poly1, poly2);
printPoly(poly3);
freePoly(poly1);
freePoly(poly2);
freePoly(poly3);
system("pause");
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/69214.html
標籤:C++ 語言
