MFC實現一元稀疏多項式運算器
首先看看效果圖,下面一一介紹各個按鈕的實現
基本要求
- 輸入並建立兩個多項式
- 多項式a與b相加,建立和多項式c
- 多項式a與b相減,建立差多項式d
- 輸出多項式a, b, c, d。輸出格式:比如多項式a為:A(x)=c1xe1+ c2xe2+…+ cmxem,其中,ci和ei分別為第i項的系數和指數,且各項按指數的升冪排列,即0≤e1<e2<…<em
功能已加強,這里我實現了輸入可以不需要保持遞增,輸出依然遞增有序,即加入了查找位置的函數。
首先看一下我界面,較為粗糙,實現計算器界面(個人感覺手動輸入更為方便,但實習的拓展要求是計算器的仿真界面Orz)
這個可以自己添加一個頭文件,用於多項式的類定義(下面的重載>> <<屬於多余的,如果單純C++控制台程序的可以應用,MFC工程可以直接忽略)。
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
bool flag = 0;
struct Term
{
double coef;//系數
int exp;//指數
Term* link;
Term(double c, int e, Term* next = NULL)
{
coef = c; exp = e; link = next;
}
Term*InsertAfter(double c, int e);
friend ostream& operator <<(ostream&, const Term&);
};
class Polynomial//多項式類定義
{
public:
Term * first;
friend ostream&operator<<(ostream&, const Polynomial&);
friend istream&operator>>(istream&, Polynomial&);
friend Polynomial operator+(Polynomial&, Polynomial&);
friend Polynomial operator-(Polynomial&, Polynomial&);
Polynomial() { first = new Term(0, -10000000); }//構造函數,建立空鏈表
Polynomial(Polynomial&R);//復制構造函數
int maxOrder();//計算最大階數
Term* find_place(int e);
bool inser_order(double c, int e);
Term*getHead()const { return first; };//取得單鏈表的頭指針
//~Polynomial();
};
Polynomial x, y;
Term* Term::InsertAfter(double c, int e)
{
link = new Term(c, e, link);
return link;
}
ostream& operator<<(ostream& out, const Term&x)
{
if (x.coef == 0.0)return out;
out << x.coef;
switch (x.exp)
{
case 0:break;
case 1:out << "X"; break;
default:out << "X" << x.exp;
break;
}
return out;
}
Polynomial::Polynomial(Polynomial&R)
{
//復制構造函數
first = new Term(0, -1);
Term *destptrr = first, *srcptr = R.getHead()->link;
while (srcptr != NULL)
{
destptrr->InsertAfter(srcptr->coef, srcptr->exp);
srcptr = srcptr->link;
destptrr = destptrr->link;
}
}
int Polynomial::maxOrder()
{
//升序排序計算最大階數,即為最后一項
Term*current = first;
while (current->link != NULL)
{
current = current->link;
}
return current->exp;
}
//尋找需要插入的位置,由inser_order(double c, int e)調用
Term* Polynomial::find_place(int e)
{
Term* current = first;
while (current->link!=NULL&¤t->link->exp<e)
{
current = current->link;
}
return current;
}
//插入,保持遞增單鏈表
bool Polynomial::inser_order(double c, int e)
{
Term*current = find_place(e);
Term*temp=NULL;
//if (current == NULL) { AfxMessageBox(_T("插入出錯!")); return false; }
Term* newnode= new Term(c, e, temp);
if (newnode == NULL) { AfxMessageBox(_T("插入出錯!")); return false; }
newnode->link = current->link;
current->link = newnode;
return true;
}
istream& operator>>(istream&in, Polynomial& x)
{
//輸入,尾插法建立多項式
Term* rear = x.getHead(); double c; int e;
while (true)
{
cout << "input a term(c,exp)" << endl;
in >> c >> e;
if (e < 0)break;
rear = rear->InsertAfter(c, e);
}
return in;
}
ostream& operator<<(ostream &out, Polynomial&x) {
Term*current = x.getHead()->link;//頭指針為空,不輸出
cout << "多項式:" << endl;
bool h = true;
while (current != NULL)
{
if (h == false && current->coef > 0)out << "+";
h = false;
out << *current;
current = current->link;
}
out << endl;
return out;
}
Polynomial operator+(Polynomial&A, Polynomial&B)
{
Term*pa, *pb, *pc, *p; double temp;
Polynomial C; pc = C.first;
pa = A.getHead()->link; pb = B.getHead()->link;
while (pa != NULL && pb != NULL)
{
if (pa->exp == pb->exp)
{
temp = pa->coef + pb->coef;
if (fabs(temp) > 0.0001)
pc = pc->InsertAfter(temp, pa->exp);
pa = pa->link; pb = pb->link;
}
else if (pa->exp < pb->exp) {
pc = pc->InsertAfter(pa->coef, pa->exp);
pa = pa->link;
}
else {
pc = pc->InsertAfter(pb->coef, pb->exp);
pb = pb->link;
}
}
if (pa != NULL)p = pa;
else p = pb;
while (p != NULL)
{
pc = pc->InsertAfter(p->coef, p->exp);
p = p->link;
}
return C;
}
Polynomial operator-(Polynomial&A, Polynomial&B)
{
Term*pa, *pb, *pc, *p; double temp;
Polynomial C; pc = C.first;
pa = A.getHead()->link; pb = B.getHead()->link;
while (pa != NULL && pb != NULL)
{
if (pa->exp == pb->exp)
{
temp = pa->coef-pb->coef;
if (fabs(temp) > 0.0001)
pc = pc->InsertAfter(temp, pa->exp);
pa = pa->link; pb = pb->link;
}
else if (pa->exp < pb->exp) {
pc = pc->InsertAfter(pa->coef, pa->exp);
pa = pa->link;
}
else {
pc = pc->InsertAfter(pb->coef*(-1), pb->exp);
pb = pb->link;
}
}
bool flag = true;
if (pa != NULL)p = pa;
else {
p = pb; flag = false;
}
while (p != NULL)
{
if(flag)
pc = pc->InsertAfter(p->coef, p->exp);
else pc = pc->InsertAfter(p->coef*(-1), p->exp);
p = p->link;
}
return C;
}
接下來實現計算器按鈕的實現,下面是按鈕1的相關代碼,其他按鈕類似
void CPolynomialDlg::OnBnClickedButton1()
{
UpdateData(true);//寫入
CString str;
mEdit.GetWindowTextW(str);//得到編輯框的文字
str = str + _T("1");//CString后面加1
mEdit.SetWindowTextW(str);//編輯框顯示更新內容
UpdateData(false);
// TODO: 在此添加控件通知處理程序代碼
}
接下來是切換多項式的代碼,並且讀入一個多項式。處理思路是一次讀入系數和指數,采取后插法實現單鏈表的建立。這里用到CString到double和int的轉化,需要注意的是,轉double會出現一些后導0,影響美觀,需要去掉。我處理的麻煩了點,在大佬那知道了這個函數CString.Delete(CString.Getlength()-1,1),就懶得改了。
void CPolynomialDlg::OnBnClickedButtonnext()
{
CString str;
CString temp, temp1, temp2, temp3;
mEdit.GetWindowTextW(str);
double c = 0; int e = 0;
int j; int i;
//Term* rear;
//if (flag) rear = y.getHead();
// else rear = x.getHead();
for (j = 0; j < str.GetLength(); j++)
{
temp = temp1 = temp2 = temp3 = "";
if (str[j] == ' ')continue;
c = 0; e = 0;
while (str[j] != ' '&&j < str.GetLength()) { temp += str[j]; j++; }
for (i = 0; i < temp.GetLength(); i++)
{
if (temp[i] != '.')temp1 += temp[i];
else break;
}
c += _ttof(temp1);//CString轉double
i++;
for (i; i < temp.GetLength(); i++)
temp2 += temp[i];
if(c<0)
c -= _ttof(temp2) / pow(10, temp2.GetLength());
else c += _ttof(temp2) / pow(10, temp2.GetLength());
while(str[j]==' ')j++;
while (str[j] != ' '&&j < str.GetLength()) { temp3 += str[j]; j++; }
e = _ttoi(temp3);
//輸入,尾插法建立多項式
// if (flag)
//rear = rear->InsertAfter(c, e);
if (!flag)x.inser_order(c, e);
else y.inser_order(c, e);
// rear->InsertAfter(c, e, y.first);
// else rear = rear->InsertAfter(c, e,x.first);
// else rear->InsertAfter(c, e, x.first);
}
if(!flag)
AfxMessageBox(_T("第一個表達式建立完畢!"));
else AfxMessageBox(_T("第二個表達式建立完畢!"));
mEdit.SetWindowTextW(_T(""));
flag = !flag;
// TODO: 在此添加控件通知處理程序代碼
}
關鍵就是計算函數了,也就是確定按鈕,這里用到了下拉框選擇操作,用index判斷即可。最后作為CString輸出也有點格式優化,大家可以看一下。
void CPolynomialDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知處理程序代碼
//CDialogEx::OnOK();
CString temp;
int index = combox.GetCurSel();
//combox.GetLBText(index, temp);
CString str;
Polynomial C;
if (index == 0)
C = x + y;
else
C = x-y;
Term*current = C.getHead()->link;//頭指針為空,不輸出
// cout << "多項式:" << endl;
bool h = true;
while (current != NULL)
{
if (h == false && current->coef > 0)str+='+';
h = false;
//str+= *current;
if (current->coef == 0.0)
continue;
CString strr, str0;
if (current->coef == 1.0&¤t->exp!=0);
else
{
strr.Format(_T("%3f"), current->coef);
int len = strr.GetLength();
int i;
for (i = len - 1; i >= 0; i--)
{
//小數點的處理
if (strr[i] == '0' || strr[i] == '.');
else break;
}
for (int j = 0; j <= i; j++)
str += strr[j];
}
switch (current->exp)//系數輸出,0,1單獨處理
{
case 0:break;
case 1:str+="X"; break;
default: {str += "X"; strr.Format(_T("%d"), current->exp); str += strr; }
break;
}
current = current->link;
}
n_Edit.SetWindowTextW(str);
}
最后的就是釋放空間的按鈕,以便實現多次運算,這里偷了個懶,一個小程序,就沒有delete了,最好還是順着單鏈表delete。
// CDialogEx::OnCancel();
n_Edit.SetWindowTextW(_T(""));//編輯框的清空
x.first = new Term(0, -1);
y.first = new Term(0, -1);