//<Pointers on C>: p63 4141
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
/*
**注意讨论负数的情况。
*/
if (n < 0)
{
printf("Negative number!\n");
return EXIT_FAILURE;
}
float a = 0;
float score = 1;
while (score != a)
{
a = score;
score = (a+(n/a)) / 2;
printf("%f\n",score);
}
return EXIT_SUCCESS;
}
//<Pointers on C>: p64 4112
#include <stdio.h>
int main(void)
{
//1 is easy to check.
printf("Prime from 1 to 100:\n1 ");
int number;
for (number = 2;number <= 100;number++)
for (int divisor = 2;divisor <= number;divisor++)
{
if (divisor == number)
{
printf("%d ",number);
break;
}
if ((number % divisor) == 0)
break;
}
printf("\n");
}
//<Pointers on C>: p64 4143
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
//is triangle?
if ((a+b)<=c || (a+c)<=b || (b+c)<=a)
{
printf("Error input.\n");
return EXIT_FAILURE;
}
if (a==b || a==c || b==c)
{
if (a==b && a==c && b==c)
{
printf("等边三角形.\n");
}
else
printf("等腰三角形.\n");
}
else
printf("不等边三角形.\n");
return EXIT_SUCCESS;
}
//<Pointers on C>: p64 4144
#include <stdio.h>
#include <string.h>
void copy_n(char dst[],char src[],int n);
int main(void)
{
char destination[100]={"\0"}; //初始化第一位之后,其余位默认初始化为'\0',绝不能不初始化
char source[]={"CHINA."};
copy_n(destination,source,8);
printf("%s\n",&destination);
}
void copy_n(char dst[],char src[],int n)
{
int len = strlen(src);
if (len >= n)
{
for (int i=0;i < n;i++)
{
dst[i]=src[i];
}
}
else
{
for (int i=0;i < len;i++)
{
dst[i]=src[i];
}
for (int i = len;i < n;i++)
{
dst[i] = '\0';
}
}
}
//<Pointers on C>: p64 4145
#include <stdio.h>
#include <string.h>
#define LOCAL
int main(void)
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
char prepreline[128] = {'\0'};
char preline[128] = {'\0'};
char line[128] = {'\0'};
while (gets(line) != NULL)
{
if (!strcmp(line,preline))
{
if (!strcmp(preline,prepreline))
;
else
printf("%s\n",line);
}
strcpy(prepreline,preline);
strcpy(preline,line);
}
}
//<Pointers on C>: p65 4146
#include <stdio.h>
#include <string.h>
#define LEN 100
#define LOCAL
int substr(char dst[],char src[],int start,int len);
int main(void)
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
char destination[LEN] = {'\0'};
char source[LEN] = {"abcdefghijklmn"};
int starting = 3;
int length = 16;
int dst_len;
dst_len = substr(destination,source,starting,length);
printf("%s\n%d\n",destination,dst_len);
}
int substr(char dst[],char src[],int start,int len)
{
if (len>strlen(src) || start<0 || len<=0)
{
;
}
else
{
for (int i=0;i < len;i++)
{
dst[i] = src[i+start];
}
}
return strlen(dst);
}
/*
** Extract the specified substring from the string in src.
*/
int
substr( char dst[], char src[], int start, int len )
{
int srcindex;
int dstindex;
dstindex = 0;
if( start >= 0 && len > 0 ){
/*
** Advance srcindex to right spot to begin, but stop if we reach
** the terminating NUL byte.
*/
for( srcindex = 0;
srcindex < start && src[srcindex] != ’\0’;
srcindex += 1 )
;
/*
** Copy the desired number of characters, but stop at the NUL if
** we reach it first.
*/
while( len > 0 && src[srcindex] != ’\0’ ){
dst[dstindex] = src[srcindex];
dstindex += 1;
srcindex += 1;
len –= 1;
}
}
/*
** Null–terminate the destination.
*/
dst[dstindex] = ’\0’;
return dstindex;
}
//Reference P65 4147
/*
** Shrink runs of white space in the given string to a single space.
*/
#define NUL ’\0’
void deblank( char *string )
{
char *dest;
char *src;
int ch;
/*
** Set source and destination pointers to beginning of the string, then
** move to 2nd character in string.
*/
src = string;
dest = string++;
/*
** Examine each character from the source string.
*/
while( (ch = *src++) != NUL ){
if( is_white( ch ) ){
/*
** We found white space. If we’re at the beginning of
** the string OR the previous char in the dest is not
** white space, store a blank.
*/
if( src == string || !is_white( dest[–1] ) )
*dest++ = ’ ’;
}
else {
/*
** Not white space: just store it.
*/
*dest++ = ch;
}
}
*dest = NUL;
}
int is_white( int ch )
{
return ch == ’ ’ || ch == ’\t’ || ch == ’\v’ || ch == ’\f’ || ch == ’\n’
|| ch == ’\r’;
}