博文

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

摘要:#include<stdio.h>#define N 10typedef 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;   }  } }} void main(){Sq L,p; L=create(); selectsort(L); for(p=L->next;p!=NULL;p=p->next) printf("%d ",p->data); getch();} &nbs......

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

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

摘要:#include<stdio.h>#define N 10typedef 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;   }  } }} void main(){Sq L,p; L=create(); selectsort(L); for(p=L->next;p!=NULL;p=p->next) printf("%d ",p->data); getch();} &nbs......

阅读全文(3550) | 评论: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];    }   }  } }  ......

阅读全文(5616) | 评论: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; } while(S!=NULL) {m=pop(S);  printf("%d",m); }}  ......

阅读全文(6840) | 评论: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); }   ......

阅读全文(3150) | 评论: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); }......

阅读全文(4214) | 评论: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......

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

C 语言概述(2006-04-07 13:40:00)

摘要:        C语言是在70年代初问世的。一九七八年由美国电话电报公司(AT&T)贝尔实验室正式发表了C语言。同时由B.W.Kernighan和D.M.Ritchit合著了著名的“THE C PROGRAMMING LANGUAGE”一书。通常简称为《K&R》,也有人称之为《K&R》标准。但是,在《K&R》中并没有定义一个完整的标准C语言,后来由美国国家标准学会在此基础上制定了一个C 语言标准,于一九八三年发表。通常称之为ANSI C。         早期的C语言主要是用于UNIX系统。由于C语言的强大功能和各方面的优点逐渐为人们认识,到了八十年代,C开始进入其它操作系统,并很快在各类大、中、小和微型计算机上得到了广泛的使用。成为当代最优秀的程序设计语言之一。         C语言是一种结构化语言。它层次清晰,便于按模块化方式组织程序,易于调试和维护。C语言的表现能力和处理能力极强。它不仅具有丰富的运算符和数据类型,便于实现各类复杂的数据结构。它还可以直接访问内存的物理地址,进行位(bit)一级的操作。由于C语言实现了对硬件的编程操作,因此C语言集高级语言和低级语言的功能于一体。既可用于系统软件的开发,也适合于应用软件的开发。此外,C语言还具有效率高,可移植性强等特点。因此广泛地移植到了各类各型计算机上,从而形成了多种版本的C语言。         目前最流行的C语言有以下几种:   ·Microsoft C 或称 MS C   ·Borland Turbo C 或称 Turbo C   ·AT&T C  这些C语言版本不仅实现了ANSI C标准,而且在此基础上各自作了一些扩充,使之更加方便、完美!         在C的基础上,一九八三年又由贝尔实验室的Bjarne Strou-strup推出了C++。 C++进一步扩充和完善了C语言,成为一种面向 对象的程序设计语言......

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