冒泡排序与堆排序的比较
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define max 100
typedef int KeyType;
typedef struct
{
KeyType Key;
}LineList;
int count1 = 0,count2 = 0,count3 = 0,count4 = 0;
void BubbleSort(LineList r[],int n)
{
int i,j,exchange;
LineList temp;
for (i = 1; i < n; i++)
{
exchange = 0;
for(j = 1; j <= n-i; j++)
{
count1++;
if(r[j].Key > r[j+1].Key)
{
temp = r[j];
r[j] = r[j+1];
r[j+1] = temp;
exchange = 1;
count2++;
}
if(exchange = 0)
{
return;
}
}
}
printf("此次冒泡排序比较次数为:%d\n",count1);
printf("此次冒泡排序移动次数为:%d\n",count2);
}
void swap(int *num1,int *num2)
{
int tmp = *num1;
*num1 = *num2;
*num2 = tmp;
count4++;
}
void down(LineList r[],int i,int n)
{
int child,tmp;
for(tmp = r[i].Key; 2*i+1<n; i = child)
{
child = 2*i+1;
if(child != n-1 && r[child+1].Key > r[child].Key)
{
child++;
count3++;
}
if(r[i].Key < r[child].Key)
{
count3++;
swap(&r[i].Key,&r[child].Key);
}
else break;
}
}
void HeapSort(LineList r[],int n)
{
int i;
for(i = (n/2)-1; i>=0; i--)
{
down(r,i,n);
}
for(i = n-1; i > 0; i--)
{
swap(&r[0].Key,&r[i].Key);
down(r,0,i);
}
printf("此次堆排序比较次数为:%d\n",count3);
printf("此次堆排序移动次数为:%d\n",count4);
}
int main()
{
int i,n;
LineList r[max];
srand((unsigned int)time(NULL));
for( i = 1; i <= max; i++)
{
r[i].Key = rand();
}
printf("本次随机生成的%d个数字为:\n",max);
for(i = 1; i <= max; i++)
{
printf("%d ",r[i]);
}
printf("\n\n");
BubbleSort(r,max);
printf("\n");
HeapSort(r,max);
}
基于栈的计算器
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<windows.h>
typedef struct ListNode
{
char name[10];
float num;
struct ListNode *next;
} LN;
typedef LN* LL;
typedef struct StackNode
{
int flag;
float data;
char sign;
struct StackNode * next;
}SN, *LSP;
typedef struct LinkStack
{
LSP top;
int count;
}LS;
LL N;
char z[100];
int ISEmpty(LS *S)
{
if(S->count==0&&S->top->next==NULL)
return 1;
return 0;
}
int PushNum(LS *S,float a)
{
LSP s=(LSP)malloc(sizeof(SN));
s->data=a;
s->flag=1;
s->next=S->top;
S->top=s;
S->count++;
return 1;
}
int PushCh(LS *S,char a)
{
LSP s=(LSP)malloc(sizeof(SN));
s->sign=a;
s->flag=2;
s->next=S->top;
S->top=s;
S->count++;
return 1;
}
int PopNum (LS *S,float *a)
{
LSP p;
if(ISEmpty(S))
return 0;
*a=S->top->data;
p=S->top;
S->top=S->top->next;
free(p);
S->count--;
return 1;
}
int PopCh (LS *S,char *a)
{
LSP p;
if(ISEmpty(S))
return 0;
*a=S->top->sign;
p=S->top;
S->top=S->top->next;
free(p);
S->count--;
return 1;
}
void CreateList(LL *L,int n)
{
LL p,l;
int i;
*L=(LL) malloc (sizeof(LN));
l=*L;
printf("请输入%d个变量及其值:\n",n);
for(i=0;i<n;i++)
{
p=(LN *) malloc (sizeof(LN));
printf("%d. ",i+1);
scanf("%s%f",p->name,&p->num);
l->next=p;
l=p;
}
l->next=NULL;
}
void PrintList(LL l)
{
int flag = 0;
while(l->next)
{
l=l->next;
if(flag)
printf(",");
printf("%s=%.2f",l->name,l->num);
flag=1;
}
printf("\n");
}
int Find(LL l,char ch[10],float *a)
{
int flag=1;
while(l->next)
{
l=l->next;
if(strcmp(ch,l->name)==0)
{
flag=0;
break;
}
}
if(flag)
return 0;
*a = l->num;
return 1;
}
void MidtoLast(LS *last)
{
LS l,p;
p.count=0;
l.count=0;
char a[1000],b,d,ch[10];
int flag=1;
int i=0,j=0;
int k;
float c;
printf("请输入表达式:\n");
scanf("%s",a);
d=a[0];
strcpy(z,a);
for(i=0;flag==1;i++,d=a[i])
{
if(d=='('||d=='*'||d=='/')
{
PushCh(&p,d);
}
else if(d=='+'||d=='-')
{
while(p.count>0&&p.top->sign!='(')
{
PopCh(&p,&b);
PushCh(&l,b);
}
PushCh(&p,d);
}
else if(d==')')
{
while(p.top->sign!='(')
{
PopCh(&p,&b);
PushCh(&l,b);
}
PopCh(&p,&b);
}
else if(d=='=')
{
while(p.count>0)
{
PopCh(&p,&b);
PushCh(&l,b);
}
flag=0;
}
else if(d>='0'&&d<='9')
{
for(j=0;a[i+j]!='='&&a[i+j]!='+'&&a[i+j]!='-'&&a[i+j]!='*'&&a[i+j]!=')'&&a[i+j]!='/'&&a[i+j]!='(';j++)
{
ch[j]=a[j+i];
ch[j+1]='\0';
}
k=j;
c=0;
i+=k-1;
for(j=0;j<k;j++)
{
c+=(float)(ch[j]-'0')*pow(10,(k-j-1));
}
PushNum(&l,c);
}
else
{
for(j=0;a[i+j]!='='&&a[i+j]!='+'&&a[i+j]!='-'&&a[i+j]!='*'&&a[i+j]!=')'&&a[i+j]!='/'&&a[i+j]!='(';j++)
{
ch[j]=a[j+i];
ch[j+1]='\0';
}
i+=j-1;
Find(N,ch,&c);
PushNum(&l,c);
}
}
while(l.count!=0){
if(l.top->flag==1){
PopNum(&l,&c);
PushNum(last,c);
}
else if(l.top->flag==2)
{
PopCh(&l,&d);
PushCh(last,d);
}
}
}
float LasttoMid(LS last)
{
float a,b,c;
char d;
LS l;
while(last.count!=0)
{
if(last.top->flag==1)
{
PopNum(&last,&c);
PushNum(&l,c);
}
if(last.top->flag==2)
{
PopNum(&l,&b);
PopNum(&l,&a);
PopCh(&last,&d);
switch(d){
case '+':
c=a+b;
break;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;
case '/':
c=a/b;
break;
}
PushNum(&l,c);
}
}
return l.top->data;
}
int main()
{
int n;
LS last;
last.count=0;
printf("请输入变量个数:");
scanf("%d",&n);
CreateList(&N,n);
system("cls");
PrintList(N);
MidtoLast(&last);
system("cls");
PrintList(N);
printf("%s%.2f",z,LasttoMid(last));
return 0;
}