C和指針課後習題(第六章)

6.1

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
char *find_char(char const *source,char const *chars)
{
	int source_len = 0,chars_len = 0;
	int i = 0,j = 0;
	char ch;
	while(*(source+i++)!='\0')
		source_len++;
	while(*(chars+j++)!='\0')
		chars_len++;
	for (i=0;i<source_len;i++)
	{
		for (j=0;j<chars_len;j++)
		{
			if (*(chars+j)==*(source+i))
			{
				ch = *(chars+j);
				return &ch;
			}
		}
	}
	return NULL;
}

int main()
{
	const char *source = "china";
	const char *chars = "America";
	char ch;
	ch = *find_char(source,chars);
	printf("%c\n",ch);
	system("pause");
	return 0;
}

6.2

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int del_substr(char *str,const char *substr)
{
	int str_len = 0;
	int sub_len = 0;
	int i =0,j=0,k;
	while(*(str+i++)!='\0')
		str_len++;
	while(*(substr+j++)!='\0')
		sub_len++;
	assert(str_len>=sub_len);
	char *p1 = str;
	const char *p2 = substr;
#if 0
	printf("str_len=%d\nsub_len=%d\n",str_len,sub_len);
#endif
	for(i=0;i<=(str_len-sub_len);i++)
	{
		k = i;
#if 0
		printf("%c\t",*(p1+i));
#endif
		for (j=0;j<sub_len;j++)
		{
			if (*(p1+k++)!=*(p2+j))
				break;
		}
		if (j==sub_len)
			return 1;
	}
	return 0;
}

int main()
{
	char *str = "ABCDEFG";
	char *substr = "CDE";
	printf("result is %d\n",del_substr(str,substr));
	system("pause");
	return 0;
}

6.3

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define max_line 128
void reverse_string(char *string)
{
	char *p = string;
	int i=0;
	int string_len = 0;
	while(*(p+i++)!='\0')
	{
		string_len++;
	}
	assert(string_len>0);
	printf("the original string is \n%s\n",string);
#if 1
	printf("string_len = %d\n",string_len);
#endif
	printf("the reverse string is\n");
	for (int i=string_len-1;i>=0;i--)
	{
		printf("%c",string[i]);
	}
	printf("\n");
}
int main()
{
	char *string;
	printf("please input a string:\n");
	string = (char *)malloc(sizeof(char)*max_line);
	gets(string);
	reverse_string(string);
	system("pause");
	return 0;
}

6.4

#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
#include <time.h>
int main(void)
{
	int i;
	int j;
	char table[MAX];
	double time = 0.00;
	clock_t start = 0,end = 0;
	start = clock();
	for(i = 2;i < MAX;i++)
		table[i] = 1;
	for(i = 2;i < MAX;i++)
		if(table[i] == 1)
			for(j = i;i*j < MAX;j++)
				table[i*j] = 0;
	for(i = 2;i < MAX;i++)
		if(table[i] == 1)
			printf("%d\n",i);
	end = clock();
	time = (double)(end-start)/CLOCKS_PER_SEC;
	printf("time eclipse is %lf\n",time);
	system("pause");
	return 0;
}

6.6

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 1000
#include <time.h>
#include <string.h>
int main(void)
{
	int i,k;
	//unsigned int j;
	int j,number1=0,number2=0,number=0 ;
	char table2[2000];
	double time = 0.00;
	clock_t start = 0,end = 0;
	start = clock();
	for (j=1000;j<=40000;j+=1000)
	{
		number1 = 0;
		char *table = (char *)malloc(sizeof(char)*j);
		printf("strlen=%d\n",strlen(table));

		for(i = 2;i < j;i++)
			table[i] = 1;
		for(i = 2;i < j;i++)
			if(table[i] == 1)
				for(k = i;i*k < j;k++)
					table[i*k] = 0;
		for(i = 2;i < j;i++)
			if(table[i] == 1)
			{
				//printf("%d\n",i);
				number1++;
			}
			printf("number1=%d\n",number1);
			printf("the prime number between %d and %d is %d\n",j-1000,j,number1-number);
			number = number1;
	}
	//char table1[MAX];
	
	
	
#if 0
		for(i = 2;i < 2000;i++)
			table2[i] = 1;
		for(i = 2;i < 2000;i++)
			if(table2[i] == 1)
				for(j = i;i*j < 2000;j++)
					table2[i*j] = 0;
		for(i = 2;i < 2000;i++)
			if(table2[i] == 1)
			{
				//printf("%d\n",i);
				number2++;
			}
			printf("number2=%d\n",number2);
			//printf("the prime number between 1000 and 2000 is %d\n",number2-number1);
#endif
		end = clock();
		time = (double)(end-start)/CLOCKS_PER_SEC;
		printf("time eclipse is %lf\n",time);
		int  num=0;
		for (i=29000;i<30000;i++)
		{
			for (j=2;j<i;j++)
			{
				if (i%j==0)
					break;
			}
			if (j==i)
				num++;
		}
		printf("num=%d\n",num);
		system("pause");
		return 0;
}

6.6這個程序有一個問題,就是數字如果在大的話會出現 Access violation writing location 0x80aa1701這個錯誤,我現在還沒有搞明白爲什麼會出現這個錯誤,我懷疑是不是由於申請的空間太大導致這個問題的出現,有沒有大神出來解釋一下,如果使用我在程序中的最後面的那種簡單的耗時的方法很容易求出來沒有什麼問題

如果有時間的將結果寫入到文本中,待續!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章