【C++】怎样读取TXT文件里的数据并且建立异质链表?

一厮倾城 2015-08-28 08:29:43
/*实验三 异质链表及其应用(12学时)

具体要求:

1.完成图形类及其派生类的定义
(1) 设计一个计算图形面积的类库。它的顶层是一个抽象类,并且提供三个纯虚函数:显示数据成员、返回面积和返回体积。
class Shape
{public:
virtual void showData()=0;
virtual double reArea()=0;
virtual double reVolume()=0;
};
第二层有Shape类派生TwoDimShape(二维图形)和ThreeShape(三维图形)类。他们增加了有关的数据成员,但没有成员函数的实现。
第三层派生具体的图形类。TwoDimShape类派生Circle(圆)、Elipse(椭圆)、Rectangle(矩形)和Triangle(三角形)等类。ThreeShape类派生Ball(球体)、Cylider(圆柱体)和RectangularParallelepiped(长方体)等类。
(2) 设计一个测试程序,全面测试你的设计,要求使用基类指针引用派生类对象的三个函数。
(3) 在按要求进行以上工作的同时,完成以下思考。
思考并验证
(1) 去掉类Shape中的第一个virtual和=0;,加上{},其余不变,输出结果会如何变化,为什么?
(2) 去掉类Shape中的第一个=0,其余不变,会产生什么问题,为什么?
(3) 如果执意不把类Shape中的showData()定义成纯虚函数或虚函数,应如何修改,使程序保持原来的效果不变?
(4)在你的程序中,多态性是如何体现的?
2.用异质链表组织上述各类图形对象。
(1) 实现单链表:将结点数据域定义为int,链表具有插入结点、删除结点、反转、遍历等功能,并测试其正确性。
(2) 将结点数据域定义修改为Shape *,用链表中的每个结点表示一个具体的图形对象,创建图形对象链表,并测试其正确性。
(3) 将所建单链表中的内容写到一个文本文件中,反转单链表并再次将其内容追加到同一文本文件中,查阅其正确性。
思考并验证
如果不采用虚函数和多态性,本题会如何?
实验三评分标准:
完成所有要求,符合软件工程要求,则最高可得100分;每缺少一个功能或存在一个运行错误,则扣除10分,不符合软件工程要求的扣20分。
注意:本题中,上述各个步骤只是建议这样做,最终只检查一个统一的完整的程序实现。
注意:完成作业的时间也在评分体系中有考虑,查的早则相对分数会高些。
*/
...全文
375 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
斯温jack 2015-08-29
  • 打赏
  • 举报
回复
void inputFile(Shape *head)//将链表输入进文件里 { ofstream fout; string str=""; fout.open("e:\\hello.txt"); fout<<str; //fout.close(); //原来fout的位置 Shape *p; p=head; while(p) { p->inData2(); p=p->next; } fout.close();//新fout的位置 delete p; }
二班的码农 2015-08-28
  • 打赏
  • 举报
回复
楼主,你代码太长了,插断点自己调调看
一厮倾城 2015-08-28
  • 打赏
  • 举报
回复
题目要求有点问题,简单地说这个程序有存档功能每次可以读档继续编辑txt文件,我的思路是每次读取后建立异质链表,如果不存档txt文件就不会有改动,如果有增删改查之类得就会清空TXT文件并重新将现有的链表内容导入txt文件,,,
一厮倾城 2015-08-28
  • 打赏
  • 举报
回复
我的这段代码每次读取数据以后就不能继续插入节点?到底在哪儿出错了?
一厮倾城 2015-08-28
  • 打赏
  • 举报
回复
#include<iostream> #include<cmath> #include<fstream> #include<cstring> #include<string> using namespace std; #define PI 3.1415926; string a; class Shape//第一层:图形类 { public: static int locate; virtual void showData()=0;//显示数据成员 virtual double reArea()=0;//显示返回面积 virtual double reVolume()=0;//显示返回体积 virtual void inData()=0; virtual void inData2()=0; virtual void inData3(ifstream &)=0; Shape *next; fstream in; }; class TwoDimShape:public Shape//第二层:二维图形 { public: double area;//面积 TwoDimShape()//构造函数 { area=0; } }; class ThreeDimShape:public Shape//第二层:三维图形 { public: double volume;//体积 ThreeDimShape() { volume=0; } }; class Circle:public TwoDimShape//第三层:二维图形Circle(圆) { public: double radius; Circle() { radius=0; } Circle(double radius) { this->radius=radius; } void showData() { area=radius*radius*PI; cout<<"圆半径:"<<radius<<endl; cout<<"圆面积:"<<area<<endl; } double reArea() { area=radius*radius*PI; return area; } void inData() { cout<<"请输入半径"<<endl; cin>>radius; } void inData2() { in.open("e:\\hello.txt",ios::in|ios::app); area=radius*radius*PI; in<<"圆半径:"<<" "<<radius<<endl; in<<"圆面积:"<<" "<<area<<endl; in.close(); } void inData3(ifstream &it) { it>>radius>>a>>area; } double reVolume() {return 0;} }; class Elipse:public TwoDimShape//第三层:二维图形Elipse(椭圆) { public: double aradius,bradius; Elipse() { aradius=0; bradius=0; } Elipse(double a,double bradius) { this->aradius=a; this->bradius=bradius; } void showData() { area=reArea(); cout<<"椭圆半长轴:"<<" "<<aradius<<endl; cout<<"椭圆半短轴"<<" "<<bradius<<endl; cout<<"椭圆面积"<<" "<<area<<endl; } double reArea() { area=aradius*bradius*PI; return area; } void inData() { cout<<"请输入半长轴"<<endl; cin>>aradius; cout<<"请输入半短轴"<<endl; cin>>bradius; } void inData2() { area=reArea(); in.open("e:\\hello.txt",ios::in|ios::app); in<<"椭圆半长轴:"<<" "<<aradius<<endl; in<<"椭圆半短轴"<<" "<<bradius<<endl; in<<"椭圆面积"<<" "<<area<<endl; in.close(); } void inData3(ifstream &it) { it>>aradius>>a>>bradius>>a>>area; } double reVolume() {return 0;} }; class Rectangle:public TwoDimShape//第三层:二维图形Rectangle(矩形) { public: double length,width; Rectangle() { length=0; width=0; } void showData() { area=reArea(); cout<<"矩形长度:"<<" "<<length<<endl; cout<<"矩形宽度:"<<" "<<width<<endl; cout<<"矩形面积:"<<" "<<area<<endl; } double reArea() { area=length*width; return area; } void inData() { cout<<"请输入长度"<<endl; cin>>length; cout<<"请输入宽度"<<endl; cin>>width; } void inData2() { area=reArea(); in.open("e:\\hello.txt",ios::in|ios::app); in<<"矩形长度:"<<" "<<length<<endl; in<<"矩形宽度:"<<" "<<width<<endl; in<<"矩形面积:"<<" "<<area<<endl; in.close(); } void inData3(ifstream &it) { it>>length>>a>>width>>a>>area; } double reVolume() { return 0; } }; class Triangle:public TwoDimShape//第三层:二维图形Triangle(三角形) { public: double base,high; Triangle() { base=0; high=0; } void showData() { area=reArea(); cout<<"三角形底:"<<" "<<base<<endl; cout<<"三角形高:"<<" "<<high<<endl; cout<<"三角形面积:"<<" "<<area<<endl; } double reArea() { area=0.5*base*high; return area; } void inData() { cout<<"请输入底部长度"<<endl; cin>>base; cout<<"请输入高度"<<endl; cin>>high; } void inData2() { area=reArea(); in.open("e:\\hello.txt",ios::in|ios::app); in<<"三角形底:"<<" "<<base<<endl; in<<"三角形高:"<<" "<<high<<endl; in<<"三角形面积:"<<" "<<area<<endl; in.close(); } void inData3(ifstream &it) { it>>base>>a>>high>>a>>area; } double reVolume() {return 0;} }; class Ball:public ThreeDimShape//第三层:三维图形Ball(球体) { public: double radius; void showData() { volume=reVolume(); cout<<"球体半径:"<<" "<<radius<<endl; cout<<"球体体积:"<<" "<<volume<<endl; } double reVolume() { volume=pow(radius,3)*4/3; return volume; } void inData() { cout<<"请输入半径"<<" "<<endl; cin>>radius; volume=reVolume(); } void inData2() { in.open("e:\\hello.txt",ios::in|ios::app); in<<"球体半径:"<<" "<<radius<<endl; in<<"球体体积:"<<" "<<volume<<endl; in.close(); } void inData3(ifstream &it) { it>>radius>>a>>volume; } double reArea() {return 0;} }; class Cylider:public ThreeDimShape//第三层:三维图形Cylider(圆柱体) { public: double radius; double high; Cylider() { radius=0; high=0; } void showData() { volume=reVolume(); cout<<"圆柱体底部圆半径:"<<" "<<radius<<endl; cout<<"圆柱体高:"<<" "<<high<<endl; cout<<"圆柱体面积:"<<" "<<volume<<endl; } double reVolume() { volume=pow(radius,2)*high*PI; return volume; } void inData() { cout<<"请输入半径"<<" "<<endl; cin>>radius; cout<<"请输入高度"<<" "<<endl; cin>>high; } void inData2() { in.open("e:\\hello.txt",ios::in|ios::app); in<<"圆柱体底部圆半径:"<<" "<<radius<<endl; in<<"圆柱体高:"<<" "<<high<<endl; in<<"圆柱体面积:"<<" "<<volume<<endl; in.close(); } void inData3(ifstream &it) { it>>radius>>a>>high>>a>>volume; } double reArea() {return 0;} }; class RectangularParallelepiped:public ThreeDimShape//第三层:三维图形RectangularParallelepiped(长方体) { public: double length,width,high; void showData() { volume=reVolume(); cout<<"长方体长度:"<<" "<<length<<endl; cout<<"长方体宽度:"<<" "<<width<<endl; cout<<"长方体高度"<<" "<<high<<endl; cout<<"长方体面积"<<" "<<volume<<endl; } double reVolume() { volume=length*width*high; return volume; } void inData() { cout<<"请输入长度"<<" "<<endl; cin>>length; cout<<"请输入宽度"<<" "<<endl; cin>>width; cout<<"请输入高度"<<" "<<endl; cin>>high; } void inData2() { volume=reVolume(); in.open("e:\\hello.txt",ios::in|ios::app); in<<"长方体长度:"<<" "<<length<<endl; in<<"长方体宽度:"<<" "<<width<<endl; in<<"长方体高度"<<" "<<high<<endl; in<<"长方体面积"<<" "<<volume<<endl; in.close(); } void inData3(ifstream &it) { it>>length>>a>>width>>a>>high>>a>>volume; } double reArea() {return 0;} }; Shape *head=NULL; int Shape::locate=0; void addList(Shape *&p,int num,Shape *&s) { int i=0; if(num==1) s=new Circle(); if(num==2) s=new Elipse(); if(num==3) s=new Rectangle(); if(num==4) s=new Triangle(); if(num==5) s=new Ball(); if(num==6) s=new Cylider(); if(num==7) s=new RectangularParallelepiped(); s->inData(); if(s->locate==0) { head=s; p=s; s->locate++; } p->next=s; p=s; p->next=NULL; s->locate++; } void delList(Shape *&head,int num)//删除链表节点 { Shape *p,*q; p=head; if(head==NULL) { cout<<"链表为空"<<endl; return; } if(head->locate=num) { head=head->next; head->locate--; return; } while(p) { if(p->locate==num) { q=p->next; p->next=q->next; delete q; head->locate--; } p=p->next; } cout<<"没有要删除的结点"<<endl; } void reverseList(Shape *p, bool bIsHead)//反转链表 { if(head ==NULL) return; Shape*pre, *cur, *ne; pre=head; cur=head->next; while(cur) { ne = cur->next; cur->next = pre; pre = cur; cur = ne; } head->next = NULL; head = pre; } void showList(Shape *head)//遍历链表 { Shape *p; p=head; while(p) { p->showData(); p=p->next; } delete p; } void inputFile(Shape *head)//将链表输入进文件里 { ofstream fout; string str=""; fout.open("e:\\hello.txt"); fout<<str; fout.close(); Shape *p; p=head; while(p) { p->inData2(); p=p->next; } delete p; } void buildList(Shape *&p,int num,Shape *&s,ifstream &in)//读取文件时调用的建立链表函数 { int i=0; if(num==1) s=new Circle(); if(num==2) s=new Elipse(); if(num==3) s=new Rectangle(); if(num==4) s=new Triangle(); if(num==5) s=new Ball(); if(num==6) s=new Cylider(); if(num==7) s=new RectangularParallelepiped(); s->inData3(in); cout<<"readFile运行了一遍"<<endl; if(s->locate==0) {head=s;p=s;s->locate++;} p->next=s; p=s; p->next=NULL; s->locate++; } void readFile(char *fileName,int delLine)//读取文件数据并且建立链表 { ifstream inf(fileName,ios::in); char string[1000]; Shape *s=NULL;Shape *p; for(int i=1;i<delLine;i++) inf.getline(string,10000); while(!inf.eof()) { inf.getline(string,1000); cout<<string<<endl; }inf.close(); inf.open(fileName,ios::in); while(!inf.eof()) { inf>>a; if(a=="圆半径:") buildList(p,1,s,inf); if(a=="椭圆半长轴:") buildList(p,2,s,inf); if(a=="矩形长度:") buildList(p,3,s,inf); if(a=="三角形底:") buildList(p,4,s,inf); if(a=="球体半径:") buildList(p,5,s,inf); if(a=="圆柱体底部圆半径:") buildList(p,6,s,inf); if(a=="长方体长度:") buildList(p,7,s,inf); } inf.close(); } int main() { Shape *p,*s=NULL; int num1,num2; cout<<"--------------------欢迎进入图形类计算程序--------------------"<<endl; cout<<"1--插入,2--删除,3--遍历,4--反转,5--存档,6--读档,7--退出 "<<endl; cout<<"--------------------------------------------------------------"<<endl; while(cin>>num1) { if(num1==1) { cout<<"************你要输入什么数据?***************"<<endl; cout<<"************ 二维图形 ***************"<<endl; cout<<"****************1--圆形**********************"<<endl; cout<<"****************2--椭圆形********************"<<endl; cout<<"****************3--矩形**********************"<<endl; cout<<"****************4--三角形********************"<<endl; cout<<"************ 三维图形 ***************"<<endl; cout<<"****************5--球体**********************"<<endl; cout<<"****************6--圆柱体********************"<<endl; cout<<"****************7--长方体********************"<<endl; cin>>num2; addList(p,num2,s); } if(num1==2) { cout<<"************请输入要删除的节点序号***********"<<endl; cin>>num2; delList(head,num2); } if(num1==3) { showList(head); } if(num1==4) { reverseList(head,true); showList(head); } if(num1==5) { inputFile(head); } if(num1==6) { head->locate=0; delete head; head=NULL; readFile("e:\\hello.txt",0); } if(num1==7) {break;} else {} cout<<"1--插入,2--删除,3--遍历,4--反转,5--存档,6--读档,7--退出 "<<endl; } }

64,692

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧