c语言模拟计算,c语言模拟计算器输入两个整数和一个运算符

发布时间:2023-01-08

本文目录一览:

  1. c语言模拟简单计算,无优先级,只计算加减乘除,输入没有空格
  2. C语言模拟计算器的功能,要求至少能实现基本四则运算(加减乘除功能),要求提示明确.能用/*说明代码意思最好
  3. c语言模拟计算器四则混合运算
  4. C语言 设计一个 模拟计算器程序

c语言模拟简单计算,无优先级,只计算加减乘除,输入没有空格

#include<stdio.h>
#include<stdlib.h>
int main()
{
    float a,b;
    int oper;
    printf("请输入a,b两个数\n");
    scanf("%f %f",&a,&b);
    printf("请输入对应的 + - * /符号,对应的是1234\n");
    scanf("%d",&oper);
    float c1=a+b,c2=a-b,c3=a*b,c4=a/b;
    switch(oper)
    {
        case 1: printf("%f\n",c1);break;
        case 2: printf("%f\n",c2);break;
        case 3: printf("%f\n",c3);break;
        case 4:
            if(b==0)
            {
                printf("除数为0,不可进行运算\n");
                exit(-1);
            }
            printf("%f\n",c4);break;
    }
    return 0;
}

C语言模拟计算器的功能,要求至少能实现基本四则运算(加减乘除功能),要求提示明确.能用/*说明代码意思最好

#include<iostream>
using namespace std;
int main()
{
    int a,b; // a是输出结果,b是临时输入数据
    char x; // x是标点符号输入
    cin >> a; // 先输入第一个数
    while(1) // 由于不知道运算式一共多长,所以用一个死循环不断读取
    {
        cin >> x; // 输入运算符
        if(x == '=') break; // 如果输入的是等号,则结束循环
        cin >> b; // 输入第二个数
        switch(x)
        {
            case '+': a += b; break;
            case '-': a -= b; break;
            case '*': a *= b; break;
            case '/':
                if(b == 0)
                {
                    cout << "除数不能为0" << endl;
                    return -1;
                }
                a /= b;
                break;
            default:
                cout << "输入的运算符错误" << endl;
                return -1;
        }
    }
    cout << "结果为:" << a << endl;
    return 0;
}

c语言模拟计算器四则混合运算

// 此算法只能进行整数四则混合运算,也不支持括号
// 按下面的格式输入表达式(末尾无=)
// 10-5+4+6/2*14*8/4-5*7+2-4*6/2-10*4-6/3
#include<stdio.h>
int jisuan(int num1, char op1, int num2)
{
    switch(op1)
    {
        case '*': return num1*num2;
        case '/': return num1/num2;
        case '+': return num1+num2;
        case '-': return num1-num2;
    }
}
int process(int num1, char op1, int num2)
{
    int r, num3;
    char op2;
    if('\n' != (op2 = getchar()))
    {
        scanf("%d", &num3);
        if('+' == op2 || '-' == op2)
        {
            num1 = jisuan(num1, op1, num2);
            r = process(num1, op2, num3);
        }
        else if('*' == op2 || '/' == op2)
        {
            if('/' == op1)
            {
                num1 = jisuan(num1, op1, num2);
                r = process(num1, op2, num3);
            }
            else
            {
                num2 = jisuan(num2, op2, num3);
                r = process(num1, op1, num2);
            }
        }
    }
    else
    {
        r = jisuan(num1, op1, num2);
    }
    return r;
}
int main()
{
    int num1, num2;
    char op;
    printf("请输入一个表达式:");
    scanf("%d%c%d", &num1, &op, &num2);
    printf("=%d\n", process(num1, op, num2));
    return 0;
}

C语言 设计一个 模拟计算器程序

#include <dos.h> /*DOS接口函数*/
#include <math.h> /*数学函数的定义*/
#include <conio.h> /*屏幕操作函数*/
#include <stdio.h> /*I/O函数*/
#include <stdlib.h> /*库函数*/
#include <stdarg.h> /*变量长度参数表*/
#include <graphics.h> /*图形函数*/
#include <string.h> /*字符串函数*/
#include <ctype.h> /*字符操作函数*/
#define UP 0x48 /*光标上移键*/
#define DOWN 0x50 /*光标下移键*/
#define LEFT 0x4b /*光标左移键*/
#define RIGHT 0x4d /*光标右移键*/
#define ENTER 0x0d /*回车键*/
void *rar; /*全局变量,保存光标图象*/
struct palettetype palette; /*使用调色板信息*/
int GraphDriver; /* 图形设备驱动*/
int GraphMode; /* 图形模式值*/
int ErrorCode; /* 错误代码*/
int MaxColors; /* 可用颜色的最大数值*/
int MaxX, MaxY; /* 屏幕的最大分辨率*/
double AspectRatio; /* 屏幕的像素比*/
void drawboder(void); /*画边框函数*/
void initialize(void); /*初始化函数*/
void computer(void); /*计算器计算函数*/
void changetextstyle(int font, int direction, int charsize); /*改变文本样式函数*/
void mwindow(char *header); /*窗口函数*/
int specialkey(void); /*获取特殊键函数*/
int arrow(); /*设置箭头光标函数*/
/*主函数*/
int main()
{
    initialize(); /* 设置系统进入图形模式 */
    computer(); /*运行计算器 */
    closegraph(); /*系统关闭图形模式返回文本模式*/
    return(0); /*结束程序*/
}
/* 设置系统进入图形模式 */
void initialize(void)
{
    int xasp, yasp; /* 用于读x和y方向纵横比*/
    GraphDriver = DETECT; /* 自动检测显示器*/
    initgraph(&GraphDriver, &GraphMode, "");
    /*初始化图形系统*/
    ErrorCode = graphresult(); /*读初始化结果*/
    if( ErrorCode != grOk ) /*如果初始化时出现错误*/
    {
        printf("Graphics System Error: %s\n", grapherrormsg( ErrorCode ));
        exit( 1 );
    }
    getpalette( palette ); /* 读面板信息*/
    MaxColors = getmaxcolor() + 1; /* 读取颜色的最大值*/
    MaxX = getmaxx(); /* 读屏幕尺寸 */
    MaxY = getmaxy(); /* 读屏幕尺寸 */
    getaspectratio( &xasp, &yasp ); /* 拷贝纵横比到变量中*/
    AspectRatio = (double)xasp/(double)yasp; /* 计算纵横比值*/
}
/*计算器函数*/
void computer(void)
{
    struct viewporttype vp;
    int color, height, width;
    int x, y, x0, y0, i, j, v, m, n, act, flag=1;
    float num1=0, num2=0, result;
    char cnum[5], str2[20]={""}, c, temp[20]={""};
    char str1[]="1230.456+-789*/Qc=^%";
    mwindow( "Calculator" );
    color = 7;
    getviewsettings( &vp );
    width=(vp.right+1)/10;
    height=(vp.bottom-10)/10;
    x = width /2;
    y = height/2;
    setfillstyle(SOLID_FILL, color+3);
    bar( x+width*2, y, x+7*width, y+height );
    setcolor( color+3 );
    rectangle( x+width*2, y, x+7*width, y+height );
    setcolor(RED);
    outtextxy(x+3*width,y+height/2,"0.");
    x =2*width-width/2;
    y =2*height+height/2;
    for( j=0 ; j<4 ; ++j )
    {
        for( i=0 ; i<5 ; ++i )
        {
            setfillstyle(SOLID_FILL, color);
            setcolor(RED);
            bar( x, y, x+width, y+height );
            rectangle( x, y, x+width, y+height );
            sprintf(str2,"%c",str1[j*5+i]);
            outtextxy( x+(width/2), y+height/2, str2);
            x =x+width+ (width / 2) ;
        }
        y +=(height/2)*3;
        x =2*width-width/2;
    }
    x0=2*width;
    y0=3*height;
    x=x0;
    y=y0;
    gotoxy(x,y);
    arrow();
    putimage(x,y,rar,XOR_PUT);
    m=0;
    n=0;
    strcpy(str2,"");
    while((v=specialkey())!=45)
    {
        while((v=specialkey())!=ENTER)
        {
            putimage(x,y,rar,XOR_PUT);
            if(v==RIGHT)
            {
                if(x==x0+6*width)
                {
                    x=x0;
                    m=0;
                }
                else
                {
                    x=x+width+width/2;
                    m++;
                }
            }
            if(v==LEFT)
            {
                if(x==x0)
                {
                    x=x0+6*width;
                    m=4;
                }
                else
                {
                    x=x-width-width/2;
                    m--;
                }
            }
            if(v==UP)
            {
                if(y==y0)
                {
                    y=y0+4*height+height/2;
                    n=3;
                }
                else
                {
                    y=y-height-height/2;
                    n--;
                }
            }
            if(v==DOWN)
            {
                if(y==7*height)
                {
                    y=y0;
                    n=0;
                }
                else
                {
                    y=y+height+height/2;
                    n++;
                }
            }
            putimage(x,y,rar,XOR_PUT);
        }
        c=str1[n*5+m];
        if(isdigit(c)||c=='.')
        {
            if(flag==-1)
            {
                strcpy(str2,"-");
                flag=1;
            }
            sprintf(temp,"%c",c);
            strcat(str2,temp);
            setfillstyle(SOLID_FILL,color+3);
            bar(2*width+width/2,height/2,15*width/2,3*height/2);
            outtextxy(5*width,height,str2);
        }
        if(c=='+')
        {
            num1=atof(str2);
            strcpy(str2,"");
            act=1;
            setfillstyle(SOLID_FILL,color+3);
            bar(2*width+width/2,height/2,15*width/2,3*height/2);
            outtextxy(5*width,height,"0.");
        }
        if(c=='-')
        {
            if(strcmp(str2,"")==0)
                flag=-1;
            else
            {
                num1=atof(str2);
                strcpy(str2,"");
                act=2;
                setfillstyle(SOLID_FILL,color+3);
                bar(2*width+width/2,height/2,15*width/2,3*height/2);
                outtextxy(5*width,height,"0.");
            }
        }
        if(c=='*')
        {
            num1=atof(str2);
            strcpy(str2,"");
            act=3;
            setfillstyle(SOLID_FILL,color+3);
            bar(2*width+width/2,height/2,15*width/2,3*height/2);
            outtextxy(5*width,height,"0.");
        }
        if(c=='/')
        {
            num1=atof(str2);
            strcpy(str2,"");
            act=4;
            setfillstyle(SOLID_FILL,color+3);
            bar(2*width+width/2,height/2,15*width/2,3*height/2);
            outtextxy(5*width,height,"0.");
        }
        if(c=='^')
        {
            num1=atof(str2);
            strcpy(str2,"");
            act=5;
            setfillstyle(SOLID_FILL,color+3);
            bar(2*width+width/2,height/2,15*width/2,3*height/2);
            outtextxy(5*width,height,"0.");
        }
        if(c=='%')
        {
            num1=atof(str2);
            strcpy(str2,"");
            act=6;
            setfillstyle(SOLID_FILL,color+3);
            bar(2*width+width/2,height/2,15*width/2,3*height/2);
            outtextxy(5*width,height,"0.");
        }
        if(c=='=')
        {
            num2=atof(str2);
            switch(act)
            {
                case 1:result=num1+num2;break;
                case 2:result=num1-num2;break;
                case 3:result=num1*num2;break;
                case 4:result=num1/num2;break;
                case 5:result=pow(num1,num2);break;
                case 6:result=fmod(num1,num2);break;
            }
            setfillstyle(SOLID_FILL,color+3);
            bar(2*width+width/2,height/2,15*width/2,3*height/2);
            sprintf(temp,"%f",result);
            outtextxy(5*width,height,temp);
        }
        if(c=='c')
        {
            num1=0;
            num2=0;
            flag=1;
            strcpy(str2,"");
            setfillstyle(SOLID_FILL,color+3);
            bar(2*width+width/2,height/2,15*width/2,3*height/2);
            outtextxy(5*width,height,"0.");
        }
        if(c=='Q') exit(0);
    }
    putimage(x,y,rar,XOR_PUT);
    return;
}
/*窗口函数*/
void mwindow( char *header )
{
    int height;
    cleardevice();
    setcolor( MaxColors - 1 );
    setviewport( 20, 20, MaxX/2, MaxY/2, 1 );
    height = textheight( "H" );
    settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
    settextjustify( CENTER_TEXT, TOP_TEXT );
    outtextxy( MaxX/4, 2, header );
    setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 );
    drawboder();
}
void drawboder(void)
{
    struct viewporttype vp;
    setcolor( MaxColors - 1 );
    setlinestyle( SOLID_LINE, 0, NORM_WIDTH );
    getviewsettings( &vp );
    rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top );
}
/*设计鼠标图形函数*/
int arrow()
{
    int size;
    int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4};
    setfillstyle(SOLID_FILL,2);
    fillpoly(8,raw);
    size=imagesize(4,4,16,16);
    rar=malloc(size);
    getimage(4,4,16,16,rar);
    putimage(4,4,rar,XOR_PUT);
    return 0;
}
/*按键函数*/
int specialkey(void)
{
    int key;
    while(bioskey(1)==0);
    key=bioskey(0);
    key=key&0xff ? key&0xff : key>>8;
    return(key);
}