1.正数n的平方根可以通过计算一系列近似值来获得,每个近似值都比前一个更加接近准确值。第一个近似值为1,接下来的近似值则通过下面 的公式来获得。
a[i+1] = (a[i] + n/a[i])/2.
编写一个程序,读入一个值,计算并打印它的平方根。实际上,由于浮点型变量的精度限制,程序无法一直计算下去。当某个近似值与前一个近似值相等时,就可以让程序停止继续计算了。
``
#include<stdio.h>
#include<stdlib.h>
float sq(float);
int main()
{
float i;
scanf("%f", &i);
if(i < 0)
{
printf(“the value of i is not positive!”);
return 1;
}
else
printf("%f", sq(i));
system("pause");
return 0;
}
float sq(float i)
{
float n,q;
n = 0;
q = 1;
while(1)
{
n = (q + i/q) / 2;
if(n != q)
q = n;
else
break;
}
return n;
}
输出结果:
2.一个整数如果只能被它本身和1整除,它就被称为质数(prime)。请编写一个程序,打印出1~100之间的所有质数。
#include<stdio.h>
#include<stdlib.h>
int is_prime(int n);
int main()
{
int n;
for(n = 2; n <= 100; n++)
{
if(is_prime(n))
printf("%d ", n);
}
system("pause");
return 0;
}
int is_prime(int n)
{
int flag = 1;
int i;
if(n == 1)
{
flag = 0;
}
else
{
for(i = 2; i < (n/2 + 1); i++)
{
if(n % i == 0)
flag = 0;
}
}
return flag;
}
输出结果:
3.等边三角形的三条边长度相等,但等腰三角形只有两条边的长度相等的。如果三角形的三条边长度都不等,那就称为不等边三角形。请编写一个程序,提示用户输入三个数,分别表示三角形三条边的长度,然后由程序判断它是什么类型的三角形。
提示:除了边的长度是否相等外,程序是否还应考虑一些其他的东西?
#include<stdio.h>
#include<stdlib.h>
int main()
{
float a, b, c;
printf("Please input triangle's three side length:\n");
scanf("%f %f %f", &a, &b, &c);
if(a <= 0 || b <= 0 || c <= 0)
{
printf("triangle's side length is not positive");
return 1;
}
if(a + b <= c || a + c <= b || b + c <= a)
{
printf("the sum of any two triangle's side length is less than the other one");
return 1;
}
if(a == b && b == c && c == a)
printf("equilateral");
else if(a==b || b == c || c == a)
printf("An isosceles triangle");
else
printf("unequal triangle");
system("pause");
return 0;
}
输出结果:
4.写一个函数,它的原型如下所示:
void copy_n(char dst[], char src[], int n);
这个函数将数组src中的字符串复制到数组dst但是有这些要求:刚好n个字符被存储到dst数组,如果src字符串的长度比n小,那么你需要添加足够的NUL字符使得有n个字符被存储,如果src中字符串的长度大于或等于n,那么复制n个字符后停止,这种情况下不是以NUL结尾,注意调用copy_n时应该在dst[0]到dst[n-1]存储,与src的长度无关,如果你计划使用strncpy来实现你的程序,祝贺你提前学到了这个知识,但在这里我的目的是让你自己规划程序的逻辑,所以你最好不要使用那些处理字符串的库函数.
void copy_n(char dst[], char src[], int n)
{
int i, j, k;
int len = strlen(src);
if(len < n)
{
for(i = 0; i < n; i++)
dst[i] = src[i];
for(j = i + 1 ; j < n; j++)
dst[j] = '\0';
}
else
{
for(k = 0; k < n; k++)
{
dst[k] = src[k];
}
dst[k] = '\0';
}
}
5.写一个程序,从标准输入中一行一行的读取,并完成如下任务:如果文件有两行或者更多行相邻的文本内容相同,那么打印其中一行,其他的都不打印,你可能要假定每一行不超过128个字符,长度不超过127个字符,每一行以换行符结尾。
考虑输入下面的文件。
This is the first line.
Another line.
And another;
And another;
And another;
And another;
Still more;
Almost down now -
Almost down now -
Anoter line.
Still more.
Finished!
假定结尾没有任何空白,根据这个文件输入,程序应该产生下列输出:
And another.
Almost down now
每一个相邻行相同都会被输出,注意Another line和Still more这两行不应该被打印出来,因为尽管它们出现两次但是不相邻,提示:使用gets来读取行,使用strcpy来复制,使用strcmp进行比较,如果两个字符串相等则返回0,否则返回非零值。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int flag = 0;
char previous[128];
char next[128];
gets(previous);
while(gets(next) != EOF)
{
if(strcmp(next,previous) != 0 && flag == 1)
{
printf("%s", next);
flag = 0;
}
if(strcmp(next, previous) == 0)
flag = 1;
else
flag = 0;
strcmp(previous, next);
}
system("pause");
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数组的字符串为空。
int substr(char dst[], char src[], int start, int len)
{
int i, len;
len = strlen(src);
if(start >= len || start < 0 || len < 0)
{
dst[0] = '\0';
i = 0;
}
else
{
for(i = 0; i < len; i++)
{
if(i+start < src_len)
dst[i] = src[i+start];
else
break;
}
dst[i] ='\0';
printf("%s:%d",dst,i);
}
return i;
}
7.编写一个函数从一个字符串中除去多余的空格,函数原型应该如下:
void deblank(char string[]);
当函数发现字符串中如果有一个地方由一个或者多个连续的空格组成,就把它们改成单个空格字符。注意当你遍历整个字符串时要确保它以NUL字符结尾。
void deblank(char string[])
{
int i,j;
int flag = 0;
for(i = 0; string[i] != '\0'; i++)
{
while(string[i] == ' ' && flag == 1)
{
for(j = i + 1; string[j] != '\0'; j++)
string[j] = string[j + 1];
string[j] = '\0';
}
if (string[i] == ' ')
flag = 1;
else
flag = 0;
}
}