(1)输入并建立多项式; (2)输出多项式,输出形式为整数序列: n,c1,e1,c2,e2,…,cn,en,其中n是多项式的项数,ci和ei分别是第i项的系数和指数,序列按指数降序排列。 (3)多项式a与多项式b相乘,建立多项式。 在写代码的过程中,对其中没有掌握的各个知识点的梳理和记录。
typedef 和 struct 在c++中如果用typedef的话,会造成区别:
struct Student { int a; }stu1;//stu1是一个Student变量 typedef struct Student2 { int a; }stu2;//stu2是一个结构体类型=struct Student使用时可以直接访问stu1.a 但是stu2则必须先 stu2 s2; 然后 s2.a=10;
在C++中,后者可以有 1. struct Student2 变量名 2. Student2 变量名 3. stu2 变量名
结构体实例和类实例的初始化方法完全相同,二者都可以应用于继承层次中。不同点是结构体默认成员为public,而类默认成员是private。
若类和结构体所有数据成员均为public型,可采取如下带花括号形式进行初始化。 注意: ① 不论值的个数多少,都必须使用花括号定界 ② 未指定值的数据成员编译器会自动初始化为默认值 ③ 这种初始化对象方式,要求所有数据成员必须为public型 ④ 这种初始化对象方式,要求类中不能编写任何构造函数
添加了构造函数的struct相当于成员全部public的类,而类的实例化必须通过构造函数,所以不能再使用{}初始化。
C++允许函数返回局部指针,前提是指针指向的地址在函数退出后仍然有效。这涉及到C++内存分配问题。如果指针指向的内容是局部数组等存在与栈内存中的,则函数执行完后内容被销毁。
C++编译器将计算机内存分为代码区和数据区。 数据区分配方式如下图所示:
不涉及动态分配的对象有严格定义的生存期。全局对象在程序启动时分配,程序结束时销毁。局部对象,在我们进入其定义所在的程序块时被创建,在程序结束时销毁。局部static对象在第一次使用前分配,在程序结束时销毁。
动态分配的对象的生存期与它们在哪里创建无关,只有显式地被释放时,才会销毁。
void f() { int* p=newint[5]; } delete []p;
1)用malloc申请多个同名指针,或new多个同名指针,指针指向最后一次开辟的空间。 2)复制指针时复制指针本身所含的地址值,而不会复制指针指向的内容。
3)void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针。如果分配失败,则返回一个空指针(NULL)。 关于分配失败的原因,应该有多种,比如说空间不足就是一种。 void free(void *FirstByte): 该函数是将之前用malloc分配的空间还给程序或者是操作系统,也就是释放了这块内存,让它重新得到自由。
4)free 和 delete 释放的都是指针指向的内存。指针是一个变量,只有程序结束时才被销毁。
贴一下代码
#include <iostream> #include <string> #include<sstream> #include <vector> using namespace std; typedef struct Polynomial{ double coef; //系数 int expn; //指数 struct Polynomial *next; Polynomial(double a, int b){coef = a; expn = b; next = NULL;} Polynomial(){}; }Polyn,*Poly; Polyn* CreatePolyn(Polyn *head,int n){ Poly h2 = NULL; int i = 1; for(; i<(n+1); i++){ cout<<"输入第"<<i<<"项的系数和指数"<<endl; double co; int ex; Poly po = new Polynomial(); cin>>co>>ex; po->coef = co; po->expn = ex; po->next = NULL; if (head == NULL){head = po; h2 = head;} // head有值 else{ head->next = po; head = head->next; } } return h2; } void PrintPolyn(Polyn *head){ /*vector<string> polystr; string tp; while(head->next != NULL){ polystr.push_back(tp); istringstream temp(head->coef); temp>>tp; polystr.push_back(tp); head = head->next; }*/ for(; head != NULL; head= head->next){ if(head->coef == 0) continue; int coe = head->coef * 100; double coe1 = coe/100; head->coef = coe1; if( coe1 >1 ) cout<<coe1; if( head->expn>1 ) cout<<"x^"<<head->expn; else if ( head->expn = 1 ) cout<<"x"; if( head->next && ! ( head->next->coef < 0 ) ) cout<<"+"; else if ( head->next && head->next->coef < 0) cout<<"-"; } }; Polyn* MultiPolyn(Poly head1,Poly head2){ Poly ml = NULL ; Poly re = NULL; while( head1!= NULL ){ double coe1 = head1->coef; int exp1 = head1->expn; for( Poly h2 = head2; h2 != NULL; h2 = h2->next){ Poly po = new Polyn(); po->coef = coe1 * h2->coef; po->expn = exp1 + h2->expn; po->next = NULL; if ( ml == NULL ) { ml = po; re = ml; }else{ ml->next = po; ml = ml->next; } } head1 = head1->next; } for( Poly p = re; p != NULL; p=p->next){ double out = p->coef; int out1 = p->expn; for( Poly p2 = p->next; p2 !=NULL; p2=p2->next){ if (p2->expn == out1) { p->coef += p2->coef; p2->coef = 0; p2->expn=0; } } cout<<p->coef<<p->expn<<endl; } return re; } int main(){ cout<<"####################################"<<endl; cout<<endl; cout<<" 一元稀疏多项式乘法计算器 "<<endl; cout<<endl; cout<<"####################################"<<endl; int n1, n2, tr=0, ta = 0, tb =0; Poly head1=NULL, head2 = NULL, head3 = NULL; while(tr != 1){ cout<<"请输入多项式a和b的项数"<<endl; cin>>n1>>n2; cout<<"确认输入1;输入其他数字重新输入"<<endl; cin>>tr; } while(ta != 1){ cout<<"依次输入多项式a每项的系数和指数,回车结束"<<endl; head1 = CreatePolyn(head1,n1); cout<<"多项式a为:"; PrintPolyn(head1); cout<<"确认输入1;输入其他数字重新输入"<<endl; cin>>ta; } while(tb != 1){ cout<<"依次输入多项式b每项的系数和指数,回车结束"<<endl; head2 = CreatePolyn(head2,n2); cout<<"多项式b为:"; PrintPolyn(head2); cout<<"确认输入1;输入其他数字重新输入"<<endl; cin>>tb; } cout<<"多项式相乘:"<<endl; PrintPolyn( MultiPolyn( head1,head2 ) ); system("pause"); }