#include<iostream> #include<cstdio> using namespace std; int main(){ int y,m,d,ans=0,flag=0; printf("请输入年月日,并用逗号隔开 "); scanf("%d,%d,%d",&y,&m,&d); if((y%400==0)||((y%100!=0)&&(y%4==0)))flag=1;//判断是否为闰年 m--; switch(m){ case 11:ans+=30; case 10:ans+=31; case 9:ans+=30; case 8:ans+=31; case 7:ans+=31; case 6:ans+=30; case 5:ans+=31; case 4:ans+=30; case 3:ans+=31; case 2:ans=ans+flag+28; case 1:ans+=31; } ans+=d; printf("这一天是这一年的第%d天 ",ans); return 0; }
模板类
// // Created by Ma_Yiling on 2020/5/15. // #ifndef CLION_ARRAY_H #define CLION_ARRAY_H template<typename T> class Array{ public: Array(int s); virtual ~Array(); virtual const T &Entry(int index)const; virtual void Enter(int index,const T & value); protected: int size; T *element; }; template<typename T> Array<T>::Array(int s){ if(s>1)size=s; else size=1; element=new T[size]; } template<typename T> Array<T>::~Array(){ delete []element; } template<typename T> const T& Array<T>::Entry(int index)const{ return element[index]; } template<typename T> void Array<T>::Enter(int index,const T &value){ element[index]=value; } #endif //CLION_ARRAY_H
#include<iostream> using namespace std; #include"Array.h" int main(){ Array<int>IntAry(5); for(int i=0;i<5;i++)IntAry.Enter(i,i); cout<<"Integer Array: "; for(int i=0;i<5;i++)cout<<IntAry.Entry(i)<<' '; cout<<endl; Array<double>DouAry(5); for(int i=0;i<5;i++)DouAry.Enter(i,(i+1)*0.35); cout<<"Double Array: "; for(int i=0;i<5;i++)cout<<DouAry.Entry(i)<<' '; cout<<endl; return 0; }
#include<iostream> using namespace std; template<typename T> class Complex{ public: Complex(T r=0,T i=0):Real(r),Image(i){}; private: T Real,Image; template<typename U> friend Complex<U> operator + (const Complex<U>&c1,const Complex<U>&c2); template<typename U> friend Complex<U> operator - (const Complex<U>&c1,const Complex<U>&c2); template<typename U> friend Complex<U> operator - (const Complex<U>&c); template<typename U> friend ostream & operator << (ostream & output,const Complex<U>&c); }; //------------------------------------------------------------------------------- template<typename T> Complex<T> operator + (const Complex<T>&c1,const Complex<T>&c2){ T r=c1.Real+c2.Real; T i=c1.Image+c2.Image; return Complex<T>(r,i); } template<typename T> Complex<T> operator - (const Complex<T>&c1,const Complex<T>&c2){ T r=c1.Real-c2.Real; T i=c1.Image-c2.Image; return Complex<T>(r,i); } template<typename T> Complex<T> operator - (const Complex<T>&c){ return Complex<T>(-c.Real,-c.Image); } template<typename T> ostream & operator << (ostream & output,const Complex<T>&c){ output<<"("<<c.Real<<","<<c.Image<<") "; return output; } //-------------------------------------------------------------------------------- int main(){ Complex<double>c1(2.5,3.7),c2(4.2,6.5); cout<<"c1 = "<<c1<<"c2 = "<<c2; cout<<"c1 + c2 = "<<c1+c2; cout<<"c1 - c2 = "<<c1-c2; cout<<"-c1 = "<<-c1; return 0; }
#include<iostream> using namespace std; const double pi=3.14159; template<typename T>class Circle{ private: T radius; static int total; public: Circle(T r=0){radius = r;total++;} void Set_Radius(T r){radius = r;} double Get_Radius(){return radius;} double Get_Girth(){return 2*radius*pi;} double Get_Area(){return pi*radius*radius;} static int ShowTotal(){ return total; } }; template<typename T>int Circle<T>::total=0; //template<typename T> //int Circle<T>::ShowTotal(){return total;} int main(){ return 0; }
模板函数
#include <iostream> using namespace std; template<typename ElementType> void SortBubble(ElementType *a,int size){ int work; ElementType temp; for(int pass=1;pass<size;pass++){ work=1; for(int i=0;i<size-pass;i++){ if(a[i]>a[i+1]){ temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; work=0; } } if(work)break; } } template <typename ElementType> void Print(ElementType *x,int n){ for(int i=0;i<n;i++) printf("%d ",x[i]); puts(""); } int main() { int n,a[15]; scanf("%d",&n); for(int i=0;i<n;i++)scanf("%d",&a[i]); SortBubble(a,n); Print(a,n); }
#include<iostream> using namespace std; template<typename T> T Max(const T a,const T b){return a>b?a:b;} template<typename T> T Max(const T a,const T b,const T c) {T t=Max(a,b);return Max(t,c);} int Max(const int a,const char b){return a>b?a:b;} int main(){ cout<<"Max(3,'a')is "<<Max(3,'a')<<endl; cout<<"Max(9.3,0.5)is "<<Max(9.3,0.5)<<endl; cout<<"Max(9,5,23)is "<<Max(9,5,23)<<endl; return 0; }
重载运算符
#include<iostream> #include<cstdio> using namespace std; class Complex{ private: double real,image; public: Complex(){real=0;image=0;} Complex(double x){real=x;image=0;} Complex(double x,double y){real=x;image=y;} friend Complex operator -(const Complex & x); friend Complex operator +(const Complex & x,const Complex & y); friend Complex operator -(const Complex & x,const Complex & y); void show(){ printf("( %.3f , %.3f ) ",real,image); } }; Complex operator -(const Complex & x){ Complex res; res.real=-x.real; res.image=-x.image; return res; } Complex operator -(const Complex & x,const Complex & y){ Complex res; res.real=x.real-y.real; res.image=x.image-y.image; return res; } Complex operator +(const Complex & x,const Complex & y){ Complex res; res.real=x.real+y.real; res.image=x.image+y.image; return res; } int main(){ Complex a(1,2),b(1),c(5,7); Complex ans=-a+b-c-c; ans.show(); }
#include<iostream> #include<cstdio> using namespace std; class point{ private: int x,y; public: point(int xx,int yy){x=xx;y=yy;} point(){x=0;y=0;} point operator ++(); point operator ++(int); void show(){ printf("( %d , %d ) ",x,y); } }; point point::operator ++(){ this->x++; this->y++; return *this; } point point::operator ++(int){ this->x++; this->y++; return *this; } int main(){ point a(1,2); ++a;a.show(); a++;a.show(); }
#include<iostream> #include<cstdio> using namespace std; class point{ private: int x,y; public: point(int xx,int yy){x=xx;y=yy;} point(){x=0;y=0;} friend point operator ++(point &); friend point operator ++(point &,int);//int为伪参数,目的是将前置式和后置式区分开来 void show(){ printf("( %d , %d ) ",x,y); } }; point operator ++(point &a){ a.x++; a.y++; return a; } point operator ++(point &a,int){ a.x++; a.y++; return a; } int main(){ point a(1,2); ++a;a.show(); a++;a.show(); }
#include<iostream> #include<cstdio> #include<cstring> using namespace std; class Name{ private: char *pName; int size; public: Name(char *pN); Name(const Name &); Name& operator=(const Name &); void show(){ printf("%s ",pName); } }; Name::Name(char *pN){ pName=new char[strlen(pN)+1]; if(pName!=0)strcpy(pName,pN); size=strlen(pN); } Name::Name(const Name & obj){ pName=new char[strlen(obj.pName)+1]; if(pName!=0)strcpy(pName,obj.pName); size=obj.size; } Name & Name::operator=(const Name & obj){ delete []pName; pName=new char[strlen(obj.pName)+1]; if(pName!=0)strcpy(pName,obj.pName); size=obj.size; return *this; } int main(){ Name Obj1("zhangsan"); Name Obj2=Obj1; Obj2.show(); Name Obj3("NoName"); Obj3.show(); Obj3=Obj2=Obj1; Obj3.show(); }
#include<iostream> #include<cstdio> using namespace std; class vector{ private: int *v; int size; public: vector(int n){v=new int[n];size=n;} ~vector(){delete []v;size=0;} int & operator [] (int i){return v[i];} }; int main(){ vector a(5); a[2]=12; cout<<a[2]<<endl; cout<<a.operator[](2)<<endl; }
#include<iostream> using namespace std; class F{ public: double operator ()(double x,double y); }; double F::operator ()(double x,double y){ return x*x+y*y; } int main(){ F f; cout<<f(5.2,2.5)<<endl; return 0; }
istream和ostream是C++的预定义流类
cin是istream的对象,cout是ostream的对象
运算符<<由ostream重载为插入操作,用于输出基本类型数据
运算符>>由istream重载为提取操作,用于输入基本类型数据
用友元函数重载<<和>>,输出和输入用户自定义的数据类型
重载的时候,形参作为实参的别名,而实参是cout,cout是类ostream的对象;同理,cin是istream的对象。
流插入和流提取运算符的重载返回的是流类引用,以符合原语义
#include<iostream> #include<cstdlib> using namespace std; class vector{ public: vector(int size=1); ~vector(); int & operator[](int i); friend ostream & operator<<(ostream & output,vector &); friend istream & operator>>(istream & input,vector &); private: int *v; int len; }; vector::vector(int size){ if(size<=0||size>100){ cout<<"The size of "<<size<<" is null! "; exit(0); } v=new int[size]; len=size; } vector::~vector(){ delete[]v; len=0; } int & vector::operator[](int i){ if(i>=0&&i<len)return v[i]; cout<<"The subscript "<<i<<" is outside ! ";exit(0); } ostream & operator << (ostream & output,vector & ary){//output是cout的别名 for(int i=0;i<ary.len;i++)output<<ary[i]<<" "; output<<endl; return output; } istream & operator >> (istream & input,vector & ary){ for(int i=0;i<ary.len;i++)input>>ary[i]; return input; } int main(){ int k; cout<<"Input the length of vector A: "; cin>>k; vector A(k); cout<<"Input the elements of vector A: "; cin>>A; cout<<"Output the elements of vector A: "; cout<<A; return 0; }
类对象的类型转换可以由两种方式实现:构造函数、转换函数
称为用户定义的类型转换或类类型转换,有隐式调用和显式调用方式
带参数的构造函数不能把一个类类型转换成基本类型
类类型转换函数是一种特殊的成员函数,提供类对象之间显式类型转换的机制
类型转换函数有两种使用方式:
隐式使用 i=a
显示使用 i=a.operator int()
使用不同函数作类型转换函数:
int i=a;//用类型转换函数进行转换
X i=a;//用构造函数进行转换
重载指针转换运算符
注意函数没有指定返回值的类型,这是语法的要求,跟其他重载函数不一样的地方,要注意
operator T * (); Array<T>::operator T *(){ return list; }
继承
静态成员在派生类中的调用:
#include<iostream> using namespace std; class B{ public: static void Add(){i++;} static int i; void out(){cout<<"static i="<<i<<endl;} }; int B::i=0; class D: private B{ public: void f(){ i=5; Add(); B::i++; B::Add(); } }; int main(){ B x;D y; x.Add(); x.out(); y.f(); cout<<"static i="<<B::i<<endl; cout<<"static i="<<x.i<<endl; // cout<<"static i="<<y.i<<endl; return 0; }
派生类的初始化
#include<iostream> #include<cstdio> #include<cstring> using namespace std; class parent_class{ private: int data1,data2; public: parent_class(int p1,int p2){data1=p1;data2=p2;} int inc1(){return ++data1;} int inc2(){return ++data2;} void display(){cout<<"data1="<<data1<<",data2="<<data2<<endl;} }; class derived_class:private parent_class{ private: int data3; parent_class data4; public: derived_class(int p1,int p2,int p3,int p4,int p5): parent_class(p1,p2),data4(p3,p4),data3(p5){} int inc1(){return parent_class::inc1();} int inc3(){return ++data3;} void display(){ parent_class::display(); data4.display(); cout<<"data3="<<data3<<endl; } }; int main(){ derived_class d1(17,18,1,2,-5); d1.inc1(); d1.display(); return 0; }
排序
/* 题目:选择排序 作者:thmyl 日期:2019-10-17 */ #include<iostream> #include<cstdio> #define maxn 1010 using namespace std; int n,a[maxn]; int main(){ printf("请输入数字的个数:"); scanf("%d",&n); printf("请输入需要排序的数字: "); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ if(a[i]>a[j])swap(a[i],a[j]); } } printf("排好序后的数列为: "); for(int i=1;i<=n;i++)printf("%d ",a[i]); puts(""); return 0; }
/* 题目:冒泡排序 作者:thmyl 日期:2019-10-17 */ #include<iostream> #include<cstdio> #define maxn 1010 using namespace std; int n,a[maxn]; int main(){ printf("请输入数字的个数:"); scanf("%d",&n); printf("请输入需要排序的数字: "); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++) for(int j=n;j>i;j--)//从后向前浮动 if(a[j]<a[j-1])swap(a[j],a[j-1]); printf("排好序后的数列为: "); for(int i=1;i<=n;i++)printf("%d ",a[i]); puts(""); return 0; }
/* ìa??:2???±??-DòáDμ??3Dò£?ê?3???óDDòêyáD ×÷??:thmyl è??ú:2019-10-18 */ #include<iostream> #include<cstdio> #define maxn 1010 using namespace std; int a[maxn],n,mn,mark; int *p[maxn]; bool vis[maxn]; int main(){ printf("请输入数字的个数:"); scanf("%d",&n); printf("请输入需要排序的数字: "); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++){//?°?òμúi′óμ?êy mn=0x7fffffff; for(int j=1;j<=n;j++) if(a[j]<mn&&!vis[j]) mn=a[j],mark=j; p[i]=&a[mark]; vis[mark]=1; } printf("排好序后的数列为: "); for(int i=1;i<=n;i++)printf("%d ",*p[i]); puts(""); return 0; }
#include<iostream> #include<cstdio> #define maxn 100010 using namespace std; int n,a[maxn],b[maxn]; void Sort(int l,int r){ if(l==r)return; int mid=(l+r)>>1; Sort(l,mid); Sort(mid+1,r); int i=l,j=mid+1; int cnt=l-1; while(i<=mid&&j<=r){ if(a[i]<=a[j]){ b[++cnt]=a[i++]; } else b[++cnt]=a[j++]; } while(i<=mid)b[++cnt]=a[i++]; while(j<=r)b[++cnt]=a[j++]; for(int i=l;i<=r;i++)a[i]=b[i]; } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); Sort(1,n); for(int i=1;i<=n;i++)printf("%d ",a[i]); puts(""); return 0; }
指针
int *a[10]:定义了10个指向整型元素的指针
int (*a)[10]:定义了一个指针,指向一个有10个整型元素的数组
#include<iostream> #include<cstdio> using namespace std; int b[10][10]; int (*a)[10]; int main(){ int cnt=0; for(int i=0;i<10;i++) for(int j=0;j<10;j++){ b[i][j]=cnt; cnt++; } a=b; for(int i=0;i<10;i++,a++){ for(int *p=*a,j=0;j<10;j++,p++){ printf("%d ",*p); } puts(""); } return 0; }
(2019.10.24)
#include<iostream> #include<cstdio> using namespace std; const int N=10; void input(int *a,int N){//事实上没有定义数组,而是传递了数组的地址,设a[]或*a都可以,用法相同 for(int i=0;i<N;i++)scanf("%d",&a[i]); } void index(int *a,int N){ for(int i=0;i<N;i++) for(int j=i+1;j<N;j++) if(a[i]>a[j])swap(a[i],a[j]); } void output(int *a,int N){ for(int i=0;i<N;i++)printf("%d ",a[i]); } int main(){ freopen("Cola.txt","r",stdin); int a[N]; input(a,N); index(a,N); output(a,N); return 0; }
#include<iostream> #include<cstdio> using namespace std; const int N=10; int main(){ int a[N]; void input(int *a,int N); void index(int *a,int N); void output(int *a,int N); input(a,N); index(a,N); output(a,N); return 0; } void input(int *a,int N){//事实上没有定义数组,而是传递了数组的地址,设a[]或*a都可以,用法相同 for(int i=0;i<N;i++)scanf("%d",&a[i]); } void index(int *a,int N){ for(int i=0;i<N;i++) for(int j=i+1;j<N;j++) if(a[i]>a[j])swap(a[i],a[j]); } void output(int *a,int N){ for(int i=0;i<N;i++)printf("%d ",a[i]); }
#include<iostream> #include<cstdio> #define N 10 using namespace std; int main(){ int a,b,c; scanf("%d%d",&a,&b); #ifdef N c=a+b; #else c=a-b; #endif printf("%d",c); return 0; }
//这里的if后面的条件必须宏定义 #include<iostream> #include<cstdio> using namespace std; int a,b; int main(){ scanf("%d",&a); #if(a>0)//Error b=a; #else b=-a; #endif printf("%d ",b); return 0; }
//不要输出一个一个的数,要先组成一个数然后再输出。 //据说某sp考试能检测你的输出方式,嗯哼哼? #include<iostream> #include<cstdio> using namespace std; int main(){ int i=0,n,a[5],sum=0; printf("Please input a number in 5 single numbers: "); scanf("%d",&n); while(n){ a[i]=n%10; n/=10; i++; } for(int i=0;i<5;i++) for(int j=i+1;j<5;j++) if(a[j]>a[i])swap(a[i],a[j]); printf("The largest number is:"); for(int i=0;i<5;i++){ sum=sum*10+a[i]; } printf("%d ",sum); printf("The smallest number is:"); i=4; while(a[i]==0)i--; swap(a[4],a[i]); sum=0; for(int i=4;i>=0;i--){ sum=sum*10+a[i]; } printf("%d ",sum); return 0; }
(2019.10.25)
标准输入输出
/*scanf*/ %f float %lf double %Lf long double /*printf*/ %f float/double %Lf long double
malloc和free函数
malloc:
int *p;
p=(int*)malloc(sizeof(int)*100);
#include<iostream> #include<cstdio> #include<malloc.h> #include<algorithm> using namespace std; int *p,n; int main(){ freopen("Cola.txt","r",stdin); scanf("%d",&n); p=(int*)malloc(sizeof(int)*(n+1)); for(int i=1;i<=n;i++)scanf("%d",&p[i]); sort(p+1,p+n+1); // free(p); for(int i=1;i<=n;i++)printf("%d ",p[i]); puts(""); free(p); return 0; }
#include<iostream> #include<cstdio> #include<cstdlib> struct node{ int data; node *next; }; int main(){ int i,j,k,m,n; node *h,*p,*q; scanf("%d%d",&n,&m); h=(node*)malloc(sizeof(node)); h->data=1; h->next=h; p=h; for(i=2;i<=n;i++){ q=(node*)malloc(sizeof(node)); q->data=i; q->next=p->next; p->next=q; p=q; } p=h; k=1;//µ±Ç°Ëù±¨µÄÊý while(p->next!=p){ if(k<m-1){ k++; p=p->next; } else if(k==m-1){ q=p->next; p->next=p->next->next; printf("%d--",q->data); free(q); k=1; p=p->next; } } printf("%d ",p->data); return 0; }
#include<iostream> #include<cstdio> #include<malloc.h> using namespace std; int n; struct Tree{ int data,v; Tree *lson; Tree *rson; }; Tree *p,*root; void Insert(int x){ Tree *q; // q=(Tree*)malloc(sizeof(Tree)); q=root; while(1){ if((p->v)>=(q->v)){ if(q->rson==NULL){ q->rson=p; return; } else { q=q->rson; } } else { if(q->lson==NULL){ q->lson=p; return; } else { q=q->lson; } } } } void dfs(Tree *q){ printf("%d ",q->v); if(q->lson!=NULL) dfs(q->lson); if(q->rson!=NULL) dfs(q->rson); } int main(){ freopen("Cola.txt","r",stdin); int x; scanf("%d",&n); root=(Tree*)malloc(sizeof(Tree)); scanf("%d",&x); root->data=1; root->v=x; root->lson=NULL; root->rson=NULL; for(int i=2;i<=n;i++){ scanf("%d",&x); p=(Tree*)malloc(sizeof(Tree)); p->data=i; p->v=x; p->lson=NULL; p->rson=NULL; Insert(x); } dfs(root); return 0; }
(2019.11.21)
链表
1 #include<iostream> 2 #include<cstdio> 3 #include<malloc.h> 4 using namespace std; 5 struct node{ 6 int data; 7 node* next; 8 }; 9 node* head=NULL; 10 bool creat_node_list(){ 11 head=(node*)malloc(sizeof(node)); 12 if(NULL==head)return false; 13 else { 14 head->data=0; 15 head->next=NULL; 16 return 1; 17 } 18 } 19 bool Insert(node* point){ 20 if(NULL==head)return 0; 21 node* p=head->next; 22 node* q=head; 23 while(p!=NULL){ 24 q=p; 25 p=p->next; 26 } 27 q->next=point; 28 point->next=NULL; 29 return 1; 30 } 31 void reverse_list(){ 32 if(head==NULL)return; 33 if(head->next==NULL)return; 34 node* p=head->next; 35 node* q=p->next; 36 node* t=NULL; 37 while(q!=NULL){ 38 t=q->next; 39 q->next=p; 40 p=q; 41 q=t; 42 } 43 head->next->next=NULL; 44 head->next=p; 45 } 46 void sort(){ 47 node* Head=head; 48 if(head==NULL)return; 49 if(Head->next==NULL)return; 50 node* pi=Head->next; 51 node* pj=pi->next; 52 for(;pi!=NULL;pi=pi->next){ 53 for(pj=pi->next;pj!=NULL;pj=pj->next){ 54 if(pj->data>pi->data) 55 swap(pj->data,pi->data); 56 } 57 } 58 } 59 bool deletenode(int id){ 60 if(head==NULL)return 0; 61 node* p=head->next; 62 int len=0; 63 while(p!=NULL){ 64 len++; 65 p=p->next; 66 } 67 if(len<id)return 0; 68 else { 69 node* q=head; 70 p=head; 71 for(int i=0;i<id;i++){ 72 q=p; 73 p=p->next; 74 } 75 node* t=p->next; 76 q->next=t; 77 free(p); 78 return 1; 79 } 80 } 81 void destorylist(){ 82 if(head==NULL)return; 83 if(head->next==NULL){ 84 free(head); 85 head=NULL; 86 return; 87 } 88 node* p=head->next; 89 while(NULL!=p){ 90 node* tmp=p; 91 p=p->next; 92 free(tmp); 93 } 94 free(head); 95 head=NULL; 96 } 97 void check(){ 98 if(head==NULL){ 99 puts("序列中没有任何元素"); 100 return; 101 } 102 else if(head->next==NULL){ 103 printf("%d ",head->data); 104 return; 105 } 106 else { 107 node* p=head; 108 node* q=p->next; 109 while(p->next!=NULL){ 110 printf("%d ",p->data); 111 p=q; 112 q=q->next; 113 } 114 printf("%d ",p->data); 115 puts(""); 116 } 117 118 } 119 int main(){ 120 creat_node_list(); 121 printf("%d ",head->data); 122 node* node1=(node*)malloc(sizeof(node)); 123 node1->data=1; 124 node1->next=NULL; 125 126 node* node2=(node*)malloc(sizeof(node)); 127 node2->data=2; 128 node2->next=NULL; 129 130 Insert(node1); 131 Insert(node2); 132 check(); 133 134 reverse_list(); 135 check(); 136 137 node* node3=(node*)malloc(sizeof(node)); 138 node3->data=3; 139 node3->next=NULL; 140 141 Insert(node3); 142 sort(); 143 check(); 144 deletenode(2); 145 check(); 146 destorylist(); 147 148 return 0; 149 }
#include<iostream> #include<cstdio> #include<malloc.h> using namespace std; struct list{ int data; list* next; }; list* head=NULL; void creat_list(){ head=(list*)malloc(sizeof(list)); if(head==NULL)return; else { head->data=0; head->next=NULL; } } list* Insert(list* h,int x){ if(h==NULL||h->next==NULL){ list* p; p=(list*)malloc(sizeof(list)); p->data=x; p->next=NULL; h->next=p; return h; } if(head->next->data>=x){//x插到第一个位置 list* p; p=(list*)malloc(sizeof(list)); p->data=x; p->next=h->next; h->next=p; return h; } else { list* q=h->next; list* p=q->next; list* t; bool flag=0;//标记是否插入成功 while(p!=NULL){ if(p->data>=x&&q->data<x){ flag=1; t=(list*)malloc(sizeof(list)); t->data=x; t->next=p; q->next=t; break; } q=p; p=p->next; } if(!flag){ t=(list*)malloc(sizeof(list)); t->data=x; t->next=NULL; q->next=t; } return h; } } list* remove(list* h,int x){ list* q=head->next; if(q->data==x){ h->next=q->next; free(q); return h; } if(q->next==NULL){ free(q); head->next=NULL; return h; } list* p=q->next; while(p!=NULL){ if(p->data==x){ q->next=p->next; free(p); return h; } q=p; p=p->next; } } void check(){ list* p=head->next; while(p!=NULL){ printf("%d ",p->data); p=p->next; } puts(""); } int main(){ freopen("Cola.txt","r",stdin); int n,x; puts("请输入整数的个数:"); scanf("%d",&n); creat_list(); puts("请输入要排序的数列:"); for(int i=1;i<=n;i++){ scanf("%d",&x); Insert(head,x); check(); } puts("请输入要删除的数的个数:"); scanf("%d",&n); puts("请依次输入要删除的数"); for(int i=1;i<=n;i++){ scanf("%d",&x); remove(head,x); check(); } return 0; }
#include<iostream> #include<cstdio> #include<cstring> #include<malloc.h> #define Abs(x) ((x)>=0?(x):(-x)) using namespace std; struct node{ int a,b;//系数为a,指数为b node* next; }; node *head[4]; void creat_node(node* &h){ h=(node*)malloc(sizeof(node)); if(h==NULL)return; else{ h->a=0; h->b=0; h->next=NULL; return; } } void Insert(node* &h,int x,int y){ if(h==NULL||h->next==NULL){ node *q; q=(node*)malloc(sizeof(node)); q->a=x; q->b=y; q->next=NULL; h->next=q; return; } else { node* p=h->next; node* q=h; node* t; t=(node*)malloc(sizeof(node)); t->a=x; t->b=y; t->next=NULL; while(p!=NULL){ q=p; p=p->next; } q->next=t; return; } } void reverse(node* &h){//把链表反转 if(h==NULL||h->next==NULL)return; node* p=h->next; node* q=h; node* t; bool flag=0;//判断是否是链表的第一个元素 while(p!=NULL){ t=p->next; if(!flag)p->next=NULL,flag=1; else p->next=q; q=p; p=t; } h->next=q; } void calc1(node* &h1,node* &h2,node* &h3){ creat_node(h3); node* p1=h1->next; node* p2=h2->next; node* p3; node* q=h3; while(p1!=NULL&&p2!=NULL){ int x=(p1->a)+(p2->a); if(x!=0){ p3=(node*)malloc(sizeof(node)); p3->a=x; p3->b=p1->b; p3->next=NULL; q->next=p3; q=p3; } p1=p1->next; p2=p2->next; } while(p1!=NULL){//易错!不能把p1直接赋值给q,否则head[3]链表会有一部分指向head[2]链表,链表之间失去独立性 p3=(node*)malloc(sizeof(node)); p3->a=p1->a; p3->b=p1->b; p3->next=p1->next; q->next=p3; q=q->next; p1=p1->next; } while(p2!=NULL){ p3=(node*)malloc(sizeof(node)); p3->a=p2->a; p3->b=p2->b; p3->next=p2->next; q->next=p3; q=q->next; p2=p2->next; } } void calc2(node* &h1,node* &h2,node* &h3){ creat_node(h3); node* p1=h1->next; node* p2=h2->next; node* p3; node* q=h3; while(p1!=NULL&&p2!=NULL){ int x=(p1->a)-(p2->a); if(x!=0){ p3=(node*)malloc(sizeof(node)); p3->a=x; p3->b=p1->b; p3->next=NULL; q->next=p3; q=p3; } p1=p1->next; p2=p2->next; } while(p1!=NULL){ p3=(node*)malloc(sizeof(node)); p3->a=p1->a; p3->b=p1->b; p3->next=p1->next; q->next=p3; q=q->next; p1=p1->next; } while(p2!=NULL){ p3=(node*)malloc(sizeof(node)); p3->a=-(p2->a); p3->b=p2->b; p3->next=p2->next; q->next=p3; q=q->next; p2=p2->next; } } void check(node* &h){ if(h==NULL||h->next==NULL)return; node* q=h->next; bool flag=0; while(q->next!=NULL){ if(!flag)printf("%d*x^%d",q->a,q->b),flag=1; else printf("%d*x^%d",Abs(q->a),q->b); if(q->next->a>0)printf(" + "); else printf(" - "); q=q->next; } if(q!=NULL)printf("%d*x^%d ",Abs(q->a),q->b); } int main(){ freopen("Cola.txt","r",stdin); int n,x; creat_node(head[1]); creat_node(head[2]); //------------------------------------------------- puts("请输入第一个多项式的项数:"); scanf("%d",&n); puts("请依次输入每一项的系数:"); // creat_node(head[1]); for(int i=n-1;i>=0;i--){ scanf("%d",&x); Insert(head[1],x,i); } // check(head[1]); reverse(head[1]); // check(head[1]); //-------------------------------------------------- puts("请输入第二个多项式的项数:"); scanf("%d",&n); puts("请依次输入每一项的系数:"); // creat_node(head[2]); for(int i=n-1;i>=0;i--){ scanf("%d",&x); Insert(head[2],x,i); } // check(head[2]); reverse(head[2]); // check(head[2]); //---------------------------------------------------- puts("两多项式之和为:"); calc1(head[1],head[2],head[3]);//加法运算 reverse(head[3]); check(head[3]); free(head[3]); //----------------------------------------------------- puts("两多项式之差为:"); calc2(head[1],head[2],head[3]);//减法运算 reverse(head[3]); check(head[3]); return 0; }