设为首页 加入收藏

TOP

C和指针 (pointers on C)――第四章:语句(下)习题解答
2015-01-22 21:13:17 来源: 作者: 【 】 浏览:14
Tags:指针 pointers 第四章 语句 习题 解答

?

第四章以下通过VS2012

?

1、

#include stdafx.h

double sqrt(double temp)
{	
	double before, after;
	before = 1.0;
	after = 1.0;
	do
	{
		before = after;
		after = (before + temp/before)/2;
	} while (before != after);
	return after;
}


int _tmain(int argc, _TCHAR* argv[])
{
	double temp,result;
	puts(input N:);
	scanf_s(%lf,&temp);
	result = sqrt(temp);
	printf(sqrt N = %lf 
,result);
	return 0;
}
2、

?

#include stdafx.h

int _tmain(int argc, _TCHAR* argv[])
{
	int length = 100;
	puts(Prime(1~100):
);
	for (int i = 1; i <= length; i++)
	{
		int ALU = 0;
		for (int j =1; j <= i; j++)
		{
			if (i%j == 0)
			{
				ALU++;
			}
		}
		if (ALU == 2)
		{
			printf(%d	, i);
		}
	}
	return 0;
}
3、

?

?

#include stdafx.h


int _tmain(int argc, _TCHAR* argv[])
{
	int a,b,c;
	puts(输入三边长度(a,b,c):
);
	do
	{
		scanf_s(%d,%d,%d,&a,&b,&c);
	} while ((a+b)<=c || (a+c)<=b || (b+c)<=a);
	if ((a == b)&&(b == c))
	{
		puts(等边!);
	}
	else 
		if ((a==b)||(b==c)||(c==a))
		{
			puts(等腰!);
		}
		else
		{
			puts(不等边!);
		}
	return 0;
}

4、2B方法:

?

?

void copy_n( char dst[], char src[], int n )
{
	int i,l_src;
	l_src = 0;
	for (i = 0; dst[i] == ''; i++)
	{
		l_src++;
	}
	if (l_src < n)
	{
		for (i = 0; i < l_src; i++)
		{
			dst[i] = src[i];
		}
		for (i = l_src; i < n; i++)
		{
			dst[i] = '';
		}
	}
	else
	{
		for (i = 0; i < n; i++)
		{
			dst[i] = src[i];
		}
	}
}

正确做法:

?

?

void copy_n( char dst[], char src[], int n )
{
	int dst_index, src_index;
	src_index = 0;
	for (dst_index = 0; dst_index < n; dst_index++)
	{
		dst[dst_index] = src[src_index];
		if (src[src_index] != 0)
		{
			src_index++;
		}
	}
}
5、涉及到太多字符串处理,略。
6、

?

?

void substr(char dst[], char src[], int start, int len)
{
	int dst_index, src_index, l_src;
	l_src =	src_index = 0;
	do
	{
		l_src++;
	} while (src[l_src] != '' );


	if (start < 0 || len < 0 || start > l_src ++)
	{
		dst[0] = '';
	}
	else
	{
		for ( dst_index = 0; dst_index < len; dst_index++)
		{
			dst[dst_index] = src[src_index + start];
			if (src[src_index] != '')
			{
				src_index++;
			}
		}
	}
}

7、

?

?

void deblank(char string[])
{
	char copy_str[] = {''};

	int str_index, copy_str_index, temp = 0;
	for ( str_index = 0; string[str_index] != ''; str_index++)
	{
		if (string[str_index] == ' ')
		{
			if (temp > 0)
			{
				continue;
			}
			else
			{
				copy_str_index++;
				copy_str[copy_str_index] = string[str_index];
				temp++;
			}
		}
		else
		{
			copy_str_index++;
			copy_str[copy_str_index] = string[str_index];
		}
	}
	copy_str[copy_str_index++] = '';
	for ( copy_str_index = 0; copy_str[copy_str_index] != ''; copy_str_index++)
	{
		string[copy_str_index]= copy_str[copy_str_index];
	}
	string[copy_str_index + 1] = '';
}


?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇编程算法 - 和为s的两个数字 代码.. 下一篇快速学习C语言一: Hello World

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: