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

📄 linear.cpp

📁 数据结构测试程序
💻 CPP
字号:
#include "stdio.h"
#include "stdlib.h"
#include "linear1.h"
#include "stack.h"
#include "queue.h"
#include "tree.h"
#include "binarytree.h"
#include "matrix.h"
#include "Huffman.h"
#include "Graph.h"
#include "doublestack.h"

int main()
{
	void testlinear();
	void testbinary();
	void testHuf();
	void testGraph();
	void testds();//double stack
	void testmatrix();
	void welcome();	
	welcome();
	printf("which function do you want to test...\n");
	printf("testHuf-----1\n");
	printf("testlinear--2\n");
	printf("testbinary--3\n");
	printf("testGraph---4\n");
	printf("testds------5      (double stack)\n");
	printf("testmatrix--6\n");
	int test=1;
	scanf("%d",&test);
	switch (test)
	{
	case 1:
		printf("you choose the testHuf------1\n");
		testHuf();
		break;
	case 2:
		printf("you choose the testlinear---2\n");
		testlinear();
		break;
	case 3:
		printf("you choose the testbinary---3\n");
		testbinary();
		break;
	case 4:
		printf("you choose the testGraph---4\n");
		testGraph();
		break;
	case 5:
		printf("you choose the testds------5\n");
		testds();
		break;
	case 6:
		printf("you choose the testmatrix--6\n");
		testmatrix();
		break;
	}
	return 1;

}

void welcome()
{
	printf("* * * * * * * * * * * * * * * * * * * * *\n");
	printf("*                                       *\n");
	printf("*  This program was writeln by HanRuihua  *\n");
	printf("*                                       *\n");
	printf("* * * * * * * * * * * * * * * * * * * * *\n");
	printf("\n\n");
}

void testlinear()
{
	int i;
	int a;
	welcome();
	linear line;
	line.initiate();
	for (i=0;i<10;i++)
	{
		scanf("%d",&a);
		line.add(a);
	}
	line.insert(5,5);
	for (i=0;i<=line.length();i++)
		printf("%d ",line.get(i));
	return ;
}

void testHuf()
{
	Huffman hp;
	hp.getweight();

	printf("\n");

	hp.code();
	hp.outweight();
	hp.outHuf();
	hp.encoder();
}

void testbinary()
{
}

void testGraph()
{
	int n;
	int e;
	Graph hp;
	printf("initate Graph now...\nintput your n(number of node)\n");
	scanf("%d",&n);
	printf("input your e(number of edge)\n");
	scanf("%d",&e);
	printf("now input your node\n");
	int i;
	for (i=0;i<n;i++)
	{
		char node;
		scanf("%c",&node);
		hp.ins_vertex(node);
	}
	printf("now input your edge\n") ;
	for (i=0;i<e;i++)
	{
		char start,end;//
		scanf("%c%c",&start,&end);
		hp.ins_arc(start,end);
	}
	printf("now ...  this is you graph\n");
	hp.outGraph();
}

void testds()
{
	doublestack stack;
	int i;
	elemt k;
	printf("==============================================================\n");
/*================================================================*/
	printf("Initiating...\n");
	stack.initiate();
	printf("Stack0's size = %d  ",stack.size(0));
	printf("Stack1's size = %d  ",stack.size(1));
	printf("All size = %d\n",stack.size(2));
	putchar('\n');
/*================================================================*/
	printf("Pushing numbers to stack 0...\n");
	for(i=0;i<10;i++)
	{
		if(stack.push(i+1,0))
		{
			printf("The %d number of 0 is %d\n",i+1,stack.gettop(0));
		}
	}
	printf("Stack0's size = %d  ",stack.size(0));
	printf("Stack1's size = %d  ",stack.size(1));
	printf("All's size = %d\n",stack.size(2));
	putchar('\n');
/*================================================================*/
	printf("Cleaning stack0...\n");
	stack.clear(0);
    printf("Stack0's size = %d  ",stack.size(0));
	printf("Stack1's size = %d  ",stack.size(1));
	printf("All's size = %d\n",stack.size(2));
	putchar('\n');
/*================================================================*/
	printf("Pushing numbers to stack 1...\n");
	for(i=0;i<10;i++)
	{
		if(stack.push(i+1,1))
		{
			printf("The %d number of 1 is %d\n",i+1,stack.gettop(1));
		}

	}
	printf("Stack0's size = %d  ",stack.size(0));
	printf("Stack1's size = %d  ",stack.size(1));
	printf("All's size = %d\n",stack.size(2));
	putchar('\n');
/*===================================================================*/
	printf("Poping numbers from stack0...\n");
	for(i=0;i<10;i++)
	{
		if(stack.pop(&k,0))
		{
			printf("%d poped.\n",k);
		}
	}
    printf("Stack0's size = %d  ",stack.size(0));
	printf("Stack1's size = %d  ",stack.size(1));
	printf("All size = %d\n",stack.size(2));
	putchar('\n');
/*==================================================================*/
	printf("Poping numbers from stack1...\n");
	for(i=0;i<10;i++)
	{
		if(stack.pop(&k,1))
		{
			printf("%d poped.\n",k);
		}
	}
    printf("Stack0's size = %d  ",stack.size(0));
	printf("Stack1's size = %d  ",stack.size(1));
	printf("All size = %d\n",stack.size(2));
	putchar('\n');
/*=================================================================*/
	putchar('\n');
}


////////////////matrix

//#define sparmattp matrix

int mult(matrix* a,matrix* b,matrix* c)
{
	if(a->nu!=b->mu)
	{
		printf("These two matrixs can not multiply!\n");
		return 0;
	}
	else if(a->tu*b->tu)
	{
		int i,j,k,crow,brow,ccol,p=0;
		element temp[maxmn];
		c->mu=a->mu,c->nu=b->nu,c->tu=0;
		c->rpos[0]=0;k=0;
		while(p<a->tu)
		{
			crow=a->data[p].i-1;
			for(i=0;i<c->nu;i++) temp[i]=0;
			while(p<a->tu && a->data[p].i-1==crow)
			{
				brow=a->data[p].j-1;
				for(j=b->rpos[brow];j<b->rpos[brow+1];j++)
				{
					ccol=b->data[j].j-1;
					temp[ccol]+=a->data[p].v*b->data[j].v;
				}
				p++;
			}
			k++;
			c->rpos[k]=c->rpos[k-1];
			for(ccol=0;ccol<c->nu;ccol++)
			{
				if(temp[ccol])
				{
					c->data[c->tu].i=crow+1;
					c->data[c->tu].j=ccol+1;
					c->data[c->tu].v=temp[ccol];
					c->tu++;
					c->rpos[k]++;
				}
			}
		}
		return 1;
	}
	else
		return 0;
}

int load_mat(matrix* a,matrix* b)
{
		int i,j,k,l;
		element x;
		scanf("%d%d",&k,&l);
		a->mu=k,a->nu=l,a->tu=0;
		a->rpos[0]=0;
		for(i=0;i<k;i++)
		{
			a->rpos[i+1]=a->rpos[i];
			for(j=0;j<l;j++)
			{
			scanf("%d",&x);
				 if(x)
				{
					a->data[a->tu].i=i+1;
					a->data[a->tu].j=j+1;
					a->data[a->tu].v=x;
					a->tu++;
					a->rpos[i+1]++;
				}
			}
		}	
		scanf("%d%d",&k,&l);
		b->mu=k,b->nu=l,b->tu=0;
		b->rpos[0]=0;
		for(i=0;i<k;i++)
		{
			b->rpos[i+1]=b->rpos[i];
			for(j=0;j<l;j++)
			{
			scanf("%d",&x);
			if(x)
				{
					b->data[b->tu].i=i+1;
					b->data[b->tu].j=j+1;
					b->data[b->tu].v=x;
					b->tu++;
					b->rpos[i+1]++;
				}
			}
		}
		return 1;
}

void help_mass()
{
//	printf("\nL-Load datas from \"data.txt\".\n");
	printf("I-Input datas yourself.\n");
	printf("M-Multiply matrix A and matrix B.\n");
	printf("S-Show marix A B and C\n");
	printf("H-Help\n");
	printf("E-Exit.\n");
}

void testmatrix()
{
	matrix a,b,c;
	char ch;

	a.initate();
	b.initate();
	c.initate();

	help_mass();
	printf("\nInput your choice:");
	ch=getchar();

	while(ch!='e' && ch!='E')
	{
		switch(ch)
		{
		case 'l':
		case 'L':
			if(!load_mat(&a,&b))
			{
				printf("\nLoad false!\n");
				a.initate();
				b.initate();
			}
			else
			{
				printf("\nLoaded.\n");
			}
			break;
		case 'i':
		case 'I':
			printf("\nInput matrix A:\n");
			a.input_mat();
			printf("\nInput matrix B:\n");
			b.input_mat();
			c.initate();
			break;
		case 'm':
		case 'M':
			if(mult(&a,&b,&c))
			{
				printf("\nMultiplied.\n");
			}
			break;
		case 's':
		case 'S':
			printf("A=\n");
			a.show_mat();
			printf("B=\n");
			b.show_mat();
			printf("C=\n");
			c.show_mat();
			break;
		case 'h':
		case 'H':
			help_mass();
			break;
		default:
			printf("\nError input.");
			break;
		}
		printf("\nInput your choice:");
		getchar();
		ch=getchar();
	}
	printf("\nThank you for useing!\n");
}

/////////////matrix



⌨️ 快捷键说明

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