錯誤如下:
yuan.cpp
i:\數據結構課程設計\yiyuanduoxiangshi\yuan.cpp(2) : error C2143: syntax error : missing ';' before 'PCH creation point'
i:\數據結構課程設計\yiyuanduoxiangshi\yuan.cpp(5) : error C2143: syntax error : missing ';' before 'PCH creation point'
i:\數據結構課程設計\yiyuanduoxiangshi\yuan.cpp(6) : error C2143: syntax error : missing ';' before '{'
i:\數據結構課程設計\yiyuanduoxiangshi\yuan.cpp(7) : error C2143: syntax error : missing ';' before 'PCH creation point'
i:\數據結構課程設計\yiyuanduoxiangshi\yuan.cpp(8) : error C2143: syntax error : missing ';' before 'PCH creation point'
i:\數據結構課程設計\yiyuanduoxiangshi\yuan.cpp(9) : error C2143: syntax error : missing ';' before 'PCH creation point'
i:\數據結構課程設計\yiyuanduoxiangshi\yuan.cpp(10) : error C2143: syntax error : missing ';' before 'PCH creation point'
i:\數據結構課程設計\yiyuanduoxiangshi\yuan.cpp(11) : fatal error C1004: unexpected end of file found
執行 cl.exe 時出錯.
yiyuanduoxiangshi.exe - 1 error(s), 0 warning(s)
代碼如下:
#include<iostream>
using namespace std;
float sum;
typedef struct PNode
{
float coef;
int expn;
struct PNode *next;
}PNode, *Polynomial;
void CreatePolyn(Polynomial &P,int n)
{//輸入m項的系數和指數,建立表示多項式的有序鏈表P
P = new PNode;
P->next = NULL;
int i;
Polynomial s,pre,q;
for( i=1; i<=n; ++i )
{
s = new PNode;
cout<<"請輸入系數和指數:"<<endl;
cin>>s->coef>>s->expn;
pre = P; // pre用於保存q的前驅,初值為頭結點
q = P->next; // *q為第一個大於輸入項指數的節點
while( q && q->expn<s->expn )
{
pre = q;
q = q->next;
}
s->next = q;
pre->next = s;
}
}
void AddPolyn(Polynomial &Pa,Polynomial &Pb)
{//多項式加法:Pa=Pa+Pb,利用兩個多項式的節點構成和多項式
Polynomial p1,p2,p3,r;
p1 = Pa->next; p2 = Pb->next; //p1和p2初值分別指向Pa和Pb的首元結點
p3 = Pa; //p3指向和多項式的當前節點,初值為Pa
while( p1 && p2 ) //p1和p2均非空
{
if( p1->expn == p2->expn ) //指數相等
{
sum = p1->coef + p2->coef; //sum保存兩項的系數和
if( sum!=0 ) //系數和不為零
{
p1->coef = sum; //修改Pa當前節點的系數值為兩項系數的和
p3->next = p1; p3 = p1; //將修改後的Pa當前節點鏈在p3之後,p3指向p1
p1 = p1->next; //p1指向後一項
r = p2; p2 = p2->next; delete r; //刪除Pb當前節點,p2指向後一項
}
else //系數和為零
{
r = p1; p1 = p1->next; delete r;
r = p2; p2 = p2->next; delete r;
}
}
else if( p1->expn < p2->expn ) //Pa當前節點的指數值小
{
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
else //Pb當前節點的指數值小
{
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
}
p3->next = p1?p1:p2; //插入非空多項式的剩余段
delete Pb;
}
void Output(Polynomial &Pa)
{
Polynomial p; //p用來指向當前節點
p = Pa->next;
cout<<"相加後的多項式為:"<<endl;
while ( p->next!=NULL )
{
cout<<p->coef<<"x^"<<p->expn<<"+";
p = p->next;
}
cout<<p->coef<<"x^"<<p->expn<<endl;
}
void Userface()
{
cout<<" 歡迎使用一元多項式加法器! "<<endl;
cout<<"--------------------------------------------- "<<endl;
// cout<<"- - "<<endl;
cout<<"- - "<<endl;
cout<<"- 1.計算 - "<<endl;
cout<<"- 2.退出 - "<<endl;
cout<<"- - "<<endl;
// cout<<"- - "<<endl;
cout<<"--------------------------------------------- "<<endl;
cout<<"請輸入序號選擇您需要的功能:"<<endl;
}
void Jisuan()
{
int n;
cout<<"請輸入第一個多項式項數:"<<endl;
cin>>n;
Polynomial Pa;
CreatePolyn(Pa,n);
cout<<"請輸入第二個多項式項數:"<<endl;
cin>>n;
Polynomial Pb;
CreatePolyn(Pb,n);
AddPolyn(Pa,Pb);
Output(Pa);
}
void Select()
{
int k;
cin>>k;
switch(k)
{
case 1: Jisuan();
break;
case 2: exit(1);
default:
cout<<"輸入錯誤,請重新輸入:"<<endl;
Userface();
Select();
break;
}
#include<iostream>
#include"Status.h"
using namespace std;
int main()
{
Userface();
Select();
return 0;
}
void Select()定義中switch少後括號