可以用鼠标操作的C语言计算器源码

合集下载

C++语言编程计算器源代码

C++语言编程计算器源代码

//计算器,函数#include <iostream>#include <string>#include <ctime>#include <conio.h>#include <cstdlib>#include <cmath>#include <vector>#include <cctype>using namespace std;double simple (string s);//简单运算bool check (char c);//检查是否为数值doublecou (double m,doublen,int c);//用于计算两个数的运算string cut (string str);//括号截取double senior (string str,double x);//高级函数计算double count (string st);//括号运算boolbr_l (char c);boolbr_r (char c);string al (string st);//特殊值与符号处理string abs_cut (string st);//绝对值处理intnum;string s;vector <double> b;//存储高级符号内值int main (){ cout<<"\n\t\t本软件由松哥独家制造\n\n"; while (true){ int i=0,j=0;num=0;string s1;b.clear ();cout<<"输入:"; cin>>s1;string s2(s1);if (s=="#") break;cout<<"输出:"<<s2<<"="<<count (s1)<<endl;} cout<<"\n\t\t感谢您的支持!!!!";}double simple (string st){ int i=0,j=0,k=0;vector <double> p;//存储读入的数字vector <int> q;//存储读入运算符while (st[i]!='\0'&&st[i]!='='){double sum=0;while (!check (st[i])) i++;while (check (st[i])){if (st[i]>64&&st[i]<91){sum=b[st[i]-65];i++;break;}if (st[i]!='.') sum=sum*10+(st[i++]-48);else {i++;double su=0,m=1;while (check (st[i])){m*=10.0;su=su*10+(st[i++]-48);}sum=sum+su/m;}}p.push_back (sum); }if (st[0]=='-') p[0]=-p[0];for (i=1;st[i]!='\0';i++){switch (st[i]){case '=':break;case '^': q.push_back (1);break;case '/': q.push_back (-3);break;case '*': q.push_back (3);break;case '+': q.push_back (4);break;case '-' :q.push_back (-4) ;break;default :if (!check (st[i])) {cout<<"含未知符号"<<st[i]<<"!\n"; return 0;}}}i=p.size (),j=q.size ();if (j==1) return cou (p[0],p[1],q[0]);else while (i!=1)for (int k=0;k<j;k++) {if (k==j-1&&abs (q[k])<abs (q[k-1])){p[k]=cou (p[k],p[k+1],q[k]); i--;j--; for (k;k<q.size ()-1;k++) {p[k+1]=p[k+2];q[k]=q[k+1];}p.pop_back ();q.pop_back ();break;}else if (abs (q[k])<=abs (q[k+1])) {p[k]=cou (p[k],p[k+1],q[k]); i--;j--;for (k;k<q.size ()-1;k++) {p[k+1]=p[k+2];q[k]=q[k+1];}p.pop_back ();q.pop_back ();if (j==1) break; } }return p[0];}bool check (char c){if (c>='0'&&c<='9'||c=='.'||c>64&&c<85) return true;else return false;}doublecou (double m,doublen,int c) {switch (c){case 1:return pow (m,n);case 3:return (m*n);case -3:return (m/n);case 4:return (m+n);case -4:return (m-n);default :cout<<c<<"无法计算!\n";} }string cut (string str){int i=0,m=0,n=0,r=0;string se;for (int j=0;j<str.size ();j++){if (br_l (str[j])) {m=j;i++;}if (br_r (str[j])) {n=j;if (n!=0) break;}}if (i==0) return str;if (m>0&&isalpha (str[m-1]))for (int k=m-1;;k--)if (k<0||!isalpha (str[k])) {k++;se=str.substr (k,m-k);int l=n-m+1;s=str.substr (m+1,l-2);b.push_back (senior (se,simple (s)));num++;l=n-k+1;str.replace (k,l,1,(char) (64+num)); returnstr;}int l=n-m+1;s=str.substr (m+1,l-2);b.push_back (simple (s));num++;str.replace (m,l,1,(char) (64+num)); returnstr;}double count (string st){ st=al (st);st=abs_cut (st);while (true){int r=0;st=cut (st);for (int i=0;i<st.size ();i++)if (br_r (st[i])) r++;if (r==0) break;}return simple (st);}boolbr_l (char c) {if (c=='('||c=='['||c=='{') return true;else return false;}boolbr_r (char c) {if (c==')'||c==']'||c=='}')return true;else return false;}string al (string st){inti,j=0;double m=0,n=1;for (i=0;i<st.size ();i++){if (st[i]=='e') {num++;st.replace (i,1,1,(char)(64+num));b.push_back (pow (2,1/log (2)));}if (st[i]=='p') {num++;st.replace (i,2,1,(char)(64+num));b.push_back (3.141593);}if (st[i]=='!'){j=i-1;while (j>0) {j--;if (st[j]=='.'){cout<<"只能计算整数的阶乘!!!\n";return "0";}if (st[j]<48||st[j]>57) {j++;break;}}for (int k=j;k<i;k++)m=m*10+(st[k]-48);for (m;m>1;m--) n*=m; num++;st.replace (j,i-j+1,1,(char)(64+num));b.push_back (n);}}i=st.find ("π");if (i<st.size ()) {num++;st.replace (i,2,1,(char)(64+num));b.push_back (acos (-1.0));}i=st.find ("∞");if (i<st.size ()) {num++;st.replace (i,3,1,(char)(64+num));b.push_back (DBL_MAX);}returnst;}stringabs_cut (string st){ inti;vector<int> m;for (i=0;i<st.size ();i++)if (st[i]=='|') m.push_back (i);i=m.size ()/2;if (i==0) return st;i--;string s=st.substr (m[i]+1,m[i+1]-m[i]-1);b.push_back (fabs (count (s)));num++;st.replace (m[i],m[i+1]-m[i]+1,1,(char) (64+num));if (i!=0) st=abs_cut (st);returnst;}double senior (string str,double x){ int i=0;string s[]={"acos","asin","atan","cos","cosh","sin", "sinh","tan","tanh","ln","log","no"};for (i;i<12;i++) if (str==s[i]) break;switch (i){case 0:return acos (x);case 1:return asin (x);case 2:return atan (x);case 3:return cos (x);case 4:return cosh (x);case 5:return sin (x);case 6:return sinh (x);case 7:return tan (x);case 8:return tanh (x);case 9:return log (x);case 10:return log10 (x);default :cout<<str<<"函数尚未收录\n"; return x;}}。

简单计算器c语言源码

简单计算器c语言源码

#include <stdio.h>#include <string.h>#include <ctype.h>#include <math.h>//expression evaluate#define iMUL 0#define iDIV 1#define iADD 2#define iSUB 3#define iCap 4//#define LtKH 5//#define RtKH 6#define MaxSize 100void iPush(float);float iPop();float StaOperand[MaxSize];int iTop=-1;//char Srcexp[MaxSize];char Capaexp[MaxSize];char RevPolishexp[MaxSize];float NumCapaTab[26];char validexp[]="*/+-()";char NumSets[]="0123456789";char StackSymb[MaxSize];int operands;//void NumsToCapas(char [], int , char [], float []);int CheckExpress(char);int PriorChar(char,char);int GetOperator(char [], char);void counterPolishexp(char INexp[], int slen, char Outexp[]); float CalcRevPolishexp(char [], float [], char [], int);void main(){char ch;char s;int bl=1;while(bl==1){int ilen;float iResult=0.0;printf("输入计算表达式(最后以=号结束):\n");memset(StackSymb,0,MaxSize);memset(NumCapaTab,0,26); //A--NO.1, B--NO.2, etc.gets(Srcexp);ilen=strlen(Srcexp);NumsToCapas(Srcexp,ilen,Capaexp,NumCapaTab);ilen=strlen(Capaexp);counterPolishexp(Capaexp,ilen,RevPolishexp);ilen=strlen(RevPolishexp);iResult=CalcRevPolishexp(validexp, NumCapaTab, RevPolishexp,ilen);printf("\n计算结果:\n%.6f\n",iResult);printf("是否继续运算?(Y/N)\n");ch=getchar();s=getchar();//接受回车if(ch=='y'||ch=='Y'){bl=1;}else{bl=0;}}}void iPush(float value){if(iTop<MaxSize) StaOperand[++iTop]=value;}float iPop(){if(iTop>-1)return StaOperand[iTop--];return -1.0;}void NumsToCapas(char Srcexp[], int slen, char Capaexp[], float NumCapaTab[]) {char ch;int i, j, k, flg=0;int sign;float val=0.0,power=10.0;i=0; j=0; k=0;while (i<slen){ch=Srcexp[i];if (i==0){sign=(ch=='-')?-1:1;if(ch=='+'||ch=='-'){ch=Srcexp[++i];flg=1;}}if (isdigit(ch)){val=ch-'0';while (isdigit(ch=Srcexp[++i])) {val=val*10.0+ch-'0';}if (ch=='.'){while(isdigit(ch=Srcexp[++i])) {val=val+(ch-'0')/power; power*=10;}} //end ifif(flg){val*=sign;flg=0;}} //end if//write Capaexp array// write NO.j to arrayif(val){Capaexp[k++]='A'+j; Capaexp[k++]=ch;NumCapaTab[j++]=val; //A--0, B--1,and C, etc.}else{Capaexp[k++]=ch;}val=0.0;power=10.0;//i++;}Capaexp[k]='\0';operands=j;}float CalcRevPolishexp(char validexp[], float NumCapaTab[], char RevPolishexp[], int slen) {float sval=0.0, op1,op2;int i, rt;char ch;//recursive stacki=0;while((ch=RevPolishexp[i]) && i<slen){switch(rt=GetOperator(validexp, ch)){case iMUL: op2=iPop(); op1=iPop();sval=op1*op2;iPush(sval);break;case iDIV: op2=iPop(); op1=iPop();if(!fabs(op2)){printf("overflow\n");iPush(0);break;}sval=op1/op2;iPush(sval);break;case iADD: op2=iPop(); op1=iPop();sval=op1+op2;iPush(sval);break;case iSUB: op2=iPop(); op1=iPop();sval=op1-op2;iPush(sval);break;case iCap: iPush(NumCapaTab[ch-'A']); break;default: ;}++i;}while(iTop>-1){sval=iPop();}return sval;}int GetOperator(char validexp[],char oper) {int oplen,i=0;oplen=strlen(validexp);if (!oplen) return -1;if(isalpha(oper)) return 4;while(i<oplen && validexp[i]!=oper) ++i;if(i==oplen || i>=4) return -1;return i;}int CheckExpress(char ch){int i=0;char cc;while((cc=validexp[i]) && ch!=cc) ++i;if (!cc)return 0;return 1;}int PriorChar(char curch, char stach){//栈外优先级高于(>)栈顶优先级时,才入栈//否则(<=),一律出栈if (curch==stach) return 0; //等于时应该出栈else if (curch=='*' || curch=='/'){if(stach!='*' && stach!='/')return 1;}else if (curch=='+' || curch=='-'){if (stach=='(' || stach==')')return 1;}else if (curch=='('){if (stach==')')return 1;}return 0;}void counterPolishexp(char INexp[], int slen, char Outexp[]) {int i, j, k,pr;char t;i=0;j=k=0;while (INexp[i]!='=' && i<slen){if (INexp[i]=='(')StackSymb[k++]=INexp[i];//iPush(*(INexp+i));else if(INexp[i]==')'){//if((t=iPop())!=-1)while((t=StackSymb[k-1])!='('){Outexp[j++]=t;k--;}k--;}else if (CheckExpress(INexp[i])) // is oparator{// printf("operator %c k=%d\n",INexp[i],k);while (k){// iPush(*(INexp+i));if(pr=PriorChar(INexp[i],StackSymb[k-1])) break;else{//if ((t=iPop())!=-1)t=StackSymb[k-1]; k--;Outexp[j++]=t;}} //end whileStackSymb[k++]=INexp[i]; //common process }else //if() 变量名{// printf("operand %c k=%d\n",INexp[i],k); Outexp[j++]=INexp[i];}i++; //}while (k){t=StackSymb[k-1]; k--;Outexp[j++]=t;}Outexp[j]='\0';}。

C语言鼠标操作方法及源码

C语言鼠标操作方法及源码

C语言鼠标操作方法及源码(转载)鼠标的操作是使用dos中断功能,中断号:33h下面给几个操作鼠标的示例,后面有更详细的中断功能说明及代码。

显示鼠标:union REGS regs;regs.x.ax=1;int86(0x33,&regs,&regs);检查是否被按下:union REGS regs;regs.x.ax=6;int86(0x33,&regs,&regs);返回值在bx寄存器中;在实际编程中一般要使用循环不断的检验是否被按下!检验鼠标位置:union REGS regs;regs.x.ax=3;int86(0x33,&regs,&regs);返回值:x坐标在regs.h.cxy坐标在regs.h.dx/*下面是各值对鼠标的设置功能*/(1)、功能00H功能描述:初始化鼠标,该操作只需要执行一次入口参数:AX=00H出口参数:AX=0000H——不支持鼠标功能,FFFFH——支持鼠标功能BX=鼠标按钮个数(在支持鼠标功能时)在支持鼠标功能的情况下,鼠标还被设置如下参数:1 、鼠标指针放在屏幕中央2 、如果当前鼠标指针是显示的,则操作后,鼠标指针被隐藏3 、鼠标指针的显示页为04 ——、根据屏幕的显示模式显示鼠标指针:文本反向显示矩形块,图形尖头形状5 、水平象素比=8:8,垂直象素比=16:86 、设置水平和垂直的显示边界为当前显示模式的最大边界7 、允许光笔仿真8 、双速门槛值=64(2)、功能01H功能描述:显示鼠标指针,通常在鼠标初始化后,用此功能显示其指针入口参数:AX =01H出口参数:无(3)、功能02H功能描述:隐藏鼠标指针,一般在程序结束时,调用此功能入口参数:AX=02H出口参数:无(4)、功能03H功能描述:读取鼠标位置及其按钮状态入口参数:AX=03H出口参数:BX =按键状态:位0=1——按下左键位1=1——按下右键位2=1——按下中键——其它位保留,内部使用CX =水平位置DX =垂直位置(5)、功能04H功能描述:设置鼠标指针位置入口参数:AX=04H,CX=水平位置,DX =垂直位置出口参数:无(6)、功能05H功能描述:读取鼠标按键信息入口参数:AX=05H,BX=指定的按键:0—左键,1—右键,2—中键出口参数:AX=按键状态,参见功能3H中BX的说明BX=按键次数CX=水平位置(最后按键时)DX=垂直位置(最后按键时)(7)、功能06H功能描述:读取鼠标按钮释放信息入口参数:AX=06H,BX=指定的按键:0—左键,1—右键,2—中键出口参数:AX=按键状态,参见功能3H中BX的说明BX=释放的次数CX=水平位置(最后释放时)DX=垂直位置(最后释放时) (8)、功能07H功能描述:设置鼠标水平边界入口参数:AX=07HCX=最小水平位置DX =最大水平位置出口参数:无,鼠标有可能因新区域变小而自动移进新区域内(9)、功能08H功能描述:设置鼠标垂直边界入口参数:AX=08HCX=最小垂直位置DX =最大垂直位置出口参数:无,鼠标有可能因新区域变小而自动移进新区域内(10)、功能09H功能描述:设置图形鼠标形状入口参数:AX=09HBX=指针的水平位置CX=指针的垂直位置ES:DX=16×16 位光标的映象地址参数说明:(BX,CX)是鼠标的指针在16×16点阵中的位置,(0,0)是左上角;ES:DX指向的存储单元内存放16×16点阵的位映象隐码,紧跟其后的是16×16点阵的光标掩码。

计算器(c 语言)实现源代码

计算器(c  语言)实现源代码

计算器(c++语言)实现源代码.txt如果真诚是一种伤害,请选择谎言;如果谎言是一种伤害,请选择沉默;如果沉默是一种伤害,请选择离开。

#include <iostream>using namespace std;#define STACK_INIT_SIZE 100#define STACKINCREMENT 10//-----------------------------------------------------------------------------------------------class Stack1{//定义字符栈public:char * base;char * top;int stacksize;Stack1();int GetTop(char &e);//取栈顶元素int Push(char e);//压栈int Pop(char &e);//跳栈};Stack1::Stack1(){base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));top=base;stacksize=STACK_INIT_SIZE;}int Stack1::GetTop(char &e){if (top==base) return -1;else {e=*(top-1);return 0;}}int Stack1::Push(char e){char *new_base=0;if(top-base>=stacksize){new_base=(char *)realloc(base,(stacksize+STACKINCREMENT)*sizeof(char));if(!new_base) return -2;else{base=new_base;top=base+stacksize;stacksize+=STACKINCREMENT;}}*top++=e;return 0;}int Stack1::Pop(char &e){if(top==base) return -1;e=*--top;return 0;}//----------------------------------------------------------------------------------------------class Stack2{//定义数字栈public:double * base;double * top;int stacksize;Stack2();int GetTop(double &e);//取栈顶元素int Push(double e);//压栈int Pop(double &e);//跳栈};Stack2::Stack2(){base=(double *)malloc(STACK_INIT_SIZE*sizeof(double));top=base;stacksize=STACK_INIT_SIZE;}int Stack2::GetTop(double &e){if (top==base) return -1;e=*(top-1);return 0;}int Stack2::Push(double e){double *new_base=0;if(top-base>=stacksize){new_base=(double*)realloc(base,(stacksize+STACKINCREMENT)*sizeof(double));if(!new_base) return -2;else{new_base=base;top=base+stacksize;stacksize+=STACKINCREMENT;}}*top++=e;return 0;}int Stack2::Pop(double &e){if(top==base) return -1;e=*--top;return 0;}//------------------------------------------------------------------------------------------------class Calculator{private:bool IsOperand(char ch);//判断ch是否是操作数bool IsOperator(char ch);//判断ch是否是操作符char Precede(char op1,char op2);//判断两个操作符优先级的高低double Operate(double left,char op,double right);//执行运算left op right int Translate(char op);//将操作符映射到行、列游标public:void Run();//执行表达式求值运算};bool Calculator::IsOperand(char ch){if(ch>47&&ch<58) return true;else return false;}bool Calculator::IsOperator(char ch){if(ch>39&&ch<44||ch==45||ch==47||ch==61) return true;else return false;}char Calculator::Precede(char op1,char op2){int op10,op20;op10=op20=0;op10=Translate(op1);op20=Translate(op2);charOP[7][7]={'>','>','<','<','<','>','>','>','>','<','<','<','>','>','>','>','>','> ','<','>','>','>','>','>','>','<','>','>','<','<','<','<','<','=','e','>','>','> ','>','e','>','>','<','<','<','<','<','e','='};return OP[op10][op20];//将操作符优先级表映射到二维数组}double Calculator::Operate(double left,char op,double right){ switch(op){case '+':return (left+right);break;case '-':return (left-right);break;case '*':return (left*right);break;case '/':return (left/right);break;}}int Calculator::Translate(char op){switch(op){case '+':return 0;break;case '-':return 1;break;case '*':return 2;break;case '/':return 3;break;case '(':return 4;break;case ')':return 5;break;case '=':return 6;break;}}void Calculator::Run(){Stack1 OPTR;Stack2 OPND;char c;//用于临时存放输入的操作数或操作符char c0;//用于存放操作符栈顶的操作符char x;//用于存放弹出相同操作符后返回的操作符double a=0,b=0;double r=0;//用于存放运算结果int flag=0;//标记异常OPTR.Push('=');OPTR.GetTop(c0);cout<<"输入格式例如“3+4/(3-1)=”"<<endl;c=getchar();while(c!='='||c0!='='){if(IsOperand(c)) {OPND.Push(c-48);c=getchar();}else if(IsOperator(c)){switch(Precede(c0,c)){case '<':OPTR.Push(c);OPTR.GetTop(c0);c=getchar();break;case '=':OPTR.Pop(x);OPTR.GetTop(c0);c=getchar();break;case '>':OPTR.Pop(c0);OPND.Pop(b);OPND.Pop(a);OPND.Push(Operate(a,c0,b));OPTR.GetTop(c0);break;default:cout<<"输入有误!"<<endl;flag=-1;break;}}else {cout<<"未输入等于号“=”!"<<endl;flag=-1;break;}if (flag==-1) {break;}}if (flag!=-1){OPND.GetTop(r);cout<<"结果为:"<<r<<endl;}}void main (){Calculator C;C.Run();}。

计算器编程c语言

计算器编程c语言

计算器编程 c语言用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的坐标值*/x = width /2;设置y的坐标值*/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); /*输出字符串"0."*/outtextxy(x+3*width,y+height/2,"0."); /*/*设置x的坐标值*/x =2*width-width/2;设置y的坐标值*/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]);/*将字符保存到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;设置str2为空串*/strcpy(str2,""); /*当压下Alt+x键结束程序,否则执行下面的循环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中*/c=str1[n*5+m]; /*判断是否是数字或小数点*/if(isdigit(c)||c=='.') /*{如果标志为-1,表明为负数*/if(flag==-1) /*{将负号连接到字符串中*/strcpy(str2,"-"); /*flag=1;} /*将标志值恢复为1*/将字符保存到字符串变量temp中*/ sprintf(temp,"%c",c); /*将temp中的字符串连接到str2中*/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); /*将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=='-'){如果str2为空,说明是负号,而不是减号*/ if(strcmp(str2,"")==0) /*设置负数标志*/flag=-1; /*else{将第二个操作数转换为浮点数*/num1=atof(str2); /*将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,""); /*将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); /*将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,""); /*将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); /*将结果保存到temp中*/sprintf(temp,"%f",result); /*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."); /*}如果选择了q回车,结束计算程序*/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 );/*设置画线方式*/将当前视口信息装入vp所指的结构中*/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); /*键盘输入*/只取特殊键的扫描值,其余为0*/ key=key&0xff? key&0xff:key>>8; /*return(key); /*返回键值*/}。

C语言鼠标操作方法及源码

C语言鼠标操作方法及源码

C语言鼠标操作方法及源码C语言不是直接支持鼠标操作的,因为C语言是一种面向过程的编程语言,主要用于系统级编程和应用程序开发。

而鼠标操作通常是通过操作系统提供的图形界面库来实现的,如Windows中的WinAPI或Linux中的X窗口系统等。

在C语言中,可以使用图形界面库或API来实现鼠标操作。

以下是一个使用WinAPI实现鼠标操作的例子:```c#include <windows.h>LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)switch (uMsg)case WM_MOUSEMOVE:int xPos = LOWORD(lParam);int yPos = HIWORD(lParam);//处理鼠标移动事件printf("鼠标移动到坐标 (%d, %d)\n", xPos, yPos);break;}case WM_LBUTTONDOWN:int xPos = LOWORD(lParam);int yPos = HIWORD(lParam);//处理鼠标左键按下事件printf("鼠标左键按下,坐标 (%d, %d)\n", xPos, yPos); break;}case WM_LBUTTONUP:int xPos = LOWORD(lParam);int yPos = HIWORD(lParam);//处理鼠标左键松开事件printf("鼠标左键松开,坐标 (%d, %d)\n", xPos, yPos); break;}//其他鼠标事件的处理...}return DefWindowProc(hwnd, uMsg, wParam, lParam);int main//创建窗口HWND hwnd;WNDCLASS wc = {0};wc.lpfnWndProc = WindowProc;wc.hInstance = GetModuleHandle(NULL);wc.lpszClassName = TEXT("MyWindowClass");RegisterClass(&wc);hwnd = CreateWindowEx(0, TEXT("MyWindowClass"), TEXT("C语言鼠标操作示例"), WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL,wc.hInstance, NULL);ShowWindow(hwnd, SW_SHOWDEFAULT);}//消息循环MSG msg;while (GetMessage(&msg, NULL, 0, 0))TranslateMessage(&msg);DispatchMessage(&msg);}return 0;```以上代码演示了一个简单的C语言程序,通过使用WinAPI,实现了基本的鼠标操作。

C++语言编程计算器源代码

C++语言编程计算器源代码

//计算器,函数#include <iostream>#include <string>#include <ctime>#include <conio.h>#include <cstdlib>#include <cmath>#include <vector>#include <cctype>using namespace std;double simple (string s);//简单运算bool check (char c);//检查是否为数值doublecou (double m,doublen,int c);//用于计算两个数的运算string cut (string str);//括号截取double senior (string str,double x);//高级函数计算double count (string st);//括号运算boolbr_l (char c);boolbr_r (char c);string al (string st);//特殊值与符号处理string abs_cut (string st);//绝对值处理intnum;string s;vector <double> b;//存储高级符号内值int main (){ cout<<"\n\t\t本软件由松哥独家制造\n\n"; while (true){ int i=0,j=0;num=0;string s1;b.clear ();cout<<"输入:"; cin>>s1;string s2(s1);if (s=="#") break;cout<<"输出:"<<s2<<"="<<count (s1)<<endl;} cout<<"\n\t\t感谢您的支持!!!!";}double simple (string st){ int i=0,j=0,k=0;vector <double> p;//存储读入的数字vector <int> q;//存储读入运算符while (st[i]!='\0'&&st[i]!='='){double sum=0;while (!check (st[i])) i++;while (check (st[i])){if (st[i]>64&&st[i]<91){sum=b[st[i]-65];i++;break;}if (st[i]!='.') sum=sum*10+(st[i++]-48);else {i++;double su=0,m=1;while (check (st[i])){m*=10.0;su=su*10+(st[i++]-48);}sum=sum+su/m;}}p.push_back (sum); }if (st[0]=='-') p[0]=-p[0];for (i=1;st[i]!='\0';i++){switch (st[i]){case '=':break;case '^': q.push_back (1);break;case '/': q.push_back (-3);break;case '*': q.push_back (3);break;case '+': q.push_back (4);break;case '-' :q.push_back (-4) ;break;default :if (!check (st[i])) {cout<<"含未知符号"<<st[i]<<"!\n"; return 0;}}}i=p.size (),j=q.size ();if (j==1) return cou (p[0],p[1],q[0]);else while (i!=1)for (int k=0;k<j;k++) {if (k==j-1&&abs (q[k])<abs (q[k-1])){p[k]=cou (p[k],p[k+1],q[k]); i--;j--; for (k;k<q.size ()-1;k++) {p[k+1]=p[k+2];q[k]=q[k+1];}p.pop_back ();q.pop_back ();break;}else if (abs (q[k])<=abs (q[k+1])) {p[k]=cou (p[k],p[k+1],q[k]); i--;j--;for (k;k<q.size ()-1;k++) {p[k+1]=p[k+2];q[k]=q[k+1];}p.pop_back ();q.pop_back ();if (j==1) break; } }return p[0];}bool check (char c){if (c>='0'&&c<='9'||c=='.'||c>64&&c<85) return true;else return false;}doublecou (double m,doublen,int c) {switch (c){case 1:return pow (m,n);case 3:return (m*n);case -3:return (m/n);case 4:return (m+n);case -4:return (m-n);default :cout<<c<<"无法计算!\n";} }string cut (string str){int i=0,m=0,n=0,r=0;string se;for (int j=0;j<str.size ();j++){if (br_l (str[j])) {m=j;i++;}if (br_r (str[j])) {n=j;if (n!=0) break;}}if (i==0) return str;if (m>0&&isalpha (str[m-1]))for (int k=m-1;;k--)if (k<0||!isalpha (str[k])) {k++;se=str.substr (k,m-k);int l=n-m+1;s=str.substr (m+1,l-2);b.push_back (senior (se,simple (s)));num++;l=n-k+1;str.replace (k,l,1,(char) (64+num)); returnstr;}int l=n-m+1;s=str.substr (m+1,l-2);b.push_back (simple (s));num++;str.replace (m,l,1,(char) (64+num)); returnstr;}double count (string st){ st=al (st);st=abs_cut (st);while (true){int r=0;st=cut (st);for (int i=0;i<st.size ();i++)if (br_r (st[i])) r++;if (r==0) break;}return simple (st);}boolbr_l (char c) {if (c=='('||c=='['||c=='{') return true;else return false;}boolbr_r (char c) {if (c==')'||c==']'||c=='}')return true;else return false;}string al (string st){inti,j=0;double m=0,n=1;for (i=0;i<st.size ();i++){if (st[i]=='e') {num++;st.replace (i,1,1,(char)(64+num));b.push_back (pow (2,1/log (2)));}if (st[i]=='p') {num++;st.replace (i,2,1,(char)(64+num));b.push_back (3.141593);}if (st[i]=='!'){j=i-1;while (j>0) {j--;if (st[j]=='.'){cout<<"只能计算整数的阶乘!!!\n";return "0";}if (st[j]<48||st[j]>57) {j++;break;}}for (int k=j;k<i;k++)m=m*10+(st[k]-48);for (m;m>1;m--) n*=m; num++;st.replace (j,i-j+1,1,(char)(64+num));b.push_back (n);}}i=st.find ("π");if (i<st.size ()) {num++;st.replace (i,2,1,(char)(64+num));b.push_back (acos (-1.0));}i=st.find ("∞");if (i<st.size ()) {num++;st.replace (i,3,1,(char)(64+num));b.push_back (DBL_MAX);}returnst;}stringabs_cut (string st){ inti;vector<int> m;for (i=0;i<st.size ();i++)if (st[i]=='|') m.push_back (i);i=m.size ()/2;if (i==0) return st;i--;string s=st.substr (m[i]+1,m[i+1]-m[i]-1);b.push_back (fabs (count (s)));num++;st.replace (m[i],m[i+1]-m[i]+1,1,(char) (64+num));if (i!=0) st=abs_cut (st);returnst;}double senior (string str,double x){ int i=0;string s[]={"acos","asin","atan","cos","cosh","sin", "sinh","tan","tanh","ln","log","no"};for (i;i<12;i++) if (str==s[i]) break;switch (i){case 0:return acos (x);case 1:return asin (x);case 2:return atan (x);case 3:return cos (x);case 4:return cosh (x);case 5:return sin (x);case 6:return sinh (x);case 7:return tan (x);case 8:return tanh (x);case 9:return log (x);case 10:return log10 (x);default :cout<<str<<"函数尚未收录\n"; return x;}}。

用c语言编写的计算器源代码

用c语言编写的计算器源代码

作品:科学计算器作者:欧宗龙编写环境:vc++6.0语言:c#include "stdafx.h"#include <stdio.h>#include <windows.h>#include <windowsx.h>#include "resource.h"#include "MainDlg.h"#include <math.h>#include <string.h>#define PI 3.141593BOOL A_Op=FALSE;BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {switch(uMsg){HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);HANDLE_MSG(hWnd, WM_MAND, Main_Onmand);HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);}return FALSE;}BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam){return TRUE;}void TrimNumber(char a[])//判断并删除小数点后无用的零for(unsigned i=0;i<strlen(a);i++){if(a[i]=='.'){for(unsigned j=strlen(a)-1;j>=i;j--){if(a[j]=='0'){a[j]='\0';}else if(a[j]=='.'){a[j]='\0';}else break;}}}}double Operate(char Operator,double n1,double n2) //判断符号,进行相应的运算{if(Operator=='0'){}if(Operator=='+'){n2+=n1;}if(Operator=='-'){n2=n1-n2;}if(Operator=='*'){n2*=n1;}if(Operator=='/'){n2=n1/n2;}if(Operator=='^'){n2=pow(n1,n2);}return n2;}////////////////////////////////////////////////void IntBinary(char a[],int n){if(n>1)IntBinary(a,n/2);sprintf(a,"%s%i",a,n%2);}void decimal(char a[],double m){if(m>0.000001){m=m*2;sprintf(a,"%s%d",a,(long)m);decimal(a,m-(long)m);}}void Binary(char a[],double Num){char DecP[256]="";double x,y;double *iptr=&y;x=modf(Num,iptr);decimal(DecP,x);IntBinary(a,(int)y);strcat(a,".");strcat(a,DecP);}////////////////////////////////////void Main_Onmand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) {static DELTIMES=0;static char str[256];static char Operator='0';static double RNum[3];switch(id){case IDC_BUTTONN1://数字1{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"1");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case IDC_BUTTONN2://数字2{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"2");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case IDC_BUTTONN3://数字3{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"3");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case IDC_BUTTONN4://数字4{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"4");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case IDC_BUTTONN5://数字5{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"5");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case IDC_BUTTONN6://数字6{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"6");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case IDC_BUTTONN7://数字7{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"7");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case IDC_BUTTONN8://数字8{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"8");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case IDC_BUTTONN9://数字9{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"9");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case IDC_BUTTONN0://数字0{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"0");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case IDC_BUTTONDEL://小数点.del{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));if(DELTIMES==0){strcat(str,".");}DELTIMES++;SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=FALSE;}break;case IDC_BUTTONADD: //加法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='+';DELTIMES=0;A_Op=TRUE;}break;case IDC_BUTTONSUB: //减法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);DELTIMES=0;A_Op=TRUE;Operator='-';}break;case IDC_BUTTONMUL: //乘法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='*';DELTIMES=0;A_Op=TRUE;}break;case IDC_BUTTONDIV: //除法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='/';DELTIMES=0;A_Op=TRUE;}break;case IDC_BUTTONXY://x的y次方{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='^';DELTIMES=0;}break;case IDC_BUTTONPI: //圆周率PI,弧度{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));if(atof(str)!=0){RNum[2]=atof(str)*PI;sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);}else{sprintf(str,"%f",PI);SetDlgItemText(hwnd,IDC_EDIT,str);}A_Op=TRUE;}break;case IDC_BUTTONSQRT: //开根号{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=sqrt(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case IDC_BUTTONSIN: //三角函数sin函数{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=sin(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case IDC_BUTTONCOS://三角函数cos函数{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=cos(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case IDC_BUTTONTAN://三角函数tan函数{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=tan(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case IDC_BUTTONSQ: //平方{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=atof(str)*atof(str);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case IDC_BUTTONCUBE://三次方{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=atof(str)*atof(str)*atof(str);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case IDC_BUTTONEX://e的x次方{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=exp(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case IDC_BUTTON10X://10的x次方{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=pow(10,atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case IDC_BUTTONLN: //ln x{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=log(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case IDC_BUTTONLOG10: //log10{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=log10(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case IDC_BUTTONBINARY: //十进制转换为二进制{char a[256]="";GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=atof(str);Binary(a,RNum[2]);strcpy(str,a);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case IDC_BUTTONCLEAR://清除数据{DELTIMES=0;Operator='0';RNum[0]=RNum[1]=RNum[2]=0;memset(str,0,sizeof(str));SetDlgItemText(hwnd,IDC_EDIT,NULL);A_Op=FALSE;}break;case IDC_BUTTONBACKSPACE://退格键{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));int i=strlen(str);str[i-1]='\0';SetDlgItemText(hwnd,IDC_EDIT,str);}break;case IDC_ENTER://Enter键{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='0';DELTIMES=0;}break;default:break;}}void Main_OnClose(HWND hwnd){EndDialog(hwnd, 0);}本人拙作,如有不足之处请谅解。

C语言实现简易计算器(可作加减乘除)

C语言实现简易计算器(可作加减乘除)

C语⾔实现简易计算器(可作加减乘除)C语⾔实现简易计算器(加减乘除)计算器作为课设项⽬,已完成答辩,先将代码和思路(注释中)上传⼀篇博客已增添、修改、整理⾄⽆错且可正常运⾏虽使⽤了栈,但初学者可在初步了解栈和结构语法后理解代码#include <stdlib.h>#include <stdio.h>#include <string.h>#define IsDouble 0#define IsChar 1//_______________________________________________________________________________________________________________________________________________________ //1.⽀持浮点数和字符的栈typedef struct {char * buffer;int typesize;int top;int max;} stack;stack * CreateStack(int max, int typesize);//创建⼀个栈void DestroyStack(stack *);//释放栈的空间void ClearStack(stack *);//清空栈int Push(stack *, void *);//⼊栈int Pop(stack *, void *);//出栈int GetSize(stack *s);//得到栈的⼤⼩int GetTop(stack *, void *);//找到栈顶int IsEmpty(stack *);//判断是否为空栈,空则下溢int IsFull(stack *);//判断栈是否已满,满则溢出stack * CreateStack(int max, int typesize){stack * s = (stack*)malloc(sizeof(stack));//为栈s malloc内存if (!s) return 0;//为结构中buffer元素malloc内存s->buffer = (char *)malloc(sizeof(char) * max * typesize);if (!s->buffer) return 0;//初始化结构中的栈顶,最⼤值,类型⼤⼩s->top = -1;s->max = max;s->typesize = typesize;return s;}void DestroyStack(stack* s){free(s->buffer);//先释放buffer的空间free(s);//在释放s的空间}void ClearStack(stack * s){s->top = -1;//清空栈(栈头位置归零)}int Push(stack * s, void * data){if (IsFull(s)) return 0;//如果栈已满则return 0,防⽌溢出//栈未满则将栈头移动打动下⼀位置,并将data中的元素拷⼊栈中buffer的第top位置s->top++;memcpy(s->buffer + s->top*s->typesize, data, s->typesize);//⼊栈成功return 1return 1;}int Pop(stack * s, void * data){if (IsEmpty(s)) return 0;//出栈判断栈是否为空,若为空则return 0//栈未空则将buffer中top位置的字符拷⼊data记录,并让栈头向前移动⼀个位置memcpy(data, s->buffer + s->top*s->typesize, s->typesize);s->top--;//成功则return 1return 1;}int GetSize(stack *s){return s -> top+1;//栈头位置+1得到⼤⼩}int GetTop(stack *s, void * data){if (IsEmpty(s)) return 0;//如果栈空return 0//栈不为空则将top位置的字符拷回data记录,得到栈头memcpy(data, s->buffer + s->top*s->typesize, s->typesize);//成功则return 1;return 1;}int IsEmpty(stack * s){return s->top == -1;//如果top为-1则栈空}int IsFull(stack * s){return s->top == s->max-1;//如果top为max-1则栈满}//___________________________________________________________________________________________________________________________________________________ //2.定义⼀个cal类型,其中data存数时sign为IsDouble,存字符时,sign为Ischartypedef struct {double data;char sign;} cal;//3.查找对应符号(找到则返回该符号下标)(找不到则说明该部分为数字返回-1)int SearchCode(char ch){char * code = "+-*/()@";//@为终⽌符,算式输⼊结束int index = 0;//while (code[index]){if (code[index] == ch) return index;index++;}return -1;}//4.得到两个符号间的优先级//与SearchCode相对应,char GetPriority(char ch, char next){//创建⼀个perferen表,第i⾏(列)对应SearchCode函数中code中的第i个字符char perferen[7][7] = {">><<<>>",">><<<>>",">>>><>>",">>>><>>","<<<<<=E",">>>>E>>","<<<<<E="};//找到两个形参对应的字符int c = SearchCode(ch);int n = SearchCode(next);//如果找不到对应运算符(字符不是运算符⽽是为数字)return Eif (c==-1 || n==-1) return 'E';//如果找到两个对应运算符则按照优先级表返回两个运算符的优先级return perferen[c][n];}//5.四则运算double add(double a, double b) { return a+b; }double sub(double a, double b) { return a-b; }double mul(double a, double b) { return a*b; }double ddiv(double a, double b) { return a/b; }//整合四种运算double calcu(double a, char ch, double b){double (*calculation[4])(double,double) = {add,sub,mul,ddiv};return calculation[SearchCode(ch)](a,b);}//6.检测字符串int CheckStr(char * buffer){int n;//遍历字符串确保算式中⽆⾮法字符若检测到⾮法字符return 0,若都合法则return 1for (n = 0;buffer[n];n++){if ((SearchCode(buffer[n]) != -1 || buffer[n] == '.' || (buffer[n] >= '0' && buffer[n] <= '9')) && buffer[n] != '@') continue;else return 0;}buffer[n] = '@';//加上终⽌符,表⽰算式结束buffer[n+1] = '\0';return 1;}//7.得到数据转化为double类型存⼊rsint GetDigit(char * buffer, int * n, double * rs){char str[30];int i,j = 0;for (i = 0;SearchCode(buffer[*n]) == -1;i++){str[i] = buffer[*n];//从*n位置开始,将这⼀串数字字符存⼊str(*n)++;}str[i] = '\0';for (i = 0;str[i];i++){if (str[i] == '.') j++;}//如果⼀段⼩数有多个⼩数点或⼩数点在数字⾸尾,return 0if (j>1 || str[i-1] == '.' || str[0] == '.') return 0;//rs接收转化为double的数据*rs = atof(str);//操作成功return 1return 1;}//8.将⽤户输⼊的buffer字符串转化为可供程序运算的calstr数组int resolu(char * buffer, cal * calstr){int i = 0, j = 0;cal c;while (buffer[i]){if (SearchCode(buffer[i]) == -1){//如果得到数据不成功则return 0if (GetDigit(buffer,&i, &c.data) == 0) return 0;//如果成功得到数据则在c.sign标记为浮点数c.sign = IsDouble;//将c存⼊数组calstr中calstr[j++] = c;}else{//若符号为运算符//判断正负号if (buffer[i] == '-' && (buffer[i-1] == '('||buffer[i-1] == '+'||buffer[i-1] == '-'||buffer[i-1] == '*'||buffer[i-1] == '/') || (i==0 && buffer[0] == '-')){ i++;if (GetDigit(buffer,&i, &c.data) == 0) return 0;//在符号的下⼀位开始查找,若找不到数字return 0//否则,给数字取相反数,c.sign标记为浮点数,存⼊calstr中c.data = 0 - c.data;c.sign = IsDouble;calstr[j++] = c;} else//如果是正号,与符号处理⽅式同理if (buffer[i] == '+' && (buffer[i-1] == '('||buffer[i-1] == '+'||buffer[i-1] == '-'||buffer[i-1] == '*'||buffer[i-1] == '/') || (i==0 && buffer[0] == '+')){ i++;if (GetDigit(buffer, &i, &c.data) == 0) return 0;c.sign = IsDouble;calstr[j++] = c;}else{//如果不是正负号,则为运算符,先强制转换为double类型存在c.data⾥,然后c.sign标记为char类型,存⼊calstrc.data = (double)buffer[i++];c.sign = IsChar;calstr[j++] = c;}}}//操作蔡成功则return 1return 1;}//9.计算出结果int result(cal * calstr, double * rs){stack * pst = CreateStack(100,sizeof(char));//运算符栈stack * pnd = CreateStack(100,sizeof(double));//数据栈double num1,num2;int n = 0;char ch = '@';Push(pst, &ch);//在转换得到的calstr中遍历直到终⽌符'@"while(ch != '@' || !(calstr[n].sign == IsChar && (char)calstr[n].data == '@')){//如果calstr的n位上是浮点数,则将这个data压栈进⼊数据栈pnd中if (calstr[n].sign == IsDouble){Push(pnd, &(calstr[n].data));n++;}//反之,如果是运算符,则要检测优先级else{switch( GetPriority(ch, (char)calstr[n].data)){//如果运算符优先级较⼩,则让ch等于优先级⼤的符号并压⼊符号栈pst中case '<':ch = (char)calstr[n].data;Push(pst, &ch);n++;break;//如果结果为等号,让符号出栈暂存到ch中case '=':if (!Pop(pst, &ch)) return 0;n++;break;//如果ch优先级较⾼,则将前两个数字及运算符出栈,分别储存⾄num2,ch,num1中,进⾏运算,得到的结果再次压栈进⼊pnd中 case '>':if (!(Pop(pnd,&num2) && Pop(pst,&ch) && Pop(pnd,&num1))) return 0;num1 = calcu(num1,ch,num2);Push(pnd, &num1);break;//如果符号顺序出错,return 0case 'E':return 0;}}//检测是否可以得到栈顶符号,栈空则return 0if (!GetTop(pst, &ch)) return 0;}//如果栈中得到了最终结果,并且取出pnd中的最终结果到rs,return 1if (GetSize(pnd) == 1 && GetTop(pnd,rs)){DestroyStack(pst);DestroyStack(pnd);return 1;}//否则 return 0else{return 0;}}//10.⽤户交互函数void treatment(){char buffer[100];//⽤户输⼊的字符串(算式)cal calstr[50];//计算⽤的数组double rs = 0;//计算结果printf("Enter your equation:");gets(buffer);//让⽤户输⼊算式buffer//⽤户不输⼊"exit"就不退出while (!(buffer[0]=='e' && buffer[1]=='x' && buffer[2]=='i' && buffer[3]=='t')){//检查buffer中字符君合法,成功将buffer转化为⽤于计算的calstr数组,成功计算出结果存⼊rsif (CheckStr(buffer) && resolu(buffer,calstr) && result(calstr,&rs)){printf("\n%lf\n",rs);}else{printf("\nError!\n");}printf("Enter \"exit\"to quit");printf("\nEnter your equation:");gets(buffer);//再次让⽤户输⼊算式}printf("\nbye\n");}//11.主函数int main(){treatment();}参考⽂献链接如下[参考⽂献]()。

C语言计算器源程序

C语言计算器源程序

#include<stdio.h>//标准输入输出#include<conio.h>//system设置颜色使用到#include<stdlib.h>//清屏时用到#include<math.h>//数学函数用到int main(){ void mnue();//菜单函数声明void add();//加法函数声明void minus();//减法函数void multiply();//乘法函数void divided();//除法函数void sinx();//sin函数void cosx();//cos函数void tanx();//tan函数void arcsin();//void arccos();void arctan();void sqrtx();void logx();void log10x();void expx();void powx();void tuichu();system("color 5B");mnue();}void mnue()//菜单{ char M;printf("\n\n\n\t\t\t ********* ********\n");printf("\t\t\t ** ** ** **\n");printf("\t\t\t ** ** ** **\n");printf("\t\t\t** 欢迎*** 使用**\n");printf("\t\t\t ** * **\n");printf("\t\t\t ** 简易计算器**\n");printf("\t\t\t ** **\n");printf("\t\t\t.........按任意键进入——》》》\n");printf("\t\t\t ** **\n");printf("\t\t\t ** **\n");printf("\t\t\t ** **\n");printf("\t\t\t *\n");getch();system("cls");while(M!='h'){printf("\n\n\n\t\t\t|------------------------------|\n");printf("\t\t\t| 加法—>A\t| 减法—>a |\n");printf("\t\t\t| 乘法—>B\t| 除法—>b |\n");printf("\t\t\t| 正弦—>C\t| 反正弦—>c |\n");printf("\t\t\t| 余弦—>D\t| 反余弦—>d |\n");printf("\t\t\t| 正切—>E\t| 反正切—>e |\n");printf("\t\t\t| ln —>F\t| log10 —>f |\n");printf("\t\t\t| 幂指数—>G\t| E 幂数—>g |\n");printf("\t\t\t| 开根—>H\t| 退出—>h |\n");printf("\t\t\t|______________________________|\n");printf("\n\t\t请输入要进行运算的法则!\n");M=getch();while(1){if('A'<=M&&M<='H' ||'a'<=M&&M<='h'){ //system("cls");break;}else{printf("\n\n\t提示:输入错误\n\t—>请正确输入!\n\a\a\a\a\a\a\a");M=getch();}}switch(M){case 'A': add();break;case 'a': minus();break;case 'B': multiply();break;case 'b': divided();break;case 'C': sinx();break;case 'c': arcsin();break;case 'D': cosx();break;case 'd': arccos();break;case 'E': tanx();break;case 'e': arctan();break;case 'F': logx();break;case 'f': log10x();break;case 'G': powx();break;case 'g': expx();break;case 'H': sqrtx();break;}}if("M==h") tuichu();}void add()//加法函数{int z, y,x;char n='Y';while(n=='Y'){printf("请输入:\n");scanf("%d%d",&x,&y);z=y+x;printf("%d+%d=%d\n",x,y,z);printf("是否继续(Y/N)\n");n=getch();if(n=='N')system("cls");}}void minus()//减法函数{int x,y,z;char n='Y';while(n=='Y'){printf("请输入:\n");scanf("%d%d",&x,&y);z=x-y;printf("%4d-%4d=%4d\n",x,y,z);printf("是否继续(Y/N)\n");n=getch();if(n=='N')system("cls");}}void multiply()//乘法函数{int x,y,z;char n='Y';while(n=='Y'){printf("请输入:\n");scanf("%d%d",&x,&y);z=x*y;printf("%4d*%4d=%4d\n",x,y,z);printf("是否继续(Y/N)\n");n=getch();if(n=='N')system("cls");}}void divided()//除法函数{float x,y,z;char n='Y';while(n=='Y'){printf("请输入:\n");scanf("%f%f",&x,&y);z=x/y;printf("%4d/%4d=%4d\n",x,y,z);printf("是否继续(Y/N)\n");n=getch();if(n=='N')system("cls");}}void sinx()//sin函数{double x,y;scanf("%lf",&x);y=sin(x);printf("sin%lf=%lf",x,y);}void cosx()//cos函数{double a,b;scanf("%lf",&a);b=cos(a);printf("cos%lf=%lf",b);}void tanx()//tan函数{double a,b;scanf("%lf",&a);b=tan(a);printf("tan%lf=%lf",b);}void arcsin(){double a,b;scanf("%lf",&a);b=asin(a);printf("arcsin%lf=%lf",b);void arccos(){double a,b;scanf("%lf",&a);b=acos(a);printf("arccos%lf=%lf",b); }void arctan(){double a,b;scanf("%lf",&a);b=atan(a);printf("arctan%lf=%lf",b); }void sqrtx(){double a,b;scanf("%lf",&a);b=sqrt(a);printf("sqrt%lf=%lf",b); }void logx(){double a,b;scanf("%lf",&a);b=log(a);printf("log%lf=%lf",a,b); }void log10x(){double a,b;scanf("%lf",&a);b=log10(a);printf("ln%lf=%lf",b);}void expx(){double a,b;scanf("%lf",&a);b=exp(a);printf("exp%lf=%lf\n",b); }void powx()double a,b,c;scanf("%lf%lf",&a,&b);c=pow(a,b);printf("pow%lf=%lf\n",c);}void tuichu(){ system("cls");printf("\n\n\n\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");printf("\n\n\t\t\t谢谢使用再见!\n\n\n");printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n\n\t"); }。

C语言计算器代码

C语言计算器代码
printf("西安工程大学计算机01班...本程序目标:简单计算器\n\n");Sleep(1000);
printf("小组组长:");Sleep(1000);
puts("尹化荣\n");Sleep(300);
printf("小组成员:");Sleep(300);
printf("尹化荣,");Sleep(300);
{
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//------------------------------------------------------------------||
//迷宫游戏,所有声明:
#define Height 23 //迷宫的高度,必须为奇数
#define Width 39 //迷宫的宽度,必须为奇数
#define Wall 1
double jiafa(double a,double b);
double jianfa(double a,double b);
double chengfa(double a,double b);
double chufa(double a,double b);
intqiuyu(int a,int b);
CONSOLE_CURSOR_INFO cci;

C编写简易计算器附源代码超详细

C编写简易计算器附源代码超详细

超详细一、因为计算器设计的控件太多,不便使用控制台应用程序完成,所以这里使用Windows窗体应用程序,并命名为Calc,如下图所示:二、向窗体中拖入需要的控件,如下图所示:(完成效果图)结果显示区(作者博客左边的文本框)是TextBox控件,并修改其name为txtShow ,按键0~9为Button控件,并将其name分别修改为btn_0、btn_1、btn_2、btn_3、btn_4、btn_5、btn_6、btn_7、btn_8、btn_9;按键【负数】的name值修改为btn_sign,按键【.】的name 修改为btn_dot,按键【+ - * /】的name值分别修改为btn_add、btn_sub、btn_mul、btn_div,按键【=】的name值修改为btn_equ,按键【倒数】的name值修改为btn_rev,按键【平方】的name值修改为btn_sqr,按键【开方】的name值修改为btn_sqrt。

右边的计算器图片空间是PictureBox,作者博客控件是LinkLabel,可以不添加,以上所有控件均可按照需求添加,只保留自己需要的按钮控件和textbox控件即可。

三、代码部分(含解释),采用switch多分支语句编写using System;using System.Drawing;using System.Collections;using ponentModel;using ;using System.Data;namespace Calc{///<summary>/// QQ:6 温柔一刀C#简易计算器的实现///</summary>public class CalcForm :{private btn_0;private btn_1;private btn_2;private btn_3;private btn_4;private btn_5;private btn_6;private btn_7;private btn_8;private btn_9;private btn_add;private btn_sub;private btn_mul;private btn_div;private btn_sqrt;private btn_sign;private btn_equ;private btn_dot;private btn_rev;private txtShow;private btn_sqr;private PictureBox pictureBox1;private LinkLabel linkLabel1;///<summary>///必需的设计器变量。

C语言计算器程序源代码

C语言计算器程序源代码

C语⾔计算器程序源代码//strcmp(s1,s2) 当s1⼤于s2时,返回1 ,s1⼩于s2时,返回-1,相等时,返回0 #include "stdio.h"#include "ctype.h"#include "string.h"#include "math.h"#define MAX 256#define STACK_SIZE 128#define WORD_LEN 8#define POP 1#define PUSH 0#define ERR -1#define END 2#define OPER 0#define NUM 1#define WORD 2#define ADD 1#define SUB 2#define MUL 3#define DIV 4#define POW 5#define FAC 6#define BRA_L 7#define BRA_R 8#define SIN 9#define COS 10#define TAN 11#define CTG 12#define LG 13 //以10为底的常⽤对数//#define LN 14//#define LOG 15//⾏标为当前操作符代号,列标为栈顶元素代号//2表⽰计算结束,0表⽰当前操作符进栈,1表⽰栈顶操作符出栈// \0 + - * / ^ ! ( ) sin cos tg ctg lgint Priority[14][14]={2, 1, 1, 1, 1, 1, 1,-1,-1, 1, 1, 1, 1, 1, /* \0 */0, 1, 1, 1, 1, 1, 1, 0,-1, 1, 1, 1, 1, 1, /* + */0, 1, 1, 1, 1, 1, 1, 0,-1, 1, 1, 1, 1, 1, /* - */0, 0, 0, 1, 1, 1, 1, 0,-1, 1, 1, 1, 1, 1, /* * */0, 0, 0, 1, 1, 1, 1, 0,-1, 1, 1, 1, 1, 1, /* / */0, 0, 0, 1, 1, 1, 1, 0,-1, 0, 0, 0, 0, 0, /* ^ */0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, /* ! */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0, /* ( */-1,1, 1, 1, 1, 1, 1, 1,-1, 1, 1, 1, 1, 1, /* ) */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0, /* sin */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0, /* cos */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0, /* tg */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0, /* ctg */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0}; /* lg */char KeyWord[36][WORD_LEN+1]={"sin", //前12个为函数,多余的⽤于扩展"cos","tan","tg","ctg","lg","","","","","","","","","","help", // 后⾯为命令,多余的为扩展"version","set","digit", //精度,⼩数点后的位数"color","radian", //弧度"degree", //⾓度"file","clr","clear","window", //窗⼝模式"fullscr", //全屏模式"","","","","","","","",""};int OperCode(char c){int code;switch(c){case '\0':code=0;break;case '+':code=1;break;case '-':code=2;break;case '*':code=3;break;case '/':code=4;break;case '^':code=5;break;case '!':code=6;break;case '(':code=7;break;case ')':code=8;break;case 's':code=9;break; //sincase 'c':code=10;break; //coscase 't':code=11;break; //tgcase 'C':code=12;break; //ctgcase 'l':code=13;break; //log default:code=-1;break;};return code;}int WordCode(char* word){int i;for(i=0;i<25;i++)if(strcmp(KeyWord[i],word)==0) break;if(i>=25)return -1;else}void help(){printf("显⽰帮助信息!\n");return;}void version(){printf("显⽰版本信息!\n");return;}void Err(int errcode,int position,char *p){printf("\n ERR:%d Position:%d %s",errcode,position,p); return;}double long factorial(int i){if(i==1 || i==0)return(1.0);elsereturn(i*factorial(i-1));}main(){char Expression[MAX+1];int Operator[STACK_SIZE];int OperStackTop;double long Number[STACK_SIZE];int NumStackTop;double long NumList[STACK_SIZE];int NumCursor,NumListSize;int OperList[STACK_SIZE];int OperCursor,OperListSize;int WordList[STACK_SIZE];int WordCursor,WordListSize;int Index[MAX+1];int IndexCursor,IndexSize;char Word[WORD_LEN+1];double long num,num1,num2,weight,tempnum;int Oper;int isDecimal,isErr,isNumber,isEnd;char CurrentOper;int i,j,k,m,n;char ch;num=0.0;num2=0.0;tempnum=0.0;Oper=-1;while(1){for(i=0;i<=MAX;i++) //表达式初始化,中间表索引初始化{Expression[i]='\0';Index[i]=-1;}for(i=0;i{Operator[i]='0';Number[i]=0.0;NumList[i]=0.0;OperList[i]=-1;WordList[i]=-1;}NumStackTop=-1; //栈顶指针初始化OperStackTop=0; //操作符栈压⼊\0Operator[OperStackTop]=OperCode('\0');NumCursor=0; //各种中间表指针初始化,各种中间表的长度初始化NumListSize=0;OperCursor=0;OperListSize=0; //操作符表中先写⼊第⼀个操作符'\0'WordCursor=0;WordListSize=0;IndexCursor=0;IndexSize=0;// Index[0]=OPER;printf("Cal>"); //初始化完成,输出提⽰符i=0;while((ch=getchar())!='\n'){if(i>MAX) /*输⼊超长,则出错*/{Err(0,i,"输⼊的表达式长度超过规定值!\n");isErr=1;break;}if(isupper(ch))ch=tolower(ch);Expression[i]=ch;i++;}if(isErr==1){isErr=0;continue;}if(strlen(Expression)==0) //直接回车continue;if(strcmp("end",Expression)==0 ||strcmp("exit",Expression)==0 || strcmp("quit",Expr ession)==0)break;//⼀下代码为编译预处理,主要处理负号,并检查括号是否配对k=0;for(i=0;Expression[i]!='\0';i++){if((i==0&&Expression[i]=='-') || (i>0&&Expression[i]=='-'&&Expression[i-1]=='(')){for(j=strlen(Expression);j>i;j--)Expression[j]=Expression[j-1];Expression[i]='0';}if(Expression[i]=='(') //检查括号k++;if(Expression[i]==')')k--;}if(k>0) //如果括号不配对{Err(1,-1,"缺少右括号 )\n");continue;}if(k<0){Err(1,-1,"缺少左括号 (\n");continue;}//编译预处理结束i=0; //词法分析while(1){if(Expression[i]=='\0'){OperList[OperListSize]=OperCode(Expression[i]);OperListSize++;Index[IndexSize]=OPER;IndexSize++;// printf("IndexSize=%d,Index[IndexSize]=%d,Expression[i]=%c\n",IndexSize,Index[IndexSize],Expression[i]); break;}isDecimal=0;isNumber=0;while(isdigit(Expression[i])||Expression[i]=='.') //读取数字{isNumber=1;if(Expression[i]=='.'){if((i<(MAX-1) && !isdigit(Expression[i+1])) || (i+1)==MAX) //不正确的⼩数点位置{Err(2,i,"⼩数点位置不正确!\n");isErr=1;isNumber=0;i++;break;}isDecimal=1;weight=0.1;i++;continue;}if(isDecimal==0)num=num*10.0+(double long)(Expression[i]-'0');{num=num+(double long)(Expression[i]-'0')*weight;weight=weight*0.1;}i++;} //数字读完if(isErr==1)break;if(isNumber==1) //如果刚才成功读取了数字,则数字⼊栈{NumList[NumListSize]=num;NumListSize++;isNumber=0;num=0.0;Index[IndexSize]=NUM;// printf("IndexSize=%d,Index[IndexSize]=%d\n",IndexSize,Index[IndexSize]); IndexSize++;}for(k=0;k<=WORD_LEN;k++)Word[k]='\0';j=0;while(isalpha(Expression[i])){if(j>=WORD_LEN) //超过长度仍然未匹配,则出错{Err(3,i,"单词长度超过规定值/未定义的单词:");printf("%s\n",Word);isErr=1;break;}Word[j]=Expression[i];j++;// printf("WORD:%s\n",Word);if(WordCode(Word)==-1) //匹配不成功{if(!isalpha(Expression[i+1]))//匹配不成功,但是下⼀个字符已经不是字母,{Err(4,i,"未定义的单词:"); //则出错,并跳出循环printf("%s\n",Word);isErr=1;break;} //匹配不成功且还能继续读取字符,则继续读取下⼀个字母i++;continue;}else //匹配成功,则单词⼊表,读取下⼀个字符{switch(WordCode(Word)){case 0:ch='s';break;case 1:ch='c';break;case 2:case 3:ch='t';break;case 4:ch='C';case 5:ch='l';break;default:ch='\0';WordList[WordListSize]=WordCode(Word);WordListSize++;Index[IndexSize]=WORD;IndexSize++;break;};if(ch!='\0'){OperList[OperListSize]=OperCode(ch);OperListSize++;Index[IndexSize]=OPER;IndexSize++;}i++;break;}} //单词读完if(isErr==1)break;if(Expression[i]==' ')i++;if(!isdigit(Expression[i]) && !isalpha(Expression[i]) && Expression[i]!='\0') {if(OperCode(Expression[i])==-1){isErr=1;Err(5,i,"未定义的操作符:");printf("%c\n",Expression[i]);break;}else{OperList[OperListSize]=OperCode(Expression[i]);OperListSize++;Index[IndexSize]=OPER;IndexSize++;i++;}} //操作符读完if(isErr==1)break;} //词法分析结束if(isErr==1){isErr=0;continue;}/* for(k=0;kprintf("NumList[%d]=%f\n",k,NumList[k]);for(k=0;kprintf("OperList[%d]=%d\n",k,OperList[k]);for(k=0;kprintf("WordList[%d]=%d\n",k,WordList[k]);for(k=0;kprintf("Index[%d]=%d\n",k,Index[k]);printf("\n\n IndexCursor=%d IndexSize=%d,\n",IndexCursor,IndexSize);printf("OperStacktop=%d,Operator[OperStackTop]=%d,NumStackTop=%d\n",OperStackTop,Operator[OperStackTop],NumStackTop); // continue;*/isEnd=0;IndexCursor=0;while(1){if(Index[IndexCursor]==NUM){if(NumCursor<0 || NumListSize<0){Err(10,-1,"索引列表与操作数列表信息不匹配\n");isErr=1;break;}NumStackTop++;Number[NumStackTop]=NumList[NumCursor];NumCursor++;IndexCursor++;continue;} //数字处理if(Index[IndexCursor]==OPER){m=OperList[OperCursor];n=Operator[OperStackTop];switch(Priority[m][n]){case ERR:Err(20,IndexCursor,"不可预见的错误!\n");isErr=1;break;case PUSH:OperStackTop++;Operator[OperStackTop]=m;OperCursor++;IndexCursor++;break;case END:isEnd=1;break;case POP:Oper=Operator[OperStackTop];OperStackTop--;switch(Oper){case BRA_L: IndexCursor++;OperCursor++;break;case ADD:if(NumStackTop>=1){num2=Number[NumStackTop];NumStackTop--;num1=Number[NumStackTop];NumStackTop--;tempnum=num1+num2;NumStackTop++;Number[NumStackTop]=tempnum;num1=0.0;num2=0.0;tempnum=0.0;}else{Err(11,IndexCursor,"加法运算缺少操作数!\n");isErr=1;}break;case SUB:if(NumStackTop>=1){num2=Number[NumStackTop];NumStackTop--;num1=Number[NumStackTop];NumStackTop--;tempnum=num1-num2;NumStackTop++;Number[NumStackTop]=tempnum;num1=0.0;num2=0.0;tempnum=0.0;}else{Err(12,IndexCursor,"减法运算缺少操作数!\n");isErr=1;}break;case MUL:if(NumStackTop>=1){num2=Number[NumStackTop];NumStackTop--;num1=Number[NumStackTop];NumStackTop--;tempnum=num1*num2;NumStackTop++;Number[NumStackTop]=tempnum;num1=0.0;num2=0.0;tempnum=0.0;}else{Err(13,IndexCursor,"乘法运算缺少操作数!\n");isErr=1;}break;case DIV:if(NumStackTop>=1){num2=Number[NumStackTop];NumStackTop--;if(num2==0.0){Err(14,IndexCursor,"除数为 0 ,不能进⾏除法运算!\n"); isErr=1;break;}num1=Number[NumStackTop];NumStackTop--;tempnum=num1/num2;NumStackTop++;Number[NumStackTop]=tempnum;num1=0.0;num2=0.0;tempnum=0.0;}else{Err(15,IndexCursor,"除法运算缺少操作数!\n"); isErr=1;}break;case POW:if(NumStackTop>=1){num2=Number[NumStackTop]; NumStackTop--;num1=Number[NumStackTop]; NumStackTop--;tempnum=pow(num1,num2); NumStackTop++;Number[NumStackTop]=tempnum;num1=0.0;num2=0.0;tempnum=0.0;}else{Err(16,IndexCursor,"乘⽅运算缺少操作数!\n"); isErr=1;}break;case FAC:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;tempnum=factorial(num2); NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(17,IndexCursor,"阶乘运算缺少操作数!\n"); isErr=1;}break;case SIN:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;tempnum=sin(num2);NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}{Err(18,IndexCursor,"正弦函数缺少参数!\n"); isErr=1;}break;case COS:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;tempnum=cos(num2);NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(19,IndexCursor,"余弦函数缺少参数!\n"); isErr=1;}break;case TAN:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;tempnum=tan(num2);NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(20,IndexCursor,"正切函数缺少参数!\n");isErr=1;}break;case CTG:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;tempnum=1.0/tan(num2); NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(21,IndexCursor,"余切函数缺少参数!\n"); isErr=1;}break;/* case LN:if(NumStackTop>=0)num2=Number[NumStackTop]; NumStackTop--;if(num2<=0.0){Err(20,IndexCursor,"⾃然对数函数真数:"); printf(" %f ⼩于0!\n",num2);isErr=1;break;}tempnum=log(num2);NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(17,IndexCursor,"⾃然对数函数缺少参数!\n"); isErr=1;}break; */case LG:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;if(num2<=0.0){Err(23,IndexCursor,"常⽤对数函数真数:"); printf(" %f ⼩于0!\n",num2);isErr=1;break;}tempnum=log10(num2);NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(22,IndexCursor,"常⽤对数函数缺少参数!\n"); isErr=1;}break;default:Err(100,IndexCursor,"运算符(代码:"); printf(" %d )暂不⽀持!\n",Oper);isErr=1;break;/*#define SIN 9#define COS 10#define TAN 11#define CTG 12#define LOG 13 */}; //switch 语句结束break;};//switchif(isErr==1 || isEnd==1)break;continue;} //运算符处理if(Index[IndexCursor]==WORD){printf(" 单词尚未处理!\n");break;}//在这⾥处理单词}//核⼼计算结束if(isErr==1){isErr=0;continue;}if(NumStackTop>0){Err(1000,-1,"多余的操作数:");printf("%f!\n",Number[NumStackTop]);}elseif(NumStackTop==0 && isEnd==1){if(fabs(Number[NumStackTop])>1e20) printf(" %.20e\n",Number[NumStackTop]); elseprintf(" %f\n",Number[NumStackTop]); isEnd=0;}}//主循环结束}//主函数结束。

C语言编写一个科学计算器

C语言编写一个科学计算器

C语言编写一个科学计算器科学计算器是一种功能强大的工具,可以进行各种复杂的科学计算。

下面是一个使用C语言编写的科学计算器的示例代码,用于演示其基本功能。

```c#include <stdio.h>#include <math.h>void mainMenprintf("------------------------\n");printf(" 科学计算器 \n");printf("------------------------\n");printf("1. 四则运算\n");printf("2. 求平方根\n");printf("3. 求立方根\n");printf("4. 求幂次方\n");printf("5. 求对数\n");printf("6. 退出\n");void arithmeticOperationfloat num1, num2;printf("请输入两个操作数:\n");scanf("%f %f", &num1, &num2);printf("1. 加法结果:%f\n", num1 + num2); printf("2. 减法结果:%f\n", num1 - num2); printf("3. 乘法结果:%f\n", num1 * num2); if (num2 != 0)printf("4. 除法结果:%f\n", num1 / num2); } elseprintf("操作数2不能为0!\n");}void squareRoofloat num;printf("请输入一个数字:\n");scanf("%f", &num);printf("平方根:%f\n", sqrt(num));void cubeRoofloat num;printf("请输入一个数字:\n");scanf("%f", &num);printf("立方根:%f\n", cbrt(num));void exponentiatiofloat base, exponent;printf("请输入底数和指数:\n");scanf("%f %f", &base, &exponent);printf("幂次方:%f\n", pow(base, exponent)); void logarithfloat num;printf("请输入一个数字:\n");scanf("%f", &num);printf("以e为底的自然对数:%f\n", log(num)); printf("以10为底的常用对数:%f\n", log10(num)); int maiint choice;domainMenu(;printf("请输入选项:\n");scanf("%d", &choice);switch (choice)case 1: arithmeticOperations(; break;case 2: squareRoot(;break;case 3: cubeRoot(;break;case 4: exponentiation(;break;case 5: logarithm(;break;case 6: printf("退出程序!\n"); break;default: printf("无效选项!\n"); break;}} while (choice != 6);return 0;```这个科学计算器的源码中,定义了一个主菜单函数`mainMenu(`,用户可以从菜单中选择执行的操作。

C语言 计算器 源代码

C语言 计算器 源代码
setcolor(BLUE); /*设置蓝色模式*/
rectangle(25,25,375,75); /*画屏显的边框*/
setcolor(GREEN); /*设置绿色模式*/
settextstyle(0,0,3); /*改变字符大小方向函数*/
if (error) {strcpy(scr_main,"error"); setcolor(RED); Flag=0; Num_flag=0; clrnum(0,1); }
inregs.x.ax=0x01; /*显示鼠标*/
int86(0x33,&inregs,&outregs); /*通用软中断库函数*/
}
/*鼠标消息*/
int mouse_message()
{
drawmouse(&Mx,&My,&Mkey); /*调用画鼠标函数*/
for (;Mk0==Mkey;)
initgraph( &GraphDriver, &GraphMode, "" ); /*初始化图形系统*/
ErrorCode = graphresult(); /*读初始化结果*/
if( ErrorCode != grOk ) /*如果初始化时出现错误*/
{
void calculator(int fun_code); /*实现计算器功能函数声明*/
void clrnum(int n0,int n1); /*清空Num数组函数声明*/
void adv(void); /*高级函数功能声明*/
double qiu_zhi(char *bds_start,char *bds_end); /*求表达式值函数声明*/

简易计算器程序代码

简易计算器程序代码

#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit fmq=P3^6;sbit wr_duan=P3^5;sbit wr_wei=P3^4;long AA=0,BB=0,jieguo;char flag=1,flag0=1,flag1=0,flag2=0,flag3=0,i=0,j=0,n=0;uchar key1,key2,key;uchar table0[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//0——9 uchar table1[]={0x79,0x77,0x77,0x3f,0x77}; //ERRORvoid key_scan();void delay(uint z);void switch_0(char z);void switch_1(char z);void switch_2(char z);void switch_3(char z);void switch_4(char z);void switch_5(char z);void switch_6(char z);void switch_7(char z);void switch_8(char z);void switch_9(char z);void SMG_display(char z);void main(){while(1){key_scan();}}void delay(uint z){uint x,y;for(x=z;x>0;x--);for(y=120;y>0;y--);}void key_scan(){P2=0xf0;if(P2!=0xf0){delay(20);if(P2!=0xf0){key1=P2;key1|=0x0f;key=key1&0xf0;P2=key1;key2=P2;key2&=0x0f;key+=key2;flag=1;P2=0xf0;}}if(flag){switch(key){case 0xe7 : flag=0;if(((i==0)&&flag0==1)||((j==0)&&(flag0==0)))break;if(flag0){n=i;switch_0(n);i++;} if(!flag0){n=j;switch_0(n);j++;}while(P2!=0xf0);break; //0case 0xeb : flag=0;if(flag0){n=i;switch_1(n);i++;}if(!flag0){n=j;switch_1(n);j++;}while(P2!=0xf0);break; //1case 0xdb : flag=0;if(flag0){n=i;switch_2(n);i++;}if(!flag0){n=j;switch_2(n);j++;}while(P2!=0xf0);break; //2case 0xbb : flag=0;if(flag0){n=i;switch_3(n);i++;}if(!flag0){n=j;switch_3(n);j++;}while(P2!=0xf0);break; //3case 0xed : flag=0;if(flag0){n=i;switch_4(n);i++;}if(!flag0){n=j;switch_4(n);j++;}while(P2!=0xf0);break; //4case 0xdd : flag=0;if(flag0){n=i;switch_5(n);i++;}if(!flag0){n=j;switch_5(n);j++;}while(P2!=0xf0);break; //5case 0xbd : flag=0;if(flag0){n=i;switch_6(n);i++;}if(!flag0){n=j;switch_6(n);j++;}while(P2!=0xf0);break; //6case 0xee : flag=0;if(flag0){n=i;switch_7(n);i++;}if(!flag0){n=j;switch_7(n);j++;}while(P2!=0xf0);break; //7case 0xde : flag=0;if(flag0){n=i;switch_8(n);i++;}if(!flag0){n=j;switch_8(n);j++;}while(P2!=0xf0);break; //8case 0xbe : flag=0;if(flag0){n=i;switch_9(n);i++;}if(!flag0){n=j;switch_9(n);j++;}while(P2!=0xf0);break; //9case 0x7e : flag=0;if((!i)&&(!j))break;flag0=0;flag1=1;while(P2!=0xf0);break; //+case 0x7d : flag=0;if((!i)&&(!j))break;flag0=0;flag1=2;while(P2!=0xf0);break; //-case 0x7b : flag=0;if((!i)&&(!j))break;flag0=0;flag1=3;while(P2!=0xf0);break; //*case 0x77 : flag=0;if((!i)&&(!j))break;flag0=0;flag1=4;while(P2!=0xf0);break; ///case 0xb7 : flag=0;if((!i)&&(!j))break;flag1=5;if(flag2)flag2=0;while(P2!=0xf0);break; //RCcase 0xd7 : flag=0;if((!i)&&(!j))break;flag2=1;flag3=1;while(P2!=0xf0);break; //=}}SMG_display(flag1);}void switch_0(char z){switch(z){case 0 :if(flag0)AA=0;if((!flag0)&&(i!=0))BB=0;break;case 1 :case 2 :case 3 :case 4 :case 5 :if(flag0)AA=(AA*10+0);if(!flag0)BB=(BB*10+0);break;default:break;}}void switch_1(char z){switch(z){case 0 :if(flag0)AA=1;if((!flag0)&&(i!=0))BB=1;break;case 1 :case 2 :case 3 :case 4 :case 5 :if(flag0)AA=(AA*10+1);if(!flag0)BB=(BB*10+1);break;default:break;}}void switch_2(char z){switch(z){case 0 :if(flag0)AA=2;if((!flag0)&&(i!=0))BB=2;break;case 1 :case 2 :case 3 :case 4 :case 5 :if(flag0)AA=(AA*10+2);if(!flag0)BB=(BB*10+2);break;default:break;}}void switch_3(char z){switch(z){case 0 :if(flag0)AA=3;if((!flag0)&&(i!=0))BB=3;break;case 1 :case 2 :case 3 :case 4 :case 5 :if(flag0)AA=(AA*10+3);if(!flag0)BB=(BB*10+3);break;default:break;}}void switch_4(char z){switch(z){case 0 :if(flag0)AA=4;if((!flag0)&&(i!=0))BB=4;break;case 1 :case 2 :case 3 :case 4 :case 5 :if(flag0)AA=(AA*10+4);if(!flag0)BB=(BB*10+4);break;default:break;}}void switch_5(char z){switch(z){case 0 :if(flag0)AA=5;if((!flag0)&&(i!=0))BB=5;break;case 1 :case 2 :case 3 :case 4 :case 5 :if(flag0)AA=(AA*10+5);if(!flag0)BB=(BB*10+5);break;default:break;}}void switch_6(char z){switch(z){case 0 :if(flag0)AA=6;if((!flag0)&&(i!=0))BB=6;break;case 1 :case 2 :case 3 :case 4 :case 5 :if(flag0)AA=(AA*10+6);if(!flag0)BB=(BB*10+6);break;default:break;}}void switch_7(char z){switch(z){case 0 :if(flag0)AA=7;if((!flag0)&&(i!=0))BB=7;break;case 1 :case 2 :case 3 :case 4 :case 5 :if(flag0)AA=(AA*10+7);if(!flag0)BB=(BB*10+7);break;default:break;}}void switch_8(char z){switch(z){case 0 :if(flag0)AA=8;if((!flag0)&&(i!=0))BB=8;break;case 1 :case 2 :case 3 :case 4 :case 5 :if(flag0)AA=(AA*10+8);if(!flag0)BB=(BB*10+8);break;default:break;}}void switch_9(char z){switch(z){case 0 :if(flag0)AA=9;if((!flag0)&&(i!=0))BB=9;break;case 1 :case 2 :case 3 :case 4 :case 5 :if(flag0)AA=(AA*10+9);if(!flag0)BB=(BB*10+9);break;default:break;}}void SMG_display(char z){ if(!flag2){if(flag0)jieguo=AA;if(!flag0)jieguo=BB;if(z==5){jieguo=0;AA=0;BB=0;i=0;j=0;n=0;flag=1;flag0=1;flag1=0;flag2=0;flag3=0;}}else{if(flag3){switch(z){case 1 :jieguo=AA+BB;break;case 2 :jieguo=AA-BB;break;case 3 :jieguo=AA*BB;break;case 4 :jieguo=AA/BB;break;default:break;}flag3=0;}}if((jieguo/1000000)<=0){wr_duan=0;P0=0x00;wr_duan=1;wr_wei=0;P0=0x02;wr_wei=1;wr_duan=0;P0=table0[jieguo%100/10];wr_duan=1;delay(1);wr_duan=0;P0=0x00;wr_duan=1;wr_wei=0;P0=0x04;wr_wei=1;wr_duan=0;P0=table0[jieguo%1000/100];wr_duan=1;delay(1);wr_duan=0;P0=0x00;wr_duan=1;wr_wei=0;P0=0x08;wr_wei=1;wr_duan=0;P0=table0[jieguo%10000/1000];wr_duan=1;delay(1);wr_duan=0;P0=0x00;wr_duan=1;wr_wei=0;P0=0x10;wr_wei=1;wr_duan=0;P0=table0[jieguo%100000/10000];wr_duan=1;delay(1);wr_duan=0;P0=0x00;wr_duan=1;wr_wei=0;P0=0x20;wr_wei=1;wr_duan=0;P0=table0[jieguo%1000000/100000];wr_duan=1;delay(1);wr_duan=0;P0=0x00;wr_duan=1;}else{wr_wei=0;P0=0x10;wr_wei=1;wr_duan=0;P0=table1[0];wr_duan=1;delay(1);wr_duan=0;P0=0x00;wr_duan=1;wr_wei=0;P0=0x08;wr_wei=1;wr_duan=0;P0=table1[1];wr_duan=1;delay(1);wr_duan=0;P0=0x00;wr_duan=1;wr_wei=0;P0=0x04;wr_wei=1;wr_duan=0;P0=table1[2];wr_duan=1;delay(1);wr_duan=0;P0=0x00;wr_duan=1;wr_wei=0;P0=0x02;wr_wei=1;wr_duan=0;P0=table1[3];wr_duan=1;delay(1);wr_duan=0;P0=0x00;wr_duan=1;wr_wei=0;P0=0x01;wr_wei=1;wr_duan=0;P0=table1[4];wr_duan=1;delay(1);wr_duan=0;P0=0x00;wr_duan=1;jieguo=0;AA=0;BB=0;i=0;j=0;n=0;flag=1;flag0=1;flag1=0;flag2=0;flag3=0;}}形发生器电路(数码管)元件清单序号名称规格封装数量代号1 插座双排针IDC11J12 插头座4脚*5mmMKDS15_4RMI1J23 插头座2.54mm*4(4针)SIP4 1J44 按键12*12 KEY1217KK1 K2 K3 K4 K5 K6 K7 K8K9 K10 K11 K12 K13 K14 K15 K161 跳线插针,短路棒SIP2 1JP19 晶振11.0592MHzXTAL-11X11 0 发光二极管红色(3mm)LED0.11D11 1 二极管IN4148DIODE-0.31D21 2 电阻1KAXIAL0.31R81 3 电阻10KAXIAL0.32R5R11 4 电阻20KAXIAL0.32R6R71排阻4K7-8 SIP9 1 R5 A11 6 排阻2K2-8 SIP9 1RA21 7 排阻4K7-4 SIP5 1RB11 8 电容30P(22P)CAP0.22C1C21 9 电容104CAP0.25C1C11 C12 C13 C92 0 电容10ufRB-.1/.21C72 1 电容100ufRB-.1/.21C92 2 集成电路AT89S52DIP41U12 3 集成电路74LS374DIP22U2U32 4 集成电路DAC0832DIP21U52 5 集成电路LM324DIP141U62 6 集成电路MC1413DIP161U72 7 数码管红色0.5英寸SM42056DS1DS2 DS3 DS4 DS5 DS62 8 集成插座14PDIP141U62 9 集成插座16PDIP161U73 0 集成插座20PDIP23U2U3 U53 1 集成插座28PDIP281数码管座3 2 集成插座40PDIP42U1数码管座v。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

可以用鼠标操作的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> /*字符操作函数*/#include <bios.h>/** **** ***** ******宏代换定义****** ****** *** *****/#define BWIDTH 50 /*按键条的宽度*/#define BHEIGHT 30 /*按键条的高度*/#define MAX_SCR 15#define PI 3.141592654/** **** ********** ***声明函数** ****** ** **** *****/void graph(void); /*画图形函数声明*/void windows(void); /*绘制窗口界面函数声明*/void button(int x,int y,int c1,int c2,int c3,char *bt); /*按键函数声明*/void scr(char scr_main[MAX_SCR],int error); /*屏显函数声明*/int mouse_message(void); /*鼠标信息函数声明*/void visbilemouse(void); /*鼠标可视化函数声明*/void getmouse(int *x,int *y,int *key); /*获得鼠标信息函数声明*/void drawmouse(int *x,int *y,int *z); /*画鼠标函数声明*/void jd(int x,int y); /*按钮焦点函数声明*/void calculator(int fun_code); /*实现计算器功能函数声明*/void clrnum(int n0,int n1); /*清空Num数组函数声明*/void adv(void); /*高级函数功能声明*/double qiu_zhi(char *bds_start,char *bds_end); /*求表达式值函数声明*/long jie_cheng(long n); /*阶乘函数声明*//** **** ***** **** ****定义全局变量* ****** **** ****/int Maxcolor,Maxx,Maxy; /*定义最大颜色数,边界*/char Num[2][MAX_SCR]={{0},{0}}; /*定义存储数据的二维数组,分别对应双目运算的数据*/double Memory=0; /*定义内存存储数据*/int Flag=0,Num_flag=0; /*二维数组的第几维数和第几个数*/char fun=0; /*功能变量,对应双目运算*/int Mx,My,Mkey,Mk0=-1; /*定义鼠标的位置和按键状态,初始键值为-1*/int jd0=-1,jd1=-1; /*定义按键焦点,按鼠标键后返回0~29或-1*/charfunction[30]={'Q','M','R','S','P','B','7','4','1','0','C','8','5','2','F','A','9','6','3','.','%','/','*','-','+','e','^','K','D ','='}; /*定义按键对应的function[fun_code]*/char Adv_ch[100]={0}; /*定义表达式数组变量*//** **** ***** ** ****** ***主函数** *** *** **** *****/void main(){int message; /*定义通信变量*/graph(); /*进入图形模式*/windows(); /*绘制窗口界面*/visbilemouse(); /*显示鼠标*/for (;message=mouse_message();)if (message!=-1) calculator(message); /*用鼠标输入数据mouse-message返回jd1到message*/closegraph(); /*关闭图形模式*/} /*message,fun-code,jd1是相互联系的通信变量*//** **** **** *** ***进入图形模式** *** *** ** ***** *****/void graph(void){int GraphDriver=DETECT,GraphMode,ErrorCode;initgraph( &GraphDriver, &GraphMode, "" ); /*初始化图形系统*/ErrorCode = graphresult(); /*读初始化结果*/if( ErrorCode != grOk ) /*如果初始化时出现错误*/{printf("Graphics System Error: %s\n",grapherrormsg( ErrorCode ) ); /*显示错误代码*/ exit(1); /*退出*/}/*读取最大颜色数,最大横纵座标*/Maxcolor=getmaxcolor();Maxx=getmaxx();Maxy=getmaxy();}/** **** *** ** ***** *绘制窗口界面***** ** ** ***** *****/void windows(void){int i,j;char btext[30][6]={"QUIT"," MC"," MR"," MS"," M+"," \033 "," 7 "," 4 "," 1 "," 0 "," C "," 8 ","5 "," 2 ","+/-","ADV"," 9 ","6 "," 3 "," . "," % "," / "," * "," - "," + ","EXP","X^Y","y\0373X","1/X"," = "};/*定义各键的显示符号*/intctext[30]={RED,GREEN,GREEN,GREEN,GREEN,RED,BLUE,BLUE,BLUE,BLUE,RED,BLU E,BLUE,BLUE,BLUE,RED,BLUE,BLUE,BLUE,BLUE,BLUE,BLUE,BLUE,BLUE,BLUE,BLU E,BLUE,BLUE,BLUE,RED}; /*定义各键的显示颜色*/setbkcolor(WHITE); /*定义背景色*/setcolor(BLUE); /*设定蓝色模式*/rectangle(10,10,Maxx-10,Maxy-10); /*画计算器边框*/line(10,300,400,300);line(400,10,400,300);bar(150,0,245,20);outtextxy(160,7,"Calculator");for (i=0;i<=5;i+=1)for (j=0;j<=4;j+=1)button(25+i*(BWIDTH+10),100+j*(BHEIGHT+10),WHITE,ctext,ctext,btext); /*画按键的矩形条*/scr(Num[Flag],0);}/** **** ***** *** ***绘制按钮** ****** ******** *****/void button(int x,int y,int c1,int c2,int c3,char *bt){setfillstyle(SOLID_FILL,c1); /*用c1颜色填充*/bar(x,y,x+BWIDTH,y+BHEIGHT); /*画按键的条形图*/setcolor(c2); /*设置c2颜色模式*/rectangle(x,y,x+BWIDTH,y+BHEIGHT); /*画矩形框*/setcolor(c3); /*设置c3颜色模式*/outtextxy(x+13,y+13,bt); /*字符输出定位函数*/}/** **** ****** ***计算器屏幕显示** ******* **** *****/void scr(char scr_main[MAX_SCR],int error){char memory_s[20]; /*定义内存数组来存储内存数据*/bar(25,25,375,75); /*画屏显的条形图*/setcolor(BLUE); /*设置蓝色模式*/rectangle(25,25,375,75); /*画屏显的边框*/setcolor(GREEN); /*设置绿色模式*/settextstyle(0,0,3); /*改变字符大小方向函数*/if (error) {strcpy(scr_main,"error"); setcolor(RED); Flag=0; Num_flag=0; clrnum(0,1); } outtextxy(30,50,scr_main); /*字符输出定位函数scr_main中存储的是主屏数据*/setcolor(RED); /*设置红色模式*/settextstyle(0,0,2); /*改变字符大小方向函数*/sprintf(memory_s,"M[%f]",Memory); /*将Memory拷贝到memory-s中再按"M[0.000000]"格式化输出*/outtextxy(30,30,memory_s); /*字符输出定位函数,内存显示*/}/** **** **** **** ***鼠标** ****** **** *****//*显示鼠标*/void visbilemouse(){union REGS inregs,outregs;inregs.x.ax=0x01; /*显示鼠标*/int86(0x33,&inregs,&outregs); /*通用软中断库函数*/}/*鼠标消息*/int mouse_message(){drawmouse(&Mx,&My,&Mkey); /*调用画鼠标函数*/for (;Mk0==Mkey;){getmouse(&Mx,&My,&Mkey); /*调用获得鼠标函数*/}if (Mk0==1){Mk0=-1;return jd1;}if (Mk0==2){Mk0=-1;return jd1;}}/*获取鼠标位置和按键,key=1是左键,key=2是右键*/void getmouse(int *x,int *y,int *key){union REGS inregs,outregs;inregs.x.ax=3; /*获取鼠标位置和状态也可以用3*/int86(0x33,&inregs,&outregs); /*中断调用*/*x=outregs.x.cx; /*cx寄存器中存的是横坐标*/*y=outregs.x.dx; /*dx寄存器中存的是列坐标*/*key=outregs.x.bx; /*bx寄存器是按键状态*/}/*按键后,返回当前鼠标的x,y和按键状态,直到按键后才返回*/void drawmouse(int *x,int *y,int *z) /*画鼠标是利用将一个空矩形存入内存中,然后再在该空矩形中画鼠标形状*/{int a=0,b=0,c=0,a_old=0,b_old=0; /*a,b的值可以随便*/int *ball; /*定义指向内存的存储图形的指针*/ball=malloc(imagesize(a,b,a+20,b+20)); /*返回矩形的大小*/getimage(a,b,a+20,b+20,ball); /*第一次将图形(一个空矩形)存入到内存中,内存中存入的是一个空矩形*/while(c==0) /*直到按键后才返回,不按键一直循环*/{getmouse(&a,&b,&c); /*a,为横坐标,b,为列坐标,c为按键状态*/if(a<0) a=0; /*保证鼠标左边不出界*/if(b<0) b=0; /*保证上面不出界*/if(a>Maxx-20) a=Maxx-20; /*保证右边不出界*/if(b>Maxy-20) b=Maxy-20; /*保证下边不出界*/if(a!=a_old || b!=b_old) /*当鼠标移动时*/{putimage(a_old,b_old,ball,0); /*在a_old,b_old输出图形,用来擦掉原来的鼠标*/jd(a,b);getimage(a,b,a+20,b+20,ball); /*这条语句是将目前鼠标所在位置的图形存贮到ball里面试着看着有无这条语句的区别*//*不需要在获取了*/setcolor(BLUE);setlinestyle(0,0,1);line(a,b,a+15,b+15/2);line(a,b,a+15/2,b+15);line(a+15/2,b+15,a+10,b+25/2);line(a+15,b+15/2,a+25/2,b+10);line(a+10,b+25/2,a+15,b+35/2);line(a+25/2,b+10,a+35/2,b+15);line(a+15,b+35/2,a+35/2,b+15);/*画鼠标*/}a_old=a;b_old=b;} /*while()结束*/Mk0=c;*x=a;*y=b;*z=c; /*返回按键后鼠标的位置*/putimage(a,b,ball,0); /*将鼠标擦掉,因为存在ball里面的是一个背景为缺省状态下的一个空矩形*/free(ball);}/** **** **** *** ***按钮焦点*** *** ** *** *****/void jd(int x,int y){int i;jd1=(y-95)/(BHEIGHT+10)+(x-20)/(BWIDTH+10)*5; /*确定鼠标具体在哪个键上,得到编号来对应键*/if (jd0==jd1) return; /*编号未变即鼠标未移位,返回*/if (!((x>20&&x<380) && (y>95&&y<295))) jd1=-1; /*超出边界,编号为-1*/if (jd0!=-1){setcolor(WHITE);setlinestyle(0,0,3);rectangle(jd0/5*(BWIDTH+10)+20,jd0%5*(BHEIGHT+10)+95,jd0/5*(BWIDTH+10)+30+B WIDTH,jd0%5*(BHEIGHT+10)+105+BHEIGHT);} /*将上次突出显示的黄色擦除*/if (jd1!=-1){setcolor(YELLOW);setlinestyle(0,0,3);rectangle((x-20)/(BWIDTH+10)*(BWIDTH+10)+20,(y-95)/(BHEIGHT+10)*(BHEIGHT+10) +95,(x-20)/(BWIDTH+10)*(BWIDTH+10)+30+BWIDTH,(y-95)/(BHEIGHT+10)*(BHEIGHT+1 0)+105+BHEIGHT);} /*突出显示鼠标所在键为黄色*/jd0=jd1; /*将上次的鼠标位置赋予jd0,以用于鼠标位置变化的判断和突出显示黄色的擦除*/}/** **** ****** ****实现计算器功能* ****** * *****/void calculator(int fun_code){int i,error=0;int dot_num=0,e_num=0;int len=0;char temp_s[20]={0};for (i=0;Num[Flag];i++) { dot_num+=Num[Flag]=='.'?1:0; e_num+=Num[Flag]=='e'?1:0; len++;}Num_flag=len;for (i=Num_flag;i<MAX_SCR;i++) Num[Flag]=0;switch(function[fun_code]){case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9': if (Num_flag<MAX_SCR-1) Num[Flag][Num_flag++]=function[fun_code]; break; case '0': if (Num_flag<MAX_SCR-1 && (atof(Num[Flag])!=0 || Num[Flag][0]=='.')) Num[Flag][Num_flag++]=function[fun_code]; break;case '.': if (Num_flag<MAX_SCR-1 && dot_num==0) Num[Flag][Num_flag++]=function[fun_code]; dot_num++; break;case 'e': if (Num_flag<MAX_SCR-1 && e_num==0) Num[Flag][Num_flag++]=function[fun_code]; e_num++; break;case 'F':if (Num[Flag][0]=='-'){for (i=0;i<MAX_SCR-1;i++) Num[Flag]=Num[Flag];}/*左移一位*/else if (Num_flag<MAX_SCR-1){for (i=MAX_SCR-2;i>0;i--) Num[Flag]=Num[Flag]; Num[Flag][0]='-';}/*右移一位*/break;case '+':case '-':case '*':case '/':case '%':case '^':case 'K': Flag=1; Num_flag=0; fun=function[fun_code]; clrnum(Flag,Flag); break;case 'B': if (Num_flag>0) Num[Flag][--Num_flag]=0; break;case 'A': closegraph();adv(); break;case 'C': Flag=0; Num_flag=0; clrnum(0,1); break;case 'M': Memory=0; break;case 'R': clrnum(Flag,Flag); gcvt(Memory,10,temp_s);strcpy(Num[Flag],temp_s); break;case 'S': Memory=atof(Num[Flag]); break;case 'P': Memory+=atof(Num[Flag]); break;case 'D': if (atof(Num[Flag]))gcvt(1/atof(Num[Flag]),10,Num[Flag]);elseerror=1;break;case '=': Flag=0; Num_flag=0;switch(fun){case '+': gcvt(atof(Num[0])+atof(Num[1]),10,Num[0]); break;case '-': gcvt(atof(Num[0])-atof(Num[1]),10,Num[0]); break;case '*': gcvt(atof(Num[0])*atof(Num[1]),10,Num[0]); break;case '/': if (atof(Num[1]))gcvt(atof(Num[0])/atof(Num[1]),10,Num[0]);else error=1;break;case '%': gcvt((long)atof(Num[0])%(long)atof(Num[1]),10,Num[0]); break;case '^': gcvt(pow(atof(Num[0]),atof(Num[1])),10,Num[0]); break;case 'K': if (atof(Num[1]))gcvt(pow(atof(Num[0]),1/atof(Num[1])),10,Num[0]);else error=1;break;}break;}scr(Num[Flag],error);}/*清空Num数组*/void clrnum(int n0,int n1){int i;for (i=0;i<MAX_SCR;i++) Num[n0]=Num[n1]=0;}/*高级功能*/void adv(){char *i;printf("Forexample:\n[1+(2-3)]*4/{5%6}+1e3+sin(pi/4)-arctan(1)*4+3^4+5!+ln2.718-log(2+3)|(10^3)\nInput esc to quit\n");for (;1;){printf("Please input:\n");gets(Adv_ch);if (!strcmp(Adv_ch,"quit") || !strcmp(Adv_ch,"esc")) exit(0);for (i=Adv_ch;i<Adv_ch+strlen(Adv_ch);i++){if (*i=='[' || *i=='{') *i='(';if (*i==']' || *i=='}') *i=')';}printf("=%f\n",qiu_zhi(Adv_ch,Adv_ch+strlen(Adv_ch)-1));}}/*求表达式的值*/double qiu_zhi(char *bds_start,char *bds_end){char *now;char temp[100]={0};int kuo_hao=0,i=0;for (now=bds_start+1;now<=bds_end;now++){if (*now=='+' || *now=='-' || *now=='*' || *now=='/' || *now=='%') i++;if (*now=='^' || *now=='!' || *now=='s' || *now=='c' || *now=='t' || *now=='l' || *now=='g' || *now=='n') i++;}if (!i){now=bds_start;for (;now<=bds_end;now++) temp[now-bds_start]=*now;if (temp[0]=='p' && temp[1]=='i') return PI;return (atof(temp));}for (now=bds_end;now>=bds_start;now--){kuo_hao+=(*now==')'?1:0); kuo_hao-=(*now=='('?1:0);if (*now=='+' && kuo_hao==0) return(qiu_zhi(bds_start,now-1)+qiu_zhi(now+1,bds_end));if (*now=='-' && kuo_hao==0) return(qiu_zhi(bds_start,now-1)-qiu_zhi(now+1,bds_end));}for (now=bds_end;now>=bds_start;now--){kuo_hao+=(*now==')'?1:0); kuo_hao-=(*now=='('?1:0);if (*now=='*' && kuo_hao==0) return(qiu_zhi(bds_start,now-1)*qiu_zhi(now+1,bds_end));if (*now=='/' && kuo_hao==0) return(qiu_zhi(bds_start,now-1)/qiu_zhi(now+1,bds_end));if (*now=='%' && kuo_hao==0) return((long)qiu_zhi(bds_start,now-1)%(long)qiu_zhi(now+1,bds_end));}for (now=bds_end;now>=bds_start;now--){kuo_hao+=(*now==')'?1:0); kuo_hao-=(*now=='('?1:0);if (*(now-3)=='a' && *(now-2)=='r' && *(now-1)=='c' && *now=='s' && *(now+1)=='i' && *(now+2)=='n' && kuo_hao==0)return(asin(qiu_zhi(now+3,bds_end)));if (*(now-3)=='a' && *(now-2)=='r' && *(now-1)=='c' && *now=='c' && *(now+1)=='o' && *(now+2)=='s' && kuo_hao==0)return(acos(qiu_zhi(now+3,bds_end)));if (*(now-3)=='a' && *(now-2)=='r' && *(now-1)=='c' && *now=='t' && *(now+1)=='a' && *(now+2)=='n' && kuo_hao==0)return(atan(qiu_zhi(now+3,bds_end)));if (*(now-3)=='a' && *(now-2)=='r' && *(now-1)=='c' && *now=='c' && *(now+1)=='s' && *(now+2)=='c' && kuo_hao==0)return(asin(1/qiu_zhi(now+3,bds_end)));if (*(now-3)=='a' && *(now-2)=='r' && *(now-1)=='c' && *now=='s' && *(now+1)=='e' && *(now+2)=='c' && kuo_hao==0)return(acos(1/qiu_zhi(now+3,bds_end)));if (*(now-3)=='a' && *(now-2)=='r' && *(now-1)=='c' && *now=='c' && *(now+1)=='o' && *(now+2)=='t' && kuo_hao==0)return(atan(1/qiu_zhi(now+3,bds_end)));if (*now=='s' && *(now+1)=='i' && *(now+2)=='n' && kuo_hao==0) return(sin(qiu_zhi(now+3,bds_end)));if (*now=='c' && *(now+1)=='o' && *(now+2)=='s' && kuo_hao==0) return(cos(qiu_zhi(now+3,bds_end)));if (*now=='t' && *(now+1)=='a' && *(now+2)=='n' && kuo_hao==0) return(tan(qiu_zhi(now+3,bds_end)));if (*now=='c' && *(now+1)=='s' && *(now+2)=='c' && kuo_hao==0) return(1/sin(qiu_zhi(now+3,bds_end)));if (*now=='s' && *(now+1)=='e' && *(now+2)=='c' && kuo_hao==0) return(1/cos(qiu_zhi(now+3,bds_end)));if (*now=='c' && *(now+1)=='o' && *(now+2)=='t' && kuo_hao==0) return(1/tan(qiu_zhi(now+3,bds_end)));}for (now=bds_end;now>=bds_start;now--){char *x;kuo_hao+=(*now==')'?1:0); kuo_hao-=(*now=='('?1:0);if (*now=='l' && *(now+1)=='n' && kuo_hao==0) return(log(qiu_zhi(now+2,bds_end)));if (*now=='|' && kuo_hao==0){x=now;for (;x>=bds_start;x--)if (*x=='l' && *(x+1)=='o' && *(x+2)=='g') break;return(log10(qiu_zhi(now+1,bds_end))/log10(qiu_zhi(x+3,now-1)));}}for (now=bds_end;now>=bds_start;now--){kuo_hao+=(*now==')'?1:0); kuo_hao-=(*now=='('?1:0);if (*now=='^' && kuo_hao==0) return(pow(qiu_zhi(bds_start,now-1),qiu_zhi(now+1,bds_end)));if (*now=='!' && kuo_hao==0) return(jie_cheng((long)qiu_zhi(bds_start,now-1)));}if (*bds_start=='(' && *bds_end==')') return qiu_zhi(bds_start+1,bds_end-1);printf("error");}/*计算阶乘*/long jie_cheng(long n){long jc=n;for (n--;n>0;n--) jc*=n;return jc;}。

相关文档
最新文档