⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 实验二(2).cpp

📁 数据结构的第二个实验
💻 CPP
字号:
#include<iostream.h> 
#include<string.h> 
#include<stdio.h> 
template <class type>class clist;

template <class type>class clistnode{
friend class clist;
public:
	clistnode( ):data(0),rlink(NULL),llink(NULL){}
//private:
	type data;
	clistnode<type>*rlink,*llink;
};
//--------------------------------------------
template <class type>class clist{
public:
	clist(){first=last=new clistnode<type>;}
	clist(int n, char*s); 
	void add(clist<type>M,clist<type>N);
	void display();
//private:
	clistnode<type>*first, *last,*pr;
};
//-------------------------------------------------------
template <class type>void clist<type>::display()
{	
	clistnode<type>*p=first;
	while (p->rlink!=first)
	{
		cout<<p->data<<endl;
		p=p->rlink;
	} cout<<p->data<<endl;
}
template <class type> clist<type>::clist(int n,char*s)
{
	first=last=new clistnode<type>;  clistnode<type>*p=first;
	for(int i=1;i<=n-1;i++)
	{  
		p->data=*s-48;      
		clistnode<type>*q=new clistnode<type>;  
		p->rlink=q;  q->llink=p;
		s++; p=q;
	}
	p->data=*s-48;
	last=p;   
	last->rlink=first;
	first->llink=last;
}
template <class type> void add(clist<type>M,clist<type>N) 
{
	clistnode<type>*p1=M.last;   clistnode<type>*p2=N.last;
	int c1=p1->data;   int c2=p2->data;
	int temp=0 ;       int h1=0,h2=0;

	
	char*c=new char[100];  char*c0;
	while(h1!=1 || h2!=1)
	{
		
		if(h1==1) c1=0;  else c1=p1->data;    
		if(h2==1) c2=0;  else c2=p2->data;    
		*c= ( c1+c2+temp )%10+48;  c0=c; 
		c++;
		temp=  ( c1+c2+temp )/10;
		p1=p1->llink;	p2=p2->llink;

		if(p1==M.last) h1=1;   
		if(p2==N.last) h2=1;
	}
	if(temp!=0) *c=temp+48;
	else  c--;
	while(*c!=NULL)
	{
		cout<<*c;
		c--;
	}
}
void main()
{
	char*s=new char[100]; char*h=new char[100];
	cout<<"please give the data A:"<<endl; 
	cin>>s; 
	cout<<"please give the data B:"<<endl; 
	cin>>h; 
	int m1=strlen(s);     
	clist<int> L1(m1,s);  
	int m2=strlen(h);    
	clist<int> L2(m2,h);
	cout<<"A+B="<<endl;
    add(L1,L2);
	delete[]s;	 delete[]h;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -