可以对各种图形做相应计算
#include<iostream>
#include<cmath>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;
double PI=3.14;
class Shape
{
public:
double type;
virtual void showData()=0;
virtual double reArea()=0;
virtual double reVolume()=0;
virtual void save(ofstream &out)=0;
Shape *next;
};
//函数分割线————————————————————————
class TwoDimShape:public Shape
{
public:
double Area,Volume;
void showData(){}
double reArea()
{
return 0;
}
double reVolume()
{
return 0;
}
};
//函数分割线————————————————————————
class ThreeShape:public Shape
{
public:
double Area,Volume;
void showData(){}
double reArea()
{
return 0;
}
double reVolume()
{
return 0;
}
};
//函数分割线————————————————————————
class Circle:public TwoDimShape
{
public:
double r;
Circle(double x=0)
{
r=x;
type=1;//
}
void save(ofstream &out)
{
out<<type<<" "<<r<<endl;
}
void showData()
{
cout<<"圆的半径是:"<<r<<endl;
cout<<"圆的面积是:"<<reArea()<<endl;
}
double reArea()
{
Area=PI*r*r;
return Area;
}
};
//函数分割线————————————————————————
class Elipse:public TwoDimShape
{
public:
double x;//长轴
double y;//短轴
Elipse(double a=0,double b=0)
{
x=a;y=b;
type=2;
}
void save(ofstream &out)
{
out<<type<<" "<<x<<" "<<y<<endl;
}
void showData()
{
cout<<"椭圆的长轴是:"<<x<<endl;
cout<<"椭圆的短轴是:"<<y<<endl;
cout<<"椭圆的面积是:"<<reArea()<<endl;
}
double reArea()
{
Area=PI*x*y;
return Area;
}
};
//函数分割线————————————————————————
class Rectangle:public TwoDimShape
{
public:
double a;//长
double b;//宽
Rectangle(double x=0,double y=0)
{
a=x;b=y;
type=3;
}
void save(ofstream &out)
{
out<<type<<" "<<a<<" "<<b<<endl;
}
void showData()
{
cout<<"矩形的长是:"<<a<<endl;
cout<<"矩形的宽是:"<<b<<endl;
cout<<"矩形的面积是:"<<reArea()<<endl;
}
double reArea()
{
Area=a*b;
return Area;
}
};
//函数分割线————————————————————————
class Triangle:public TwoDimShape
{
public:
double a;//底
double h;//高
Triangle(double x=0,double b=0)
{
a=x;h=b;
type=4;
}
void save(ofstream &out)
{
out<<type<<" "<<a<<" "<<h<<endl;
}
void showData()
{
cout<<"三角形的底是:"<<a<<endl;
cout<<"三角形的高是:"<<h<<endl;
cout<<"三角形的面积是:"<<reArea()<<endl;
}
double reArea()
{
Area=a*h*0.5;
return Area;
}
};
//函数分割线————————————————————————
class Ball:public ThreeShape
{
public:
double r;
Ball(double x=0)
{
r=x;
type=5;
}
void save(ofstream &out)
{
out<<type<<" "<<r<<endl;
}
void showData()
{
cout<<"球的半径为"<<r<<endl;
cout<<"球的体积为:"<<reVolume()<<endl;
}
double reVolume()
{
Area=(4/3)*PI*r*r*r;
return Volume;
}
};
//函数分割线————————————————————————
class Cvlider:public ThreeShape
{
public:
double b; //圆柱体的半径
double c; //圆柱体的高
Cvlider(double r=0,double x=0)
{
b=r;c=x;type=6;
}
void save(ofstream &out)
{
out<<type<<" "<<b<<" "<<c<<endl;
}
void showData()
{
cout<<"圆柱体的半径和高是:"<<b<<" "<<c<<endl;
cout<<"圆柱体的体积是"<<reVolume()<<endl;
}
double reVolume()
{
Volume=3.14*b*b*c;
return Volume;
}
};
//函数分割线————————————————————————
class Changfangti:public ThreeShape
{
public:
double a; //长方体的长
double b; //长方体的宽
double c; //长方体的高
Changfangti(double x=0,double y=0,double z=0)
{
a=x;b=y;c=z;type=7;
}
void save(ofstream &out)
{
out<<type<<" "<<a<<" "<<b<<" "<<c<<endl;
}
void showData()
{
cout<<"长方体的长宽高是:"<<a<<" "<<b<<" "<<c<<endl;
cout<<"长方体的体积是:"<<reVolume()<<endl;
}
double reVolume()
{
Volume=a*b*c;
return Volume;
}
};
//函数分割线————————————————————————
void menu1()
{
cout<<"____________________________________________"<<endl;
cout<<"***************1.插入节点*******************"<<endl;
cout<<"***************2.删除节点*******************"<<endl;
cout<<"***************3.遍历链表*******************"<<endl;
cout<<"***************4.反转***********************"<<endl;
cout<<"***************5.存档***********************"<<endl;
cout<<"***************6.读档***********************"<<endl;
cout<<"***************0.退出***********************"<<endl;
cout<<"____________________________________________"<<endl;
cout<<endl;
cout<<endl;
}
//函数分割线————————————————————————
void menu2()
{
cout<<"请输入您要操作的图形"<<endl;
cout<<"________________________________________"<<endl;
cout<<"***************1.圆形*******************"<<endl;
cout<<"***************2.椭圆*******************"<<endl;
cout<<"***************3.矩形*******************"<<endl;
cout<<"***************4.三角形*****************"<<endl;
cout<<"***************5.球体*******************"<<endl;
cout<<"***************6.圆柱体*****************"<<endl;
cout<<"***************7.长方体*****************"<<endl;
cout<<"***************0.退出*******************"<<endl;
cout<<"________________________________________"<<endl;
cout<<endl;
cout<<endl;
}
Shape *head=NULL;
//函数分割线————————————————————————
void Creatlist()//增加节点
{
menu2();
int n;
cin>>n;
//=====方便代码好看分隔线=====
switch(n)
{ case 1:
{
Shape *p,*s;
if(head==NULL)
{
double r;
cout<<"请输入圆的半径:"<<endl;
cin>>r;
p=new Circle(r);
head=p;
head->next=NULL;
}
else
{
s=head;
while(s->next)
{
s=s->next;
}
double r;
cout<<"请输入圆的半径:"<<endl;
cin>>r;
p=new Circle(r);
s->next=p;
s=p;
s->next=NULL;
}
}break;
//=====方便代码好看分隔线=====
case 2:
{
Shape *p,*s;
if(head==NULL)
{
double a,b;
cout<<"请输入椭圆的长轴和短轴"<<endl;
cin>>a>>b;
p=new Elipse(a,b);
head=p;
head->next=NULL;
}
else
{
s=head;
while(s->next)
{
s=s->next;
}
double a,b;
cout<<"请输入椭圆的长轴和短轴"<<endl;
cin>>a>>b;
p=new Elipse(a,b);
s->next=p;
s=p;
s->next=NULL;
}
}break;
//=====方便代码好看分隔线=====
case 3:
{
Shape *p,*s;
if(head==NULL)
{
double a,b;
cout<<"请输入矩形的长和宽"<<endl;
cin>>a>>b;
p=new Rectangle(a,b);
head=p;
head->next=NULL;
}
else
{
s=head;
while(s->next)
{
s=s->next;
}
double a,b;
cout<<"请输入矩形的长和宽"<<endl;
cin>>a>>b;
p=new Rectangle(a,b);
s->next=p;
s=p;
s->next=NULL;
}
}break;
//=====方便代码好看分隔线=====
case 4:
{
Shape *p,*s;
if(head==NULL)
{
double a,b;
cout<<"请输入三角形的底和高"<<endl;
cin>>a>>b;
p=new Triangle(a,b);
head=p;
head->next=NULL;
}
else
{
s=head;
while(s->next)
{
s=s->next;
}
double a,b;
cout<<"请输入三角形的底和高"<<endl;
cin>>a>>b;
p=new Triangle(a,b);
s->next=p;
s=p;
s->next=NULL;
}
}break;
//=====方便代码好看分隔线=====
case 5:
{
Shape *p,*s;
if(head==NULL)
{
double r;
cout<<"请输入球的半径:"<<endl;
cin>>r;
p=new Ball(r);
head=p;
head->next=NULL;
}
else
{
s=head;
while(s->next)
{
s=s->next;
}
double r;
cout<<"请输入球的半径:"<<endl;
cin>>r;
p=new Ball(r);
s->next=p;
s=p;
s->next=NULL;
}
}break;
//=====方便代码好看分隔线=====
case 6:
{
Shape *p,*s;
if(head==NULL)
{
double a,b;
cout<<"请输入圆柱体的半径和高"<<endl;
cin>>a>>b;
p=new Cvlider(a,b);
head=p;
head->next=NULL;
}
else
{
s=head;
while(s->next)
{
s=s->next;
}
double a,b;
cout<<"请输入圆柱体的半径和高"<<endl;
cin>>a>>b;
p=new Cvlider(a,b);
s->next=p;
s=p;
s->next=NULL;
}
}break;
//=====方便代码好看分隔线=====
case 7:
{
Shape *p,*s;
if(head==NULL)
{
double a,b,c;
cout<<"请输入长方体的长宽高"<<endl;
cin>>a>>b>>c;
p=new Changfangti(a,b,c);
head=p;
head->next=NULL;
}
else
{
s=head;
while(s->next)
{
s=s->next;
}
double a,b,c;
cout<<"请输入长方体的长宽高"<<endl;
cin>>a>>b>>c;
p=new Changfangti(a,b,c);
s->next=p;
s=p;
s->next=NULL;
}break;
}
}
}
//函数分割线————————————————————————
void showlist()
{
Shape *p;
p=head;
while(p)
{
p->showData();
p=p->next;
}
cout<<endl;
cout<<endl;
}
//函数分割线————————————————————————
void shanchulist()
{
Shape *p,*q;
int n,i;
cout<<"你想删除第几个节点?"<<endl;
cin>>n;
p=head;
if(head==NULL)
{
cout<<"链表为空"<<endl;
return;
}
if(n==1)
{
head=head->next;
cout<<"删除成功"<<endl;
return;
}
else
{
while(p->next!=NULL&&i<=n)
{
p=head;
while(p->next)
{
p=p->next;
}
}
q->next=p->next;
delete p;
cout<<"删除成功"<<endl;
return;
}
}
//函数分割线————————————————————————
void fanzhuanlist()
{
Shape *p=NULL;
Shape *tmp = NULL;
if(head == NULL)
{
return;
}
tmp = head -> next;
while (tmp->next != NULL)
{
p = tmp->next;
tmp->next = p->next;
p->next = head->next;
head->next = p;
}
p=head;
head=head->next;
tmp->next=p;
p->next=NULL;
cout<<"反转成功!"<<endl;
}
void get()
{
double type;
Shape *p,*s;
ifstream creat("d:\\图形数据.txt",ios::in);
{
while(creat>>type)
{
if(type==1)
{
double r;
creat>>r;
p=new Circle(r);
}
else if(type==2)
{
double a,b;
creat>>a>>b;
p=new Elipse(a,b);
}
else if(type==3)
{
double a,b;
creat>>a>>b;
p=new Rectangle(a,b);
}
else if(type==4)
{
double a,b;
creat>>a>>b;
p=new Triangle(a,b);
}
else if(type==5)
{
double r;
creat>>r;
p=new Ball(r);
}
else if(type==6)
{
double a,b;
creat>>a>>b;
p=new Cvlider(a,b);
}
else if(type==7)
{
double a,b,c;
creat>>a>>b>>c;
p=new Changfangti(a,b,c);
}
if (head == NULL)
{
head = p;
p->next = NULL;
}
else
{
p->next = head;
head = p;
}
}
}
creat.close();
cout<<"读档成功"<<endl;
}
void save()
{
Shape *p;
p=head;
ofstream read("d:\\图形数据.txt",ios::out);
while(p)
{
p->save(read);
p=p->next;
}
read.close();
cout<<"存档成功"<<endl;
return;
}
//函数分割线————————————————————————
int main()
{
int n;
while(n)
{
cout<<"请选择操作"<<endl;
menu1();
cin>>n;
system("cls");
switch(n)
{
case 1:
Creatlist();
break;
case 2:
shanchulist();
break;
case 3:
showlist();
break;
case 4:
fanzhuanlist();
break;
case 5:
save();
break;
case 6:
get();
break;
}
}
}