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这个错误,我现在还没有搞明白为什么会出现这个错误,我怀疑是不是由于申请的空间太大导致这个问题的出现,有没有大神出来解释一下,如果使用我在程序中的最后面的那种简单的耗时的方法很容易求出来没有什么问题
如果有时间的将结果写入到文本中,待续!!!