鱼C论坛

 找回密码
 立即注册
查看: 822|回复: 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 | 显示全部楼层
第一题代码:

  1. #include <iostream>
  2. using namespace std;

  3. const double PI = 3.14159265358979323846;

  4. // 求圆周长函数
  5. double getCircumference(double radius) {
  6.     return 2 * PI * radius;
  7. }

  8. // 求圆面积函数
  9. double getArea(double radius) {
  10.     return PI * radius * radius;
  11. }

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

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

  19.     return 0;
  20. }
复制代码


第二题代码:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

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

  9.     for (char ch : str) {
  10.         if (isupper(ch)) {
  11.             upper_cnt++;
  12.         } else if (islower(ch)) {
  13.             lower_cnt++;
  14.         } else if (isdigit(ch)) {
  15.             digit_cnt++;
  16.         }
  17.     }

  18.     cout << "大写字母数量: " << upper_cnt << endl;
  19.     cout << "小写字母数量: " << lower_cnt << endl;
  20.     cout << "数字数量: " << digit_cnt << endl;
  21. }

  22. // 将字符串大小写进行转换
  23. void switchCase(string& str) {
  24.     for (char& ch : str) {
  25.         if (isupper(ch)) {
  26.             ch = tolower(ch);
  27.         } else if (islower(ch)) {
  28.             ch = toupper(ch);
  29.         }
  30.     }
  31. }

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

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

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 14:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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