• calc


    大学学的是计算机专业,所以又拾起c++
    写个计算器练练手
    以后可能还会更新

    /*
    四则运算器1.0
    只能进行两个数之间的运算
    并且中间不能有空格。。。。
    */
    #include <cstdio>
    #include <iostream>
    
    int main()
    {
        int a,b;
        char c;
        scanf("%d%c%d",&a,&c,&b);
        switch(c)
        {
            case '+':a+=b;break; 
            case '-':a-=b;break;
            case '*':a*=b;break;
            case '/':a/=b;break;
        }
        printf("%d",a);
    
        return 0;
    }
    

    ——————————————————————————————分割线
    9.30更新
    因为1.0实在太过“淳朴”
    所以写完后立即着手更新

    /*
    四则运算器2.0
    只能进行两个数之间的运算
    对空格没什么要求
    */
    #include <cstdio>
    #include <iostream>
    
    char d;
    int work(bool flag)
    {
    	int emp=0;char c;
    	while(c>'9' || c<'0')c=getchar();
    	while(c>='0'&&c<='9'){emp=emp*10+c-'0';c=getchar();}
    	if(flag)d=c;
    	return emp;
    }
    int main()
    {
        int a,b;
    	a=work(true);b=work(false);
        switch(d)
        {
            case '+':a+=b;break; 
            case '-':a-=b;break;
            case '*':a*=b;break;
            case '/':a/=b;break;
        }
        printf("%d",a);
    
        return 0;
    }
    

    ————————————————————分割线
    肝了两天的原神。。。。。
    10.3更新

    /*
    四则运算器2.0
    可以进行多项式运算,不过只能从右向左依次进行(没错就是从右向左,后输入的算式先运行,因为是用递归实现的运算)
    不识别括号
    */
    #include <cstdio>
    #include <iostream>
    
    double work()
    {
    	double a=0;char c=getchar();
    	while(c< '0' || c> '9')c=getchar();
    	while(c>='0' && c<='9'){a=a*10.0+c-'0';c=getchar();}
        switch(c)
        {
    		case '+':a+=work();break;
            case '-':a-=work();break;
            case '*':a*=work();break;
            case '/':a/=work();break;
        }
    	return a;
    }
    int main()
    {
        printf("%lf",work());
        return 0;
    }
    

    --------------------------------分割线
    10.13更新
    这段时间忙的有点头疼
    抽空来更新

    /*
    四则运算器2.2
    从左向右依次计算
    不支持括号
    由getchar改为scanf
    同样用=来解决最后的输入完成判定
    感觉有上世纪的计算器那味了
    */
    #include <cstdio>
    #include <iostream>
    
    double a=0,ans=0;
    char c;
    
    void work()
    {
    	switch(c)
    	{
    		case '+':ans+=a;break;
    		case '-':ans-=a;break;
    		case '*':ans*=a;break;
    		case '/':ans/=a;break;
    	}
    	return ;
    }
    int main()
    {
    	printf("Don't forget to input the '='");
    	scanf("%lf%c%lf",&ans,&c,&a);
    	work();
    	while(c=getchar())
    	{
    		if(c=='=')break;
    		scanf("%lf",&a);
    		work();
    	}
    	printf("%lf",ans);
    	return 0;
    }
    
  • 相关阅读:
    python中单例模式
    python中常用的内置方法
    面向对象之反射
    绑定方法与非绑定方法
    python多态与抽象类
    python的组合与封装
    面向对象之继承与派生
    面向对象之类与对象
    python模块与包
    数据结构与算法_语言和框架特性前瞻和bug修复
  • 原文地址:https://www.cnblogs.com/kuaileyongheng/p/13755549.html
Copyright © 2020-2023  润新知