c语言常见代码示例.docx_第1页
c语言常见代码示例.docx_第2页
c语言常见代码示例.docx_第3页
c语言常见代码示例.docx_第4页
c语言常见代码示例.docx_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C语言常用模板 By ZXC目录I. 常用函数1. 数字拆分2. 顺序查找3. 折半查找4. 冒泡排序5. 选择排序6. 进制转换(BD)7. 求最大公约数8. 随机数生成9. 判断n是否为素数10. 动态数组生成II. 字符串处理1. 字符串库函数(复制,连接,比较)2. 判断字符ch在字符串a中出现次数3. 比较两字符串中数字大小4. 求字符串a中的最长单词5. 判断字符串a是否与字符串b开头匹配6. 找三个字符串的最大公共子串并返回公共字串指针7. 分解句子III. 链表相关1. #define(注:使用该部分代码可以如此define)2. 向一个有序单向链表STRU中插入一个节点关键字为num(int)3. 同上,关键字为num(char)4. *链表(单向、循环均可)节点插入关键字为num(int)5. *同上,关键字为num(char)6. 创建单向链表,数据从键盘输入(依赖.2-.5任意一个)7. 从硬盘中读取链表(依赖.2-.5任意一个)8. 将a,b链表连成一个新链表(依赖.2-.5任意一个)9. 打印链表10. 将链表(单向or循环)存入硬盘 11. 将链表(单向or循环)倒序12. 链表(单向or循环)节点计数13. 删除节点(单向or循环)IV. 若干代码1. 日历计算2. 计算日期的差值3. 代数式的计算4. 矩阵求逆数字拆分(五位)(非函数)int in,a,b,c,d,e;a=in/10000;b=(in%10000)/1000;c=(in%1000)/100;d=(in%100)/10;e=in%10;顺序查找int search(int b, int n, int x) /n为数组长度, int k; for (k=0;(kn)&(bk!=x);k+) if (kn) return(k); else return(-1);折半查找int search(int b, int n, int x) int L,R,mid; L=0;R=n-1; while(L=R) mid=(L+R)/2; if (x=bmid) return(mid);else if (x R) return(-1); mid = (L + R)/2; if(x = bmid) return mid; else if(x bmid) return bsearch(b, x, L, mid-1); else return bsearch(b, x, mid+1, R);冒泡排序*void bubble(int *a,int n) /*定义两个参数:数组首地址与数组大小*/ int i,j,temp; for(i=0;in-1;i+) for(j=i+1;jaj) temp=ai; ai=aj; aj=temp; *冒泡排序void bubble(int *a,int n)/*定义两个参数:数组首地址与数组大小*/ int i,j,k; for(i=0;in-1;i+) for(j=0;jn-1-i;j+) /*注意循环的上下限*/ if(aj+1aj) k=aj+1; aj+1=aj; aj=k; 选择排序void choise(int *a,int n) int i,j,k,temp; for(i=0;in-1;i+) k=i; /*给记号赋值*/ for(j=i+1;jaj) k=j; /*是k总是指向最小元素*/ if(i!=k) /*当k!=i是才交换,否则ai即为最小*/ temp=ai; ai=ak; ak=temp; 进制转换/10-2#include #include #include char *d_b(int a)char *binbuf;binbuf=(char *)malloc(32); itoa(a, binbuf, 2); return binbuf;/2-10int b_d(char *str)int a=0,i,n;for(n=0;strn!=0;n+);for(i=0;in;i+)a+=(stri-48)b)m=a;n=b; elsem=b;n=a;for(;n!=0;)k=m%n;m=n;n=k;return(m);随机数生成(非函数)#include #include #include int i;srand(time(NULL); / 以当前的时间为基础,初始化生成随机数的种子i=rand() %100; /rand() 以srand()为种子,随机生成随机数(不大于100)判断n是否为素数int f(int n) /是则返回1,输入0,1则返回-1int j,t;if(n2)return -1;for(j=2,t=1;jn;j+)t=n%j;if(t=0)break;if(t!=0)return 1;else return 0;动态数组生成(支持下标访问)#include/三维int *CreateGrid(int m, int n, int t) /生成三维数组int *a,i,j;a=(int *)malloc(m*sizeof(int *);/动态生成连续面指针数组*a=(int *)malloc(m*n*sizeof(int *);/动态生成连续行指针数组*a=(int *)malloc(m*n*t*sizeof(int);/动态生成数组空间for(i=0;im;i+)*(a+i)=*a+i*n;for(j=0;jn;j+)*(*(a+i)+j)=*a+i*n*t+j*t;return a;void FreeGrid(int *a) /释放三维数组free(*a);free(*a);free(a);float *CreateGrid(int m, int n, int t) /生成三维float数组float *a;int i,j;a=( float *)malloc(m*sizeof(float *);/动态生成连续面指针数组*a=( float *)malloc(m*n*sizeof(float *);/动态生成连续行指针数组*a=( float *)malloc(m*n*t*sizeof(float);/动态生成数组空间for(i=0;im;i+)*(a+i)=*a+i*n;for(j=0;jn;j+)*(*(a+i)+j)=*a+i*n*t+j*t;return a;void FreeGrid(float *a) /释放三维float数组free(*a);free(*a);free(a);/二维int *CreateGrid(int m, int n) /生成二维数组int *a,i;a=(int *)malloc(m*sizeof(int *);/动态生成连续行指针数组*a=(int *)malloc(m*n*sizeof(int);/动态生成数组空间for(i=0;im;i+)*(a+i)=*a+i*n;return a;void FreeGrid(int *a) /释放二维数组free(*a);free(a);float *CreateGrid(int m, int n) /生成二维float数组float *a;int i;a=(float *)malloc(m*sizeof(float *);/动态生成连续行指针数组*a=( float *)malloc(m*n*sizeof(float);/动态生成数组空间for(i=0;im;i+)*(a+i)=*a+i*n;return a;void FreeGrid(float *a) /释放二维float数组free(*a);free(a);字符串库函数char *myscpy(char *s,char *t,int n)/复制t的前n个字符至sint i;for (i=0;in;i+)*s+=*t+;*s=0;return(s-n);char *myscat(char *s,char *t,int n) /连接t的前n个字符至s的末尾int i;while(*(+s)!=0);for (i=0;in;i+)*s+=*t+;*s=0;return(t-n);int myscmp(char *s,char *t,int n) /比较s和t的前n个字符for (int i=0;i*(t+i)return 1; else if(*(s+i)b,返回值为1)int cmp(char *a,char *b)int n=0,m=0;while(*a)n=10*n+(*a-48);a+;while(*b)m=10*m+(*b-48);b+;if(nm)return 1;if(nm)return -1;return 0;求字符串a中的最长单词#include #includechar *lword(char *a) /求字符串a中的最长单词,返回最长单词首地址int i,k=0,j=0,w,s,n;char *o;for(i=0;ai!=0;i+);n=i;for (i=0,w=0;ik)k=j;s=i-k;j=0;else j=0;o=(char *)malloc(k+1);for (i=s,j=0;jk;i+,j+)oj=ai;oj=0;return o;判断字符串a是否与字符串b开头匹配,是则为1int mach(char *a,char *b) while(*a!=0)if(*a!=*b)return 0;a+;b+;return 1;找三个字符串的最大公共子串并返回公共字串指针#include#includechar *ser(char *s1,char *s2,char *s3)int n=0,i,j;char *s;s=s1;while(*s+!=0)n+;s=(char *)malloc(n*sizeof(char);for(i=n;i0;i-)for(j=n-i;j=0;j-)strcpy(s,s1+j);*(s+i)=0;if(strstr(s2,s)!=0&strstr(s3,s)!=0)return s;return NULL;分解句子/将字符串截成单个单词,并将单词指针存放在指针数组中,并返回单词数#include#include#include#define NU 0int div(char *str1,char *s) /*s为字符串数组首地址int w=0,i=0; /w判断是否为新单词开始char *str;str=(char *)malloc(strlen(str1)+1);strcpy(str,str1);while (*str!=NU)if(*str= )w=0;*str=NU; /将空格改为结束符else if(w=0)w=1;*(s+i)=str;i+;str+;return i; /返回单词数#define*#define STRU STR#define NUM num#define LEN sizeof(STRU)#define FORMATI %d,&stru1-num#define FORMATO %dn,p-numP.S:其中STR 为结构体类型,num 为关键字,FORMATI 与 FORMATO 别为输入输出格式控制符。*向一个有序单向链表STRU中插入一个节点, 关键字为num(int) (使用方式为head=insert(head,p);)STRU *insert(STRU *head, STRU *p)STRU *p1,*p2;if(head=NULL)p-next=NULL;return p;if(p-NUMNUM)p-next=head;return p;p2=head;while(p2!=NULL&p-NUMp2-NUM)p1=p2;p2=p1-next;if(p2=NULL)p1-next=p;p-next=NULL;elsep1-next=p;p-next=p2;return head;*向一个有序单向链表STRU中插入一个节点, 关键字为num(char)(使用方式为head=insert(head,p);) #include STRU *insert(STRU *head, STRU *p)STRU *p1,*p2;if(head=NULL)p-next=NULL;return p;if(strcmp(p-NUM,head-NUM)next=head;return p;p2=head;while(p2!=NULL&strcmp(p-NUM,p2-NUM)next;if(p2=NULL)p1-next=p;p-next=NULL;elsep1-next=p;p-next=p2;return head;链表(单向、循环均可)节点插入关键字为num(int)(使用方式为head=insert(head,p);)STRU *insert(STRU *head, STRU *p)STRU *p1,*p2;if(head=NULL)p-next=NULL; return p;if(p-NUMNUM)p-next=head;p1=head;while(p1-next)if(p1-next=head)p1-next=p;break;p1=p1-next;return p;p2=head;while(p2!=NULL&p-NUMp2-NUM)p1=p2;p2=p1-next;if(p2=head)break;if(p2=NULL)p1-next=p;p-next=NULL;else if(p2=head)p1-next=p;p-next=head;elsep1-next=p;p-next=p2;return head;*链表(单向、循环均可)节点插入关键字为num(char)(使用方式为head=insert(head,p);)STRU *insert(STRU *head, STRU *p)STRU *p1,*p2;if(head=NULL)p-next=NULL;return p;if(strcmp(p-NUM,head-NUM)next=head;p1=head;while(p1-next)if(p1-next=head)p1-next=p;break;p1=p1-next;return p;p2=head;while(p2!=NULL&strcmp(p-NUM,p2-NUM)0)p1=p2;p2=p1-next;if(p2=head)break;if(p2=NULL)p1-next=p;p-next=NULL;else if(p2=head)p1-next=p;p-next=head;elsep1-next=p;p-next=p2; return head;创建链表,数据从键盘输入(依赖insert)STRU *creat(void)STRU *head=NULL,*p;while (1)p=(STRU *)malloc(LEN);scanf(FORMATI);if(p-NUM=0)break; /此处为终止条件head=insert(head,p);return head;从硬盘中读取链表,n为1时为单向链表(依赖insert)STRU *LoadList(char *s,int n)/s为文件名STRU *head,*p;FILE *fp;fp=fopen(s,r);if(fp=NULL)printf(cant open %s,s);return NULL;head=(STRU *)malloc(LEN);fscanf(fp,FORMATI);/此处数据读入head指针内if(n=1)head-next=NULL;else head-next=head;while (!feof(fp)p=(STRU *)malloc(LEN);if(fscanf(fp,FORMATI)=-1)break; /消除文件末尾的回车的影响 head=insert(head,p);fclose(fp);return head;将a,b链表(乱序)连成一个新链表(有序), 该函数依赖insert STRU *link(STRU *a,STRU *b) if(a=NULL&b=NULL)return NULL;STRU *p,*head=NULL;while(a!=NULL)p=a-next;head=insert(head,a);a=p;while(b!=NULL)p=b-next;head=insert(head,b);b=p;return head;打印链表void print(STRU *p) for(;p!=NULL;p=p-next)printf(FORMATO);将链表(单向or循环)存入硬盘, s为文件名int SaveList(STRU *head,char *s) FILE *fp;STRU *p=head;fp=fopen(s,w);if (fp=NULL)printf(cant open file %s,s);return -1;while(p)fprintf(fp, FORMATO);p=p-next;if(p=head)break;fclose(fp);return 0;将链表(单向or循环)倒序STRU *fv(STRU *head) if(head=NULL)return NULL;STRU *p1,*p2=NULL,*p3;p3=head;dop1=p2;p2=p3;p3=p3-next;p2-next=p1;while(p3!=head&p3!=NULL);p3-next=p2;return p2;链表(单向or循环)节点计数int f(STRU *head) if(head=NULL)return 0;int n;STRU *p;p=head;for(n=1;p!=NULL&p-next!=head;n+,p=p-next);return n;删除节点(单向or循环)STRU *del(STRU *head, int key)STRU *p1,*p2;p2=head-next;if(head-NUM=key)free(head);return p2;for(;p2!=NULL&p2!=head;p1=p2,p2=p2-next)if(p2-NUM=key)p1-next=p2-next;free(p2);return head;return head;日历计算#include /注:每周第一天为星期一int leap(int year) /判断闰年return (year%4=0&year%100!=0)|year%400=0);int mon(int month) /每月天数switch(month)case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31; case 4:case 6:case 9:case 11:return 30;case 2:return 28;return -1;int days(int year) /第year年之前天数int day,leaps,i;year-;leaps=year/400;day=year*365+leaps*97;for(i=1;i=year%400;i+)if(leap(i)day+;return day;int main(void)int year,month,date,day=0,w,d,x=0,i;char *str7=星期天,星期一,星期二,星期三,星期四,星期五,星期六;printf(请输入日期(年,月,日):);scanf(%d,%d,%d,&year,&month,&date);for(i=1;i2);day=d+days(year);x=day%7;w=(int)(day/7)+1-(int)(days(year)/7);if(x=0)w-=1; printf(第%d天,第%d周,%sn,d,w,strx);return 0;计算日期的差值#include typedef structint year;int month;int day;DATE;int leap(int year) /判断闰年return (year%4=0&year%100!=0)|year%400=0);int mon(int month) /每月天数switch(month)case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;case 4:case 6:case 9:case 11:return 30;case 2:return 28;return -1;int days(int year) /第year年之前天数int day,leaps,i;year-;leaps=year/400;day=year*365+leaps*97;for(i=1;i=year%400;i+)if(leap(i)day+;return day;int ds(DATE *d) /公元元年到DATE的天数int day=0,i;for(i=1;imonth;i+)day+=mon(i);day+=d-day+(leap(d-year)&(d-month2);day+=days(d-year);return day;int dayd(DATE *d1,DATE *d2)return(ds(d2)-ds(d1);int main(void)DATE d1,d2;int dif;printf(请输入日期1(年 月 日):);scanf(%d %d %d,&d1.year,&d1.month,&d1.day);printf(请输入日期2(年 月 日):);scanf(%d %d %d,&d2.year,&d2.month,&d2.day);dif=dayd(&d1,&d2);if(d1.month1|d2.month1|d1.day1|d2.day1)printf(输入错误n);printf(相差%d天。n,dif);return 0;代数表达式的计算(计算含有x,+,-,*./和数字的字符串的值;l,r分别为字符串左右界,x为实数x的值)double f(char *l, char *r, double x)if (l=r)if (*l=x) return x;else return (double)*l-0;if(*l=(&*r=)return f(l+1,r-1,x);int w=0;char *i;for (w=0,i=l+1;il;i-) /从右往左扫描,判断减法if (w=0&*i=-)return f(l,i-1,x)-f(i+1,r,x);if(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论