Author Archive

getc 和 _fillbuf

05月 8th, 2011

转载自: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)的缓存中。

不要过打折的生活

04月 22nd, 2011

很多人都认为节省能让他们的生活更保险,所以,他们把现在的生活过得潦草而廉价。其实这是对生活没有自信的表现。我绝不主张挥霍浪费,但你要知道,健康的享受是人生的进步。当你为了将来而省略了生活中应有的享受时,你的生活就打了九折;如果牺牲了自由与亲情,你的生活就打了七折;如果你放弃了自己的意愿和爱情,那你的生活就打了对折,再富足的生活也经不起打折.

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

03月 28th, 2011

题目描述:编写程序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&lt;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()区别

03月 26th, 2011

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

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

03月 25th, 2011

题目要求:编写函数 itob(n ,s, b ) 将整数n转化成以b为底的数.并将结果以字符串形式保存在数组s中.

 
void itob(int n,char s[],int b)
{
	int i=0,j,k;
	char temp;
	while(n!=0)
	{
		k=n%b;
		s[i++]=(k< =9)?k+'0':k+'a'-10;
		n=n/b;
	}
	k=i;
	i--;
	for(j=0,i;j<i;j++,i--)
	{
		temp=s[j];
		s[j]=s[i];
		s[i]=temp;
	}
	s[k]='\0';
}

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

03月 24th, 2011

题目: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:%d\n",numstr,number);
	system("pause");
}

习题1-13(改造)

03月 24th, 2011

原题:编写一个程序,打印输入中单词长度的脂肪图.
改造:编写一个程序,统计长度为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&lt;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&lt;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

03月 22nd, 2011

习题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 >= 1) && ((c!=' '&&c!='\t'&&c!='\n')))
		{    
            nspace = 0;
            putchar('\n');
            putchar(c);
 
        }
 
    }
	system("pause");
}

约瑟夫问题

03月 21st, 2011
 #include <stdio .h>
 #include <stdlib .h>
 #include <string .h>
 
 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->no));
	h->next = NULL;
	p=h;
	for(i=1;i<n ;i++)
	{
		if((s=(CHILD *)malloc(sizeof(CHILD)))==NULL)
		{
			printf("Cannot assignment memory !\n");
			exit(1);
		}
		p->next=s;
		printf("please enter the student id:");
		scanf("%d",&(s->no));
		s->next = NULL;
		p=s;
	}
	p->next = h;
    p=h;
	while(--m) 
	{	
		s=p;
		p=p->next;
	}
 
 
	while(n--)
	{
		for(i=1; i<count ; i++)
		{
			s=p;
			p=p->next;
		}
		s->next=p->next;
 
 
		printf("%4d ",p->no);
		if(n%10==0) printf("\n");
 
		free(p);
		p=s->next;
	}
   system("pause");
 }
</count></n></string></stdlib></stdio>

Linux环境下如何安装C++编译器Code::Block(转)

03月 3rd, 2011

安这个东东有点崩溃,整了一个下午。由于之前没有过Linux使用及开发经验,初次涉及Linux环境下的开发,多少感觉不太适应,特别是对于各种软件的安装,相比Windows确实要麻烦许多。
前几天买了一本书叫《Linux程序设计第3版》,据说相当经典,经典当然不能错过,特别是对于一个刚刚开始学习Linux的新手而言。花了一天半时间狂翻了300多页,多多少少心里知道这是讲啥东东,感觉还不错,嘿~
心里痒痒就开始整Linux,虽然说vim+gcc+gdb已经能够满足Linux下C++的开发了,但还是想找一个IDE作为辅助集成开发环境。查了很多篇博客,看来大家都还比较推荐这款Code::Block,总结了一些文章的观点,原因可以从以下几点看出:

Read more »