|
阅读:2022回复:11
Some original codes(only for reference:))
Huffman:
#include <stdio.h> typedef struct t_bitree { int data; struct t_bitree *left, *right; } bitree; bitree bitrees[200] = { {6, NULL, NULL}, {7,NULL,NULL}, {8, NULL, NULL}, {9, NULL, NULL}, {10,NULL, NULL}, {12,NULL, NULL}, {14,NULL,NULL}}; int number = 7; void insertNewNode(bitree *n, int dest); bitree * huffman(int from) { if(from>=number-1) return &(bitrees[from]); else { bitree *n = (bitree*) malloc(sizeof(bitree)); n -> data = bitrees[from].data + bitrees[from+1].data; n -> left = (bitree*) malloc(sizeof(bitree)); *(n -> left) = bitrees[from++]; n -> right= (bitree*) malloc(sizeof(bitree)); *(n -> right) = bitrees[from]; insertNewNode(n, from); return huffman(from); } } void insertNewNode(bitree *n, int dest) { int source = dest + 1; while(source < number) { if(bitrees[source].data < n->data) { bitrees[dest] = bitrees[source]; dest = source++; } else break; } bitrees[dest] = *n; } void *freeBitree(bitree *root) { if(root->left != NULL) { freeBitree(root->left); free(root->left); } if(root->right!= NULL) { freeBitree(root->right); free(root->right); } } void output(bitree *huff, int level) { if(huff != NULL) { char space[]="\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; space[level++] = '\0'; printf("%s%d\n", space, huff->data); output(huff->left, level); output(huff->right, level); } } void main() { bitree * huff = huffman(0); output(huff, 0); freeBitree(huff); } |
|
|
1C#
发布于:2001-07-04 18:28
Re:Some original codes(only for reference:))
BiSearch:
#include <stdio.h> int orderedList[10] = { 4,23, 30,39, 48, 57, 64, 78, 89, 110}; int binarySearch(int from, int to, int value) { int mid; if(from == to) return from; mid = (from+to)/2; if(orderedList[mid] > value) return binarySearch(from, mid, value); if(orderedList[mid] == value) return mid; return binarySearch(mid+1, to, value); } void main() { int value; printf("value=?"); scanf("%d", &value); printf("%d is in block %d", value, binarySearch(0,9, value)); } |
|
|
2C#
发布于:2001-07-04 18:29
Re:Some original codes(only for reference:))
N!:
#include <stdio.h> #define MAX 2000 int digit[MAX]; int digNum; void multiply(int n); void main() { int n, nMax; printf("\nn=?"); scanf("%d", &nMax); digNum = 1; digit[0] = 1; for(n=1; n<=nMax; n++) { int i; multiply(n); printf("%d!=", n); for(i=digNum-1; i>=0; i--) printf("%d", digit); printf("\n"); } } void multiply(int n) { int rest = 0; int i; for(i=0; i<digNum; i++) { rest += digit * n; digit = rest % 10; rest /= 10; } while(rest>0) { digit[digNum++] = rest % 10; rest /= 10; } } |
|
|
3C#
发布于:2001-07-04 18:30
Re:Some original codes(only for reference:))
MERGE:
#include <stdio.h> #define n 10 int R[n] = {12, 28, -9, 7, 90, 81, 53, -20, -3, 11}; int mergeSort(int Source[], int from, int to) { int mid; if(from >= to) return; mid = (from + to) / 2; mergeSort(Source, from, mid); mergeSort(Source, mid+1, to); merge(Source, from, mid, mid+1, to); } void merge(int Source[], int from1, int to1, int from2, int to2) { if(from1 > to1 || from2 > to2 ) return; else if(Source[from1] > Source[from2]) { int temp, i; temp = Source[from2]; for(i=from2; i > from1; i--) Source = Source[i-1]; Source[from1] = temp; to1 = from2; from2 ++; } merge(Source, from1+1, to1, from2, to2); } |
|
|
4C#
发布于:2001-07-04 18:34
Re:Some original codes(only for reference:))
表达式的语法是否合法判断(编译):(此程序在运行后会有指针问题,采用的LL分析法)
#include "Stdio.h" typedef struct{ short base[20],*top; }*Stack; Stack stack; static char *point,expression[20],token[10]; static char reservor[]="+*()i=-/"; static char *reserver[]={"sin","cos","tan","ctan","lan","exp"}; static char *list[]={"54010300","50010300","05502020"}; void Intia() { stack->top=stack->base; *stack->top++=6; *stack->top++=8; point=expression; } int EmptyStack() { if(stack->base==stack->top) return 1; return 0; } void Pop(value) short *value; { if(!EmptyStack()) *value=*--stack->top; else{ printf("\nstack is empty"); exit(1); } } void Push(value) short value; { if(value==10&&*(stack->top-1)==10) return; if(stack->top-stack->base<19) *stack->top++=value; else{ printf("\nstack is full"); exit(1); } } short Reserve_1() { short i=6; while(i) if(!strcmp(token,reserver[--i])) return 0; return -1; } short Reserve_2() { short i=8; while(i) if(reservor[--i]==token[0]) return (i=i>5?i-5:i+1); return -1; } int NextWord() { char *token1=token; while(*point==' ') point++; if(!*point) return 7; if(*point<='a'||*point>='z'||*point=='i'){ printf("%c",*point); *token1++=*point++; *token1='\0'; return(Reserve_2()); } if(*point>='a'&&*point<='z'){ while(*point>='a'&&*point<='z') *token1++=*point++; if(*--point=='i') token1--; else point++; *token1='\0'; printf("%s",token); return(Reserve_1()); } } void LL() { char flag; short current,stack_num; current=NextWord(); while(!EmptyStack()){ if(current==-1){ printf("the current word \"%s\" is not defined",*token); exit(1); } Pop(&stack_num); flag='1'; if(stack_num>7) switch (flag=list[stack_num-8][current]){ case '1' :Push(10);Push(4);Push(8);break; case '2' :break; case '3' :Push(10);break; case '4' :Push(9);break; case '5' :Push(10);Push(9);break; defaut :printf("Error!!!");exit(1); } else if(current!=stack_num){ printf("\n\tError:'%c' is missing",reservor[stack_num-1]); exit(1); } if(flag!='2'&¤t!=6) current=NextWord(); } if(current==6&&(current=NextWord())==7) printf("\t:is valid!!!"); else printf("\t:Should be Nothing after '=';!!!"); } void main() { gets(expression); Intia(); LL(); } |
|
|
5C#
发布于:2001-07-04 18:39
Re:Some original codes(only for reference:))
数据结构小学期作业(最短路径问题的DJESTRA算法),程序用文件方式输入(输入文件格式如下)
输入文件(书上给出的城市图): zhenzhou 695J 511Y 349I 534W nanzhou 1892M 216N 676Y 1154X chengdu 842Y 1100O 967D guiyang 639O 607F 902E zhuzhou 409W 367H 675G 672F liouzhou 255P guangzhou 140Q nanchang 622R 825S xvzhou 674V 651S beijing 668X 137V shengyang 704V 397U 305L changchun 242T wunumuqi xining kunming nanning shenzhen fuzhou shanghai haerbin dalian tianjin wuhan huhehaote xian CODE:(图的存储采用连结式) #include "stdio.h" typedef struct lnode{ short tail,info; struct lnode *next; }Arcbox,*ArcBox; typedef struct{ char *data; ArcBox out; }VexNode; typedef struct{ VexNode list[26]; short vex,arc; }Graph; Graph G; void GetGraph() { FILE *fp; char string[10],*str,ch; short line,distance=0; ArcBox p; str=string; G.vex=25; for(line=G.vex;line;line--) G.list[line].out=G.list[line].data=NULL; if(!(fp=fopen("a:file1.c","r"))){ printf("ERROR:a:file1.c cannot be opened"); exit(1); } line++; ch=fgetc(fp); while(ch!=EOF){ putchar(ch); if((ch>='a')&&(ch<='z')) *str++=ch; else if((ch>='A')&&(ch<='Z')) { p=(ArcBox)malloc(sizeof(Arcbox)); p->info=distance; p->tail=ch-'A'+1; p->next=G.list[line].out; G.list[line].out=p; distance=0; } else if((ch<='9')&&(ch>='0')) distance=distance*10+ch-'0'; else if(ch=='\n'){ *str='\0'; G.list[line].data=(char *)malloc(str-string+1); strcpy(G.list[line++].data,string); str=string; } ch=fgetc(fp); } fclose(fp); } void distance(short v0,short w,short *d) { ArcBox p; for(p=G.list[v0>w?w:v0].out;p&&p->tail!=(v0>w?v0:w);p=p->next); if(p) *d=p->info; else *d=10000; } PrintPath(short *path,short *d,short location) { if(location){ PrintPath(path,d,path[location]); printf("-->%s[%d]",G.list[location].data,d[location]); } } void ShortestPath(short v0) { short path[26],final[26],min; short d[26],dis,i,w,v; for(i=G.vex;i;i--){ path=final=0; distance(v0,i,&d); } final[v0]=1; for(i=G.vex-1;i;i--){ min=9999; for(w=1;w<=G.vex;w++) if((!final[w])&&(d[w]<min)) min=d[v=w]; final[v]=1; for(w=1;w<=G.vex;w++){ distance(v,w,&dis); if(!final[w]&&(min+dis<d[w])){ d[w]=min+dis; path[w]=v; } } } for(i=G.vex;i;i--){ if(i==v0) break; printf("\n%s",G.list[v0].data); PrintPath(path,d,i); } } void main() { short vex; GetGraph(); for(vex=G.vex;vex;vex--) ShortestPath(vex); printf("\n\t\t\tPROGRAM IS OVER!!!"); printf("\n\t\tChenJinSong CopyRight 2000"); } |
|
|
6C#
发布于:2001-07-04 18:42
Re:Some original codes(only for reference:))
数据结构小学期之迷宫问题(文件输入方式)
文件如下: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 1 0 1 1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1 1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1 1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 CODE:(采用低归的完成) #include "stdio.h" typedef struct { short l,r; }position; short mark[13][17],counter=0; position stack[40]; void GetMap() { FILE *fp; short *mark1=mark,i,j; char ch; if(!(fp=fopen("a:file2.c","r"))) { printf("\nERROR:file2.c cannot be opened"); exit(1); } ch=fgetc(fp); while(ch!=EOF) { if((ch<='9')&&(ch>='0')) *(mark1++)=ch-'0'; ch=fgetc(fp); } fclose(fp); } short p(short l,short r) { if(mark[l][r]) return 0; if((l==11)&&(r==15)) { stack[counter].l=11; stack[counter].r=15; return 1; } mark[l][r]=1; if((p(l-1,r))||(p(l+1,r))||(p(l,r+1))||(p(l,r-1))|| (p(l+1,r+1))||(p(l-1,r+1))||(p(l+1,r-1))||(p(l-1,r-1))){ stack[++counter].l=l; stack[counter].r=r; return 1; } } void PrintMap() { short l,r; printf("\n\n\t\tThis program is searchpath\nMap is :"); for(l=1;l<12;l++){ printf("\n\t\t"); for(r=1;r<16;r++) printf("%d ",mark[l][r]); } } void PrintPath() { short count; if(p(1,1)){ printf("\nThe way is:\n"); count=counter+1; while(count--) printf("(%d,%d)-->",stack[count].l,stack[count].r); printf("\b\b\b \n\t\tChenJinSong CopoyRight 2000\n"); } else printf("\nERROR:NO WAY!!!"); } void main() { GetMap(); PrintMap(); PrintPath(); } |
|
|
7C#
发布于:2001-07-04 18:43
Re:Some original codes(only for reference:))
数据结构之二叉排序树问题
#include "stdio.h" typedef struct lnode { char *data; short freqence; struct lnode *l,*r; }lnode,*Tree; char sentence[]="everyone round you can hear you when you speak"; void GetWord(char **word) { char s1[9],*s2,*s3=sentence; s2=s1; while(1){ if((*s3==' ')||!*s3){ *s2='\0'; *word=(char *)malloc(s2-s1+1); strcpy(*word++,s1); if(!*s3) return; s2=s1; s3++; } else *s2++=*s3++; } } void Insert(Tree *tree,char *word) { Tree tree1; short i; if(!(*tree)) { tree1=(Tree)malloc(sizeof(lnode)); tree1->data=(char *)malloc(strlen(word)+1); strcpy(tree1->data,word); tree1->l=tree1->r=NULL; tree1->freqence=1; *tree=tree1; return; } if(!(i=strcmp((*tree)->data,word))) (*tree)->freqence++; else if(i<0) Insert(&(*tree)->r,word); else Insert(&(*tree)->l,word); } void Print(Tree tree) { if(tree){ Print(tree->l); printf("\t\t%-30s%d\n",tree->data,tree->freqence); Print(tree->r); } } void main() { Tree tree=NULL; char *word[9]; short counter=0; printf("\n\n\t\t\tRunning Program\n"); printf("The sentence is:%s\n",sentence); printf("\t\tWORD:\t\t\tFREQENCE\n"); GetWord(word); while(counter<9) Insert(&tree,word[counter++]); Print(tree); printf("\t\t\tProgram_3 is over!!!"); printf("\n\t\tChenJinSong CopyRight 20000\n"); } |
|
|
8C#
发布于:2001-07-04 18:50
Re:Some original codes(only for reference:))
数据结构之图书管理程序(用B树完成),但此程序只是做了插入和删除两个主要功能,其中的查找和插入是基本相同,此程序的界面就非常不怎么地了,只是完成了两个功能(其算法在书上),惭愧:fool::fool::fool::fool::fool::fool:
#include "stdio.h" typedef struct lnode{ struct lnode *node[3]; int num[3]; }lnode,*B_Tree; B_Tree tree1; int e,elem,find=0,low=0; void Search_Insert(B_Tree tree) { B_Tree tree3; if(!tree){ tree1=(B_Tree)malloc(sizeof(lnode)); tree1->node[0]=tree1->node[1]=NULL; tree1->num[0]=1; tree1->num[1]=e; return; } if(e==tree->num[1]) return; if(tree->num[0]==1) if(e>tree->num[1]){ Search_Insert(tree->node[1]); if(tree1){ tree->num[0]=2; tree->num[2]=tree1->num[1]; tree->node[1]=tree1->node[0]; tree->node[2]=tree1->node[1]; free(tree1); tree1=NULL; return; } } else{ Search_Insert(tree->node[0]); if(tree1){ tree->num[0]=2; tree->num[2]=tree->num[1]; tree->num[1]=tree1->num[1]; tree->node[2]=tree->node[1]; tree->node[0]=tree1->node[0]; tree->node[1]=tree1->node[1]; free(tree1); tree1=NULL; return; } } else if(tree->num[2]==e) return; else if(tree->num[1]>e){ Search_Insert(tree->node[0]); if(tree1){ tree3=(B_Tree)malloc(sizeof(lnode)); tree3->num[0]=1; tree3->num[1]=tree->num[1]; tree3->node[0]=tree1; tree3->node[1]=tree; tree->num[0]=1; tree->num[1]=tree->num[2]; tree->node[0]=tree->node[1]; tree->node[1]=tree->node[2]; tree1=tree3; } } else if(tree->num[2]>e){ Search_Insert(tree->node[1]); if(tree1){ tree3=(B_Tree)malloc(sizeof(lnode)); tree3->num[0]=tree->num[0]=1; tree3->num[1]=tree->num[2]; tree3->node[0]=tree1->node[1]; tree3->node[1]=tree->node[2]; tree->node[1]=tree1->node[0]; tree1->node[0]=tree; tree1->node[1]=tree3; } } else{ Search_Insert(tree->node[2]); if(tree1){ tree3=(B_Tree)malloc(sizeof(lnode)); tree3->num[0]=1; tree3->num[1]=tree->num[2]; tree->num[0]=1; tree3->node[0]=tree; tree3->node[1]=tree1; tree1=tree3; } } } void Chang_1(B_Tree tree, short p) { B_Tree btree; tree->num[0]=2; if(p){ btree=tree->node[0]; tree->num[2]=tree->num[1]; tree->num[1]=btree->num[1]; tree->node[0]=btree->node[0]; tree->node[1]=btree->node[1]; tree->node[2]=tree1; free(btree); } else{ btree=tree->node[1]; tree->node[0]=tree1; tree->node[1]=btree->node[0]; tree->node[2]=btree->node[1]; tree->num[2]=btree->num[1]; free(btree); } tree1=tree; } void Chang_2(B_Tree tree,short p) { B_Tree btree,btree1; btree=(B_Tree)malloc(sizeof(lnode)); btree->num[0]=1; btree->num[1]=tree->num[1]; if(p){ btree1=tree->node[0]; btree->node[1]=tree1; btree->node[0]=btree1->node[2]; tree->num[1]=btree1->num[2]; tree->node[0]->num[0]=1; tree->node[1]=btree; } else{ btree1=tree->node[1]; tree->num[1]=btree1->num[1]; btree->node[0]=tree1; btree->node[1]=btree1->node[0]; btree1->num[1]=btree1->num[2]; btree1->node[0]=btree1->node[1]; btree1->node[1]=btree1->node[2]; btree1->num[0]=1; tree->node[0]=btree; } low=0; } void Chang_3(B_Tree tree, short p) { B_Tree btree; tree->num[0]=1; if(!p){ btree=tree->node[1]; btree->num[0]=2; btree->num[2]=btree->num[1]; btree->num[1]=tree->num[1]; btree->node[2]=btree->node[1]; btree->node[1]=btree->node[0]; btree->node[0]=tree1; tree->node[0]=btree; tree->num[1]=tree->num[2]; tree->node[1]=tree->node[2]; } else if(p==1){ btree=tree->node[2]; tree->node[1]=btree; btree->num[0]=2; btree->num[2]=btree->num[1]; btree->num[1]=tree->num[1]; btree->node[2]=btree->node[1]; btree->node[1]=btree->node[0]; btree->node[0]=tree1; } else{ btree=tree->node[1]; btree->num[0]=2; btree->num[2]=tree->num[2]; btree->node[2]=tree1; } low=0; } void Chang_4(B_Tree tree,short p) { B_Tree btree,btree1; btree=(B_Tree)malloc(sizeof(lnode)); btree->num[0]=1; if(!p){ tree->node[0]=btree; btree1=tree->node[1]; btree->node[0]=tree1; btree->node[1]=btree1->node[0]; btree->num[1]=tree->num[1]; tree->num[1]=btree1->num[1]; btree1->num[0]=1; btree1->num[1]=btree1->num[2]; btree1->node[0]=btree1->node[1]; btree1->node[1]=btree1->node[2]; } else if(p==1){ btree1=tree->node[2]; tree->node[1]=btree; btree->node[0]=tree1; btree->node[1]=btree1->node[0]; btree->num[1]=tree->num[2]; tree->num[2]=btree1->num[1]; btree1->num[0]=1; btree1->num[1]=btree1->num[2]; btree1->node[0]=btree1->node[1]; btree1->node[1]=btree1->node[2]; } else{ btree1=tree->node[1]; tree->node[2]=btree; btree->num[1]=tree->num[2]; btree->node[0]=btree1->node[2]; btree->node[1]=tree1; btree1->num[0]=1; tree->num[2]=btree1->num[2]; } low=0; } void DelTree(B_Tree tree) { if(!tree){ printf("\n%d:is not found!!",elem); return; } if(tree->num[0]==1){ if(elem>=tree->num[1]){ if(elem==tree->num[1]) if(!tree->node[0]){ free(tree); tree1=NULL; low=1; return; } else{ tree1=tree; find=1; } DelTree(tree->node[1]); if(low) if(tree->node[0]->num[0]==2) Chang_2(tree,1); else Chang_1(tree,1); return; } else if(elem<tree->num[1]){ if(!tree->node[0]&&find){ if(tree1->num[1]==elem) tree1->num[1]=tree->num[1]; else tree1->num[2]=tree->num[1]; free(tree); tree1=NULL; low=1; return; } DelTree(tree->node[0]); if(low) if(tree->node[1]->num[0]==2) Chang_2(tree,0); else Chang_1(tree,0); } } else if(elem<tree->num[1]){ if(!tree->node[0]&&find){ if(tree1->num[1]==elem) tree1->num[1]=tree->num[1]; else tree1->num[2]=tree->num[1]; tree->num[0]=1; tree->num[1]=tree->num[2]; tree->node[0]=tree->node[1]; tree->node[1]=tree->node[2]; return; } DelTree(tree->node[0]); if(low) if(tree->node[1]->num[0]==2) Chang_4(tree,0); else Chang_3(tree,0); } else if(elem<tree->num[2]){ if(tree->num[1]==elem) if(!tree->node[0]){ tree->num[0]=1; tree->num[1]=tree->num[2]; return; } else{ tree1=tree; find=1; } DelTree(tree->node[1]); if(low) if(tree->node[2]->num[0]==2) Chang_4(tree,1); else Chang_3(tree,1); } else if(elem>=tree->num[2]){ if(elem==tree->num[2]) if(!tree->node[0]){ tree->num[0]=1; return; } else{ tree1=tree; find=1; } DelTree(tree->node[2]); if(low) if(tree->node[1]->num[0]==2) Chang_4(tree,2); else Chang_3(tree,2); } } void Print(B_Tree tree, short i) { if(tree) if(tree->num[0]==1){ Print(tree->node[0],i+1); printf("%d(%d) ",tree->num[1],i); Print(tree->node[1],i+1); } else{ Print(tree->node[0],i+1); printf("%d(%d) ",tree->num[1],i); Print(tree->node[1],i+1); printf("%d(%d) ",tree->num[2],i); Print(tree->node[2],i+1); } } void main() { B_Tree tree; tree1=NULL; printf("\nInput the elem that will be inserted:\n"); while(printf("The elem is :"),scanf("%d",&e),e){ Search_Insert(tree); if(tree1){ tree=tree1; tree1=NULL; } } printf("\t\tThe result of insert is:\n"); Print(tree,1); printf("\nInput the elem that will be deleted:"); scanf("%d",&elem); DelTree(tree); if(low){ tree=tree1; low=0; find=0; } printf("\t\tThe result of delete is:\n"); Print(tree,1); } -------------------- Optimistic is my color We spend our time on something from dawn to dark Day in and day out Sun rise and down Make an own goal to confront life Self-discovery is the first step toward self-appreciation 成功只会降临在自认为会成功的人身上 |
|
|
9C#
发布于:2001-07-04 18:54
Re:Some original codes(only for reference:))
自动机(编译)
#include"stdio.h" typedef struct lnode1{ char ch,*stateset; struct lnode1 *next; }*LINK1,Lnode1; typedef struct lnode2{ char ch,stateno; struct lnode2 *next; }*LINK2,Lnode2; typedef struct lnode3{ char *stateset; LINK2 vex; struct lnode3 *next; }*LINK3,Lnode3; LINK1 graph[20]; LINK3 result; char str[10],*str1,token[10]; char location[50],*loc,string[20]; void Intial() { char *str2=token,*str3=string; FILE *fp; LINK1 *graph1=graph+20; printf("\n\nFile is:\n"); while(graph1>graph) *--graph1=NULL; if(!(fp=fopen("d:\pine.txt","r"))){ printf("file is not found!!"); exit(1); } loc=location; while((*str2++=fgetc(fp))!='\n'); *--str2='\0'; while((*str3++=fgetc(fp))!='\n'); *--str3='\0'; while((*loc++=fgetc(fp))!=EOF); *--loc='\0'; loc=location; printf("%s",location); } void GetState() { short value=0; if(*loc<'0'||*loc>'9'){ printf("number is expected!!"); exit(1); } while(*loc<='9'&&*loc>='0') value=value*10+*loc++-'0'; *str1++=value; if(*loc==')') return; if(*loc++!=','){ printf(", is expected!!"); exit(1); } } void GetGraph() { short value,state=1; LINK1 head; while(1){ while(*loc==' '||*loc=='\n') loc++; if(*loc=='\0') return; if((*loc<'a'||*loc>'z')&&*loc!='$'){ printf("Char is expected!!"); exit(1); } head=(LINK1)malloc(sizeof(Lnode1)); head->next=graph[state]; graph[state]=head; head->ch=*loc++; if(*loc++!='('){ printf("( is expected!!"); exit(1); } str1=str; while(*loc!=')') GetState(); *str1++='\0'; head->stateset=(short *)malloc((str1-str)*sizeof(short)); strcpy(head->stateset,str); if(*++loc=='\n') state++; } free(str); free(location); } void Closure(state,keyword,address) short state; char keyword,*address; { LINK1 head=graph[state]; while(head&&head->ch!=keyword) head=head->next; if(!head) return; strcpy(address,head->stateset); } void Insert(str,ch) char *str,ch; { char *str1; while(*str&&ch>*str) str++; if(*str==ch) return; str1=str; while(*str1++); while(str1>str){ *str1=*(str1-1); str1--; } *str=ch; } void Enclosure(state,keyword,str) short state; char keyword,*str; { char str1[11],*str3,*str2=str; *str='\0'; Closure(state,keyword,str); while(*str2){ str3=str1; *str3='\0'; Closure(*str2++,'$',str1); while(*str3) Insert(str,*str3++); } } short Check(str,level) short *level; char *str; { LINK3 head=result; short i=0; while(head&&strcmp(head->stateset,str)){ head=head->next; i++; } *level=i; if(head) return 0; return 1; } void CreatResult() { LINK3 head,head1; LINK2 head2; short level; char str[11],str2[11],*str1,*str3,*token1; head1=head=result=(LINK3)malloc(sizeof(Lnode3)); head->next=head->vex=NULL; Enclosure(1,'$',str); Insert(str,1); head->stateset=(char *)malloc(strlen(str)+1); strcpy(head->stateset,str); while(head){ token1=token; while(*token1){ str[0]='\0'; str1=head->stateset; while(*str1){ str3=str2; Enclosure(*str1++,*token1,str2); while(*str3) Insert(str,*str3++); } if(*str) if(Check(str,&level)){ head2=(LINK2)malloc(sizeof(Lnode2)); head2->next=head->vex; head->vex=head2; head2->stateno=level+1; head2->ch=*token1; head1->next=(LINK3)malloc(sizeof(Lnode3)); head1=head1->next; head1->vex=head1->next=NULL; head1->stateset=(char *)malloc(strlen(str)+1); strcpy(head1->stateset,str); } else{ head2=(LINK2)malloc(sizeof(Lnode2)); head2->next=head->vex; head->vex=head2; head2->stateno=level; head2->ch=*token1; } token1++; } head=head->next; } } void Print(stateset) char *stateset; { while(*stateset) printf("%d ",*stateset++); } void PrintGraph() { short state=1; LINK1 head,*graph1=graph+1; printf("\nThe input is :"); while(*graph1){ head=*graph1; printf("\n\tState%d:\t",state++); while(head){ printf("%c-->(",head->ch); Print(head->stateset); printf("\b) "); head=head->next; } graph1++; } } void PrintResult() { short state=1; LINK3 head=result; printf("\nThe result is :"); printf("\n\tState NO.\tState set"); while(head){ printf("\n\t %d\t\t ",state++); Print(head->stateset); head=head->next; } } short NextState(state,ch) short state,ch; { LINK3 head=result; LINK2 head1; while(--state&&head) head=head->next; head1=head->vex; while(head1&&head1->ch!=ch) head1=head1->next; if(!head1) return -1; return(head1->stateno); } void Inspect() { char *str=string; short state=1; while(*str&&(state=NextState(state,*str))>=0) str++; if(*str) printf("\nThis string is invalid!!"); else printf("\nThe last state is :%d",state); } void main() { Intial(); GetGraph(); CreatResult(); PrintGraph(); PrintResult(); Inspect(); } |
|
|
10C#
发布于:2001-07-04 19:02
Re:Some original codes(only for reference:))
算24问题(给出指定的的目标结果和计算的数字个数)但是此程序有点问题。但是有些情况下还是工作的,呵呵。我个人认为思路还是对的,具体的地方还要更改,整个算法思路是把各种计算的表示方式全部列举出来
#include "stdio.h" #include "assert.h" #include "malloc.h" typedef struct { int left,right,flag; char token; }TOKEN; int FoundFlag=0,level=0,totalvalue,number; TOKEN *token; int Compare(float value) { if( value<0.0001 && value>-0.0001 ) return 1; return 0; } void Swap(int *head,int *tail) { int itempt; itempt = *head; *head = *tail; *tail = itempt; } void Copy(float str[],float string[],int head[],int tail[],int first,int second, int length) { int len,i; len = 0; i = 1; while( len<first ){ str = string[len]; head[i++] = tail[len++]; } len++; while( len<second ){ str = string[len]; head[i++] = tail[len++]; } len++; while( len<=length ){ str = str[len]; head[i++] = tail[len++]; } } short Computation(float *string,float *first,float *second,int counter,int *flag ,int *flag1) { float fir,sec; fir = *first; sec = *second; token[level].flag = !!(*flag) + (!!*flag1)*2; if( *flag ) token[level].left = *flag; else token[level].left = ( int ) fir; if( *flag1 ) token[level].right = *flag1; else token[level].right = ( int ) sec; switch ( counter ){ case 1 : *string = fir+sec; token[level].token = '+'; return 1; case 2 : *string = fir-sec; token[level].token = '-'; return 1; case 3 : *string = fir*sec; token[level].token = '*'; return 1; case 4 : *string = sec-fir; token[level].token = '-'; Swap(&token[level].left,&token[level].right); return 1; case 5 : if( Compare(sec) ) return 0; token[level].token = '/'; *string = fir/sec; return 1; default: if( Compare(fir) ) return 0; token[level].token = '/'; *string = sec/fir; Swap(&token[level].left,&token[level].right); return 1; } } void Initial(float **string,int **flag,int length) { *string = (float *)malloc( length*sizeof(float) ); assert( *string ); *flag = (int *)malloc( length*sizeof(int) ); assert( *flag ); } void Sort(float *str,int *Flag,int tail) { float *string; int first,second,*flag,inspect,tempt; if( !tail ){ if( Compare( *str-totalvalue ) ) FoundFlag = 1; return; } Initial(&string,&flag,tail); for(first=0; !FoundFlag&&first<tail; first++) for(second=first+1; !FoundFlag&&second<=tail; second++){ Copy(string,str,flag,Flag,first,second,tail); tempt = 1; while( !FoundFlag && tempt<=6 ){ inspect = Computation(string,str+first,str+second,tempt,Flag+first,Flag+seco nd); *flag = ++level; if( inspect ) Sort(string,flag,tail-1); level--; tempt++; } } free(string); free(flag); } void GetCommand(float **str,int **Flag) { float *string; int counter,*flag,tempt; printf("\n\n\tThis is 24 problem\nInput the totalvalue( int ):"); scanf("%d",&totalvalue); printf("Input the numbers( short ):"); scanf("%d",&number); string = ( float *)malloc( number*sizeof(float) ); assert( string ); flag = (int *)malloc( number*sizeof(int) ); assert( flag ); token = (TOKEN *)malloc( (number-1)*sizeof(TOKEN) ); assert( token ); *str = string; *Flag = flag; printf("Please input %d ( int ) numbers\n",number); for( counter=1; counter<=number; counter++,string++,flag++ ){ printf("\t%d-th number is :",counter); scanf("%d",&tempt); *string = ( float )tempt; *flag = 0; } } void GetResult(int loc) { int left,right,flag; char ch; left = token[loc].left; right = token[loc].right; flag = token[loc].flag; ch = token[loc].token; if( !flag ){ printf("(%d%c%d)",left,ch,right); return; } printf("("); if( flag%2 ) GetResult( left-1 ); else printf("%d",left); printf("%c",ch); if( flag>1 ) GetResult( right-1 ); else printf("%d",right); printf(")"); } void PutResult() { if( !FoundFlag ){ printf("\nNo resolution !"); return; } printf("\nThe reselution is:"); GetResult( number-2); } void main() { float *str; int *flag; GetCommand(&str,&flag); Sort( str,flag,number-1 ); PutResult(); free( str ); free( flag ); } |
|
|
11C#
发布于:2001-07-04 19:04
Re:Some original codes(only for reference:))
最后总结一下,呵呵。望大家多多指教。可能有些程序有问题了。大家就将就将就;);););)
--------------------
Optimistic is my color
We spend our time on something from dawn to dark Day in and day out Sun rise and down Make an own goal to confront life Self-discovery is the first step toward self-appreciation 成功只会降临在自认为会成功的人身上 |
|