鱼C论坛

 找回密码
 立即注册
查看: 1064|回复: 2

求源代码

[复制链接]
发表于 2023-5-26 11:11:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1)用户定义一个求圆周长、一个求圆面积的两个函数,在主函数中输入圆的半径,调用两个函数,即可求出圆周长和圆面积。
2)用户定义一个求字符串中所有大写英文字符和小写英文字符和数字总数的函数,和一个能将大小写进行转换的函数。在主函数中输入一串字符串,调用该两个函数,即可统计该输入的字符串共哟多少大小写英文和数字总数,并将大小写进行转换输出。

俩个题目的代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-26 11:12:52 | 显示全部楼层
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]); // 转换为大写字母
        }
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-27 20:29:02 | 显示全部楼层
第一题代码:
#include <iostream>
using namespace std;

const double PI = 3.14159265358979323846;

// 求圆周长函数
double getCircumference(double radius) {
    return 2 * PI * radius;
}

// 求圆面积函数
double getArea(double radius) {
    return PI * radius * radius;
}

int main() {
    double radius;
    cout << "请输入圆的半径: ";
    cin >> radius;

    // 调用函数并输出结果
    cout << "圆的周长为:" << getCircumference(radius) << endl;
    cout << "圆的面积为:" << getArea(radius) << endl;

    return 0;
}

第二题代码:
#include <iostream>
#include <string>
using namespace std;

// 统计字符串中大小写英文字母和数字的数量
void countChar(string str) {
    int upper_cnt = 0;  // 大写字母数量
    int lower_cnt = 0;  // 小写字母数量
    int digit_cnt = 0;  // 数字数量

    for (char ch : str) {
        if (isupper(ch)) {
            upper_cnt++;
        } else if (islower(ch)) {
            lower_cnt++;
        } else if (isdigit(ch)) {
            digit_cnt++;
        }
    }

    cout << "大写字母数量: " << upper_cnt << endl;
    cout << "小写字母数量: " << lower_cnt << endl;
    cout << "数字数量: " << digit_cnt << endl;
}

// 将字符串大小写进行转换
void switchCase(string& str) {
    for (char& ch : str) {
        if (isupper(ch)) {
            ch = tolower(ch);
        } else if (islower(ch)) {
            ch = toupper(ch);
        }
    }
}

int main() {
    string str;
    cout << "请输入一串字符串: ";
    getline(cin, str);

    // 调用函数并输出结果
    countChar(str);
    switchCase(str);
    cout << "转换后的字符串为: " << str << endl;

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-27 10:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表