随笔分类 -  C语言

摘要:1 /* 2 int main() 3 { 4 int c,nl; 5 nl=0; 6 while((c=getchar())!=EOF) 7 { 8 if(c=='\n') 9 ++nl;10 }11 printf("%d\n",nl);12 13 }*/14 15 16 #include<stdio.h>17 #include<stdlib.h>18 19 void main()20 {21 int c,nl,space_n;22 nl... 阅读全文
posted @ 2013-01-20 15:15 cpoint
摘要:VA_LIST 是在C语言中解决变参问题的一组宏他有这么几个成员1) va_list型变量:#ifdef _M_ALPHAtypedef struct { char *a0; /* pointer to first homed integer argument */ int offset; /* byte offset of next parameter */} va_list;#elsetypedef char * va_list;#endif2)_INTSIZEOF 宏,获取类型占用的空间长度,最小占用长度为int的整数倍:#define... 阅读全文
posted @ 2011-06-30 16:23 cpoint
摘要:/****scanf.c - read formatted data from stdin** Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.**Purpose:* defines scanf() - reads formatted data from stdin********************************************************************************/#include <cruntime.h>#include <st 阅读全文
posted @ 2011-06-30 15:43 cpoint
摘要:原子是一个指向唯一的、不可改变的0个或任意多个字节序列的指针。大多数原子都是指向以空字符结束的字符串,但是任何一个指向任意字节序列的指针都可以是原子。任何原子只可以出现一次。原子通常被当作数据结构中的关键字使用。Atom接口:(atom.h)#ifndef ATOM_INCLUDEED#define ATOM_INCLUDEEDextern int Atom_length(const char *str);extern const char *Atom_new(const char *str,int len);extern const char *Atom_string(const char 阅读全文
posted @ 2011-06-04 18:56 cpoint
摘要:一个模块由两部分组成:接口和实现。接口指明模块要做什么,它声明了使用该模块代码可用的标识符、类型和例程;实现指明模块是如何完成其接口声明的目标的。 在C语言中,接口在头文件声明,头文件的文件扩展名通常为.h。该头文件声明了客户调用程序可以使用的宏、类型、数据结构、变量以及例程。下面的例子说明了接口中使用的一些约定。接口的定义:arith.h1 extern int Arith_max(int x,int y);2 extern int Arith_min(int x,int y);3 extern int Arith_div(int x,int y);4 extern int Arith_m. 阅读全文
posted @ 2011-06-03 23:14 cpoint
摘要:最近在看David R. Hanson 的《C Interfaces and Implementations》,文中第一章提到了Literate Programming作者举了一个例子:功能:用于检测输入中相邻且相同的单词#include<stdio.h>#include<math.h>#include<errno.h>#include<stdlib.h>#include<ctype.h>#include<string.h>int linenum;int getword(FILE *,char *,int);void do 阅读全文
posted @ 2011-06-03 00:50 cpoint
摘要:int**malloc2d(intr,intc){inti;int**t=malloc(r*sizeof(int*));for(i=0;i<r;i++)t[i]=malloc(c*sizeof(int));returnt;}int**a=malloc2d(M,N)ps:矩阵相乘for(i=0;i<N;i++)for(j=0;j<N;j++)for(k=0,c[i][j]=0.0;k<N;k++) c[i][j]+=a[i][k]*b[k][j]; 阅读全文
posted @ 2011-05-10 22:18 cpoint
摘要:七种qsort排序方法<本文中排序都是采用的从小到大排序>一、对int类型数组排序int num[100];Sample:int cmp ( const void *a , const void *b ){return *(int *)a - *(int *)b;}qsort(num,100,sizeof(num[0]),cmp);二、对char类型数组排序(同int类型)char word[100];Sample:int cmp( const void *a , const void *b ){return *(char *)a - *(int *)b;}qsort(word,1 阅读全文
posted @ 2011-04-27 16:50 cpoint
摘要:#include<stdio.h>#include<stdlib.h>typedefstructnode*link;structnode{intitem;linknext;};intmain(){inti,N,M;scanf("%d%d",&N,&M);linkt=malloc(sizeof*t),x=t;t->item=1;t->next=t;for(i=2;i<=N;i++){x=(x->next=malloc(sizeof*x));x->item=i;x->next=t;}while(x!=x 阅读全文
posted @ 2011-04-26 15:46 cpoint
摘要:如果抛掷硬币N次,看到头像的期望值是N/2次,但实际值也可能是0~N次,以下程序进行M次试验,M和N都从命令行获取。它使用一个数组f来跟踪出现“i次头像”的概率,其中0<=i<=N。然后打印试验结果的柱状图。#include<stdlib.h>#include<stdio.h>intheads(){returnrand()<RAND_MAX/2;}main(intargc,char*argv[]){inti,j,cnt;intN=atoi(argv[1]),M=atoi(argv[2]);int*f=malloc((N+1)*sizeof(int)); 阅读全文
posted @ 2011-04-25 23:08 cpoint
摘要:若i为素数,则设置a[i]为1;反之则设置为0.首先,将所有数组的元素设置为1,表示没有已知的非素数。然后将已知为非素数(即为已知素数的倍数)的索引对应的数组元素设置为0。如果将所有较小的素数的倍数都设置为0之后,a[i]仍然保持为1,则可判断它是所找的素数。#include<stdio.h>#defineN10000intmain(){inti,j,a[N];for(i=2;i<N;i++)a[i]=1;for(i=2;i<N;i++)if(a[i])for(j=i;j<N/i;j++)a[i*j]=0;for(i=2;i<N;i++)if(a[i])pr 阅读全文
posted @ 2011-04-25 22:54 cpoint
摘要:C语言标准库函数C 语言中有大量的标准库函数,根据功能不同,声明于不同的头文件中。这些库函数在C++中也能使用。下面分类列举了一些C 语言常用库函数,由于篇幅所限,只列出函数名字及其作用。数学函数数学库函数声明在 math.h 中,主要有:abs(x) 求整型数x 的绝对值cos(x) x(弧度)的余弦fabs(x) 求浮点数x 的绝对值ceil(x) 求不小于x 的最小整数floor(x) 求不大于x 的最小整数log(x) 求x 的自然对数log10(x) 求x 的对数(底为10)pow(x, y) 求x 的y 次方sin(x) 求x(弧度)的正弦sqrt(x) 求x 的平方根字符处理函数 阅读全文
posted @ 2011-04-25 21:40 cpoint
摘要:方法一:形参给出第二维的长度。例如:#include<stdio.h>#include<math.h>#include<ctype.h>#include<string.h>#include<stdlib.h>voidfunc(intn,charstr[][5]){inti;for(i=0;i<n;i++)printf("\nstr[%d]=%s\n",i,str[i]);}intmain(){char*p[3];charstr[][5]={"abc","def",&qu 阅读全文
posted @ 2011-04-20 00:32 cpoint
摘要:原理: 在C语言中数组下标访问可以看成指针的偏移访问1、对表进行检查,看看它是否真的已满2、如果表确实已满,使用realloc()函数扩展表的长度,并进行检查,确保realloc()操作成功进行。3、在表中增加所需要的项目code:intcurrent_element=0;inttotal_element=128;char*dynamic=malloc(total_element);char*ptr;voidadd_element(charc){if(current_element==total_element-1){total_element*=2;ptr=(char*)realloc(dy 阅读全文
posted @ 2011-04-20 00:17 cpoint
摘要:#include<time.h>clock_tstart,end;doubleduration;start=clock();//dosomethingend=clock();duration=(double)(end-start)/CLOCKS_PER_SEC;//秒 阅读全文
posted @ 2011-04-13 22:44 cpoint
摘要:#include<stdio.h>#include<math.h>#define N 10void quicksort(int a[],int low,int high);int split(int a[],int low,int high);main(){ int a[N],i; printf("Enter %d numbers to be sorted: ",N); for(i=0;i<N;i++) { scanf("%d",&a[i]); } quicksort(a,0,N-1); printf("I 阅读全文
posted @ 2010-12-08 15:34 cpoint
摘要:一个是关于去除一个数组中的重复数问题:如定义一个int型数组:{1,1,3,5,6,3,6,9};要求写一个函数得到如下数据{1,3,5,6,9},即重复出现的数只保存第一次出现的那一个数据。源代码如下:代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-... 阅读全文
posted @ 2010-09-09 01:15 cpoint
摘要:问题提出:设x,y为非负整数,试计算集合M={2^x*3^y|x>=0,y>=0}的元素小于指定整数n的个数,并求这些元素从小到大排序的第m项。以下给出案例求解的三种设计:1.穷举求解设计要点:集合元素由2的幂与3的幂及其乘积组成,设元素从小到大排序的第k项为f(k)。显然,f(1)=1,f(2)=2。设置a从3开始递增至n循环,对每一个a(赋值给j),逐次试用2试商,然后逐次试用3试... 阅读全文
posted @ 2010-09-09 01:02 cpoint

浏览次数:travelocity promotion codes