[代码分享]

atoi函数的模拟实现

#include
#include
int atoi(char* p)
{
	int i = 0;
	int ch = 0;
	while (isspace(p[i]) != 0)
	{
		i++;
	}
	if (p[i] == '-')
	{
		ch = '-';
		i++;
	}
	else if (p[i] == '+')
	{
		ch = '+';
		i++;
	}
	else if (isdigit(p[i]))
	{
		;
	}
	else
		return 0;
	int sum = 0;
	while (isdigit(p[i]) != 0)
	{
		sum = sum * 10 + (p[i] - '0');
		i++;
	}
	if (ch == '-')
		return -sum;
	else
		return sum;
}

输入年份及该年中的第几天,计算并输出这一天的年/月/日



int main()
{
	int year, day;
	int sum,i=1,c;
	int a[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
	int b[13]= { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; 
	scanf_s("%d%d", &year, &day);
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
	{
		sum = a[0];
		while (sum < day)
		{
			sum += a[i];
			i++;
		}
		c = a[i-1]-(sum - day);
		printf("%d/%d/%d", year, i - 1, c);
	}
	else
	{
		sum = b[0];
		while (sum < day)
		{
			sum += b[i];
			i++;
		}
		c = b[i - 1] - (sum - day);
		printf("%d/%d/%d", year, i - 1, c);
	}

	return 0;
}

模拟实现strlen

递归实现strlen
int  my_strlen(const char*p)
{
	static int count=-1;
	count++;
	if (*p == '\0')
		return count;
	else
		my_strlen(++p);
}

int main()
{
	char a[] = { "ertyl" };
	int b;
	b = my_strlen(a);
	printf("%d", b);
	return 0;
}

你可能感兴趣的:(c语言)