2021-11-09 Project2 Sum

该程序使用C++实现了一系列数据结构(队列、链表、堆栈)的操作,包括插入、删除、查询和打印。用户可以输入随机数个数和类型,将生成的随机数写入文件,然后读取并选择数据结构进行操作。程序支持文件读写,具有友好的交互界面。
摘要由CSDN通过智能技术生成

在这里插入图片描述
main.cpp

#include <iostream>
#include "conio.h"
using namespace std;
#include"fileprocess.h"
#include"tip.h"
#include"LinearList.h"
#include"LinkList.h"
#include"LinearStack.h"
#include"LinkStack.h"
#include"LinearQueue.h"
#include"LinkQueue.h"

int main()
{
	int N,a,b,d,k,index,c=0;                 //随机数个数和用户输入选择 k:结点插在哪个位置 
	char path1[30],path2[30];                //写入路径,和读取路径 
	//全部初始化成float类型 
	LinkQueue<float> linkqueue;
	LinkList<float> linklist;
	LinkStack<float> linkstack;
	float e,x,f;
	linestar();                            // ************************************* 
	tip1_1();                              // 一、请输入需要产生的随机数个数N=
	scanf("%d",&N);                        // 接受随机数个数N
	LinearQueue<float> linearqueue(N+2);
	LinearList<float> linearlist(N+2);
	LinearStack<float> linearstack(100);
	linestar();                            // ************************************* 
	tip1_2();                              // 请选择随机数的类型:1:整数 2:浮点数 3:退出 
	scanf("%d",&a);                        // 接受变量a 
	linestar();                            // ************************************* 
	if(a==3){return 1;}                    // 输入3以及不是1和2的东西 退出程序 
	else if(!(a==1||a==2)){                // 
		error_1();                         // 错误:输入不在选项内 
		return 1;                          // 
	}
	tip2_1();                              // 二、请输入存储的文件名:
	scanf("%s",&path1);                    //
	wrand(a,N,path1);linestar();           // 写入随机数 
	do{
		do{
			tip3_1();                          // 三、请选择读取随机数的数据结构:1:队列 2:链表 3:堆栈 4:退出程序 
			scanf("%d",&b);linestar();         // 接受变量b
			if(b==4){return 1;}                // 输入4以及不是1、2和3的东西 退出程序 
			else if(!(b==1||b==2||b==3)){      //
				error_1();                     // 错误:输入不在选项内 
				return 1;                      // 
			}
			tip4_1();                          // 四、请选择构建形式:1:数组 2:链表 3:返回上级(三)菜单 
			scanf("%d",&c);linestar();	       // 接受变量c
		}while(c==3);                          // 如果是c==3就要返回上级菜单 
		tip5_1();                              // 五、请输入读取的文件名:
		scanf("%s",&path2);linestar();         // ************************************* 
		float *A;                              // 定义一个动态数组用来存放从文件读取的数 
		A=new float[N];                        // 开辟空间 
		A= rrand(N,path2);                     // 读取 
		for (int i=0;i<N;i++){                       //以何种结构来存数据                                     
			if(b==1&&c==1){                          //
				linearqueue.Insert(A[i]);            //
			}                                        //
			if(b==2&&c==1){                          //                         
				linearlist.Insert(i+1,A[i]);         // 
			}                                        //
			if(b==3&&c==1){                          //   
				linearstack.Push(A[i]);              //                     
			}                                        //
			if(b==1&&c==2){                          //
				linkqueue.Insert(A[i]);              //
			}                                        //
			if(b==2&&c==2){                          // 
				linklist.Insert(i+1,A[i]);           //
			}                                        //
			if(b==3&&c==2){                          // 
				linkstack.Push(A[i]);                //
			}   
		}    
			again:                         
	       	tip6_0();                                    // 六、请选择操作:1:插入 2:删除 3:查询 4:打印 5:返回菜单(三)
			scanf("%d",&d);linestar();                   // 接受变量d
			if(d==1){      								 // 1.插入 
				cout<<"请输入要插入的值:";              // 接受变量e 
				cin>>e;
				if(b==1&&c==2){                                                                            
					linkqueue.Insert(e);                       
				}                                        
				if(b==2&&c==2){
					cout<<"请指定插入位置:";               
					cin>>k; 
					linklist.Insert(k,e);                         
				}                                        
				if(b==3&&c==2){                         
					linkstack.Push(e);                       
				}                                        
				if(b==1&&c==1){                          
					linearqueue.Insert(e);                         
				}                                        
				if(b==2&&c==1){
					cout<<"请指定插入位置:";               
					cin>>k;                                                  
					linearlist.Insert(k,e);                         
				}                                        
				if(b==3&&c==1){                           
					linearstack.Push(e);                                     
				}                                        
			}                                            
			if(d==2){                                    //2.删除  
				if(b==1&&c==2){                          
	                linkqueue.Delete(x);                                     
	                cout<<"已删除队尾元素:"<<x<<endl;
				}                                       
				if(b==2&&c==2){
					cout<<"请输入删除元素的位置:";                          
	                cin>>k;
					linklist.DeleteByIndex(k,x);          
					cout<<"已删除"<<k<<"位置的元素"<<x<<endl;
				}                                        
				if(b==3&&c==2){                          
	                linkstack.Pop(x);        
	                cout<<"已删除栈顶元素:"<<x<<endl;
				}                                      
				if(b==1&&c==1){                          
	                linearqueue.Delete(x);         
	                cout<<"已删除队尾元素:"<<x<<endl;
				}                                        
				if(b==2&&c==1){                                              
					linearlist.DeleteByIndex(k,x);
					cout<<"已删除"<<k<<"位置的元素"<<x<<endl;
				}
				if(b==3&&c==1){                           
	        		linearstack.Pop(x);
					cout<<"已删除栈顶元素:"<<x<<endl;                                   
				}
			}
			if(d==3){                                    // 3.查询(根据值查找) 
				cout<<"请输入要查找的值:"; 
				cin>>f;
				if(b==1&&c==2){       
					index=linkqueue.Find(f);                 
				}                                        
				if(b==2&&c==2){                          
	                index=linklist.Find(f);       
				}                                        
				if(b==3&&c==2){                          
	                index=linkstack.Find(f);         
				}                                        
				if(b==1&&c==1){                          
	                index=linearqueue.Find(f);        
				}                                        
				if(b==2&&c==1){                                                 
					index=linearlist.Find(f); 
				}
				if(b==3&&c==1){   
					index=linearstack.Find(f);                          
				}
				if(index)
					cout<<"查找成功,其位置在"<<index<<endl; 
				else{cout<<"查找失败"<<endl;}       
			}
			if(d==4){                                    // 4.打印 
				if(b==1&&c==2){                         
	               	cout<<linkqueue<<endl;        
				}                                      
				if(b==2&&c==2){                         
	                cout<<linklist<<endl;     
				}                                        
				if(b==3&&c==2){                          
	                cout<<linkstack<<endl; 
				}                                       
				if(b==1&&c==1){                          
	                cout<<linearqueue<<endl;         
				}                                     
				if(b==2&&c==1){                                                  
					cout<<linearlist<<endl; 
				}
				if(b==3&&c==1){                         
					cout<<linearstack<<endl;                                      
				}
			}
			if(d!=5)
			{
				goto again;
			}
	}while(d==5);
cout<<"全部结束,再见!"<<endl; 
return 0;
}

fileprocess.h

#include <iostream>
#include <time.h>
using namespace std;

/*随机数写入函数,其中a=1为整数,a=2为小数*/ 
wrand(int a,int N,char path[]){
	FILE *fp=fopen(path,"w");
	srand((unsigned)time( NULL ) );                          //使用系统时间初始化随机数种子
	switch(a)
	{
		case 1:
				for(int i=0;i<N;i++)
				{
					fprintf(fp,"%d ",rand()%(100-10+1)+0);          //文件写入[0,100]的整数
				};
				break;
		case 2:
				for(int i=0;i<N;i++)
				{
					fprintf(fp,"%f ",0+1.0*rand()/RAND_MAX *(1-0));//文件写入[0,1]的浮点数
				};
				break;
		default: break; 
	}
	fclose(fp);
}

/*从文件读取小数到数组中*/ 
float* rrand(int N,char path[]){
	FILE *fp1=fopen(path,"r");
	float f;
	float *array;
	array=new float[N];
	for (int i=0;i<N;i++)
	{
		fscanf(fp1,"%f",&array[i]);
	}
	return array;
}

tips.h

#ifndef TIP_
#define TIP_
#include <iostream>
using namespace std;
/*输出提示语句*/
void linestar(){printf("*************************************\n");};
void tip1_1(){printf("project 2\n");linestar();printf("一、请输入需要产生的随机数个数N=");};//n
void tip1_2(){printf("请选择随机数的类型:\n1:整数\n2:浮点数\n3:退出\n");linestar();printf("请输入(1,2或3):");};//a
void tip2_1(){printf("二、请输入存储的文件名:");};
void tip3_1(){printf("三、请选择读取随机数的数据结构:\n1:队列\n2:链表\n3:堆栈\n4:退出程序\n");linestar();printf("请输入(1,2,3或4):");};//b
void tip4_1(){printf("四、请选择构建形式:\n1:数组\n2:链表\n3:返回上级(三)菜单\n");linestar();printf("请输入(1,2或3):");};//c
void tip5_1(){printf("五、请输入读取的文件名:");};
void tip6_0(){printf("六、请选择操作:\n1:插入\n2:删除\n3:查询\n4:打印\n5:返回菜单(三)\n");linestar();};//d
void error_1(){printf("错误:输入不在选项内\n");};
void error_2(){printf("错误:读入失败\n");}; 
#endif

点击跳转
#include"LinearList.h"
#include"LinkList.h"
#include"LinearStack.h"
#include"LinkStack.h"
#include"LinearQueue.h"
#include"LinkQueue.h"

运行截图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值