|
1)求圆周长和面积的代码:
#include <stdio.h>
#define PI 3.1415926
float circleArea(float radius); // 函数声明
float circleCircumference(float radius);
int main() {
float radius;
printf("请输入圆的半径:");
scanf("%f", &radius);
printf("圆的周长为:%f\n", circleCircumference(radius));
printf("圆的面积为:%f\n", circleArea(radius));
return 0;
}
float circleArea(float radius) { // 函数定义
return PI * radius * radius;
}
float circleCircumference(float radius) {
return 2 * PI * radius;
}
2)求字符串中大小写英文和数字总数,并转换大小写的代码:
#include <stdio.h>
#include <ctype.h> // 包含ctype.h头文件
int countChar(char str[]); // 函数声明
void convertCase(char str[]);
int main() {
char str[100];
printf("请输入一串字符串:");
scanf("%s", str);
printf("该字符串中共有%d个数字和英文字母\n", countChar(str));
convertCase(str);
printf("转换后的字符串为:%s\n", str);
return 0;
}
int countChar(char str[]) { // 函数定义
int count = 0;
for (int i = 0; str[ i] != '\0'; i++) {
if (isalnum(str[ i])) { // 判断是否为数字或英文字母
count++;
}
}
return count;
}
void convertCase(char str[]) {
for (int i = 0; str[ i] != '\0'; i++) {
if (isupper(str[ i])) { // 判断是否为大写字母
str[ i] = tolower(str[ i]); // 转换为小写字母
} else if (islower(str[ i])) { // 判断是否为小写字母
str[ i] = toupper(str[ i]); // 转换为大写字母
}
}
} |
|