位段结构的使用示例

位段结构也是一种结构体类型,只不过该类型含有以位作为单位定义存储长度的整数类型位段成员。在某些应用中,有些信息在存储时,并不需要使用一个完整的字节,而只是需要使用一个或者几个二进制位。采用位段结构,既节省存储空间,也方便操作。
在《编程之美》中的1.2中国象棋将帅问题中,采用位段结构来解决此题,很方便,效率也高。也利于理解。
中国象棋将帅问题描述:
假设棋盘中只有“将”和“帅”二子。设A为“将”,B为“帅”。A,B二子分别被限制在己方的3*3的格子运动。每一步A,B可以横向或竖向运动,但不能沿对角线运动。另外:A,B不能碰面,也就是说, A,B不能在同一条竖线上。编写一个程序,输出A,B所有合法位置。求代码中只能使用一个变量
当采用位段结构来解决这个问题的时候,代码如下:

struct {
		unsigned char a:4;
		unsigned char b:4;
	}i;
	for(i.a=1;i.a< =9;i.a++)
		for(i.b=1;i.b<=9;i.b++)
			if(i.a%3!=i.b%3)
				printf("A=%d , B=%dn",i.a,i.b);

可以看到解决方案很清晰,也易懂。
关于此题的其他解决方案,请参考《编程之美》

getc 和 _fillbuf

转载自:http://hi.baidu.com/beyoniger/blog/item/bc28e9075606c7ce7a894766.html

#define getc(p) (–(p)->cnt >= 0 ? (unsigned char) *(p)->ptr++ : _fillbuf(p))
自己的理解:
_fillbuf(FILE *fp) 的功能是,
如果还没有给流(*FILE)分配缓存,需要给它分配缓存;
调用 read 系统调用,一次读入 bufsize 个字符到流(*FILE)的缓存中,供 getc、getchar、fgetc 等函数逐步读取;
当缓存中的字符被读完时,_fillbuf 再次读入 bufsize 个字符到流(*FILE)的缓存中。

<C程序设计语言>习题:5-10

题目描述:编写程序expr,计算从命令行输入的逆波兰表达式的值,其中每个运算符或操作符用一个单独的参数出现.
例如:expr 2 3 4 + * 用于计算(3+4)*2的值

#include <stdio .h>
#include <stdlib .h>
#include <ctype .h>
#define BUFSIZE 100
 
int stack[BUFSIZE];
int sp=0;
 
void push(int c)
{
	if(sp<bufsize )
		stack[sp++]=c;
	else
		printf("Error:stack full ,can't push %c n",c);
}
 
int pop(void)
{
	if(sp>0)
		return stack[--sp];
	else
	{
		printf("Error: stack empty n");
		return 0;
	}
}
 
void main(int argc , char* argv[])
{
	int i=1;
	int op2;
	if(argc<2)
	{
		printf("Usage: expr [parameter...] ");
	}
	while((--argc>0)&&((*++argv)[0]!='#'))
	{
		if(isdigit((*argv)[0]))
		{
			push(atoi(*argv));
		}
		else
		{
			switch((*argv)[0])
			{
			case '+' : push(pop()+pop()); break;
			case '-' :
				op2 = pop();
				push(pop()-op2);
				break;
			case '*' : push(pop()*pop()); break;
			case '/' :
				op2 = pop();
				if(op2!=0)
				{
					push(pop()/op2);
				}
				break;
			}
		}
		i++;
	}
	printf("result = %d",stack[0]);
	system("pause");
}
 
</bufsize></ctype></stdlib></stdio>

getch()和getchar()区别

只这么一句话,我想就可以理解了 :
当缓冲区不空时,getch()从缓冲区内获取字符,当缓冲区为空的时候,getch()函数从调用getchar()函数直接从输入中读取字符.

<c语言程序设计>习题2-3

题目:16进制到十进制转化

#include <stdio .h>
#include <stdlib .h>
 
int htoi(char s[])
{
	bool checkChar(char s);
	int i=0;
	long n=0 ;
	if(s[i]==0)
	{
		++i;
		if(s[i]=='x'||s[i]=='X')
			++i;
	}
	for(i;checkChar(s[i]);i++)
	{
		if(s[i]>='0'&&s[i]< ='9')
		{
			n = n*16+s[i]-'0';
		}
		if(s[i]>='a'&&s[i]< ='f')
		{
			n = n*16+s[i]-'a'+10;
		}
		if(s[i]>='A'&&s[i]< ='F')
		{
			n = n*16+s[i]-'a'+10;
		}
	}
}
 
	return n;
}
bool checkChar(char s)
{
	int temp = (int)s;
	if((temp>=48&&temp< =57)||(temp>=65&&temp< =70)||(temp>=97&&temp< =102))
	{
		return true;
	}
	else
		return false;
}
void main(int argc , char* argv[])
{
	char numstr[100];
	long number=0;
	gets(numstr);
 
	number=htoi(numstr);
	printf("16jinzhi: %s convert to 10jinzhi:%dn",numstr,number);
	system("pause");
}

习题1-13(改造)

原题:编写一个程序,打印输入中单词长度的脂肪图.
改造:编写一个程序,统计长度为x的单词的个数.

#include <stdio .h>
#include <stdlib .h>
void main(int argc , char* argv[])
{
	int  nspace = 0;
	int  count =0 ;
	int wlen[30];
	int i;
    char c;
   for(i=0;i<30;i++)
   {
	  wlen[i]=0;
   }
    while((c=getchar()) != EOF){
        if(((c!=' '&&c!='t'&&c!='n')) && (nspace == 0))
		{   
            count++;
        }
        if(c==' '||c=='t'||c=='n'){   
			if(nspace==0)
			{
				wlen[count]++;
			}
            nspace++;
        }
        if((nspace >= 1) && ((c!=' '&&c!='t'&&c!='n')))
		{    
            nspace = 0;
			count=1;       
        }
    }
 
	for(i=1;i<30;i++)
	{
		if(wlen[i]!=0)
		{
			printf("word length equals to %d have %d n",i,wlen[i]);
		}
	}
 
	system("pause");
}
</stdlib></stdio>

< c程序设计语言 >习题:1-12

习题1-12 :编写程序,以一行一个单词的形式打印输入

#include  "stdio.h"
void main(int argc , char* argv[])
{
	int  nspace = 0;
    char c;
    c = getchar(); 
    while((c=getchar()) != EOF){
        if(((c!=' '&&c!='t'&&c!='n')) && (nspace == 0))
		{   
            putchar(c);
 
        }
        if(c==' '||c=='t'||c=='n'){                  
            nspace++;
        }
        if((nspace &gt;= 1) && ((c!=' '&&c!='t'&&c!='n')))
		{    
            nspace = 0;
            putchar('n');
            putchar(c);
 
        }
 
    }
	system("pause");
}

约瑟夫问题

 #include &lt;stdio .h&gt;
 #include &lt;stdlib .h&gt;
 #include &lt;string .h&gt;
 
 typedef struct child
 {
	int no ;
	struct child *next;
 }CHILD;
 void main()
 {
	CHILD *h, *p,*s;
	int m,n,i,count;
	printf("please input children's count:n");
	scanf("%d",&n);
	printf("please input From which to count:n");
	scanf("%d",&m);
	printf("please input From which to stand out: n");
	scanf("%d",&count);
	if((h=(CHILD *)malloc(sizeof(CHILD)))==NULL)
	{
		printf("Cannot assignment memory !n");
		exit(1);
	}
	printf("please enter the student id:");
	scanf("%d",&(h-&gt;no));
	h-&gt;next = NULL;
	p=h;
	for(i=1;i&lt;n ;i++)
	{
		if((s=(CHILD *)malloc(sizeof(CHILD)))==NULL)
		{
			printf("Cannot assignment memory !n");
			exit(1);
		}
		p-&gt;next=s;
		printf("please enter the student id:");
		scanf("%d",&(s-&gt;no));
		s-&gt;next = NULL;
		p=s;
	}
	p-&gt;next = h;
    p=h;
	while(--m) 
	{	
		s=p;
		p=p-&gt;next;
	}
 
 
	while(n--)
	{
		for(i=1; i&lt;count ; i++)
		{
			s=p;
			p=p-&gt;next;
		}
		s-&gt;next=p-&gt;next;
 
 
		printf("%4d ",p-&gt;no);
		if(n%10==0) printf("n");
 
		free(p);
		p=s-&gt;next;
	}
   system("pause");
 }
&lt;/count&gt;&lt;/n&gt;&lt;/string&gt;&lt;/stdlib&gt;&lt;/stdio&gt;