用字符double写出用C语言计算三角形面积的代码
时间: 2024-05-14 07:19:43 浏览: 21
以下是使用C语言计算三角形面积的代码:
```
#include <stdio.h>
int main() {
double base, height, area;
printf("Enter the base and height of the triangle: ");
scanf("%lf %lf", &base, &height);
area = 0.5 * base * height;
printf("The area of the triangle is: %lf\n", area);
return 0;
}
```
在上述代码中,我们定义了三个double类型的变量base、height和area,分别表示三角形的底边、高和面积。然后,我们使用printf函数提示用户输入三角形的底边和高,使用scanf函数将用户输入的值存储到变量base和height中。最后,我们使用公式0.5 * base * height计算三角形的面积,并将结果存储到变量area中。最后,我们使用printf函数输出三角形的面积。
相关问题
删除字符串中所有空格 用c语言写出代码
在C语言中,可以使用循环遍历字符串的每个字符,判断是否为空格,如果不是空格则将其保留,最后将保留的字符重新组成一个新的字符串。以下是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
void removeSpaces(char* str) {
int count = 0;
for (int i = 0; str[i]; i++) {
if (str[i] != ' ') {
str[count++] = str[i];
}
}
str[count] = '\0';
}
int main() {
char str[100];
printf("请输入字符串:");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // 去除fgets函数读取的换行符
removeSpaces(str);
printf("删除空格后的字符串为:%s\n", str);
return 0;
}
```
这段代码中,`removeSpaces`函数用于删除字符串中的空格。在`main`函数中,首先使用`fgets`函数获取用户输入的字符串,并使用`strcspn`函数去除输入字符串末尾的换行符。然后调用`removeSpaces`函数删除字符串中的空格,并输出删除空格后的字符串。
用c语言写出解压字符串的代码
以下是用C语言实现的解压字符串的代码:
```c
#include <stdio.h>
#include <string.h>
void decompress(char *str)
{
int len = strlen(str);
int i = 0, j = 0;
while (i < len)
{
if (str[i] == '\\' && str[i+1] != '\0')
{
switch (str[i+1])
{
case 'n':
putchar('\n');
break;
case 't':
putchar('\t');
break;
case '\\':
putchar('\\');
break;
default:
putchar(str[i+1]);
break;
}
i += 2;
}
else
{
putchar(str[i]);
i++;
}
}
}
int main()
{
char str[] = "Hello\\nWorld\\t\\!\\";
decompress(str);
return 0;
}
```
这个代码中,我们定义了一个 `decompress` 函数来解压传入的字符串。在函数内部,我们使用一个循环来遍历整个字符串。如果当前字符是 `\`,我们就判断下一个字符是什么,然后输出相应的字符。如果当前字符不是 `\`,我们就直接输出它。最后,在主函数中,我们定义了一个测试字符串并传入 `decompress` 函数来测试它的功能。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)