博文

词法分析器(C语言版)(2007-06-06 22:59:00)

摘要:这是我用C语言编写的能识别C语言的关键字之类的一个词法分析器,需要有一个程序文件,然后在程序的第一行把#define Filaname “文件名”的那个文件名换成要识别的文件的准确路径,之后就运行就可以了,这个程序是在TurboC 2.0和本网站提供的TC for windows 3.1中调试成功的,如有问题请及时联系我,谢谢!(自认为不足之处就是没有调用函数,一个main弄到底,还有就是没有注释,看不懂的话千万不要硬撑啊!) #define Filename "1.txt"
#include<string.h>
#include<stdio.h> main()
{char keyword[32][9]={"auto","break","case","char","const","continue","default","double","do","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
 char sing[40][5]={"->","++","--","<<",">>","<=",">=","==","!=","&&","||","?:","+=","-=","*=","/=","%=",">>=","<<=","&=","|=","^=","=",".","!","~","-","*","&'","/","%","+","<",">","^","|'","?",":",",",";"};
 char *s;
 char temp[10][9],ci[9];
 char *go,*now;
 int i,j,n,k,out,tag1=0,tag2=0,tag3=0;
 FILE ......

阅读全文(5982) | 评论:1

用C语言编写的模拟进程控制的程序(2006-10-05 11:47:00)

摘要:本程序是在Turbo C 2.0环境下编写的,预计在其他C程序编译系统中也没问题。 #define null 0
#define M "         Menu\n\n1.Creat a process\n2.Destroy a process\n3.Blockaprocess\n4.Wakeup a process\n5.Suspend a process\n6.Active a process\n7.Observe the processes\n\nPlease select the item you want to realize:" struct PCB
{char name;
 int ko;
 int cha;
} struct PCB pcb[10];
int i=0; void creat()
{char a;
 int b;
 printf("Please input the name of the process:");
 scanf("%c",&a);
 printf("\nPlease input the ko of the process:");
 scanf("%d",&b);
 i++;
 pcb[i].name=a;
 pcb[i].ko=b;
 pcb[i].cha=1;
 printf(M);
} void destroy()
{char a;
 int j;
 printf("Select a process you want to destroy:");
 scanf("%c",&a);
 printf("\n");
 for(j=1;j<=10;j++)
 {if(pcb[j].name==a)
  {pcb[j].name=0;
 &nbs......

阅读全文(6100) | 评论:10

FIFO算法模拟(2006-07-04 12:18:00)

摘要:#define M 4 #define N 10 #include<stdio.h>   typedef struct Queue {char *head;  char *tail:  int=length; }Que;   Que create() {Que Q;  Q.head=(Queue)malloc(sizeof(Que));  Q.tail=Q.head;  Q.length=0;  return(Q); }   Que push(Que Q,char c) {*++Q.tail=c;  Q.length++;  if(Q.length>M)printf("Overflow!");  else return(Q); }   char pop(Que Q) {char c;  if(Q.length==0)exit(NULL);  Q.length++; else return(c); }   void visit(Que Q) {int count;  char e;  for(count=0;count<M;count++)  {e=pop(Q);   printf("%c",e);   push(Q,pop(Q));  } }   main() {Que Q,p;  char c,e;  c=getchar();  while(c!=NULL)  {for(p=Q.head;p<=Q.tail;p++)   {if(*p==c)visit(Q);    else    {e=pop(Q);     push(Q,c);    }   }  } }......

阅读全文(5173) | 评论:2

用链式存储实现的选择排序(2006-07-02 08:06:00)

摘要:#include<stdio.h>
#define N 10
typedef struct Seq
{int data;
 struct Seq *next;
}Seq,*Sq; Sq create()
{Sq L,p;
 int i;
 L=(Sq)malloc(sizeof(Seq));
 L->next=NULL;
 for(i=N;i>0;i--)
 {p=(Sq)malloc(sizeof(Seq));
  scanf("%d",&p->data);
  p->next=L->next;
  L->next=p;
 }
 return(L);
} int minkey(Sq L)
{Sq p,q;
 int n;
 p=L->next;
 n=p->data;
 for(;p!=NULL;p=p->next)
 {if(p->data<n)
  n=p->data;
 }
 return(n);
} void selectsort(Sq L)
{Sq p,q,s;
 for(p=L;p->next!=NULL;p=p->next)
 {for(q=p->next;q!=NULL;q=q->next)
  {s=q->next;
   if(s->data==minkey(p))
   {q->next=s->next;
    s->next=p->next;
    p->next=s;
   }
  }
 }

阅读全文(3011) | 评论:0

顺序链表的简单选择排序(2006-06-25 07:55:00)

摘要:#include<stdio.h>
#define N 10
typedef struct Seq
{int data;
 struct Seq *next;
}Seq,*Sq; Sq create()
{Sq L,p;
 int i;
 L=(Sq)malloc(sizeof(Seq));
 L->next=NULL;
 for(i=N;i>0;i--)
 {p=(Sq)malloc(sizeof(Seq));
  scanf("%d",&p->data);
  p->next=L->next;
  L->next=p;
 }
 return(L);
} int minkey(Sq L)
{Sq p,q;
 int n;
 p=L->next;
 n=p->data;
 for(;p!=NULL;p=p->next)
 {if(p->data<n)
  n=p->data;
 }
 return(n);
} void selectsort(Sq L)
{Sq p,q,s;
 for(p=L;p->next!=NULL;p=p->next)
 {for(q=p->next;q!=NULL;q=q->next)
  {s=q->next;
   if(s->data==minkey(p))
   {q->next=s->next;
    s->next=p->next;
    p->next=s;
   }
  }
 }

阅读全文(3418) | 评论:0

LRU算法实现(2006-05-31 14:17:00)

摘要:这是模拟计算机中高速缓存分组读取的程序,有意者请帮忙改下程序,有错之处,请多指教。 #define M 4 #define N 20 main() {char row[M],s[N],t;  int i,j;  printf("You are allowed to input a maximum of N objects.");  for(i=0;i<N;i++)  {for(j=0;j<M;j++)   {if(s[i]!='\0'&&s[i]==row[j])    {for(;j>0;j--)      {t=row[i];       row[j]=row[j-1];       row[j-1]=t;      }    }    else if(s[i]!='\0'&&s[i]!=row[j])    {for(j=M-1;j>=1;j--)     row[j]=row[j-1];     row[0]=s[i];    }   }  } }  ......

阅读全文(5506) | 评论:1

用栈做的数制转换(十进制转换为八进制)(2006-05-12 16:04:00)

摘要:这是我用栈实现的十进制数转换为八进制数,其他进制转换同理,由于是在Turbo C++ 3.0上编写的,所以在Turbo C 2.0 或其他版本的C编译工具上可能不能通过编译而成为可执行文件。 #define maxlength 20
#include<malloc.h>
typedef struct stacknode
{int *base;
 int *top;
 int length;
}stack; stack *initstack()
{stack *S;
 (*S).base=(stack*)malloc(maxlength*sizeof(stack));
 if(!(*S).base)exit(0);
 (*S).top=(*S).base;
 return(S);
} stack *push(stack *S,int r)
{*(*S).top++=r;
 if((*S).top-(*S).base>=maxlength)
 {(*S).top=(int*)realloc((*S).base,sizeof(int));
  *(*S).top++=r;
 }
 return(S);
} int pop(stack *S)
{int r;
 if((*S).top==(*S).base)exit(0);
 r=*(--(*S).top);
 return(r);
} void main()
{stack *S;
 int n,m;
 printf("Please input the number you want to convert:");
 scanf("%d",&n);
 printf("\n");
 S=initstack();
 while(n!=0)
 {push(S,n%8);
  n=n/8;......

阅读全文(6703) | 评论:0

Hanoi塔问题(2006-05-04 17:18:00)

摘要:解决汉诺塔问题。[printf("(++c).move  disk  n  from  x  to  z\n",);即move(x,n,z)] void  hanoi(int  n,int  x,int  y,int  z) {int  c=0;   if(n==1)printf("%d".move  disk  %d  from  %d  to  %d\n",++c,n,x,z);   else   {hanoi(n-1,x,z,y);     printf("%d.move  disk  %d  from  %d  to  %d\n",++c,n,x,z);                          hanoi(n-1,y,x,z);   } }   main() {int  n,x,y,z;   scanf(%d  %d  %d  %d",&n,&x,&y,&z);                hanoi(n,x,y,z); }   ......

阅读全文(3031) | 评论:0

Fabs(2006-05-04 17:05:00)

摘要:这是一个关于fabs的程序,输入t,输出大于t的最小fabs值。 main() {int f1=0,f2=1,fn,t;   fn=f1+f2;   scanf("%d",&t);   while(fn<=t)   {f1=f2;     f2=fn;     fn=f1+f2;   }   printf("The  number  you  want  is:%d\n",fn); }......

阅读全文(4100) | 评论:0

统计一个字符串在另一个字符串中出现次数(2006-05-04 16:45:00)

摘要:这是一个在一个字符串中统计出另一个子串出现的次数,并返回次数n,保存在文件out.dat中。   #include<stdio.h>   Void  ReadWrite(char  *str1,char  *str2) {FILE  *fp;   fp=fopen("C:\\out.dat","w+");   if(fp)   {if(*str1)     {fprintf(fp,"%s",*str1);       str1++;     }     if(*str2)     {fprintf(fp,%s",*str2);        str2++;    }   }   else  printf("Can't  find or  something  is  wrong  to  the  file!"); }   int  findstr(char  *str1,char  *str2) {char  *p,*r;   int  n=0;   while(*str1)   {p=str1;     r=str2;     while(*r)     {if(*r==*p)       {r++;         p++;       }     &n......

阅读全文(4347) | 评论:0