1 表达式求值(stack)
/*
author: denny wqf363@hotmail.com
compile: g++ exp.c -o exp
input: 3+2
output: The value of the expression is: 5.000000
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
struct datastack
{
double v[100];
int top;
}da;
struct opratorstack
{
char v[100];
int top;
}op;
double calculate(double a,char ch,double b);
char proceed(char c1,char c2);
double str2double(char *s);
double expression(char str[]);
/*1*/
double calculate(double a,char ch,double b)
{
double result;
int i;
switch(ch)
{
case'+': result=a+b; break;
case'-': result=a-b; break;
case'/': if(b==0)
{
printf("fen mu cann't 0!/n");
break;
}
else result=1.0*a/b; break;
case'*': result=a*b; break;
case'^': result=pow(a,b);
}
return result;
}
/*2*/
char proceed(char c1,char c2)
{
char ch;
switch(c1)
{
case'+':case'-':
switch(c2)
{
case'+':case'-':case')':case'#': ch='>'; break;
default: ch='<';
}break;
case'*':case'/':case'^':
switch(c2)
{
case'(':case'^': ch='<';break;
default: ch='>';
}break;
case'(':
switch(c2)
{
case')': ch='='; break;
default: ch='<';
}break;
case'#':
switch(c2)
{
case'#': ch='='; break;
default: ch='<';
}break;
}
return ch;
}
/*3*/
double str2double(char *s)
{
double t1,t2;
char *p=s+strlen(s)-1;
t1=t2=0;
while(*s)
{
if(*s=='.') break;
t1=t1*10+*s-48;
s++;
}
if(*s=='.')
{
while(*p!='.'&&s!=NULL)
{
t2=t2/10.0+*p-48;
p--;
}
}
t2=t2/10.0;
return t1+t2;
}
/*4*/
double expression(char str[])
{
int i=0,j=0;
double a,b,c;
char ch,ch1,s[100];
da.top=op.top=-1;
/* da.v[++da.top]=0;*/
op.top++;
op.v[op.top]='#';
strcat(str,"#");
while(!(str[i]=='#'&&op.v[op.top]=='#'))
{
if( (i==0 || str[i-1]=='(') && str[i]=='-' ) da.v[++da.top]=0;
if(str[i]>='0'&&str[i]<='9'||str[i]=='.')
{
strcpy(s, "");
j=0;
while(str[i]>='0'&&str[i]<='9'||str[i]=='.')
s[j++]=str[i++];
s[j]='/0';
da.top++;
da.v[da.top]=str2double(s);
}
ch1=proceed(op.v[op.top],str[i]);
switch(ch1)
{
case'<': op.v[++op.top]=str[i++]; break;
case'=': op.top--; i++; break;
case'>': {
ch=op.v[op.top--];
b=da.v[da.top--];
a=da.v[da.top--];
c=calculate(a,ch,b);
da.v[++da.top]=c;
}
}
}
return da.v[da.top];
}
int main()
{
char str[200];
int k,s;
puts("Please input an expression:/n");
gets(str);
printf("The value of the expression is: %f/n",expression(str));
printf("Up to date! Written by denny. 11.4th,2002./n");
return 0;
}
2 哈夫曼树(haffman tree)
/*
author: denny wqf363@hotmail.com
compile: gcc huffman.c
input:
output:
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX 50
/*最大叶子数 */
#define MAXM 2*MAX-1
/*最大结点数*/
const float MAXFLOAT=1e38;
typedef struct
{ float w;
int parent,lch,rch;
}nodetype;
typedef nodetype huftree[MAXM];
typedef struct
{ char ch;
int bits[MAXM],end;
}codetype;
typedef codetype hufcode[MAXM];
huftree ht;
hufcode hc;
int m,n;
/*总结点数m,叶子数n*/
select(int t,int *s1,int *s2)
/*在ht[i],i={0,1,……,t}中选取未选过的权重最小的*s1,*s2*/
{ int i;
float w1,w2;
w1=w2=MAXFLOAT;
*s1=*s2=-1;
for(i=0;i<=t;i++)
{ if(ht[i].parent==-1)
{
if(ht[i].w<w1)
{ w2=w1;
*s2=*s1;
w1=ht[i].w;
*s1=i;
}
else if(ht[i].w<w2)
{ w2=ht[i].w;
*s2=i;
}
}
}
}
creat()
{ int i,child,parent,s1,s2;
char ch;
float weight;
printf("please input NO. of code:");
scanf("%d",&n);
m=2*n-1;
for(i=0;i<m;i++)
ht[i].parent=ht[i].lch=ht[i].rch=-1;
for(i=0;i<n;i++)
{ printf("input ch,weigh:");
fflush(stdin);
scanf("%c %f",&ch,&weight);
ht[i].w=weight;
hc[i].ch=ch;
}
for(i=n;i<m;i++)
{ select(i-1,&s1,&s2);
ht[i].w=ht[s1].w+ht[s2].w;
ht[s1].parent=ht[s2].parent=i;
ht[i].lch=s1;
ht[i].rch=s2;
}
for(i=0;i<n;i++)
{ child=i;
parent=ht[i].parent;
hc[i].end=0;
while(parent!=-1)
{ if(child==ht[parent].lch)
hc[i].bits[hc[i].end]=0;
else
hc[i].bits[hc[i].end]=1;
hc[i].end++;
child=parent;
parent=ht[child].parent;
}
}
}
printcode()
{ int i,j;
for(i=0;i<n;i++)
{ printf("%c:",hc[i].ch);
for(j=hc[i].end-1;j>=0;j--)
printf("%d",hc[i].bits[j]);
printf("/n");
}
}
showtree(int p,int col)
{ int i;
for(i=1;i<=col;i++)
printf(" ");
printf("(%6.2f):",ht[p].w);
if(p<n)
printf("%c",hc[p].ch);
printf("/n");
if(p>=n)
{
showtree(ht[p].lch,8+col);
showtree(ht[p].rch,8+col);
}
}
main()
{ char str[250];
creat();
gets(str);
printcode();
showtree(m-1,1);
}