1>正数n的平方根可以通过计算一系列近似值来获得,每个近似值都比前一个更加接近准确值。第一个近似值是1,接下来的近似值则通过下面的公式来获得。编写一个程序,读入一个值,计算并打印它的平方根。
int main()
{
float a = 1 ;
float b = 0;
float n = 0;
printf("请输入:\n");
scanf("%f", &n);
if (n <= 0)
{
printf("输入数字有误!");
}
else
{
do
{
b = a;
a = (b + n / b) / 2;
printf("%f\n", a);
} while (b != a);
printf("平方根为:%f\n", a);
}
system("pause");
return 0;
}
2.一个整数如果只能被它本身和1整除,它就被称为质数,请编写一个程序,打印出1-100之间的所有质数
int main()
{
int i = 0;
int j = 0;
int k = 0;
for (i = 3; i <= 100; i+=2)//偶数不可能为质数
{
k = (int)sqrt(i);
for (j = 2; j <= k; j++)//注意j的范围,可以提高计算效率
{
if (i%j == 0)
break;
}
if (i%j != 0)
printf("%d\n", i);
}
system("pause");
return 0;
}
3.等边三角形的三条边长度都相等,但等腰三角形只有两条边长度是相等的。如果三角形的三条边长度都不相等,就称为不等边三角形
请编写一个程序,提示用户输入三个数,分别表示三角形三条边的长度,然后由程序判断它是什么类型的三角形。
提示:除了边的长度是否相等外,程序是否还应考虑一些其他东西?
int main()
{
int a = 0;
int b = 0;
int c = 0;
printf("请输入:\n");
scanf("%d%d%d", &a, &b, &c);
if ((a + b > c&&a - b<c) || (a + c>b&&a - c<b) || (b + c>a&&b - c < a))
{
if ((a == b) || (a == c) || (b == c))
printf("这是一个等腰三角形\n");
if ((a == b) && (b == c))
printf("这是一个等边三角形\n");
else
printf("这是一个不等边三角形\n");
}
else
{
printf("这三条边不能构成三角形!\n");
}
system("pause");
return 0;
}
4.编写函数copy_n,它的函数原型如下:void copy_n(char dst[],char src[],int n);
这个函数用于把一个字符串从数组src复制到dst,但有如下要求:必须正好复制n个字符到dst,不能多,也不能少。如果src字符串的长度小于n,你必须在复制后的
字符串尾部补充足够的NUL字符,使它的长度正好为n。如果src的长度长于或等于n,那么你在dst中存储了n个字符便可停止。此时,数组dst将不是以NUL结尾。
注意调用copy_n时,它应该在dst[0]至dst[n-1]的空间中存储一些东西,但也只限于那些位置,这与src的长度无关。
void copy_n(char dst[], char src[], int n)
{
int n_copyed = 0;//已经复制的字节数
while (*src != '\0')//注意*src
{
if (n_copyed == n)//n<sizeof(src)
{
break;
}
*dst++ = *src++;
n_copyed++;
}
if (n_copyed < n)//n>sizeof(src)
{
int i = 0;
for (i = 0; i < (n - n_copyed); i++)
{
*dst = '\0';
dst++;
}
}
}
int main()
{
char dst[10] = { 0 };
char src[10] = { "asdf" };
int n = 3;
copy_n(dst, src, n);
printf(dst);
system("pause");
return 0;
}
5.编写一个程序,从标准输入一行一行地读取文本,并完成如下任务:如果文件中有两行或更多行相邻的文本内容相同,那么就打印出其中一行,其余的行不打印,
你可以假设文件中的文本行在长度上不会超过128个字符。
提示:使用gets函数读取输入行,使用strcpy来复制它们,strcmp对其进行比较,相等函数返回0,不相等返回非0值
This is the first line
Another line
And another
And another
And another
And another
Still more
Almost done now--
Almost done now--
Another line
Still more
Finished
#define MAX_LINE 20 //最大处理行数
#define MAX_COLS 128//最大处理列数
//处理输入行数据
void range(char input[MAX_LINE][MAX_COLS], char output[MAX_LINE], int i)
{
int flag =0;
while (flag <= i)
{
if (strcmp(input[flag], input[flag + 1]) == 0)
{
strcpy(output, input[flag]);
if (strcmp(output, input[flag + 2]) != 0)
printf("%s\n", output);
}
flag++;
}
}
int main()
{
char input[MAX_LINE][MAX_COLS] = { 0 }; //存放输入行的
char output[MAX_LINE] = { 0 }; //存放输出行
int i=0;
//把输入行依次存入input数组中用ctrl+z结束输入
while (gets(input[i]) != NULL)
{
i++;
}
range(input, output, i);//调用了range函数
system("pause");
return 0;
}
6.请编写一个函数,它从一个字符串中提取一个字符串。函数的原型应该如下:
int substr(char dst[],char src[],int start,int len);
函数的任务是从src数组起始位置向后偏移start个字符的位置开始,最多复制len个非NULL字符到dst数组。复制完毕后,dst数组必须以NULL字节结尾
函数的返回值是存储于dst数组中的字符串长度。
如果,start所指定的位置越过了src数组的尾部,或者start或len的值为负,那么复制到dst数组的是个空字符串。
#include <assert.h>
int substr(char dst[], char src[], int start, int len)
{
assert(dst);
assert(src);
char *p = src + start;//指针变量指向要提取字符串的地址
int i = 0;
int n = strlen(p);//start后面字符串的长度
if (n < len)
len = n;
while (len)
{
dst[i] = src[start-1];
i++;
len--;
start++;
}
dst[i + 1] = '\0';
i = i + 1;
return i;
}
int main()
{
char src[20] = "heloworld";
char dst[20] = { 0 };
int len = 6;
int start = 1;
substr(dst, src, start, len);
printf("%s", dst);
system("pause");
return 0;
}
7.编写一个函数,从一个字符串中除去多余的空格。函数原型:
void deblank(char string[])
当函数发现字符串中如果有一个地方由一个或者多个连续的空格组成,就把它们改成单个空格字符。注意当你遍历整个字符串是要确保它以NUL字符结尾
void deblank(char string[])
{
int i = 0;
int len = 0;
while (1)
{
string[i] = getchar();
if (string[i] == '\n')
break;
i++;
}
len = i;
for (i = 0; i < len; i++)
{
if (string[i] == ' ' && string[i - 1] == ' ')
continue;
putchar(string[i]);
}
}
int main()
{
char c[120];
printf("请输入:\n");
deblank(c);
system("pause");
return 0;
}
转载于:https://blog.51cto.com/seann/1740215