前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C语言部分函数原型实现

C语言部分函数原型实现

作者头像
阳光岛主
发布2019-02-20 16:39:56
8220
发布2019-02-20 16:39:56
举报
文章被收录于专栏:米扑专栏米扑专栏

学习、研究库函数的实现,能使你考虑问题更加严谨、全面,培养良好的编程风格和习惯

最近想学习一下C语言库函数的内部实现,于是自己简单地写了几个

贴出以下代码,希望大家能够帮忙指正、优化、完善,

特别是考虑不周和执行效率上给出指导意见

阅读本文前,可以先参考本博客的上一篇文章 C语言函数小集合

调试环境: VS2008(C)

代码语言:javascript
复制
// sysFunc.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <ctype.h>
/**************************************/
/**********     isalnum()     *********/
/**************************************/
int m_isalnum(char ch)
{
	return ((ch<='9'&&ch>='0')
			||(ch<='z'&&ch>='a')
			||(ch<='Z'&&ch>='A')); 
}
void fun_isalnum()
{
	char str[]="A12a;D*s06";
	int i;
	printf("Test isalnum.: %s/n", str);
	for(i=0; str[i]!=NULL; i++) /* #define NULL 0 */
	{
		if(m_isalnum(str[i])) /* isalnum(str[i]) */
			printf("%c is a number or character./n", str[i]);
	}
}
/**************************************/
/**********     isalpha()     *********/
/**************************************/
int m_isalpha(char ch)
{
	return ((ch<='z'&&ch>='a')
			||(ch<='Z'&&ch>='A')); 
}
void fun_isalpha()
{
	char str[]="A12a;D*s06";
	int i;
	printf("/nTest isalpha: %s/n", str);
	for(i=0; str[i]!='/0'; i++)
	{
		if(m_isalpha(str[i])) /* isalpha(str[i]) */
			printf("%c is a character./n", str[i]);
	}
}
/**************************************/
/**********     islower()     *********/
/**************************************/
int m_islower(char ch)
{
	return (ch<='z'&&ch>='a');
}
void fun_islower()
{
	char str[]="A12a;D*s06";
	int i;
	printf("/nTest islower: %s/n", str);
	for(i=0; str[i]!='/0'; i++)
	{
		if(m_islower(str[i])) /* isalpha(str[i]) */
			printf("%c is a lower character./n", str[i]);
	}
}
/**************************************/
/**********     isupper()     *********/
/**************************************/
int m_isupper(char ch)
{
	return (ch<='Z'&&ch>='A');
}
void fun_isupper()
{
	char str[]="A12a;D*s06";
	int i;
	printf("/nTest isupper: %s/n", str);
	for(i=0; str[i]!='/0'; i++)
	{
		if(m_isupper(str[i])) /* isalpha(str[i]) */
			printf("%c is a isupper character./n", str[i]);
	}
}
/**************************************/
/**********     isxdigit()    *********/
/**************************************/
int m_isxdigit(char ch)
{
	return (ch<='9'&&ch>='0');
}
void fun_isxdigit()
{
	char str[]="A12a;D*s06";
	int i;
	printf("/nTest isxdigit: %s/n", str);
	for(i=0; str[i]!='/0'; i++)
	{
		if(m_isxdigit(str[i])) /* isalpha(str[i]) */
			printf("%c is a isxdigit character./n", str[i]);
	}
}
/**************************************/
/**********     tolower()     *********/
/**************************************/
char m_tolower(char ch)
{
	if(ch<='Z'&&ch>='A')
		return (ch+'a'-'A');
	return '0';
}
void fun_tolower()
{
	char str[]="A12a;D*s06";
	int i;
	char ch; 
	printf("/nTest tolower: %s/n", str);
	for(i=0; str[i]!='/0'; i++)
	{
		ch=str[i];
		if(m_isalpha(ch)) /* isalpha(str[i]) */
		{
			if(ch<='Z'&&ch>='A')
				ch=m_tolower(ch);
			printf("%c is a tolower character./n", ch);
		}
	}
}
/**************************************/
/**********     toupper()     *********/
/**************************************/
char m_toupper(char ch)
{
	if(ch<='z'&&ch>='a')
		return (ch+'A'-'a');
	return '0';
}
void fun_toupper()
{
	char str[]="A12a;D*s06";
	int i;
	char ch;
	printf("/nTest toupper: %s/n", str);
	for(i=0; str[i]!='/0'; i++)
	{
		ch=str[i];
		if(m_isalpha(ch)) /* isalpha(str[i]) */
		{
			if(ch<='z'&&ch>='a')
				ch=m_toupper(ch);
			printf("%c is a toupper character./n", ch);
		}
	}
}
/**************************************/
/**********       atoi()      *********/
/**************************************/
int m_atoi(char *str)
{
	char *s=str;
	int sum=0, flag=1;
	if(*s==NULL)
		return 0;
	while(*s==' ') /* skip space */
		s++; 
	if(*s=='-')
	{
		flag=-1;
		s++;
	}
	while(*s!='/0' && m_isxdigit(*s))
	{
		sum=*s-'0'+sum*10;
		s++;
	}
	
	return flag*sum;
}
void fun_atoi()
{
	char str[]="   -123.456def";
	printf("/nTest atoi.../n");
	printf("%s char to int: %d/n", str, m_atoi(str));
}
/**************************************/
/**********       itoa()      *********/
/**************************************/
void m_itoa(int n)
{
	char *s=NULL;
	int i, j, mod, flag=0;
	char num[20];
	if(n<0) 
	{
		flag=1;
		n=-n;
	}
	i=0;
	do 
	{
		mod=n%10;
		n=n/10;		
		num[i++]=mod+'0';
	} while (n>0);
	
	if(flag) /* print '-' */
		printf("-");
	for(j=i-1; j>=0; j--) /* print reverse char */
	{
		printf("%c", num[j]);
	}
}
void fun_itoa()
{
	int n=1234;
	printf("/nTest itoa.../n");
	printf("%d char to int: ", n);
	m_itoa(n);
	printf("/n");
}
/**************************************/
/**********       atof()      *********/
/**************************************/
float m_atof(char *str)
{
	char *s=str;
	if(*s=='/0')
		return 0.0;
	
	float sum=0.0;
	int flag=1, pow=0;
	if(*s=='-')
	{
		flag=-1;
		s++;
	}
	while(*s!='/0')
	{
		if(*s=='.') /* dot */
		{
			pow=1;
			s++;
			continue;
		}
		sum=*s-'0'+sum*10;
		pow*=10;
		s++;
	}
	return flag*sum/pow;
}
void fun_atof()
{
	char str[]="-123.345";
	printf("/nTest atof.../n");
	printf("%s char to int: %f/n", str, m_atof(str));
}
/**************************************/
/**********       ftoa()      *********/
/**************************************/
void m_ftoa(float n)
{
	char num[20];
	int i, j, val, mod, flag, pow;
	float dot;
	if(n<-1e-6)
	{
		flag=1;
		n=-n;
	}
	i=0;
	val=(int)n; /* integer */
	dot=n-val;  /* decimal */
	do
	{
		mod=val%10;
		val=val/10;
		num[i++]=mod+'0';
	}while(val>0);
	if(dot>1e-6)
	{
		num[i]='.';
		pow=10;
	}
	do 
	{
		dot=dot*pow;
		mod=(int)dot;
		num[i++]=mod+'0';
		dot=dot-mod; /* error, why, float... help me */
	} while (dot>1e-6);
	if(flag)
		printf("-");
	for(j=i-1; j>=0; j--)
		printf("%c", num[j]);
}
void fun_ftoa() 
{
	float n=-1234.5678;
	printf("/nTest ftoa.../n");
	printf("%d char to int: ", n);
	m_ftoa(n);
	printf("/n");
}
/**************************************/
/**********       main()      *********/
/**************************************/
int _tmain(int argc, _TCHAR* argv[])
{
	fun_isalnum();
	fun_isalpha();
	fun_islower();
	fun_isupper();
	fun_isxdigit();
	fun_tolower();
	fun_toupper();
	fun_atof();
	fun_atoi();
	fun_itoa();
	fun_ftoa();
	return 0;
}

运行结果:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010年06月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档