1、计算一个正数n的平方根,用连续的近似值计算,计算的值会越来越逼近正确值,第一个近似值是1,后面的近似值由下面的公式计算:
a
i
+
1
=
(
a
i
+
(
n
/
a
i
)
)
/
2
a_{i+1} = (a_i + (n/a_i))/2
ai+1=(ai+(n/ai))/2
写一个程序读取一个值计算并打印出它的平方根,如果你把所有的近似值打印出来,你可以看到它们快速的逼近正确值,在练习中,程序被浮点型的数据精准度限制,如果一个近似值与前一个相等,那么就停止运算。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
double n;
double next = 1;
printf("input a positive number:");
scanf("%lf", &n);
if (n <= 0)
{
printf("Error! can't input negative\n");
return -1;
}
while (next != ((next + n/next) / 2))
{
printf("%lf\n", next);
next = (next + n/next) / 2;
}
printf("done!\n");
return 0;
}
2、质数不能被除了它自己和1的其他任何值整除,写一个程序打印出1-100内所有的质数。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int i;
int j;
for (i = 2; i <= 100; i++)
{
int is_prime = 1;
for (j = 2; j < (i/2 + 1); j++)
{
if (!(i % j))
{
is_prime = 0;
break;
}
}
if (is_prime)
{
printf("%d, ", i);
}
}
printf("\n");
return 0;
}
3、 三条边都相等的三角形叫等边三角形,有两条边相等的三角形的等腰三角形,三条边都不相等叫做不等边三角形,写一个程序提示输入三角形的三条变长,程序需要判断这些数字代表的是什么三角形,提示:什么是程序另外要检查的?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
float a, b, c;
printf("please input three number:");
scanf("%f %f %f", &a, &b, &c);
if ((a <= 0) || (b <= 0) || (c <= 0))
{
printf("Error! the length is not positive\n");
return -1;
}
if (((a + b) <= c) || ((a + c) <= b) || ((b + c) <= a))
{
printf("Error! can't form a triangle\n");
return -1;
}
if ((a == b) && (a == c))
{
printf("this is a equilateral triangle\n");
}
else if ((a == b) || (a == c) || (b == c))
{
printf("this is a isosceles triangle\n");
}
else
{
printf("this is a scalene triangle\n");
}
return 0;
}
4、写一个函数,它的原型如下所示:
void copy_n(char dst[], char scr[], int n);
这个函数将数组src中的字符串复制到数组dst,但是有这些要求:刚好n个字符被存储到dst数组,如果src字符串的长度比n小,那么你需要添加足够的NULL字符使得有n个字符被存储,如果src中字符串的长度大于或等于n,那么复制n个字符后停止,这种情况下不是以NULL结尾,注意调用copy_n时应该在dst[0]到dst[n-1]存储,与src的长度无关。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
void copy_n(char dst[], char src[], int n)
{
int i;
int len;
for (len = 0; src[len] != '\0'; len++)
;
for (i = 0; i < n; i++)
{
if (i < len)
{
dst[i] = src[i];
}
else
{
dst[i] = '\0';
}
}
}
int main(int argc, char *argv[])
{
char out[MAX];
int n;
n = atoi(argv[2]);
copy_n(out, argv[1], n);
printf("%s\n", out);
return 0;
}
5、写一个程序从标准输入中一行一行的读取,相邻行有一行或多行相同则打印,其他的都不打印, 你可能要假定每一行不超过128个字符,长度不超过127个字符,每一行以换行符结尾。
提示:使用gets来读取行,使用strcpy来复制,使用strcmp进行比较,如果两个字符串相等则返回0,否则返回非零值。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int main(void)
{
int i;
int had_out = 0;
int lines;
char input[MAX][128];
printf("please input the number of line: ");
scanf("%d", &lines);
gets();
for (i = 0; i < lines; i++)
{
printf("line %d: ", i+1);
gets(input[i]);
}
for (i = 1; i < lines; i++)
{
if (!strcmp(input[i-1], input[i]))
{
if (had_out == 0)
{
printf("%s\n", input[i]);
had_out = 1;
}
}
else
{
had_out = 0;
}
}
return 0;
}
6、编写一个函数从字符串中提取子字符串,函数原型如下:
int substr(char dst[],char src[],int start,int len);
从src数组其实位置向后偏移start个字符的位置开始,最多复制len个非NUL字符,dst必须以NUL字符结尾,函数的返回值是存储于dst数组中的字符串长度,如果start所指定的位置超过了src的尾部,或者start,len的值为负,那么复制到dst数组的字符串为空。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int substr(char dst[], char src[], int start, int len)
{
int i;
int src_len = strlen(src);
if ((start >= src_len) || (start < 0) || (len < 0))
{
dst[0] = '\0';
return 0;
}
for (i = 0; i < len; i++)
{
if (dst[i] = src[start++])
;
else
break;
}
printf(
"%s: %d\n", dst, i);
return i;
}
int main(int argc, char *argv[])
{
int i;
int start;
int len;
char dst[MAX];
start = atoi(argv[2]);
len = atoi(argv[3]);
substr(dst, argv[1], start, len);
return 0;
}
7、编写一个函数从一个字符串中除去多余的空白,函数原型如下:
void deblank(char string[]);
当函数发现一个或多个地方有一个或连续多个的空格组成,就把它们改成单个字符,注意当你遍历整个字符串时要确保它以NUL字符结尾。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void deblank(char string[])
{
int i = 0;
int j = 0;
char tem[100];
int flag = 0;
while (string[i])
{
if (string[i] == ' ')
{
if (flag == 0)
{
tem[j++] = string[i++];
flag = 1;
}
else
{
i++;
}
}
else
{
tem[j++] = string[i++];
flag = 0;
}
}
tem[j] = '\0';
strcpy(string, tem);
}
int main(int argc, char *argv[])
{
int i;
puts(argv[1]);
deblank(argv[1]);
puts(argv[1]);
return 0;
}