C和指针课后习题(第四章)

45 篇文章 2 订阅
31 篇文章 0 订阅

4.1

#include <stdio.h>
#include <math.h>
#include <assert.h>

float fun(float a,float number)
{
	float b = (a+(number/a))/2;
	return b;
}
int main()
{
	float number;
	float a=1,b;
	printf("please input a number:\n");
	scanf("%f",&number);
	assert(number>=0);
	b = fun(a,number);
	printf("a=%f\tb=%f\n",a,b);
	printf("%f\n",fabs(a-b));
	while(fabs(a-b)>=1e-6)
	{
		a = b;
		b = fun(a,number);
		printf("a=%f\tb=%f\n",a,b);
	}
	return 0;
}

4.2

#include <stdio.h>
#include <assert.h>
int prime(int number)
{
	int temp=0;
	int i;
	for(i=2;i<number;i++)
		if(number%i==0)
			break;
	if(number==i)
		{
			printf("prime: %d ",number);
			return 1;
		}
	else
			return 0;
}
int main()
{
	int i,j=0;
	printf("the prime number is:\n");
	for(i=2;i<=100;i++)
		{
			//prime(i);
			if((j+1)%5==0)
				printf("\n");
			//j = j+1;
			//printf("j=%d\n",j);
			if(prime(i)==1)
				j = j+1;
		}
	return 0;
	
}

4.3

#include <stdio.h>
#include <assert.h>
int main()
{
    float a,b,c;
    printf("please input three numbers:\n");
    scanf("%f%f%f",&a,&b,&c);
    assert(a>0&&b>0&&c>0);
    assert((a+b)>c&&(b+c)>a&&(a+c)>b);
    if(a==b==c)
        printf("等边三角形!\n");
    else
        if(a==b||a==c||c==b)
        printf("等腰三角形!\n");
    else
        printf("普通三角形!\n");
    return 0;
    
}

4.4

#include <stdio.h>
#include <malloc.h>
#include <error.h>
#include <string.h>
#define MAX_LENGTH 100
#define NDEBUG
#include <assert.h>
void copy_n(char dst[],char src[],int n)
{
	int i=0;
	int length=0;
	while(src[i++]!='\0')
		length++;
	if(length>=n)
		for(i=0;i<n;i++)
			{
				dst[i] = src[i];
				dst[n]='\0';
			}
	else
		{
			for(i=0;i<length;i++)
				dst[i] = src[i];
			for(i=length;i<=n;i++)
				dst[i] = '\0';
		}		
	
}
int main()
{
	char *dst,*src;
	int number;
	int i;
	src = (char *)malloc(sizeof(char)*MAX_LENGTH);
	if(src==NULL)
	{
		perror("Wrong location!\n");
		printf("the error is %s\n",strerror(errno));
		exit(1);
	}
	printf("please input a string:\n");
	gets(src);
	//puts(src);
	//fllush(stdin);
	setbuf(stdin,NULL);
	printf("how many charactor do you want to copy?\n");
	scanf("%d",&number);
	assert(number>0);
	//printf("number=%d\n",number);
	dst = (char *)malloc(sizeof(char)*(number+1));
	if(dst==NULL)
        {
                perror("Wrong location!\n");
                printf("the error is %s\n",strerror(errno));
                exit(1);
        }

	memset(dst,0,number+1);
	//printf("zxc\n");
	copy_n(dst,src,number);
	//printf("number=%d\n",number);
	printf("the string of src is:");
	puts(src);
	printf("the string of dst is:");
	puts(dst);
	return 0;
}

4.5

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
//define NDEBUG
#define MAX_LINE 128
#include <assert.h>
int main()
{
	char *filename="/home/tony/ZF/1.txt";
	FILE *fp=NULL;
	if((fp=fopen(filename,"w+"))==NULL)
	{
		perror("You goofed!");
		printf("error=%d.\n",errno);
		printf("the error is %s\n",strerror(errno));
		return -1;
	}
	char string[MAX_LINE];
	char finish[]="Finished!";
	char **cmp;
	int i,j,number=0;
	puts("please input a string:\n");
	while(1)
	{
		gets(string);
		if(strcmp(string,finish)==0)
			break;
		fprintf(fp,"%s",string);
		fprintf(fp,"\n");
		number++;
	}
	fclose(fp);
	int *a = (int *)malloc(sizeof(int)*number);
	memset(a,0,number);
	printf("the number is %d\n",number);
	if((fp=fopen(filename,"r"))==NULL)
        {
                perror("You goofed!");
                printf("error=%d.\n",errno);
                printf("the error is %s\n",strerror(errno));
                return -1;
        }
	if((cmp=(char **)malloc(sizeof(char*)*(1+number)))==NULL)
	{
		perror("You goofed!");
                printf("error=%d.\n",errno);
                printf("the error is %s\n",strerror(errno));
                return -2;
	}
	for(i=0;i<number;i++)
	{
		if((cmp[i]=(char *)malloc(sizeof(char)*MAX_LINE))==NULL)
		{
			 perror("You goofed!");
               		 printf("error=%d.\n",errno);
                	 printf("the error is %s\n",strerror(errno));
                	 return -3;
		}
	}
	i = 0;
	while(1)
	{
		fgets(string,MAX_LINE,fp);
		printf("%s",string);
		strcpy(cmp[i++],string);
		if(i==number)
			break;
	}
	fclose(fp);
	printf("\nthe string is:\n");
	for(i=0;i<number;i++)
		puts(cmp[i]);
	printf("the same string is:\n");
	for(i=0;i<number-1;i++)
	{
		//if(strcmp(cmp[i],"$$$")==0)
		//	continue;
		for(j=i+1;j<number;j++)
		{
			if(strcmp(cmp[i],cmp[j])==0)
			{
				//cmp[j]="$$$";
				//puts(cmp[i]);
				if(-1 == a[i])
					a[i]+=0;
				else
					a[i]+=1;
				
				a[j] = -1;	
			}
		
		}
	}
	for(i=0;i<number;i++)
		{
			//printf("%d\t",a[i]);
			if(a[i]>0)
				puts(cmp[i]);
		}
	
	return 0;
	
}

4.6

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>
#include <assert.h>

int substr(char dst[],char src[],int start,int len)
{
	int src_length;
	int i = 0,j;
	src_length = strlen(src);
	if(start<0||len<0)
		{
			dst = "";
			printf("the string of dst is:%s\n",dst);
			return 0;
		}
	else if(start+len>=src_length)
		{	
			j = start;
			while(src[j]!='\0')
			{
				dst[i++] = src[j++];
			}	
			dst[i] = '\0';	
			printf("the string of dst is:%s\n",dst);
			return (start+len)-src_length;
		}
	else
		{
			for(j=start;j<(start+len);j++)
				dst[i++] = src[j];
			dst[i] = '\0';
			printf("the string of dst is:%s\n",dst);
			return len;
		}

}
int main()
{
	char *dst,*src;
	int start,len;
	src = (char *)malloc(sizeof(char)*128);
	//dst = (char *)malloc(sizeof(char)*len);
	printf("the source string is: %s\n",src);
	gets(src);
	printf("start and len:\n");
	scanf("%d%d",&start,&len);
	dst = (char *)malloc(sizeof(char)*len);
	substr(dst,src,start,len);
	return 0;
}

4.7

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>

void deblank(char string[])
{
	int i = 0,j,k;
	while(string[i]!='\0')
	{
		k = 0;
		while(string[k++]!='\0')
		{
			if(' ' == string[k] && ' '==string[k+1])
			{
				for(j=k;string[j]!='\0';j++)
					string[j] = string[j+1];
			}
		}
		i++;
	}
}
int main()
{
	char *string;
	string = (char *)malloc(sizeof(char)*128);
	printf("please input a string:\n");
	gets(string);
	printf("the string is: %s\n",string);
	deblank(string);
	printf("the string is: %s\n",string);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值