当前位置:首页 » 编程语言 » c语言中可视化程序设计计算器代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言中可视化程序设计计算器代码

发布时间: 2022-05-23 20:02:44

c语言编写计算器程序

C语言编写计算器

  • 我们可以用printf和scanf函数输出结果和获取用户的输入。需要<stdio.h>头文件。scanf函数在读取数据的时候不需要再一行上输入每个数据,只要数据和数据之间留出空白就可以了。先声明两个变量number1和number2,operation变量用来存储运算符。用scanf函数获取这两个数字和运算符。分别用%lf %c %lf

❷ 求一道C语言中关于计算器的程序代码

这个已经有计算功能了,但记事本还没有,给你急用。因为C没有专门的字符串类型:有事再联系405719864// C语言计算器.cpp : Defines the entry point for the console application.
//
/*注意!本程序用C编写,在VC++6.0中运行通过,如果要有记事本功能,可能要用C++写,因为C没有专门的字符串类型*/
#include "stdafx.h"
#include <stdio.h>
#include <ctype.h>char c,operate;//c用于保存计算符号,operate表示当前输入
double n,m;//记录输入的数字
bool check_c,check_n,check_m;//各个输入的开关,当输入发生改变时,可以有根据地操作double count(double n,char ch,double m)//根据不同的计算符号计算结果
{
if(ch == '+') return n+m;
if(ch == '-') return n-m;
if(ch == '*') return n*m;
if(ch == '/')
{
if(m != 0) return n/m;
else printf("0不能作除数!\n");
}
return n;
}int main()
{
n=m=0;
check_c=check_n=check_m=false;
printf("0\n"); printf("若要结束,请输入任意字母!\n");
while(scanf("%c",&operate) != EOF)//表示输入以EOF结束
{
if(operate == 0x0d || operate==' ' || operate=='\n') continue;//表示排除无效字符
if(operate == '#')
{
n=m=0;
check_c=check_n=check_m=false;
printf("0\n");
}
else
{
if(isdigit(operate))
{
if(!check_n) { n=n*10+operate-'0'; printf("%.0lf\n",n); }
else { m=m*10+operate-'0'; check_m=true; check_c=true; printf("%.0lf\n",m); }
}
else
{
if(operate=='+' || operate=='-' || operate=='*' || operate=='/' || operate=='=')//出现运算符号
{
if(operate != '=')//非“=”,则第一个数字输入结束,并记录当前计算符号
{
check_n=true;
if(!check_c) c=operate;
}
if(check_n && check_m)//两个数字都有输入,就进行计算
{
n=count(n,c,m);
printf("%.0lf\n",n);
if(operate != '=')//出现连续等号,连续计算,非等号,则记录并从新输入第二个数字
{
c=operate;
check_c=false;
m=0;
check_m=false;
}
}
else printf("%.0lf\n",n);
}
else
{
printf("输入数据无效!\n");//表示以任意字母字符结束
break;
}
}
}
}
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>/*字符操作函数*/
#defineUP0x48/*光标上移键*/
#defineDOWN0x50/*光标下移键*/
#defineLEFT0x4b/*光标左移键*/
#defineRIGHT0x4d/*光标右移键*/
#defineENTER0x0d/*回车键*/
void*rar;/*全局变量,保存光标图象*/
structpalettetypepalette;/*使用调色板信息*/
intGraphDriver;/*图形设备驱动*/
intGraphMode;/*图形模式值*/
intErrorCode;/*错误代码*/
intMaxColors;/*可用颜色的最大数值*/
intMaxX,MaxY;/*屏幕的最大分辨率*/
doubleAspectRatio;/*屏幕的像素比*/
voiddrawboder(void);/*画边框函数*/
voidinitialize(void);/*初始化函数*/
voidcomputer(void);/*计算器计算函数*/
voidchangetextstyle(intfont,intdirection,intcharsize);/*改变文本样式函数*/
voidmwindow(char*header);/*窗口函数*/
intspecialkey(void);/*获取特殊键函数*/
intarrow();/*设置箭头光标函数*/
/*主函数*/
intmain()
{
initialize();/*设置系统进入图形模式*/
computer();/*运行计算器*/
closegraph();/*系统关闭图形模式返回文本模式*/
return(0);/*结束程序*/
}
/*设置系统进入图形模式*/
voidinitialize(void)
{
intxasp,yasp;/*用于读x和y方向纵横比*/
GraphDriver=DETECT;/*自动检测显示器*/
initgraph(&GraphDriver,&GraphMode,"");
/*初始化图形系统*/
ErrorCode=graphresult();/*读初始化结果*/
if(ErrorCode!=grOk)/*如果初始化时出现错误*/
{
printf("GraphicsSystemError:%s\n",
grapherrormsg(ErrorCode));/*显示错误代码*/
exit(1);/*退出*/
}
getpalette(&palette);/*读面板信息*/
MaxColors=getmaxcolor()+1;/*读取颜色的最大值*/
MaxX=getmaxx();/*读屏幕尺寸*/
MaxY=getmaxy();/*读屏幕尺寸*/
getaspectratio(&xasp,&yasp);/*拷贝纵横比到变量中*/
AspectRatio=(double)xasp/(double)yasp;/*计算纵横比值*/
}
/*计算器函数*/
voidcomputer(void)
{
structviewporttypevp;/*定义视口类型变量*/
intcolor,height,width;
intx,y,x0,y0,i,j,v,m,n,act,flag=1;
floatnum1=0,num2=0,result;/*操作数和计算结果变量*/
charcnum[5],str2[20]={""},c,temp[20]={""};
charstr1[]="1230.456+-789*/Qc=^%";/*定义字符串在按钮图形上显示的符号*/
mwindow("Calculator");/*显示主窗口*/
color=7;/*设置灰颜色值*/
getviewsettings(&vp);/*读取当前窗口的大小*/
width=(vp.right+1)/10;/*设置按钮宽度*/
height=(vp.bottom-10)/10;/*设置按钮高度*/
x=width/2;/*设置x的坐标值*/
y=height/2;/*设置y的坐标值*/
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.");/*输出字符串"0."*/
x=2*width-width/2;/*设置x的坐标值*/
y=2*height+height/2;/*设置y的坐标值*/
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]);
/*将字符保存到str2中*/
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);/*移动光标到x,y位置*/
arrow();/*显示光标*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,"");/*设置str2为空串*/
while((v=specialkey())!=45)/*当压下Alt+x键结束程序,否则执行下面的循环*/
{
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];/*将字符保存到变量c中*/
if(isdigit(c)||c=='.')/*判断是否是数字或小数点*/
{
if(flag==-1)/*如果标志为-1,表明为负数*/
{
strcpy(str2,"-");/*将负号连接到字符串中*/
flag=1;
}/*将标志值恢复为1*/
sprintf(temp,"%c",c);/*将字符保存到字符串变量temp中*/
strcat(str2,temp);/*将temp中的字符串连接到str2中*/
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,"");/*将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)/*如果str2为空,说明是负号,而不是减号*/
flag=-1;/*设置负数标志*/
else
{
num1=atof(str2);/*将第二个操作数转换为浮点数*/
strcpy(str2,"");/*将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,"");/*将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,"");/*将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,"");/*将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,"");/*将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)/*根据运算符号计算*/
{
case1:result=num1+num2;break;/*做加法*/
case2:result=num1-num2;break;/*做减法*/
case3:result=num1*num2;break;/*做乘法*/
case4:result=num1/num2;break;/*做除法*/
case5:result=pow(num1,num2);break;/*做x的y次方*/
case6: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);/*将结果保存到temp中*/
outtextxy(5*width,height,temp);/*显示结果*/
}
if(c=='c')
{
num1=0;/*将两个操作数复位0,符号标志为1*/
num2=0;
flag=1;
strcpy(str2,"");/*将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);/*如果选择了q回车,结束计算程序*/
}
putimage(x,y,rar,XOR_PUT);/*在退出之前消去光标箭头*/
return;/*返回*/
}
/*窗口函数*/
voidmwindow(char*header)
{
intheight;
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();/*画边框*/
}
voiddrawboder(void)/*画边框*/
{
structviewporttypevp;/*定义视口类型变量*/
setcolor(MaxColors-1);/*设置当前颜色为白色*/
setlinestyle(SOLID_LINE,0,NORM_WIDTH);/*设置画线方式*/
getviewsettings(&vp);/*将当前视口信息装入vp所指的结构中*/
rectangle(0,0,vp.right-vp.left,vp.bottom-vp.top);/*画矩形边框*/
}
/*设计鼠标图形函数*/
intarrow()
{
intsize;
intraw[]={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);/*消去光标箭头图象*/
return0;
}
/*按键函数*/
intspecialkey(void)
{
intkey;
while(bioskey(1)==0);/*等待键盘输入*/
key=bioskey(0);/*键盘输入*/
key=key&0xff?key&0xff:key>>8;/*只取特殊键的扫描值,其余为0*/
return(key);/*返回键值*/
}

❹ c语言设计简单计算器代码

#include <iostream.h>
#include <stack>
using namespace std;
int precede(char op1,char op2)//> 1,= 0,<-1
{
switch(op1)
{
case '+':
if(op2=='+' || op2=='-' || op2==')' || op2=='#')
return 1;
return -1;
case '-':
if(op2=='+' || op2=='-' || op2==')' || op2=='#')
return 1;
return -1;
case '*':
if(op2=='(')
return -1;
return 1;
case '/':
if(op2=='(')
return -1;
return 1;
case '(':
if(op2==')')
return 0;
return -1;
case ')':
return 1;
case '#':
return -1;
default:
break;
}
return 0;
}
int compute(int num1,char op,int num2)
{
int num=0;
switch(op)
{
case '+':
num=num1+num2;
break;
case '-':
num=num1-num2;
break;
case '*':
num=num1*num2;
break;
case '/':
num=num1/num2;
break;
}
return num;
}
int calculator(char str[205])
{
int i,pre,num,num1,num2;
char op1;
bool flag;
stack<char> charsta;
stack<int> intsta;
charsta.push('#');
i=strlen(str);
str[i]='#';
str[i+1]='\0';
i=0,num=0;
flag=false;
while(str[i])
{
if(str[i]>='0' && str[i]<='9')
{
flag=true;
num=num*10+str[i]-'0';
i++;
}
else
{
if(flag)
{
flag=false;
intsta.push(num);
num=0;
}
op1=charsta.top();
if(op1=='#' && str[i]=='#')
break;
pre=precede(op1,str[i]);
switch(pre)
{
case 0:
charsta.pop();
i++;
break;
case 1:
charsta.pop();
num2=intsta.top();
intsta.pop();
num1=intsta.top();
intsta.pop();
intsta.push(compute(num1,op1,num2));
break;
case -1:
charsta.push(str[i]);
i++;
break;
}
}
}
num=intsta.top();
intsta.pop();
return num;
}
int main()
{
char str[205];
int i,n;
cin>>n;//数据组数
for(i=0;i<n;i++)
{
cin>>str; //输入表达式,如((5-3)*2-1)
printf("%d\n",calculator(str));//计算结果
}
return 0;
}

❺ 如何用C语言设计一个程序模拟有图形界面的计算器

  • 首先,打开Vs 2010,如图

❻ C语言程序设计简易计算器

1、首先,打开Vs 2010,如图。

2、找到左上角的新建并点击,给文件为简单计算器,单击确定。

3、点击下一步,注意勾选空项目,点击下一步,点击完成。

4、点击左侧的源文件,右击选择“添加—>项目”,选择C++文件,命名为简单计算器,因为是C程序,注意后缀名要加上.c,点击确定完成文件新建工作。

5、输入以下代码,好了,一个简单的计算器便做好了

❼ 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; /*设置x的坐标值*/
y = height/2; /*设置y的坐标值*/
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."); /*输出字符串"0."*/
x =2*width-width/2; /*设置x的坐标值*/
y =2*height+height/2; /*设置y的坐标值*/
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]);
/*将字符保存到str2中*/
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); /*移动光标到x,y位置*/
arrow(); /*显示光标*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,""); /*设置str2为空串*/
while((v=specialkey())!=45) /*当压下Alt+x键结束程序,否则执行下面的循环*/
{
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;

❽ c语言一个简单计算器的设计程序,代码如下,不是很看得懂,求解释!

先说头文件
从第一个开始 基本输入输出
第二个 第三个 都是 输入输出,都差不多,
定义了 一个 整形 a 数组 里面有10000个数
定义了 一个字符型 b 里面有10000个数
然后 定义 i,num,c 没有赋值
其余的那几个 就是定义给一个值
在 do while语句里
叫你输入一个整数 一个字符,值 传递给了 num和c
这里是先做后循环,然后a是数组加上 cnt1加1的值给num,另一个给 c,开始循环,并判断 c如果不等于 =号 ,就做下面的赋值,
下面接上for循环,i的循环条件来自于cnt,做一次 j减一次i,剩下的就都是判断数组的符号,看起来像是个计算器,他们的条件就是 条件1和条件2和条件3和条件4这样,判断完给一个返回,把值返回给数组,然后再显示出来,如果有输入错误 或判断错误 就会从最下面error那句打印出来
不懂可以再来追问我

❾ 求助用C语言程序设计一个计算器

已经在TC下编译通过,有界面
#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; /*设置x的坐标值*/
y = height/2; /*设置y的坐标值*/
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."); /*输出字符串"0."*/
x =2*width-width/2; /*设置x的坐标值*/
y =2*height+height/2; /*设置y的坐标值*/
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]);
/*将字符保存到str2中*/
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); /*移动光标到x,y位置*/
arrow(); /*显示光标*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,""); /*设置str2为空串*/
while((v=specialkey())!=45) /*当压下Alt+x键结束程序,否则执行下面的循环*/
{
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]; /*将字符保存到变量c中*/
if(isdigit(c)||c=='.') /*判断是否是数字或小数点*/
{
if(flag==-1) /*如果标志为-1,表明为负数*/
{
strcpy(str2,"-"); /*将负号连接到字符串中*/
flag=1;
} /*将标志值恢复为1*/
sprintf(temp,"%c",c); /*将字符保存到字符串变量temp中*/
strcat(str2,temp); /*将temp中的字符串连接到str2中*/
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,""); /*将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) /*如果str2为空,说明是负号,而不是减号*/
flag=-1; /*设置负数标志*/
else
{
num1=atof(str2); /*将第二个操作数转换为浮点数*/
strcpy(str2,""); /*将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,""); /*将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,""); /*将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,""); /*将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,""); /*将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; /*做x的y次方*/
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); /*将结果保存到temp中*/
outtextxy(5*width,height,temp); /*显示结果*/
}
if(c=='c')
{
num1=0; /*将两个操作数复位0,符号标志为1*/
num2=0;
flag=1;
strcpy(str2,""); /*将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); /*如果选择了q回车,结束计算程序*/
}
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 );/*将当前视口信息装入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; /*只取特殊键的扫描值,其余为0*/
return(key); /*返回键值*/
}

❿ 怎么用C语言程序设计一个简单计算器

#include<<a href="https://www..com/s?wd=stdio.h&tn=44039180_cpr&fenlei=-bIi4WUvYETgN-TLwGUv3EPH6srjc4rH61" target="_blank" class="-highlight">stdio.h</a>>

void main() { float x,y,z; char c;

scanf("%f%c%f",&x,&c,&y);

switch ( c ) {

case '+': z=x+y; break;

case '-': z=x-y; break;

case '*': z=x*y; break;

case '/': z=( y==0 )?(0):(x/y); break;

default: z=0; break;

}

printf("%f%c%f=%f ",x,c,y,z);

}