数据结构大作业C++实现

目录

一、不同排序算法不同数量大小的数据集,运行时间/比较次数/交换次数的比较

二、约瑟夫环问题

1、顺序表实现

2、循环链表实现

3、双向链表实现

4、数学方式实现

三、N!及斐波那契数列递归和非递归实现,以及C语言下高精度实现

1、斐波那契递归和非递归实现

2、N!递归和非递归实现

3、高精度实现

四、四则运算表达式的实现

一、不同排序算法不同数量大小的数据集,运行时间/比较次数/交换次数的比较

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
#include <time.h>
using namespace std;
const int N = 10000;
int a[N], temp[N];
unsigned int count_Exc, count_Com;
 
inline void Swap(int&a, int&b)//inline可以把函数指定为内联函数。解决频繁调用函数的大量消耗栈空间问题 
{
	int temp = a;
	a = b;
	b = temp;
	count_Exc++;
}
 
inline void CopyData()
{
	for (int i = 0; i < N;i++)
		temp[N] = a[N];
}
 
inline int Compare(int a,int b)
{
	count_Com++;
	return a - b;
}
 
inline void ReadDate()
{
	count_Exc = 0;
	count_Com = 0;
	ifstream datafile("data.txt");
	int i=0;
	if (!datafile.is_open())
		cout << "无法打开文件data.txt!" << endl;
	else
		while (!datafile.eof()&&i<N)
			datafile >> a[i++];
	datafile.close();
}
 
void CreatData()
{
	ofstream datafile("data.txt");
	for (int i = 0; i < N; i++)
	{
		datafile << rand() << "	";
		if ((i + 1) % 10 == 0)
			datafile << endl;
	}
	datafile.close();
}
 
void Print()
{
	for (int i = 0; i < N; i++)
	{
		cout << a[i] << "	";
		if ((i + 1) % 10 == 0)
			cout << endl;
	}
	cout << "******************************************************************************" << endl;
}
 
 
void bubbleSort()
{
	for (int i = 0; i<N - 1; i++)
		for (int j = 0; j < N - 1 - i; j++)
		{
			if (Compare(a[j],a[j+1])>0)
				Swap(a[j], a[j + 1]);
		}
}
 
void selectSort()
{
	int index;
	for (int i = 0; i < N - 1; i++)
	{
		index = i;
		for (int j = i + 1; j < N; j++)
		{
			if (Compare(a[j],a[index])<0)
				index = j;
		}
		Swap(a[i], a[index]);
	}
}
 
void insertSort()
{
	int preindex, current;
	for (int i = 1; i < N; i++)
	{
		preindex = i - 1;
		current = a[i];
		while (preindex > -1 && Compare(a[preindex] , current)>0)
		{
			a[preindex + 1] = a[preindex];
			count_Exc++;//
			preindex--;
		}
		a[preindex + 1] = current;
		count_Exc++;//
	}
}
void quickSort(int arry[] = a, int left = 0, int right = N - 1)
{
	if (right <= left)
		return;
	int pivotindex = (left + right) / 2;
	Swap(arry[pivotindex], arry[right]);
	//划分
	int L = left - 1, R = right;
	do
	{
		while (Compare(arry[++L] , arry[right])<0);
		while (L < R &&Compare(arry[right] , arry[--R])<0);
		Swap(arry[L], arry[R]);
	} while (L < R);
 
	Swap(arry[L], arry[right]);
	quickSort(arry, left, L - 1);
	quickSort(arry, L + 1, right);
}
 
 
int main()
{
	clock_t start_time;
    clock_t finish_time;
    clock_t real_time;
    float current_time;
    float program_time;
    start_time=clock();
	CreatData();
	cout << "排序算法		" << "比较次数		" << "交换次数" << endl;
	
	ReadDate();
	current_time=(float)start_time / CLOCKS_PER_SEC;
    printf("Program start time: %f\n",current_time);
	bubbleSort();
	cout << "冒泡排序		" << setw(8) << count_Com << setw(24) << count_Exc << endl;
	finish_time=clock();
    current_time=(float)finish_time/CLOCKS_PER_SEC;
    printf("Program finish time:%f\n",current_time);
    program_time=(float)(finish_time-start_time)/CLOCKS_PER_SEC;
    printf("Program complete time: %f\n",program_time);
    printf("\n");
    ReadDate();
    current_time=(float)start_time / CLOCKS_PER_SEC;
    printf("Program start time: %f\n",current_time);
	selectSort();
	cout << "选择排序		" << setw(8) << count_Com << setw(24) << count_Exc << endl;
	finish_time=clock();
    current_time=(float)finish_time/CLOCKS_PER_SEC;
    printf("Program finish time:%f\n",current_time);
    program_time=(float)(finish_time-start_time)/CLOCKS_PER_SEC;
    printf("Program complete time: %f\n",program_time);
    printf("\n");
	ReadDate();
	current_time=(float)start_time / CLOCKS_PER_SEC;
    printf("Program start time: %f\n",current_time);
	insertSort();
	cout << "插入排序		" << setw(8) << count_Com << setw(24) << count_Exc << endl;
	finish_time=clock();
    current_time=(float)finish_time/CLOCKS_PER_SEC;
    printf("Program finish time:%f\n",current_time);
    program_time=(float)(finish_time-start_time)/CLOCKS_PER_SEC;
    printf("Program complete time: %f\n",program_time);
	printf("\n");
	ReadDate();
	current_time=(float)start_time / CLOCKS_PER_SEC;
    printf("Program start time: %f\n",current_time);
	quickSort();
	cout << "快速排序		" << setw(8) << count_Com << setw(24) << count_Exc << endl;
	finish_time=clock();
    current_time=(float)finish_time/CLOCKS_PER_SEC;
    printf("Program finish time:%f\n",current_time);
    program_time=(float)(finish_time-start_time)/CLOCKS_PER_SEC;
    printf("Program complete time: %f\n",program_time);

	system("pause");
}


二、约瑟夫环问题

1、顺序表实现

#include<bits/stdc++.h>
using namespace std;
//用数组实现约瑟夫环问题
int a[110]={0};   //元素值为0表示未出局
int main()
{
	int N,M;
	int cnt=0,i=0,k=0;  //cnt表示目前出局的人数 
	cin>>N>>M;  //n人 数字m 
	while(cnt!=N)
	{
		i++;   //i是每个人的编号,也是数组下标 
		if(i>N) i=1;  //当i>N,i需要重新从第1个人开始
		if(a[i]==0)   //元素值为0的报数,元素值为非0的代表出局
		{
			k++;
			if(k==M)  //代表已经某个人已经报到M,需要出局 
			{
				a[i]=1;  //编号为i的这个人出局 赋值1 
				cnt++;   //出局的人数+1 
				cout<<i<<" ";  //输出出局的人的编号 
				k=0;   //k重新计,让下一个人重新从1开始报数   
			}
		}
	}
	return 0;
}

2、循环链表实现

#include<bits/stdc++.h>
using namespace std;
//用链表实现约瑟夫环问题 (循环链表)  
typedef struct node  //typedef用来重命名struct node这种数据类型,将其命名为Node 
{
	int data;
	struct node* next;
}Node;
 
void ysflb(int N,int M)  //总共有N个人,报到数字为M的人出局 
{
	//初始化循环链表
	Node *head = NULL,*p=NULL,*r=NULL;   //head为头指针,指向链表的第一个结点,一开始赋值为NULL,代表不指向任何结点 
	head = (Node*)malloc(sizeof(Node));  //让head指向一个实际的空间
	if(NULL==head)  //内存空间可能会申请失败,大多数情况不会申请失败 
	{
			cout<<"Memory Failed!";
			return;
	} 
	head->data=1;       //从1开始编号 
	head->next=NULL;    //一开始整个链表只有一个Node(结点),这个Node有两个域,分别是data和next
	                    //data从1开始,next指向NULL,总共需要N个结点,现在创建了一个,还需要N-1个 
    p=head;             //head要保持不能改变,才能够找到链表的起始位置,一开始p也指向第一个结点
	                    //p等一下会被使用,用它可以便于创建剩下的N-1个结点 
						 
	//尾插法创建链表,已经有一个1号结点了,还需要创建剩下的n-1个结点 
	for(int i=2;i<=N;i++)
	{
		r=(Node*)malloc(sizeof(Node)); 
		r->data=i;
		r->next=NULL;
		//插入结点 
		p->next=r;
		p=r;
		
	}
	//创建循环链表
	p->next=head;   //最后一个结点的next指向头结点
	p=head;         //为后续方便,将p指向头结点
	
	//约瑟夫环的模拟
	while(p->next!= p)  //如果p的next=p,说明目前只有一个元素 
	{
		for(int i=1;i<M;i++)  //报到数字为M的时候出局 
		{
			  r=p;   //保留出局的前一个结点 
			  p=p->next; //p指向的是要出局的这个结点,需要保留前一个结点
		}
		// 输出
		cout<<p->data<<" ";
		r->next=p->next;    //删除p的目的,此时p指向哪里?  :  
		p=p->next;  //更新p重新进行报数 
	} 
	cout<<p->data; 
}
 
int main()
{
	int m,n;
	cin>>m>>n; 
	ysflb(m,n);
	return 0;
} 

3、双向链表实现

#include <iostream>
using namespace  std;
//1.定义小朋友节点类
class Child
{
public:
	int childNo;         //当前小孩的编号
	Child* leftchild;    //记录小孩对象的左邻居
	Child* rightchild;   //记录小孩对象的右邻居
public:
	//构造函数
	Child(int num = 0)
	{
		childNo = num;
		leftchild = NULL;
		rightchild = NULL;
	}

};

//2.定义圆圈游戏类
class Circle
{
public:
	int scale;  //当前正在参与游戏的人数
	Child children[1000];

public:
	// 构造函数:初始化每个小孩对象节点的编号
	Circle(int num = 1000)
	{
		scale = num;


		for (int i = 0; i < num; i++)
		{
			children[i] = Child(i);
		}
	};

	//构建节点的循环链表函数:确立每个小孩的左右邻居
	void Build()
	{
		for (int i = 0; i < scale; i++)
		{
			if (i == 0)
			{
				children[i].rightchild = &children[1];
				children[i].leftchild = &children[scale - 1];
			}
			else if (i == (scale - 1))
			{
				children[i].rightchild = &children[0];
				children[i].leftchild = &children[scale - 2];
			}
			else
			{
				children[i].rightchild = &children[i + 1];
				children[i].leftchild = &children[i - 1];
			}
		}

	}
	//游戏运行函数:从当前节点顺时针循环开始数num个数
	void Run(int T)
	{
		int count = 1;
		Child* current = &children[0];
		while (scale > 1)
		{
			//quit the circle
			if (count == T)
			{
				// 当前数到T的小孩退出游戏,参与游戏人数减少1个
				scale = scale - 1;

				//把当前的这个小孩的左右邻居用链表连接起来
				current->leftchild->rightchild = current->rightchild;
				current->rightchild->leftchild = current->leftchild;
				current = current->rightchild;
				count = 1;
			}
			else  //count the next	            		
			{
				count = count + 1;
				current = current->rightchild;
			}

		}

		cout << "The last child id is " << current->childNo << endl;
	}
};

int main()
{

	Circle nodes(5); // 圆圈类内有100个节点对象(小孩)

	nodes.Build();

	nodes.Run(3);     // 开始循环运行数“7”的游戏,输出最后的获胜者

	system("pause");
	return 0;

}

4、数学方式实现

#include<stdio.h>
//核心函数 
int f ( int n, int m )       //n代表总人数,m代表第几个人 
   {  
       int r = 0;//即f(1)=0;  
       for(int i = 2; i <= n; i++)  
           r = (r + m) % i;//即f(i)=[f(i-1)+m]%n;  
       return r + 1; //即f(n)=1;  
    }
    
//主函数
int main()
{
	int n,m;
	int result;
	printf("请依次输入总人数和所报的数"); 
	scanf("%d %d",&n,&m);
	result=f(n,m);
	printf("最后留下来的人为:%d",result);
	return 0;
 }

三、N!及斐波那契数列递归和非递归实现,以及C语言下高精度实现

1、斐波那契递归和非递归实现

#include<bits/stdc++.h>
using namespace std;
/*long fib(int n)
{
	if(n==1) return 1;
	if(n==2) return 1;
	else
		return fib(n-1)+fib(n-2);
}*/
double fib(int n)
{
	if(n==1||n==2) return 1;
	double f1,f2,f3;
	f1=1,f2=1;
	for(int i=3;i<=n;i++)
	{
		f3=f1+f2;
		f1=f2;
		f2=f3;
	}
	return f3;
}
int main()
{
	
	clock_t start_time;
	clock_t finish_time;
	clock_t real_time;
	float current_time;
	float program_time;
	start_time=clock();
	current_time=(float)start_time / CLOCKS_PER_SEC;
	printf("Program start time: %f\n",current_time);
	for(int i=1;i<=1476;i++)
	{
	cout<<i<<' '<<fib(i)<<endl;
    }
    finish_time=clock();
	current_time=(float)finish_time / CLOCKS_PER_SEC;
	printf("Program finish time: %f\n",current_time);
	program_time=(float)(finish_time-start_time)/CLOCKS_PER_SEC;
	printf("Program complete time:%f\n",program_time);
	return 0;
}

2、N!递归和非递归实现

#include<bits/stdc++.h>
#include<time.h>
using namespace std;
/*int jiecheng(int n)
{
	int t=1;
	for(int i=1;i<=n;i++)
	{
		
		t*=i;
	}
	return t;
}*/
double fact(int n)
{
	if(n==0)
		return 1;
	else
		return n*fact(n-1); 
}
int main()
{
	clock_t start_time;
	clock_t finish_time;
	clock_t real_time;
	float current_time;
	float program_time;
	start_time=clock();
	int p;
	current_time=(float)start_time / CLOCKS_PER_SEC;
	printf("Program start time: %f\n",current_time);
	for(int i=1;i<=170;i++)
	{
		cout<<i<<' '<<fact(i)<<endl;
	}
	finish_time=clock();
	current_time=(float)finish_time / CLOCKS_PER_SEC;
	printf("Program finish time: %f\n",current_time);
	program_time=(float)(finish_time-start_time)/CLOCKS_PER_SEC;
	printf("Program complete time:%f\n",program_time);
	return 0;
}

3、高精度实现

#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
using namespace std;
int s[10005],i,j,n,k;
int main()
{
      while (~scanf("%d",&n))
      {
             memset(s,0,sizeof(s));
             s[0]=1; k=0;
             for (i=2;i<=n;i++)
             {
                   for (j=0;j<=k;j++) 
                      s[j]*=i; 
                   for (j=0;j<k;j++) 
                   {
                         s[j+1]+=s[j]/10000;
                         s[j]%=10000;
                   }
                   while (s[k]>10000)
                   {
                         s[k+1]=s[k]/10000;
                         s[k]%=10000;
                         k++;
                   }
             } 
             printf("%d",s[k]);
             i=k-1;;
             for (;i>=0;i--) 
             {
                   if (s[i]<1000) printf("0");
                   if (s[i]<100)  printf("0");
                   if (s[i]<10)   printf("0");
                   printf("%d",s[i]);
             }
             printf("\n");
      }
      return 0;   
}

四、四则运算表达式的实现

#include<bits/stdc++.h>
using namespace std;
#define true 1
#define false 0
#define OPSETSIZE 8
typedef int Status;
int getIndex(char theta);
char getPriority(char theta1,char theta2) 
{
	const char priority[][7]=
	{
		{'>','>','<','<','<','>','>'},
		{'>','>','<','<','<','>','>'},
		{'>','>','>','>','<','>','>'},
		{'>','>','>','>','<','>','>'},
		{'<','<','<','<','<','=','0'},
		{'>','>','>','>','0','>','>'},
		{'<','<','<','<','<','0','='},
	 };
	 int index1=getIndex(theta1);
	 int index2=getIndex(theta2);
	 return priority[index1][index2];
}
int getIndex(char theta)
{
	int index=0;
	switch(theta)
	{
		case'+':
			index=0;
			break;
		case'-':
			index=1;
			break;
		case'*':
			index=2;
			break;
		case'/':
			index=3;
			break;
		case'(':
			index=4;
			break;
		case')':
			index=5;
			break;
		case'#':
			index=6;
			break;
		default:break;
	}
	return index;
 } 
typedef struct StackChar
{
	
	char c;
	struct StackChar *next;
}SC;
typedef struct StackFloat
{
	
	float f;
	struct StackFloat *next;
}SF;
SC *push(SC *s,char c) 
{
	SC *p=(SC*)malloc(sizeof(SC));
	p->c=c;
	p->next=s;
	return p;
} 
SF *push(SF *s,float f)
{
	SF *p=(SF*)malloc(sizeof(SF));
	p->f=f;
	p->next=s;
	return p;
} 
SC *pop(SC *s)
{ 
	SC *q=s;
	s=s->next;
	free(q);
	return s; 
}
SF *pop(SF *s)
{
	SF *q=s;
	s=s->next;
	free(q);
	return s;
}
float Operate(float a,unsigned char theta,float b)
{
	switch(theta)
	{
		case'+': return a+b;
		case'-': return a-b;
		case'*': return a*b;
		case'/': return a/b;
		default :return 0;
	}
}
char OPSET[OPSETSIZE]={'+','-','*','/','(',')','#'};
Status In(char Test,char *TestOp)
{
	int Find=false;
	for(int i=0;i<OPSETSIZE;i++)
	{
		if(Test==TestOp[i])
		{
			Find=true;
		}
		
	 } 
	return Find;
}
Status ReturnOpOrd(char op,char *TestOp)
{
	for(int i=0;i<OPSETSIZE;i++)
	{
		if(op==TestOp[i])
		return i;
	}
}
float EvaluateExpression(char *Myexpression)
{
	SC *OPTR=NULL;
	SF *OPND=NULL;
	char TempData[20];
	float Data,a,b;
	char theta,*c,Dr[]={'#','\0'};
	OPTR=push(OPTR,'#');
	c=strcat(Myexpression,Dr);
	strcpy(TempData,"\0");
	while(*c!='#'||OPTR->c!='#')
	{
		if(!In(*c,OPSET))
		{
			Dr[0]=*c;
			strcat(TempData,Dr);
			c++;
			if(In(*c,OPSET))
			{
				Data=atof(TempData);
				OPND=push(OPND,Data);
				strcpy(TempData,"\0");
			}
		}
		else
		{
			switch(getPriority(OPTR->c,*c))
			{
				case'<':
					OPTR=push(OPTR,*c);
					c++;
					break;
				case'=':
					OPTR=pop(OPTR);
					c++;
					break;
				case'>':
					theta=OPTR->c;OPTR=pop(OPTR);
					b=OPND->f;OPND=pop(OPND);
					a=OPND->f;OPND=pop(OPND);
					OPND=push(OPND,Operate(a,theta,b));
					break;
			}
		}
		
	}
	return OPND->f;
}
int main()
{
	char s[128];
	cout<<"请输入表达式:";
	gets(s);
	cout<<"该表达式的值为:";
	printf("%s\b=%g\n",s,EvaluateExpression(s));
	return 0; 
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
问题描述:杂志订阅管理系统能够实现客户对杂志的订阅和管理员对杂志的管理,需要创建三个不同的角色:管理员、订户、游客。 基本功能: (1)订户信息的注册 订户需要在订阅杂志之前进行个人信息的注册,要求至少需要用户名和密码。 (2)信息的添加 管理员能够对订户信息进行管理(此处可以灵活处理,例如可以添加或者删除或者修改)。也能够实现对杂志信息的查询,添加,修改,删除等操作,杂志信息至少要设定杂志的编号、杂志的名称、杂志的数量、杂志的价格等。 (3)查询模块 三个角色均能对杂志信息进行查询,要求实现对全部杂志进行浏览和按杂志的某个字段进行信息的查询。要求在全部浏览的时候能够实现按照某个字段的顺序进行浏览,即按某个字段对所有杂志信息进行排序。(可以采用不同的排序算法实现此功能)。 (4)订阅模块 订户能够对杂志进行浏览查询,并实现订阅,注意数量的同步问题,一旦订阅之后不可改退,杂志数量要相应减少。订阅信息要包括订户的用户名,杂志编号,杂志数量,总金额。订户可以查看自己的所有订阅信息。 (5)管理模块 管理员可以查询库存为零的杂志,可以对某个库存为零的杂志信息进行删除,或者修改杂志数量。 扩展功能: (1)文件保存 要求对订户信息、杂志信息、订阅信息进行合理保存,实现对文件的读取。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值