声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

楼主: 风花雪月

[C/C++] [分享]c语言 ---- 经典百例[zz]

[复制链接]
发表于 2006-3-7 15:30 | 显示全部楼层

平方根.c

<P>#define Epsilon 1.0E-6  /*控制解的精度*/ <BR>#include &lt;stdio.h&gt;<BR>#include &lt;math.h&gt; </P>
<P>main() <BR>{<BR> float num,pre,this; <BR>  do <BR> {   <BR>   scanf("%f",&amp;num);/*输入要求平方根的数*/ <BR> }while(num&lt;0); <BR> if (num==0) <BR>   printf("the root is 0"); <BR> else <BR> {<BR>  this=1; <BR>    do<BR>    { <BR>     pre=this;<BR>      this=(pre+num/pre)/2; <BR>  }while(fabs(pre-this)&gt;Epsilon);/*用解的精度,控制循环次数,fabs()是求绝对值的函数*/ <BR> } <BR> printf("the root is %f",this); <BR>} </P>
回复 支持 反对
分享到:

使用道具 举报

发表于 2006-3-7 15:32 | 显示全部楼层

圆周率.c

#include&lt;stdio.h&gt;<BR>long a=10000,b,c=2800,d,e,f[ 2801 ],g; <BR>main()<BR>{<BR> for(;b-c;)<BR>  f[ b++ ]=a/5; <BR> for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a) <BR>  for(b=c;d+=f[ b ]*a,f[ b ]=d%--g,d/=g--,--b;d*=b);<BR>}
发表于 2006-3-7 15:33 | 显示全部楼层

灯塔问题.c

//灯塔问题<BR>#include &lt;iostream.h&gt;<BR>#include &lt;fstream.h&gt;<BR>#include &lt;conio.h&gt;<BR>int sz[11][11],cf=1,k,n,a[20],b[20],c[20];<BR>void shuru(void);<BR>void shuchu(void);<BR>bool panduan(void);<BR>void goujian(void);<BR>void main()<BR>{<BR>    int i,j,lj=0,d;<BR>    shuru();<BR>for(i=1;i&lt;=n;i++)cf=cf*2;<BR>for(i=0;i&lt;cf;i++)<BR>{<BR>    d=i;<BR>    for(j=1;j&lt;=n;j++)<BR>    {sz[n][n-j+1]=d%2;d=d/2;}<BR>    goujian();<BR>    if(panduan()==true){lj=lj+1;shuchu();}<BR>}<BR>cout&lt;&lt;"共有"&lt;&lt;lj&lt;&lt;"种情况"&lt;&lt;endl;<BR>getch();<BR>}<BR>void goujian(void)<BR>{<BR>    int i1,j1;<BR>    for(i1=n-1;i1&gt;0;i1--)<BR>    {<BR>for(j1=1;j1&lt;=i1;j1++)<BR>{<BR>    if(sz[i1+1][j1]==1&amp;&amp;sz[i1+1][j1]==1)<BR>        sz[i1][j1]=0;<BR>    if(sz[i1+1][j1]==0&amp;&amp;sz[i1+1][j1+1]==0)<BR>        sz[i1][j1]=0;<BR>    if(sz[i1+1][j1]==1&amp;&amp;sz[i1+1][j1+1]==0)<BR>        sz[i1][j1]=1;<BR>    if(sz[i1+1][j1]==0&amp;&amp;sz[i1+1][j1+1]==1)<BR>        sz[i1][j1]=1;<BR>}<BR>}<BR>}<BR>bool panduan()<BR>{<BR>    int pd=1,j1;<BR>for(j1=1;j1&lt;=k;j1++)<BR>    if(sz[a[j1]][b[j1]]!=c[j1]) pd=0;<BR>if(pd==0) return false;else return true;<BR>}<BR>void shuchu(void)<BR>{<BR>    int i2,j2;<BR>for(i2=1;i2&lt;=n;i2++)<BR>{<BR>    for(j2=1;j2&lt;=n-i2;j2++) cout&lt;&lt;" ";<BR>for(j2=1;j2&lt;=i2;j2++) cout&lt;&lt;sz[i2][j2]&lt;&lt;" ";<BR>cout&lt;&lt;endl;<BR>    }<BR>cout&lt;&lt;endl;<BR>}<BR>void shuru(void)<BR>{<BR>//    char filename[18];<BR>ifstream input;<BR>// cout&lt;&lt;"Input filename:";<BR>// cin&gt;&gt;filename;<BR>// input.open(filename);<BR>input.open("dt.txt");<BR>input&gt;&gt;n;<BR>k=0;<BR>do{<BR>    k++;<BR>input&gt;&gt;a[k]&gt;&gt;b[k]&gt;&gt;c[k];<BR>}while((a[k]!=0)&amp;&amp;(b[k]!=0));<BR>k--;<BR>}
发表于 2006-3-7 15:34 | 显示全部楼层

16进制10进制.c

<P>//返回16进制字符串s对应的整数值,遇到任何一个非法字符都返回-1。<BR>int HexToDec(char *s)<BR>{<BR> char *p = s;</P>
<P> //空串返回0。<BR> if(*p == '\0')<BR>  return 0;<BR> <BR> //忽略开头的'0'字符<BR> while(*p == '0')<BR>  p++;</P>
<P> int dec = 0;<BR> char c;</P>
<P> //循环直到字符串结束。<BR> while(c = *p++)<BR> {<BR>  //dec乘16<BR>  dec &lt;&lt;= 4;<BR>  <BR>  //数字字符。<BR>  if(c &gt;= '0' &amp;&amp; c &lt;= '9')<BR>  {<BR>   dec += c - '0';<BR>   continue;<BR>  }</P>
<P>  //小写abcdef。<BR>  if(c &gt;= 'a' &amp;&amp; c &lt;= 'f')<BR>  {<BR>   dec += c - 'a' + 10;<BR>   continue;<BR>  }</P>
<P>  //大写ABCDEF。<BR>  if(c &gt;= 'A' &amp;&amp; c &lt;= 'F')<BR>  {<BR>   dec += c - 'A' + 10;<BR>   continue;<BR>  }</P>
<P>  //没有从任何一个if语句中结束,说明遇到了非法字符。<BR>  return -1;<BR> }</P>
<P> //正常结束循环,返回10进制整数值。<BR> return dec;<BR>}<BR></P>
发表于 2006-3-7 15:35 | 显示全部楼层

矩阵转换.c

<P>void trans(int *p,int n)<BR>{<BR> int i,j,temp;<BR> int *pi,*pj;<BR> for(i=0;i&lt;=n-1;i++)<BR> {<BR>  for(j=0;j&lt;=i;j++)<BR>      {<BR>   pi =p +i*n;  /*p首地址 */<BR>   pj =p +j*n;<BR>   temp=pi[j];<BR>   pi[j]=pj;<BR>   pj=temp;<BR>      }</P>
<P> }<BR> return;<BR>}</P>
<P>main()<BR>{<BR> int a[4][4]={ 1, 2, 3, 4,<BR>        5, 6, 7, 8,<BR>        9, 10,11,12,<BR>        13,14,15,16};<BR> int i,j;<BR>     printf("before transform:\n");<BR>     for(i=0;i&lt;=3;i++)<BR>     {<BR>      for(j=0;j&lt;=3;j++)<BR>       printf("  %d",a[j]);<BR>   printf("\n");<BR>       }<BR>     trans((int*)a,4);<BR>     <BR>     printf("after transform:\n");<BR>     for(i=0;i&lt;=3;i++)<BR>       {<BR>        for(j=0;j&lt;=3;j++)<BR>         printf("  %d",a[j]);<BR>    printf("\n");<BR>       }<BR>     return;<BR>}</P>
发表于 2006-3-7 15:35 | 显示全部楼层

杨辉三角形.c

<P>#include &lt;stdio.h&gt;<BR>int c(x,y);<BR>main()<BR>{<BR>int i,j,n=13;<BR>printf("N=");<BR>while(n&gt;12)<BR>scanf("%d",&amp;n);<BR>for(i=0;i&lt;=n;i++)<BR>{<BR>for(j=0;j&lt;12-i;j++)<BR>printf(" ");<BR>for(j=1;j&lt;i+2;j++)<BR>printf("%6d",c(i,j));<BR>printf("\n");<BR>}<BR>}</P>
<P>int c(x,y)<BR>int x,y;<BR>{<BR>int z;<BR>if((y==1)||(y==x+1))return(1);<BR>z=c(x-1,y-1)+c(x-1,y);</P>
<P>return(z);<BR>}</P>
发表于 2006-3-7 15:36 | 显示全部楼层

数组操作.c

<P>#include &lt;stdio.h&gt;  </P>
<P>void main()<BR>{<BR>   char strg[40],*there,one,two;<BR>   int *pt,list[100],index;</P>
<P>   strcpy(strg,"This is a character string.");</P>
<P>   one = strg[0];                    /* one 和 two是相同的 */<BR>   two = *strg;<BR>   printf("第一输出的是 %c %c\n",one,two);</P>
<P>   one = strg[8];                   <BR>   two = *(strg+8);<BR>   printf("第二输出的是 %c %c\n",one,two);</P>
<P>   there = strg+10;           /* strg+10 等同于 strg[10] */<BR>   printf("第三输出的是 %c\n",strg[10]);<BR>   printf("第四输出的是 %c\n",*there);</P>
<P>   for (index = 0;index &lt; 100;index++)<BR>      list[index] = index + 100;<BR>   pt = list + 27;<BR>   printf("第五输出的是 %d\n",list[27]);<BR>   printf("第六输出的是 %d\n",*pt);<BR>}<BR></P>
发表于 2006-3-7 15:36 | 显示全部楼层

桶排序.c

<P>#include&lt;stdio.h&gt;</P>
<P>void comp(int k[],int m,int l)<BR>{<BR> int i=10,j=0,z=1,y=1,x,w,b[500][10];<BR> for(w=0;w&lt;m;w++)<BR>  for(x=0;x&lt;10;x++)<BR>  {<BR>   b[w][x]=-1;<BR>  }<BR> while(z&gt;0)<BR> {<BR>  z=l/i;<BR>  i=i*10;<BR>  ++j;  //记录最大数的位数<BR> }<BR> i=10;<BR> while(j&gt;0)<BR> {<BR>  for(z=0;z&lt;=m;z++)<BR>  {<BR>   x=(k[z]/y)%i;<BR>   b[z][x]=k[z];<BR>  }<BR>  w=0;<BR>  for(z=0;z&lt;10;z++)<BR>   for(x=0;x&lt;m;x++)<BR>   {<BR>    if(b[x][z]&gt;=0)<BR>    {<BR>     k[w]=b[x][z];<BR>     b[x][z]=-1;<BR>     w++;<BR>    }<BR>   }<BR>  --j;<BR>  y=y*10;<BR> }<BR> for(z=0;z&lt;m;z++)<BR> {<BR>  printf("%d ",k[z]);<BR> }<BR>}</P>
<P>main()<BR>{<BR> int n,m=0,l=0;<BR> int a[500];<BR> printf("请输入正整数,负数表示结束:");<BR> scanf("%d",&amp;n);<BR> while(n&gt;=0)<BR> {<BR>  a[m]=n;<BR>  ++m;<BR>  if(n&gt;l)<BR>   l=n;//记录最大数<BR>  scanf("%d",&amp;n);<BR> }</P>
<P> comp(a,m,l);<BR>}</P>
发表于 2006-3-7 15:37 | 显示全部楼层

读写文本文件.c

<P>#include "stdio.h"<BR>void main()<BR>{<BR>   FILE *funny,*printer,*fp;<BR>   char c;</P>
<P>   funny = fopen("TENLINES.TXT","r"); /*打开文本文件 */<BR>   printer = fopen("PRN","w");        /*开启打印机*/<BR>   fp = fopen("weew.TXT","w");<BR>   do{<BR>     c = getc(funny);    /* 从文件中得到一个字符 */<BR>     if(c != EOF)<BR>     {<BR>       putchar(c);      //屏幕上显示字符<BR>       putc(c,printer); // 打印机上打印字符<BR>     <BR>     }<BR>   }while (c != EOF);    /*重复直到 EOF (end of file)  */<BR>   fprintf(fp,"%s",*funny);<BR>   fclose(funny);<BR>   fclose(printer);<BR>   fclose(fp);//关闭打印机<BR>}<BR></P>
发表于 2006-3-7 15:38 | 显示全部楼层

文件复制.c

<P>#include&lt;stdio.h&gt;<BR>#include&lt;stdlib.h&gt;</P>
<P>void main(int argc,char *argv[])<BR>{<BR>    FILE *in,*out;</P>
<P>    if(argc!=3){<BR>        printf("\n Usage: Hcopy sourcefile targetfile.\n");<BR>        exit(1);<BR>    }<BR>    if((in=fopen(argv[1],"rb"))==NULL){<BR>        printf("\n Cannot open the source file.\n");<BR>        exit(2);<BR>    }<BR>    if((out=fopen(argv[2],"wb"))==NULL){<BR>        printf("\n Cannot open the targetfile.\n");<BR>        exit(3);<BR>    }</P>
<P>    /* start copy */<BR>    while(!feof(in))<BR>        putc(getc(in),out);</P>
<P>    fclose(in);<BR>    fclose(out);<BR>}</P>
发表于 2006-3-7 15:38 | 显示全部楼层

文件加密.c

<P>给文件加密的技术很多,其中又分为不同等级,以适合不同场合的需要.这里给出最简单的文件加密技术,即采用文件逐字节与密码异或方式对文件进行加密,当解密时,只需再运行一遍加密程序即可.</P>
<P>下面是一个实例程序,能对任意一个文件进行加密,密码要求用户输入,限8位以内(当然你可以再更改).程序有很好的容错设计,这是我们应该学习的.</P>
<P>/* Turbo 2.0 pass. give file a password! */</P>
<P>#include&lt;stdio.h&gt;<BR>#include&lt;stdlib.h&gt;<BR>#include&lt;conio.h&gt;<BR>#include&lt;string.h&gt;</P>
<P>void dofile(char *in_fname,char *pwd,char *out_fname);/*对文件进行加密的具体函数*/</P>
<P>void main(int argc,char *argv[])/*定义main()函数的命令行参数*/<BR>{<BR>    char in_fname[30];/*用户输入的要加密的文件名*/<BR>    char out_fname[30];<BR>    char pwd[8];/*用来保存密码*/</P>
<P>    if(argc!=4){/*容错处理*/<BR>    printf("\nIn-fname:\n");<BR>    gets(in_fname);/*得到要加密的文件名*/</P>
<P>    printf("Password:\n");<BR>    gets(pwd);/*得到密码*/</P>
<P>        printf("Out-file:\n");<BR>        gets(out_fname);/*得到加密后你要的文件名*/<BR>        </P>
<P>dofile(in_fname,pwd,out_fname);<BR>      }<BR>    else{/*如果命令行参数正确,便直接运行程序*/<BR>        strcpy(in_fname,argv[1]);<BR>        strcpy(pwd,argv[2]);<BR>        strcpy(out_fname,argv[3]);<BR>        dofile(in_fname,pwd,out_fname);<BR>    }</P>
<P>}</P>
<P><BR>/*加密子函数开始*/<BR>void dofile(char *in_fname,char *pwd,char *out_file)<BR>{<BR>    FILE *fp1,*fp2;<BR>    register char ch;<BR>    int j=0;<BR>    int j0=0;</P>
<P>    fp1=fopen(in_fname,"rb");<BR>    if(fp1==NULL){<BR>    printf("cannot open in-file.\n");<BR>    exit(1);/*如果不能打开要加密的文件,便退出程序*/<BR>    }<BR>    fp2=fopen(out_file,"wb");<BR>    if(fp2==NULL){<BR>    printf("cannot open or create out-file.\n");<BR>    exit(1);/*如果不能建立加密后的文件,便退出*/<BR>    }<BR>    while(pwd[++j0]);<BR>    ch=fgetc(fp1);</P>
<P>/*加密算法开始*/<BR>    while(!feof(fp1)){<BR>    fputc(ch^pwd[j&gt;=j0?j=0:j++],fp2);/*异或后写入fp2文件*/<BR>    ch=fgetc(fp1);<BR>    }<BR>    fclose(fp1);/*关闭源文件*/<BR>    fclose(fp2);/*关闭目标文件*/<BR>}</P>
<P>/*程序结束*/</P>
发表于 2006-3-7 15:39 | 显示全部楼层

文件连接.c

<P>/****************fcat.c***************/</P>
<P>#include&lt;stdio.h&gt;<BR>#include&lt;stdlib.h&gt;<BR>#define BUFSIZE 256</P>
<P>void main(int argc,char *argv[])<BR>{<BR>    int i;<BR>    char buff[BUFSIZE];<BR>    FILE *fp1,*fp2;</P>
<P>    if(argc==1){<BR>        printf("Rsage: fcat filename linke_fname[link_fname...].");<BR>        printf("\n");<BR>        exit(1);<BR>    }<BR>    if((fp1=fopen (argv[1],"a"))==NULL){<BR>        printf("file %s cannot opened.\n",argv[1]);<BR>        exit(1);<BR>    }<BR>    for(i=2;i&lt;argc;i++){<BR>        if((fp2=fopen(argv,"r"))==NULL){<BR>            printf("file %s cannot opened.\n");<BR>            exit(1);<BR>        }<BR>        while(fgets(buff,BUFSIZE,fp2)!=NULL)<BR>            fputs(buff,fp1);<BR>        fclose(fp2);<BR>    }<BR>    fclose(fp1);<BR>}</P>
发表于 2006-3-7 15:40 | 显示全部楼层

网络最短路径Dijkstra算法.c

<P>/*<BR>* File: shortest.c<BR>* Description: 网络中两点最短路径 Dijkstra 算法<BR>*              Shortest Path Dijkstra Algorithm<BR>* Created: 2001/11/25<BR>* Author: Justin Hou [mailtjustin_hou@hotmail.com]<BR>*/</P>
<P>#include &lt;stdio.h&gt;<BR>#define true  1<BR>#define false 0<BR>#define I  9999                                /* 无穷大        */<BR>#define N  20                                  /* 城市顶点的数目 */</P>
<P>int cost[N][N] = {<BR>    {0,3,I,I,I,1,I,I,I,I,I,I,I,I,I,I,I,I,I,I},<BR>    {3,0,5,I,I,I,6,I,I,I,I,I,I,I,I,I,I,I,I,I},<BR>    {I,5,0,4,I,I,I,1,I,I,I,I,I,I,I,I,I,I,I,I},<BR>    {I,I,4,0,2,I,I,I,6,I,I,I,I,I,I,I,I,I,I,I},<BR>    {I,I,I,2,0,I,I,I,I,7,I,I,I,I,I,I,I,I,I,I},<BR>    {1,I,I,I,I,0,1,I,I,I,2,I,I,I,I,I,I,I,I,I},<BR>    {I,6,I,I,I,1,0,6,I,I,I,7,I,I,I,I,I,I,I,I},<BR>    {I,I,1,I,I,I,6,0,2,I,I,I,3,I,I,I,I,I,I,I},<BR>    {I,I,I,6,I,I,I,2,0,8,I,I,I,4,I,I,I,I,I,I},<BR>    {I,I,I,I,7,I,I,I,8,0,I,I,I,I,5,I,I,I,I,I},<BR>    {I,I,I,I,I,2,I,I,I,I,0,4,I,I,I,3,I,I,I,I},<BR>    {I,I,I,I,I,I,7,I,I,I,4,0,3,I,I,I,4,I,I,I},<BR>    {I,I,I,I,I,I,I,3,I,I,I,3,0,3,I,I,I,5,I,I},<BR>    {I,I,I,I,I,I,I,I,4,I,I,I,3,0,7,I,I,I,2,I},<BR>    {I,I,I,I,I,I,I,I,I,5,I,I,I,7,0,I,I,I,I,3},<BR>    {I,I,I,I,I,I,I,I,I,I,3,I,I,I,I,0,5,I,I,I},<BR>    {I,I,I,I,I,I,I,I,I,I,I,4,I,I,I,5,0,8,I,I},<BR>    {I,I,I,I,I,I,I,I,I,I,I,I,5,I,I,I,8,0,6,I},<BR>    {I,I,I,I,I,I,I,I,I,I,I,I,I,2,I,I,I,6,0,4},<BR>    {I,I,I,I,I,I,I,I,I,I,I,I,I,I,3,I,I,I,4,0}<BR>};<BR>int dist[N];                                          /* 存储当前最短路径长度 */<BR>int v0 = 'A' - 65;                                    /* 初始点是 A          */</P>
<P>void main()<BR>{<BR>    int final[N], i, v, w, min;</P>
<P>    /* 初始化最短路径长度数据,所有数据都不是最终数据 */<BR>    for (v = 0; v &lt; N; v++) {<BR>    final[v] = false;<BR>        dist[v] = cost[v0][v];<BR>    }</P>
<P>    /* 首先选v0到v0的距离一定最短,最终数据 */<BR>    final[v0] = true;</P>
<P>    /* 寻找另外 N-1 个结点 */<BR>    for (i = 0; i &lt; N-1; i++) {<BR>        min = I;                                      /* 初始最短长度无穷大  */<BR>        <BR>        /* 寻找最短的边 */<BR>        for (w = 0; w &lt; N; w++) {<BR>            if (!final[w] &amp;&amp; dist[w] &lt; min) {<BR>                min = dist[w];<BR>                v = w;<BR>        }<BR>        }<BR>        final[v] = true;                              /* 加入新边          */</P>
<P>        for (w = 0; w &lt; N; w++) {                      /* 更新 dist[] 数据  */<BR>            if (!final[w] &amp;&amp; dist[v] + cost[v][w] &lt; dist[w]) {<BR>                dist[w] = dist[v] + cost[v][w];<BR>            }<BR>        }<BR>    }</P>
<P>    for (i = 0; i &lt; N; i++) {                          /* 显示到监视器      */<BR>        printf("%c-&gt;%c: %2d\t", v0 + 65, i + 65, dist);<BR>    }<BR>}</P>
发表于 2006-3-7 15:40 | 显示全部楼层

矩阵乘法动态规划.c

<P>/*<BR>* File:        multi.c<BR>* Description:  矩阵乘法动态规划<BR>* Created:      10:20 2001-12-3<BR>* Author:      Justin Hou [mailtjustin_hou@hotmail.com]<BR>*<BR>*/</P>
<P>#include &lt;stdio.h&gt;<BR>#define  N  7</P>
<P>int middle[N][N];</P>
<P>void Show(int, int);</P>
<P>void main()<BR>{<BR>        int i, j, l, k;<BR>        unsigned long m[N+1][N+1], min;<BR>        int r[N+1] = {10, 20, 50, 1, 100, 4, 20, 2};                            /* 矩阵维数 */</P>
<P>        /* 初始化 */<BR>        for (i = 1; i &lt;= N; i++) {<BR>                m = 0;<BR>        }<BR>        /* 每此增量加一 */<BR>        for (l = 1; l &lt; N; l++) {</P>
<P>                /* 对于差值为 l 的两个元素 */<BR>                for (i = 1; i &lt;= N - l; i++) {<BR>                        j = i + l; </P>
<P>                        /* 求其最小组合方式 */<BR>                        min = m + m[i+1][j] + r[i-1] * r * r[j];<BR>                        middle[j] = i;<BR>                        for (k = i + 1; k &lt; j; k++) {<BR>                                if (min &gt; m[k] + m[k+1][j] + r[i-1] * r[k] * r[j]) {<BR>                                        min = m[k] + m[k+1][j] + r[i-1] * r[k] * r[j];<BR>                                        middle[j] = k;<BR>                                }<BR>                        }<BR>                        m[j] = min;<BR>                }<BR>        }<BR>        printf("M = ");<BR>        Show(1, N);<BR>        printf("\nMultiply Count: %d\n", m[1][N]);<BR>}<BR>                        <BR>void Show(int i, int j)<BR>{<BR>        int k, m;</P>
<P>        if (i == j){<BR>                printf("M%d", i);                              /* 如果下一个是矩阵,输出  */<BR>        }<BR>        else {<BR>                m = middle[j];                              /* 分割成左右两组          */<BR>                if (i != m) printf("(");                        /* 如果下一个显示的不是矩阵 */<BR>                Show(i, m);                                    /* 显示左边的内容          */<BR>                if (i != m) printf(")");                        /* 如果上一个显示的不是矩阵 */<BR>                printf(" x ");                                  /* 打印乘法符号            */<BR>                if (m+1 != j) printf("(");                      /* 如果下一个显示的不是矩阵 */<BR>                Show(m + 1, j);                                /* 显示右边的内容          */<BR>                if (m+1 != j) printf(")");                      /* 如果下一个显示的不是矩阵 */<BR>        }</P>
<P>}</P>
发表于 2006-3-7 15:41 | 显示全部楼层

万年历.c

<P>#include "stdio.h"        /* Required for MS-DOS use */ <BR>#define ENTER   0x1C0D  /* Enter key */ <BR>int year, month, day; <BR>static char *days[8] = {"         ","Sunday   ","Monday      ","Tuesday  ", <BR>                        "Wednesday","Thursday ","Friday   ","Saturday "}; <BR>struct TIMEDATE { <BR>        int year;       /* year 1980..2099              */ <BR>        int month;      /* month 1=Jan 2=Feb, etc.      */ <BR>        int day;        /* day of month 0..31           */ <BR>        int hours;      /* hour 0..23                   */ <BR>        int minutes;    /* minute 0..59                 */ <BR>        int seconds;    /* second 0..59                 */ <BR>        int hsecs;      /* 1/100ths of second 0..99     */ <BR>        char dateline[47];      /* date &amp; time together */ <BR>        }; <BR>static struct TIMEDATE today; <BR>main() <BR> { <BR>  char cmonth[3]; <BR>  char cday[3]; <BR>  char cyear[5]; <BR>  double getdays(); <BR>  double daynumb, numbnow; <BR>  int weekday, retcode, dayer, i; <BR>  dayer = datetime(&amp;today); <BR>  clrscn(); <BR>  for (i=0;i&lt;3;++i)cmonth='\0'; <BR>  for (i=0;i&lt;3;++i)cday='\0'; <BR>  for (i=0;i&lt;5;++i)cyear='\0'; <BR>  putstr(5,8,14,"Enter date in MM DD YYYY format:"); <BR>  while (retcode != ENTER) <BR>    { <BR>     retcode = bufinp(5,41,13,2,cmonth); <BR>     if (retcode != ENTER) retcode = bufinp(5,44,13,2,cday); <BR>     if (retcode != ENTER) retcode = bufinp(5,47,13,4,cyear); <BR>    } <BR>  year = atoi(&amp;cyear); <BR>  month = atoi(&amp;cmonth); <BR>  day = atoi(&amp;cday); <BR>  daynumb = getdays(year, month, day); <BR>  numbnow = getdays(today.year, today.month, today.day); <BR>  weekday = weekdays(daynumb); <BR>  if (numbnow - daynumb == 0) <BR>      printf("\n\n%02d-%02d-%d is",month, day, year); <BR>  if (numbnow - daynumb &gt; 0) <BR>      printf("\n\n%02d-%02d-%d was",month, day, year); <BR>  if (numbnow - daynumb &lt; 0) <BR>      printf("\n\n%02d-%02d-%d will be",month, day, year); <BR>  printf(" a %s\n",days[weekday]); <BR> }  /* end MAIN */ <BR>/************************************************************ <BR> * GETDAYS  - From integer values of year (YYYY), month     * <BR> *            (MM) and day (DD) this subroutine returns a   * <BR> *            double float number which represents the      * <BR> *            number of days since Jan 1, 1980 (day 1).     * <BR> *            This routine is the opposite of GETDATE.      * <BR> ************************************************************/ <BR> double getdays(year, month, day) <BR>  int year, month, day; <BR>  { <BR>   int y,m; <BR>   double a,b,d, daynumb; <BR>   double floor(),intg(); <BR>   /********************************** <BR>   ** make correction for no year 0 ** <BR>   **********************************/ <BR>   if (year &lt; 0) y = year + 1; <BR>     else y = year; <BR>   /********************************************************* <BR>   ** Jan and Feb are months 13 and 14 in this calculation ** <BR>   *********************************************************/ <BR>   m = month; <BR>   if (month &lt; 3) <BR>     { <BR>       m = m + 12; <BR>       y = y - 1; <BR>     } <BR>   /************************** <BR>   ** calculate Julian days ** <BR>   **************************/ <BR>   d = floor(365.25 * y) + intg(30.6001 * (m + 1)) + day - 723244.0; <BR>   /********************************************** <BR>   ** use Julian calendar if before Oct 5, 1582 ** <BR>   **********************************************/ <BR>   if (d &lt; -145068.0) daynumb = d; <BR>   /************************************* <BR>   ** otherwise use Gregorian calendar ** <BR>   *************************************/ <BR>     else <BR>       { <BR>        a = floor(y / 100.0); <BR>        b = 2 - a + floor(a / 4.0); <BR>        daynumb = d + b; <BR>       } <BR>   return(daynumb); <BR>   } /* end GETDAYS */ <BR>/******************************************************** <BR> * GETDATE - This routine takes a double float number   * <BR> *          representing the number of days since Jan 1,* <BR> *          1980 (day 1) and returns the year month and * <BR> *          day as pointer integers                     * <BR> *          This routine is the opposite of GETDAYS     * <BR> ********************************************************/ <BR> getdate(numb) <BR>   double numb; <BR>   { <BR>    double a,aa,b,c,d,e,z; <BR>    double date; <BR>  <BR>    date = numb; <BR>    z = intg(date + 2444239.0); <BR>    if (date &lt; -145078.0) a = z; <BR>      else <BR>        { <BR>         aa = floor((z - 1867216.25) / 36524.25); <BR>         a = z + 1 + aa - floor(aa/4.0); <BR>        } <BR>    b = a + 1524.0; <BR>    c = intg((b - 122.1) / 365.25); <BR>    d = intg(365.25 * c); <BR>    e = intg((b - d) / 30.6001); <BR>   day = b - d - intg(30.6001 * e); <BR>   if (e &gt; 13.5) month = e - 13.0; <BR>     else month = e - 1.0; <BR>   if (month &gt; 2) year = c - 4716.0; <BR>     else year = c - 4715.0; <BR>   if (year &lt; 1) --year; <BR>    return; <BR>   } /* end GETDATE */ <BR>/******************************************************** <BR> * WEEKDAYS - This routine takes a double float number  * <BR> *          representing the number of days since Jan 1,* <BR> *          1980 (day 1) and returns the day of the week* <BR> *          where 1 = Sunday, 2 = Tuesday, etc.         * <BR> ********************************************************/ <BR> int weekdays(numb) <BR>   double numb; <BR>   { <BR>    double dd; <BR>    int day; <BR>  <BR>    dd = numb; <BR>    while (dd &gt; 28000.0) dd = dd - 28000.0; <BR>    while (dd &lt; 0) dd = dd + 28000.0; <BR>    day = dd; <BR>    day = ((day + 1) % 7) + 1; <BR>    return(day); <BR>   } <BR>/******************************************************** <BR> * FRACT -  This routine takes a double float number    * <BR> *          and returns the fractional part as a double * <BR> *          float number                                * <BR> ********************************************************/ <BR> double fract(numb) <BR>   double numb; <BR>   { <BR>    int inumb; <BR>    double fnumb; <BR>  <BR>    while (numb &lt; -32767) numb += 32767; <BR>    while (numb &gt; 32767) numb -= 32767; <BR>    inumb = numb; <BR>    fnumb = inumb; <BR>    return(numb-fnumb); <BR>   } /* end FRACT */ <BR>/******************************************************** <BR> * FLOOR -  This routine takes a double float number    * <BR> *          and returns the next smallest integer       * <BR> ********************************************************/ <BR> double floor(numb) <BR>   double numb; <BR>   { </P>
<P>    double fract(), intg(); <BR>    double out; <BR>    out = intg(numb); <BR>    if (numb &lt; 0 &amp;&amp; fract(numb) != 0) out -= 1.0; <BR>    return(out); <BR>   } /* end FLOOR */ <BR>/******************************************************** <BR> * INTG  -  This routine takes a double float number    * <BR> *          and returns the integer part as a double    * <BR> *          float number                                * <BR> ********************************************************/ <BR> double intg(numb) <BR>   double numb; <BR>   { <BR>    double fract(); <BR>    return(numb - fract(numb)); <BR>   } /* end INTG */</P>
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-13 10:31 , Processed in 0.065767 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表