1、C语言学习文档多个空格变一个空格#include main() char c; int spc; spc=0; while(c=getchar()!=EOF) while(c= ) +spc; c=getchar(); if(spc=1) putchar( ); spc=0; putchar(c); else putchar(c); 计数A到Z#include /#define IN 1/#define OUT 0main() int E,state,count,f; /state=OUT; /state=IN; count=0; f=0; while(E=getchar()!=EOF) wh
2、ile(E=a & E=1) +f; count=0; /else / state=OUT; printf(%d,f);括号匹配# includemain() char c; int count1=0,count2=0,count3=0; while(c=getchar()!=EOF) if(c=() count1+; if(c=) count1-; if(c=) count2+; if(c=) count2-; if(c=) count3+; if(c=) count3-; if(count1=0)printf(括号配对成功n);else printf(括号配对失败n);if(count2=
3、0) printf(中括号配对成功n);else printf(中括号配对失败n);if(count3=0) printf(大括号配对成功n);else printf(大括号配对失败n);面积#include #include main() int x3,y3; int i,j; float p,s,a,b,c; for(i=0;i3;i+) scanf(%d,&xi); for(j=0;jnext; if(p=NULL) return; while(p-next) q=p; while(q-next)/从p的后继结点开始查找 if(q-next-data=p-data) r=q-next;/
4、找到重复结点,用r指向,删除r q-next=r-next; free(r); else q=q-next; p=p-next; void main() LinkList h=createLinkList3(); printf(从键盘输入的单链表h为:); printLinkList(h); purLinkList(h); printf(删除重复结点后的单链表h为:); printLinkList(h);删除相同的数#includemain() int c; int n; int arry10; for(c=0;c10;c+) scanf(%d,&arryc); for(c=0;c10;c+)
5、 printf(%dt,arryc); printf(n); printf(输入一个数:n); scanf(%d,&n); for(c=0;c10;c+) if(a=arryc) while(c9) arryc=arryc+1; c+; for(c=0;c9;c+) printf(%dt,arryc); 输入10个数输出最大值最小值,和平均数#include main() int x10; int i; int max,min; float sum,avg; sum=0; avg=0; for(i=0;i10;i+) scanf(%d,&xi); for(i=0;imax) max=xi; e
6、lse if(ximin) min=xi; sum=sum+xi; avg=sum/10; printf(max=%dn min=%dn sum=%fn avg=%fn,max,min,sum,avg);顺序查找int locationSeqList(SeqList *l, datatype x) int i=0; while(ilast&l-datai!=x) i+; if(il-last) return -1; else return i;/返回的是下标void createST(ST *st)/创建顺序存储静态查找表 int i=1, k; st-elem=malloc(sizeof(i
7、nt)*LEN); printf(输入查找表中的值:n); scanf(%d, &k); while(k!=0) st-elemi+=k; scanf(%d, &k); st-length=i-1;int sequenceSearchST(ST st, int k)/静态查找表顺序查找 int i; st.elem0=k; for(i=st.length;st.elemi!=k;i-) ; return i;void main() ST st; int flag, k; createST(&st); printf(查找表的长度为:%dn, st.length); printf(输入要查找的值:
8、); scanf(%d, &k); flag=sequenceSearchST(st, k); if(flag) printf(查找成功,要查找的值在查找表中 的位置是:%dn, flag); else printf(查找不成功!);验证:#includeint searchprimary(int x) int i; for(i=2;i=x-1;i+) if(x%i=0) break; if(i=x-1) return 0; else return 1;main() int i,j; for(i=6;i=20;i=i+2) for(j=3;ji;j=j+2) i=j+(i-j); if(sea
9、rchprimary(j)=1)&(searchprimary(i-j)=1) printf(%d=%d+%dn,i,j,i-j);break; 指针求最大值#includemain() int a10; int *p_max; int i; for(i=0;i5;i+) scanf(%d,&ai); for(i=0;i5;i+) printf(a%d=%dn,i,ai); p_max=&a0; for(i=1;i5;i+) if(a0ai) p_max=&ai; printf(%dn,*p_max); 指针排序从大到小#includemain() int i,j; int a5; int *
10、p,q; for(i=0;i5;i+) scanf(%d,&ai); for(i=0;i5;i+) printf(a%d=%dn,i,ai); p=&a0; for(i=0;i5-1;i+) for(j=i+1;j5;j+) if(*(p+i)*(p+j) q=*(p+i); *(p+i)=*(p+j); *(p+j)=q; for(i=0;i5;i+) printf(%dn,*(p+i); 带函数返回指针求最大值#includeint max(int *p,int *q)int *p_max; if(*p*q) p_max=q; return p_max; main() int a10; i
11、nt *p_max; int i; for(i=0;i5;i+) scanf(%d,&ai); for(i=0;i5;i+) printf(a%d=%dn,i,ai); p_max=&a0; for(i=1;i5;i+) p_max=max(p_max,&ai); printf(max=%dn,*p_max); 求和求平均# includevoid get(int arry,int n,int *p,float *q) int i,a; float b; a=0; b=0; for(i=0;in;i+) a=a+arryi; b=a/(float)n; *p=a; *q=b;void main
12、() int a8=5,8,9,6,10,3,1; int i; int sum; float avg; for(i=0;i8;i+) printf(a%d=%dn,i,ai); get(a,8,&sum,&avg); printf(the sum is %dn,sum); printf(the avg is %.2fn,avg);求最大最小# includevoid get(int arry,int n,int *p,int *q) int i; *p=arry0; *q=arry0; for(i=1;in;i+) if(*parryi) *q=arryi; void main() int
13、a8=5,8,9,6,10,3,1,16; int i; int max; int min; for(i=0;i8;i+) printf(a%d=%dn,i,ai); get(a,8,&max,&min); printf(the max is %dn,max); printf(the min is %dn,min);排序# includevoid getorder(int a,int n) int i,j; for(i=0;in-1;i+) for(j=i+1;jn;j+) if(aiaj) ai=ai+aj; aj=ai-aj; ai=ai-aj; main() int arry8=3,5,
14、9,1,2,7,6,4; int i; for(i=0;i8;i+) printf(arry%d= %d n,i,arryi); getorder(arry,8); printf(the new sequence is :n); for(i=0;i8;i+) printf(arry%d= %d n,i,arryi); 改进法排序# includevoid getorder(int a,int n) int i,j,k,temp; for(i=0;in-1;i+) for(j=i+1;jn;j+) k=i; if(akaj) k=j; if(i!=k) temp=ai; ai=aj; aj=te
15、mp; /* if(aiaj) temp=ai; ai=aj; aj=temp; ai=ai+aj; aj=ai-aj; ai=ai-aj; */ main() int arry8=3,5,9,1,2,7,6,4; int i; for(i=0;i8;i+) printf(arry%d= %d n,i,arryi); getorder(arry,8); printf(the new sequence is :n); for(i=0;i8;i+) printf(arry%d= %d n,i,arryi); 无暇素数#includeint searchprimary(int x) int i; for(i=2;i=x-1;i+) if(x%i=0) break; if(i=x-1) return 0; else return 1;int conserve(int x) int a,b,c; a=x%10; b=(x/10)%10; c=x/100; x=a*100+b*10+c; return x;main() int i,j;for(i=100;i1000;i+) j=conserve(i); if(searchprimary(i)=1&searchprimary(j)=1) printf(%d and %dn,i,j);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1