3.2四则运算

例3-5:求解 简单的四则运算表达式,输入一个形式如操作数 运算符 操作数的死者运算表达式,输出运算结果。

#include <stdio.h>
int main()
{
    double value1, value2;
    char c;
    printf("Enter an expression");//提示输入一个表达式
    scanf("%lf%c%lf", &value1, &c, &value2);

    if (c == '+')//加法
    {
        printf("sum=%.2f\n", value1 + value2);
    }
    else if (c == '-')//减法
    {
        printf("min=%.2f\n", value1 - value2);
    }
    else if (c == '*')//乘法
    {
        printf("mul=%.2f\n", value1 * value2);
    }
    else if (c == '/')//除法
    {
        printf("div=%.2f\n", value1 / value2);
    }
    else
    {
        printf("unkown\n");
    }

    return 0;
}

使用函数的方法

#include <stdio.h>
float sum(float a, float b)//对加法函数进行定义
{
    float c;
    c = a + b;
    return (c);
}
float sub(a, b) float a, b;//对减法函数定义
{
    return (a - b);
}
float mult(float a, float b) //对乘法函数进行定义
{
     return (a * b); 
}
float div(float a, float b)//对除法函数进行定义
{
    return (a / b);
}
fn() { printf("To be developed..."); }//可扩展函数
main()
{
    float x, y, result;
    char c;
    printf("input calculate for formula:");
    scanf("%f%c%f", &x, &c, &y);//输入数据和运算符
    if (c == '+')
        result = sum(x, y);
    else if (c == '-')
        result = sub(x, y);
    else if (c == '*')
        result = mult(x, y);
    else if (c == '/')
        result = div(x, y);
    else 
        fn();
    printf("result is %f\n", result);//输出结果
    return (c);
}

例3-7:输入10个字符。统计其中英文字母,数字字符,和其他字符的个数、

#include <stdio.h>
int main()
{
    int digit, letter, other;
    char ch;
    int i;
    digit = letter = other = 0;
    printf("Enter 10 characters:");
    for (i = 1; i <= 10; i++)//循环执行了10次
    {
        ch = getchar();
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
        {
            letter++;
        }
        else if (ch >= '0' && ch <= '9')
        {
            digit++;
        }
        else
        {
            other++;
        }
    }
    printf("letter=%d,digit=%d,other=%d", letter, digit, other);

    return 0;
}

练习3-4:统计字符:输入10个字符。统计其中英文字母,数字字符,空格或回车和其他字符的个数、

#include <stdio.h>
int main()
{
    int digit, letter, other,kongge;
    char ch;
    int i;
    digit = letter = other = 0;
    printf("Enter 10 characters:");
    for (i = 1; i <= 10; i++)//循环执行了10次
    {
        ch = getchar();
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
        {
            letter++;
        }
        else if (ch >= '0' && ch <= '9')
        {
            digit++;
        }
        else if((ch=' ')||(ch='\n'))
        {
            kongge++;
        }
        else
        {
            other++;
        }
    }
    printf("letter=%d,digit=%d,other=%d,kongge=%d", letter, digit, other,kongge);

    return 0;
}

练习3-5:输出闰年:输出21世纪中截止某个年份之前的所有闰年年份,判断闰年的条件是:能被4整除但不能被100整除,或者能被400整除。试编写相应程序。

#include <stdio.h>
int main()
{
    int year,i;
    printf("Enter year:");
    scanf("%d",&year);
    for(i=2000;i<=year;i++)
    {
        if((i%4==0&&i%100!=0)||i%400==0)
        {
            printf("%d\n",i);
        }
    }
            
    if(year<2000&&year>=2100)
    {
        printf("Invalid value!");
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值