C++四则运算源程序代码

合集下载

C语言-四则运算

C语言-四则运算

四则运算姓名:学号:班级:1.功能结构图2.程序功能进行整数的加减乘除和求模运算。

程序采用随机产生1~100的两个数进行运算每种运算有10个题目用户输入对应的答案程序提示答案的对错最后统计正确率。

每次给出两次答题机会。

3.程序流程图4.函数列表及功能5.源程序代码#include<stdio.h>#include<stdlib.h>#include<time.h>#define N 10int f(int a,int b){ //自定义函数int result;result=a+b;return result;}int f1(int a,int b){int result;result=a-b;return result;}int f2(int a,int b){int result;result=a*b;return result;}int f3(int a,int b){int result;result=a*b/b;return result;}int mod(int a,int b){int result;result=a%b;return result;}int main(){int a,b,res,ans;int i,count;int op,c;srand((unsigned)time(NULL)); while(1){printf("\n---加减乘除运算练习系统---\n");printf("1.加法运算\n");printf("2.减法运算\n");printf("3.乘法运算\n");printf("4.除法运算\n");printf("5.求模运算\n");printf("6.混合运算\n");printf("0.退出练习\n");printf(" 请输入数字0~6:");scanf("%d",&op); //输入相应数字进行练习switch(op){case 1:printf("--请进行加法运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%100+1;b=rand()%100+1;res=f(a,b); //调用加法函数printf("%d+%d=",a,b);scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 2:printf("--请进行减法运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%100+1;b=rand()%100+1;res=f1(a,b); //调用减法函数printf("%d-%d=",a,b);scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 3:printf("--请进行乘法运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%100+1;b=rand()%100+1;res=f2(a,b); //调用乘法函数printf("%d*%d=",a,b);scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 4:printf("--请进行除法运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%10+1;b=rand()%10+1;res=f3(a,b); //调用除法函数printf("%d/%d=",a*b,b);scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 6:printf("--请进行混合运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%100+1;b=rand()%100+1;c=rand()%5; //产生0~4的随机数{switch(c) //随机出现进行混合运算{case 0:printf("%d+%d=",a,b);res=f(a,b);break;case 1:printf("%d-%d=",a,b);res=f1(a,b);break;case 2:printf("%d*%d=",a,b);res=f2(a,b);break;case 3:a=a%10+1;b=b%10+1;printf("%d/%d=",a*b,b);res=f3(a,b);break;case 4:printf("%d%%%d=",a,b);res=mod(a,b);break;}}scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 5:printf("--请进行求模运算--\n");count=0;for(i=1;i<=N;i++){a=rand()%100+1;b=rand()%100+1;res=mod(a,b); //调用求模函数printf("%d%%%d=",a,b);scanf("%d",&ans); //输入数值if(ans==res){printf("Very Good!\n");count++;}else{printf("wrong! 请重新输入\n");scanf("%d",&ans);if(ans==res)printf("Very Good!\n");elseprintf("Wrong Answer!\n");}}printf("***正确率为%.0f%%***\n",100.0*count/N); break;case 0:goto END; //转到结束}}END:printf("---练习结束---\n");return 0;}6程序运行图选取起始和加法混合运算程序开始显示6种练习方式输入0为退出。

C四则运算程序代码

C四则运算程序代码

四则表达式计算一、程序分析和设计把整个表达式存为字符数组,按照四则运算规则寻找其中的运算符号,然后提取左右两侧操作数计算,用计算结果替换掉计算的表达式,这样整体表达式会被简化,继续重复这个过程,直到找不到运算符为止。

1)运算表达式的存储定义字符数组来存储即可,注意由于运算过程中可能会出现小数,而小数会导致表达式长度增加,所以定义时字符数组长度应该足够(比如1000)并且计算结果应该减少精度,比如保留小数点后面三位。

2)数和字符数组之间的转化提取出来的操作数实际为字符序列,计算前我使用 atof(定义在stdlib.h)转化为浮点数运算,同样的,运算结果要替换表达式中的字符序列也需要先转化为字符串才行,我使用 sprintf 函数,该函数定义在 stdio.h 中字符串的连接使用了 strcat(string.h) 等函数。

3)计算逻辑设pstr 为计算表达式//先算乘除for(int i=1;i<strlen(pstr);i++){If(如果当前位置字符为*或者/){把运算符保存在ysf中从该位置左边找前边的操作符获取左边要计算的数据coml和该数据前的字符串strleft从该位置右边找后边的操作符获取右边要计算的数据comr和该数据后的字符串 strright将数据变换成浮点型运算再转换为字符型的结果连接 strleft + 结果 + stright 组成新字符串替换掉 pstr }}//再找加减//代码与找乘除的过程类似为了代码简单,中途设计了几个计算函数double jia(char x[],char y[]);//字符加法返回double的值double jian(char x[],char y[]);//字符减法返回double的值double cheng(char x[],char y[]);//字符乘法返回double的值double chu(char x[],char y[]);//字符除法返回double的值double xsd(char x[]);//字符检测是否有小数,返回double的值为了可以重复输入,开头使用了个do while ,为了可以输入-1+2这种表达式,我设计了个判断,如果第一字符是+或-我给整体表达式前加了个0字符这样就转化为一般的字符了!如果是* /则提示出错,别重新输入!二、程序主要代码#include<iostream.h>#include<stdlib.h>//atof函数的头文件#include<stdio.h>//sprintf函数的头文件#define MAX 255//宏定义常量#include<string.h>double cmjia(char x[],char y[]);//声明函数double cmjian(char x[],char y[]);double cmcheng(char x[],char y[]);double cmchu(char x[],char y[]);double compute(char pstr[]);double xsd(char x[]);//主函数void main(){do{double t;//存储运算结果char a[]="0";char linshi[MAX];//计算临时用的char biaodashi[MAX];//用来存放表达式for(;;)//输入表达式{cout<<"请输入一个正确的数学表达式,形如7+8*6,按回车结束"<<endl;//输入表达式cin>>biaodashi;if(biaodashi[0]=='*'||biaodashi[0]=='/')cout<<"您输入的表达式有误,请重新输入"<<endl;//判断首字符是否为*/,如果是提示错误别返回重新输入else break;}if(biaodashi[0]=='-'||biaodashi[0]=='+')//判断首字符是否为+-,如果是给字符前加字符0 {strcat(a,biaodashi);strcpy(biaodashi,a);}strcpy(linshi,biaodashi);t=compute(linshi);//计算cout<<"运算结果是:"<<biaodashi<<'='<<t<<endl;//输出结果}while(1);//计算完返回,接着输入}//自定义函数double xsd(char x[])//判断是否有小数点,并把字符型转化为浮点型{for(int i=0;i<strlen(x);i++)if(x[i]=='.'){double num = 0.0;sscanf(x, "%f", &num);//return num;}elsereturn atof(x);}double cmjia(char x[],char y[])//计算两字符加法,并返回double型值{double a,b;a=xsd(x);b=xsd(y);double c=a+b;return c;}double cmjian(char x[],char y[])//计算两字符减法,并返回double型值{double a,b;a=xsd(x);b=xsd(y);double c=a-b;return c;}double cmcheng(char x[],char y[])//计算两字符乘法,并返回double型值{double a,b;a=xsd(x);b=xsd(y);double c=a*b;return c;}double cmchu(char x[],char y[])//计算两字符除法,并返回double型值{double a,b;a=xsd(x);b=xsd(y);double c=a/b;return c;}double compute(char pstr[])//{char strleft[MAX],strright[MAX],coml[MAX],comr[MAX],com[MAX],ysf;//ysf代表运算符double comzhi,zhi;//comzhi代表中途计算的值,com代表comzhi所对应的字符型int n=0;//先算乘除for(int i=0;i<strlen(pstr);i++){if(pstr[i]=='*'||pstr[i]=='/'){ysf=pstr[i];//获取左边要计算的数据coml和该数据前的字符串strleftfor(int j=i-1;j>=0;j--){if(pstr[j]=='-'||pstr[j]=='+'){for(int k=0;k<=j;k++) strleft[k]=pstr[k];strleft[k]='\0';//获取左边要计算数据前的字符串strleftfor(k=j+1,n=0;k<i;k++,n++) coml[n]=pstr[k];coml[n]='\0';//获取左边要计算的数据comlbreak;}elsefor(int k=0;k<i;k++){coml[k]=pstr[k];strleft[0]='\0';}}//获取右边要计算的数据comr和该数据后的字符串strrightfor(j=i+1;j<=strlen(pstr);j++){if(pstr[j]=='-'||pstr[j]=='+'||pstr[j]=='*'||pstr[j]=='/'||pstr[j]=='\0'){for(int k=0,n=j;k<strlen(pstr)-i-1;k++,n++) strright[k]=pstr[n];strright[k]='\0';for(k=i+1,n=0;k<j;k++,n++) comr[n]=pstr[k];comr[n]='\0';break;}}if(ysf=='*')//如果是*{comzhi=cmcheng(coml,comr);sprintf(com,"%.3f",comzhi);}else//如果是/{comzhi=cmchu(coml,comr);sprintf(com,"%.3f",comzhi);}strcat(strleft,com);//把数据前的字符和运算后的字符连接strcat(strleft,strright);//把数据后的字符和运算后的字符连接strcpy(pstr,strleft);//替换pstr}//重新找操作符}//再找加减for(i=0;i<strlen(pstr);i++){if(pstr[i]=='+'||pstr[i]=='-'){ysf=pstr[i];//获取左边要计算的数据comlfor(int k=0;k<i;k++) coml[k]=pstr[k];coml[k]='\0';//获取右边要计算的数据和该数据后的字符串strrightfor(int j=i+1;j<=strlen(pstr);j++){if(pstr[j]=='-'||pstr[j]=='+'||pstr[j]=='\0'){for(int k=0,n=j;k<=strlen(pstr)-j;k++,n++) strright[k]=pstr[n];strright[k]='\0';for(k=i+1,n=0;k<j;k++,n++) comr[n]=pstr[k];comr[n]='\0';break;}}if(ysf=='+'){comzhi=cmjia(coml,comr);sprintf(com,"%.3f",comzhi);}else{comzhi=cmjian(coml,comr);sprintf(com,"%.3f",comzhi);}strcat(com,strright);strcpy(pstr,com);}}//没找到,说明全部+-都计算完了zhi=atof(pstr);return zhi;}三、运行效果四、总结还无法提醒形如3-45*+或输入sgfs-34等式的错误,别重新输入,无法计算带括号和函数的表达式等更复杂的四则表达式。

用c语言编写加减乘除程序

用c语言编写加减乘除程序

用c语言编写加减乘除程序加减乘除是数学上最基本的四则运算,而用计算机语言实现这些运算则是计算机科学最基本的知识之一。

在c语言中,实现四则运算需要使用基本的算术运算符,并需要注意数据类型的匹配。

加法运算是最简单的四则运算之一,使用c语言执行加法运算的方法是,用“+”符号分隔两个运算数并用“=”符号赋值给结果变量。

例如,将两个整数相加并输出结果,代码如下:```#include <stdio.h>int main(){int a = 5, b = 7, sum;sum = a + b;printf("The sum of %d and %d is %d", a, b, sum);}```这段代码将输出结果:“The sum of 5 and 7 is 12”,其中sum 变量存储了a和b两个变量的和。

减法运算的实现方法与加法运算类似,只需将运算符改为“-”即可,例如:```#include <stdio.h>int main(){int a = 5, b = 7, diff;diff = a - b;printf("The difference between %d and %d is %d", a, b, diff);}```这段代码将输出结果:“The difference between 5 and 7 is -2”,其中diff变量存储了a和b两个变量的差。

乘法运算可以使用“*”符号来实现,例如:```#include <stdio.h>int main(){int a = 5, b = 7, prod;prod = a * b;printf("The product of %d and %d is %d", a, b, prod);return 0;```这段代码将输出结果:“The product of 5 and 7 is 35”,其中prod变量存储了a和b两个变量的积。

c语言四则运算程序

c语言四则运算程序

c语言四则运算程序C语言是一种通用的计算机程序设计语言,用于开发软件和操作系统等工程项目。

C语言的语法简洁,可以编写出高效的代码,因此在编写四则运算程序中被广泛应用。

四则运算指的是加减乘除四种基本运算。

在C语言中,可以使用基本的算术运算符来进行四则运算,例如“+”表示加法,“-”表示减法,“*”表示乘法,“/”表示除法。

在程序中使用scanf函数获取用户输入的数字,然后通过计算输出运算结果。

下面展示一个简单的加法程序,用户输入两个数字,程序通过相加运算输出结果:#include<stdio.h>int main(){int a,b,c;printf("Please enter two numbers:");scanf("%d %d",&a,&b);c=a+b;printf("%d + %d = %d\n",a,b,c);return 0;}以上程序展示了如何使用scanf函数获取用户的输入。

scanf函数的第一个参数是格式化字符串,指定要读取的类型及其数量,例如“%d”表示整数,“%f”表示浮点数。

第二个参数是存储输入值的变量名或指针,它们必须与格式化字符串中要读取的类型相匹配。

当用户输入两个整数后,程序将它们相加并输出结果。

输出语句中使用了占位符“%d”,它们对应的是printf函数中的后三个参数,分别是a,b,c的值。

除了加法,减法、乘法、除法的处理也很相似。

下面展示一个利用switch语句实现四则运算的程序:#include<stdio.h>int main(){int a,b,c;char op;printf("Please enter two numbers:");scanf("%d %d",&a,&b);printf("Please enter the operator:");scanf(" %c",&op);switch(op){case '+': c=a+b;break;case '-': c=a-b;break;case '*': c=a*b;break;case '/': c=a/b;break;default: printf("Invalid operator!");return1;break;}printf("%d %c %d = %d\n",a,op,b,c);return 0;}这个程序通过switch语句根据输入的运算符选择相应的操作。

C语言编写四则运算

C语言编写四则运算

C语言编写四则运算#include<stdio.h>//计算器#include<malloc.h>#define STACK_SIZE100 //max size of the stack#define STACK_INCREMENT10 //realloc sizetypedef struct FStack//stack of int{float* base;float* top;int stacksize;}FStack;void Init(FStack* s){s->base = (float*)malloc(STACK_SIZE * sizeof(FStack));if (!s->base){printf("overflow!\n");return;}s->top = s->base;s->stacksize = STACK_SIZE;}bool isEmpty(FStack* s){if (s->top == s->base){return true;}else{return false;}}void Push(FStack* s, float e){if (s->top - s->base >= s->stacksize){printf("stack is full!\nrealloc %d\n", STACK_INCREMENT);s->base = (float*)realloc(s->base, (s->stacksize +sizeof(FStack)));if (!s->base){printf("overflow!\n");return;}s->top = s->base + s->stacksize;s->stacksize += STACK_INCREMENT;}*(s->top) = e;(s->top)++;}float GetTop(FStack* s){if (s->top == s->base){printf("stack is empty!\n");return 0;}float e = *(s->top - 1);return e;}void Pop(FStack* s){if (s->top == s->base){printf("stack is empty!\n");return;}s->top--;}typedef struct CStack//stack of char{char* base;char* top;int stacksize;}CStack;void Init(CStack* s){s->base = (char*)malloc(STACK_SIZE * sizeof(CStack));if (!s->base){printf("overflow!\n");return;}s->top = s->base;s->stacksize = STACK_SIZE;}bool isEmpty(CStack* s){if (s->top == s->base){return true;}else{return false;}}void Push(CStack* s, int e){if (s->top - s->base >= s->stacksize){printf("stack is full!\nrealloc %d\n", STACK_INCREMENT);s->base = (char*)realloc(s->base, (s->stacksize +sizeof(CStack)));if (!s->base){printf("overflow!\n");return;}s->top = s->base + s->stacksize;s->stacksize += STACK_INCREMENT;}*(s->top) = e;(s->top)++;}char GetTop(CStack* s){if (s->top == s->base){printf("stack is empty!\n");return 0;}char e = *(s->top - 1);return e;}void Pop(CStack* s){if (s->top == s->base){printf("stack is empty!\n");return;}s->top--;}bool isOper(char ch){if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {return true;}else{return false;}}int Priority(char ch){int p;switch(ch){case'(':p = 0;break;case'+':case'-':p = 1;break;case'*':case'/':case'%':p = 2;break;}return p;}float Calculate(float f1, float f2, char oper) {float f3;switch(oper){case'+':f3 = f1 + f2;break;case'-':f3 = f1 - f2;break;case'*':f3 = f1 * f2;break;case'%':f3 = (float)((int)f1 % (int)f2);break;case'/':if (f2 == 0){printf("\nDevided by zero!");exit(1);}else{f3 = f1 / f2;}break;}return f3;}float StrtoFloat(char* str, int* pos){float fRes;int i = *pos;int k;char n[50];for(k= 0; str[i] >= '0'&& str[i] <= '9'|| str[i] == '.'; i++, k++) {n[k] = str[i];}n[k] = '\0';*pos = i;fRes = atof(n);return fRes;}bool Check(char* str){int i = 0;while (str[i] != '\0'){if (str[i] != '+' && str[i] != '-' && str[i] != '*' && str[i] != '/' && str[i] != '%' && str[i] != '.' && str[i] != '(' && str[i] != ')' && (str[i] < '0' || str[i] > '9')){return false;}i++;}return true;}void main(){char exp[100];int i;float f, f1, f2;char oper;FStack fstack;CStack cstack;Init(&fstack);Init(&cstack);printf("The expression is:");gets(exp);if (!Check(exp)){printf("input error! exit now!\n");exit(1);}for (i = 0; exp[i] != '\0' && exp[i] != -52; i++){if (!isOper(exp[i])){f = StrtoFloat(exp, &i);Push(&fstack, f);}if (isOper(exp[i])){if (!isEmpty(&cstack)){while (!isEmpty(&cstack) && Priority(exp[i]) <= Priority(GetTop(&cstack))){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Push(&cstack, exp[i]);}else{Push(&cstack, exp[i]);}}else if (exp[i] == '('){Push(&cstack, exp[i]);}else if (exp[i] == ')'){while (GetTop(&cstack) != '(' && !isEmpty(&cstack)){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Pop(&cstack);}}while (!isEmpty(&cstack)){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}printf("\nThe result is:%f\n", GetTop(&fstack));Pop(&fstack);}。

四则运算计算器源代码(C++)

四则运算计算器源代码(C++)

四则运算计算器源代码(C++)// calculatorsDlg.h : header file//#if !defined(AFX_CALCULATORSDLG_H__3146ED92_B203_4223_A987_FF2D22CCBD42__INCLUDED_) #define AFX_CALCULATORSDLG_H__3146ED92_B203_4223_A987_FF2D22CCBD42__INCLUDED_#if _MSC_VER > 1000#pragma once#endif// _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////// CCalculatorsDlg dialogclass CCalculatorsDlg : public CDialog{// Constructionpublic:CCalculatorsDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CCalculatorsDlg)enum { IDD = IDD_CALCULATORS_DIALOG };// CString m_shuru;//记?录?输?入?的?字?符?//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CCalculatorsDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected:HICON m_hIcon;;// Generated message map functions//{{AFX_MSG(CCalculatorsDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnButton0();afx_msg void OnButton1();afx_msg void OnButton2();afx_msg void OnButton3();afx_msg void OnButton4();afx_msg void OnButton5();afx_msg void OnButton6();afx_msg void OnButton7();afx_msg void OnButton8();afx_msg void OnButton9();afx_msg void OnBUTTONAdd();afx_msg void OnBUTTONSub();afx_msg void OnBUTTONMul();afx_msg void OnBUTTONDiv();afx_msg void OnBUTTONResult();afx_msg void OnEDIT();afx_msg void OnBUTTONClear();afx_msg void OnBUTTONDelete();afx_msg void OnBUTTONpoint();void Result();//}}AFX_MSGDECLARE_MESSAGE_MAP()private:char buffer[100];int m_bit;//记?录?运?算?符?bool ele;//用?于 ?判D断?小?数簓点?的?输?入?int d;//记?录?小?数簓点?后ó的?位?数簓double m_begin;////记?录?输?入?数簓据Ydouble m_end;//记?录?每?个?符?号?的?运?算?结á果?double m_num;bool ele2;//用?于 ?判D断?程ì序ò是?否?有瓺错洙?误óbool ele3;//用?于 ?判D断?正y负o数簓public:CString m_shuru;afx_msg void OnBnClickedButtonexit();afx_msg void OnBnClickedButtonsign();};//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif// !defined(AFX_CALCULATORSDLG_H__3146ED92_B203_4223_A987_FF2D22CCBD42__INCLUDED_)// calculatorsDlg.cpp : implementation file//#include"stdafx.h"#include"calculators.h"#include"calculatorsDlg.h"#include"math.h"#include"string.h"#include"stdio.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CCalculatorsDlg dialogCCalculatorsDlg::CCalculatorsDlg(CWnd* pParent /*=NULL*/): CDialog(CCalculatorsDlg::IDD, pParent){//变?量?的?初?始?化ˉm_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);ele=false;//表括?示?开a始?是?没?有瓺小?数簓点?d=0;m_bit=0;m_num=0;m_end=0;m_shuru = _T("");ele2=true;ele3=true;}void CCalculatorsDlg::DoDataExchange(CDataExchange* pDX) {CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CCalculatorsDlg)// DDX_Text(pDX, IDC_Form1, m_shuru);//}}AFX_DATA_MAP// DDX_Text(pDX, IDC_Form1, m_shuru);// DDV_MaxChars(pDX, m_shuru, 100);// DDX_Text(pDX, IDC_Form1, m_shuru);// DDX_Text(pDX, IDC_Form1, m_shuru);// DDX_Text(pDX, IDC_Form1, m_shuru);DDX_Text(pDX, IDC_Form1, m_shuru);}BEGIN_MESSAGE_MAP(CCalculatorsDlg, CDialog)//{{AFX_MSG_MAP(CCalculatorsDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON0, OnButton0)ON_BN_CLICKED(IDC_BUTTON1, OnButton1)ON_BN_CLICKED(IDC_BUTTON2, OnButton2)ON_BN_CLICKED(IDC_BUTTON3, OnButton3)ON_BN_CLICKED(IDC_BUTTON4, OnButton4)ON_BN_CLICKED(IDC_BUTTON5, OnButton5)ON_BN_CLICKED(IDC_BUTTON6, OnButton6)ON_BN_CLICKED(IDC_BUTTON7, OnButton7)ON_BN_CLICKED(IDC_BUTTON8, OnButton8)ON_BN_CLICKED(IDC_BUTTON9, OnButton9)ON_BN_CLICKED(IDC_BUTTONAdd, OnBUTTONAdd)ON_BN_CLICKED(IDC_BUTTONSub, OnBUTTONSub)ON_BN_CLICKED(IDC_BUTTONMul, OnBUTTONMul)ON_BN_CLICKED(IDC_BUTTONDiv, OnBUTTONDiv)ON_BN_CLICKED(IDC_BUTTONResult, OnBUTTONResult)ON_EN_CHANGE(IDC_Form1, OnEDIT)ON_BN_CLICKED(IDC_BUTTONClear, OnBUTTONClear)ON_BN_CLICKED(IDC_BUTTONDelete, OnBUTTONDelete)ON_BN_CLICKED(IDC_BUTTONpoint, OnBUTTONpoint)//}}AFX_MSG_MAPON_BN_CLICKED(IDC_BUTTONExit, &CCalculatorsDlg::OnBnClickedButtonexit) ON_BN_CLICKED(IDC_BUTTONSign, &CCalculatorsDlg::OnBnClickedButtonsign) END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CCalculatorsDlg message handlersBOOL CCalculatorsDlg::OnInitDialog(){CDialog::OnInitDialog();// Add "About..." menu item to system menu.;// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control}void CCalculatorsDlg::OnSysCommand(UINT nID, LPARAM lParam){if ((nID & 0xFFF0) == IDM_ABOUTBOX)//关?联 对?话°框ò{CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);}}// If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework.void CCalculatorsDlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}}// The system calls this to obtain the cursor to display while the user drags// the minimized window.HCURSOR CCalculatorsDlg::OnQueryDragIcon(){return (HCURSOR) m_hIcon;}void CCalculatorsDlg::Result()//用?来ぁ?处鋦理え?按恪?下?符?号?键ü时骸?的?处鋦理え?函ˉ数簓{UpdateData(FALSE); //数簓据Y刷¢新?switch(m_bit){default: //当獭?只?有瓺一?个?输?入?值μ时骸?,?按恪?下?符?号?键ü,?不?做?计?算?处鋦理え?,?等台?待鋣第台?二t个?输?入?值μ键ü入?m_end=m_num;break;case 1: //加ó法ぁ?计?算?m_end=m_end+m_num;break;case 2: //减?法ぁ?计?算?m_end=m_end-m_num;break;case 3: //乘?法ぁ?计?算?m_end=m_end*m_num;break;case 4: //除y法ぁ?计?算?if(m_num==0) //当獭?除y数簓为a0时骸?,?显?示?“°error!”±,?并¢结á束?计?算?{m_shuru="error!";ele2=false;UpdateData(FALSE); //数簓据Y刷¢新?return;}else{m_end=m_end/m_num;}break;}m_num=0;d=0;ele=false;ele3=true;}void CCalculatorsDlg::OnButton0() //按恪?下?0键ü时骸?的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"0";if(ele==false){if(ele3==true){m_num=m_num*10;}}else{d++;}UpdateData(FALSE); //数簓据Y刷¢新?}void CCalculatorsDlg::OnButton1() //按恪?下?1键ü时骸?的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"1";if(ele==false){if(ele3==true){m_num=m_num*10+1;}elsem_num=m_num*10-1;}else{m_begin=1;d++;for(int i=0;i<d;i++){m_begin=m_begin/10;}if(ele3==true){m_num=m_begin+m_num;}elsem_num=m_num-m_begin;}UpdateData(FALSE); //数簓据Y刷¢新?}void CCalculatorsDlg::OnButton2() //按恪?下?2键ü时骸?的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"2";if(ele==false){if(ele3==true){m_num=m_num*10+2;}elsem_num=m_num*10-2;}else{m_begin=2;d++;for(int i=0;i<d;i++){m_begin=m_begin/10;}if(ele3==true){m_num=m_begin+m_num;}elsem_num=m_num-m_begin;}UpdateData(FALSE); //数簓据Y刷¢新?}void CCalculatorsDlg::OnButton3() //按恪?下?3键ü时骸?的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"3";if(ele==false){if(ele3==true){m_num=m_num*10+3;}elsem_num=m_num*10-3;}else{m_begin=3;d++;for(int i=0;i<d;i++){m_begin=m_begin/10;}if(ele3==true){m_num=m_begin+m_num;}elsem_num=m_num-m_begin;}UpdateData(FALSE); //数簓据Y刷¢新?}void CCalculatorsDlg::OnButton4() //按恪?下?4键ü时骸?的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"4";if(ele==false){if(ele3==true){m_num=m_num*10+4;}elsem_num=m_num*10-4;}else{m_begin=4;d++;for(int i=0;i<d;i++){m_begin=m_begin/10;}if(ele3==true){m_num=m_begin+m_num;}elsem_num=m_num-m_begin;}UpdateData(FALSE); //数簓据Y刷¢新?}void CCalculatorsDlg::OnButton5() //按恪?下?5键ü时骸?的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"5";if(ele==false){if(ele3==true){m_num=m_num*10+5;}elsem_num=m_num*10-5;}else{m_begin=5;d++;for(int i=0;i<d;i++){m_begin=m_begin/10;}if(ele3==true){m_num=m_begin+m_num;}elsem_num=m_num-m_begin;}UpdateData(FALSE); //数簓据Y刷¢新?}void CCalculatorsDlg::OnButton6() //按恪?下?6键ü时骸?的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"6";if(ele==false){if(ele3==true){m_num=m_num*10+6;}elsem_num=m_num*10-6;}else{m_begin=6;d++;for(int i=0;i<d;i++){m_begin=m_begin/10;}if(ele3==true){m_num=m_begin+m_num;}elsem_num=m_num-m_begin;}UpdateData(FALSE); //数簓据Y刷¢新?}void CCalculatorsDlg::OnButton7() //按恪?下?7键ü时骸?的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"7";if(ele==false){if(ele3==true){m_num=m_num*10+7;}elsem_num=m_num*10-7;}else{m_begin=7;d++;for(int i=0;i<d;i++){m_begin=m_begin/10;}if(ele3==true){m_num=m_begin+m_num;}elsem_num=m_num-m_begin;}UpdateData(FALSE); //数簓据Y刷¢新?}void CCalculatorsDlg::OnButton8() //按恪?下?8键ü时骸?的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"8";if(ele==false){if(ele3==true){m_num=m_num*10+8;}elsem_num=m_num*10-8;}else{m_begin=8;d++;for(int i=0;i<d;i++){m_begin=m_begin/10;}if(ele3==true){m_num=m_begin+m_num;}elsem_num=m_num-m_begin;}UpdateData(FALSE); //数簓据Y刷¢新?}void CCalculatorsDlg::OnButton9() //按恪?下?9键ü时骸?的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"9";if(ele==false){if(ele3==true){m_num=m_num*10+9;}elsem_num=m_num*10-9;}else{m_begin=9;d++;for(int i=0;i<d;i++){m_begin=m_begin/10;}if(ele3==true){m_num=m_begin+m_num;}elsem_num=m_num-m_begin;}UpdateData(FALSE); //数簓据Y刷¢新?}void CCalculatorsDlg::OnBUTTONAdd() //按恪?下?“°+”±号?键ü的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"+";Result();m_bit=1;}void CCalculatorsDlg::OnBUTTONSub() //按恪?下?“°-”±号?键ü的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"-";Result();m_bit=2;}void CCalculatorsDlg::OnBUTTONMul() //按恪?下?“°*”±号?键ü的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"*";Result();m_bit=3;}void CCalculatorsDlg::OnBUTTONDiv() //按恪?下?“°/”±号?键ü的?处鋦理え?函ˉ数簓{m_shuru=m_shuru+"/";Result();m_bit=4;}void CCalculatorsDlg::OnBUTTONResult() //按恪?下?“°=”±号?键ü的?处鋦理え?函ˉ数簓{Result();if(ele2==true){sprintf_s(buffer,"%10.12g",m_end);m_shuru=buffer;UpdateData(FALSE); //数簓据Y刷¢新?,显?示?目?前°计?算?结á果?m_bit=0;}}void CCalculatorsDlg::OnEDIT(){// TODO: If this is a RICHEDIT control, the control will not// send this notification unless you override the CDialog::OnInitDialog()// function and call CRichEditCtrl().SetEventMask()// with the ENM_CHANGE flag ORed into the mask.// TODO: Add your control notification handler code here}void CCalculatorsDlg::OnBUTTONClear(){ele=false;//消?除y小?数簓点?按恪?下?标括?记?m_num=0;m_shuru="";//输?入?清?空?m_begin=0;//清?0m_end=0;//清?0m_bit=0;//当獭?前°操õ作痢?符?为a无T效§操õ作痢?d=0;UpdateData(FALSE); //数簓据Y刷¢新?ele2=true;}void CCalculatorsDlg::OnBUTTONDelete(){if (ele==false ) //判D断?当獭?前°是?否?有瓺小?数簓点?,?如?果?没?有瓺m_num=m_num/10-fmod(m_num/10,1); //去ǎ?掉?小?数簓位?else{if (ele==true && d>0) //如?果?是?小?数簓{for (int j=0;j<d-1;j++) //移?动ˉ小?数簓点?(辍?换?成é整?数簓来ぁ?处鋦理え?)?{m_num=m_num*10; //移?动ˉ至á只?有瓺一?个?小?数簓位?}m_num=m_num-fmod(m_num,1); //去ǎ?掉?小?数簓位?for (int k=0;k<d-1;k++) //小?数簓点?移?至á原-来ぁ?位?置?(辍?再õ转羇化ˉ为a小?数簓)? {m_num=m_num/10;}}d--;}int n=m_shuru.GetLength();m_shuru.Delete(n-1,1);if (d==0)ele=false;//如?果?全?部?清?空?了?ele2=true;UpdateData(FALSE); //数簓据Y刷¢新?}void CCalculatorsDlg::OnBUTTONpoint(){if(ele==false){m_shuru=m_shuru+".";UpdateData(FALSE);//数簓据Y刷¢新?}ele=true;}void CCalculatorsDlg::OnBnClickedButtonexit() {exit(0);//退?出?计?算?器ô程ì序ò}void CCalculatorsDlg::OnBnClickedButtonsign() {if(ele3==true){m_shuru=m_shuru+"-";ele3=false;}else{m_shuru=m_shuru+"+";ele3=true;}}。

课程设计四则运算源程序

课程设计四则运算源程序
return destination;
}
char *midstr(char *source,char *destination,int start,int length)
{
source+=start-1;
*(destination+--length+1)=0;
while(length>=0)
{ *(destination+length)=*(source+length--); }
return destination;
}
char *right(char *source,char *destination,int length)
{
while(*source!=0) { source++; }
cin>>oristr;
assignstr(&oristr[0],&strn[0]);
cout<<"\nStaring bracket removal loop";
while(charinstr(&strn[0],'('))
{
for(z=0;z<=len(&strn[0]);z++)
char opstr[6]="^*/+-";
int leftnr;
int rightnr;
int oppos;
int z;
int result;
for(int pos_in_opstr=0;pos_in_opstr<=4;pos_in_opstr++)

C语言编写四则运算

C语言编写四则运算

C语言编写四则运算#include<> ; i++, k++){n[k] = str[i];}n[k] = '\0';*pos = i;fRes = atof(n);return fRes;}bool Check(char* str){int i = 0;while (str[i] != '\0'){if(str[i] != '+'&& str[i] != '-'&& str[i] != '*'&& str[i] != '/' && str[i] != '%' && str[i] != '.' && str[i] != '(' && str[i] != ')' && (str[i] < '0' || str[i] > '9')){return false;}i++;}return true;}void main(){char exp[100];int i;float f, f1, f2;char oper;FStack fstack;CStack cstack;Init(&fstack);Init(&cstack);printf("The expression is:");gets(exp);if (!Check(exp)){printf("input error! exit now!\n");exit(1);}for (i = 0; exp[i] != '\0' && exp[i] != -52; i++){if (!isOper(exp[i])){f = StrtoFloat(exp, &i);Push(&fstack, f);}if (isOper(exp[i])){if (!isEmpty(&cstack)){while (!isEmpty(&cstack) && Priority(exp[i]) <= Priority(GetTop(&cstack))){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Push(&cstack, exp[i]);}else{Push(&cstack, exp[i]);}}else if (exp[i] == '('){Push(&cstack, exp[i]);}else if (exp[i] == ')'){while (GetTop(&cstack) != '(' && !isEmpty(&cstack)) {oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}Pop(&cstack);}}while (!isEmpty(&cstack)){oper = GetTop(&cstack);Pop(&cstack);f2 = GetTop(&fstack);Pop(&fstack);f1 = GetTop(&fstack);Pop(&fstack);f = Calculate(f1, f2, oper);Push(&fstack, f);}printf("\nThe result is:%f\n", GetTop(&fstack));Pop(&fstack);}。

c语言编写四则运算运算器

c语言编写四则运算运算器

#include<stdio.h>#include<ctype.h>#include<stdlib.h>char token[61]; /*存放表达式字符串的数组*/int n=0;void error(void) /*报告错误函数*/{printf("ERROR!\n");exit(1);}void match(char expected) /*检查字符匹配的函数*/ {if(token[n]==expected)token[++n]=getchar();else error();}double term(void); /*计算乘除的函数*/double factor(void); /*处理括号和数字的函数*/ double exp(void) /*计算加减的函数*/{double temp=term();while((token[n]=='+')||(token[n]=='-'))switch(token[n]){case'+':match('+');temp+=term();break;case'-':match('-');temp-=term();break;}return temp;}double term(void){double div;double temp=factor();while((token[n]=='*')||(token[n]=='/'))switch(token[n]){case'*':match('*');temp*=factor();break;case'/':match('/');div=factor();if(div==0) /*处理除数为零的情况*/{printf("The divisor is zero!\n");exit(1);}temp/=div;break;}return temp;}double factor(void){double temp;char number[61];int i=0;if(token[n]=='('){match('(');temp=exp();match(')');}else if(isdigit(token[n])||token[n]=='.'){while(isdigit(token[n])||token[n]=='.') /*将字符串转换为浮点数*/ {number[i++]=token[n++];token[n]=getchar();}number[i]='\0';temp=atof(number);}else error();return temp;}main(){double result;FILE *data=fopen("61590_4.dat","at");if(data==NULL)data=fopen("61590_4.dat","wt");if(data==NULL)return 0;token[n]=getchar();result=exp();if(token[n]=='\n'){token[n]='\0';printf("%s=%g\n",token,result); fprintf(data,"%s=%g\n",token,result); }else error();fclose(data);return 0;getch();}。

C语言编写的四则运算器

C语言编写的四则运算器
}
/* Source1、Source2加起来到Destination中*/
char *AddStrings_f(char *chpDestination, char *chpSource1, char *chpSource2)
{
char *chpTempdest=chpDestination;
while(*chpSource1!=0) /*先把chpSource1放入chpDestination */
/*关系运算*/
int Nexus(char strIn[]);
/* =============================================== */
/*全局变量声明*/
int Debug=1; /*调试信息显示开关0不显示,1显示*/
char *TF_Info[3]={"FALSE","TURE","Error"}; /*关系运算信息*/
}
sBuf[sPos]=sChar;
strcpy(chpString,sBuf);
}
else
return 0;
return 1;
}
/*替换字符串中的某个字符'#' to '-' */
void StrReplace(char *chpString,char strOld ,char strNew)
{
char sBuf[Max];
/*函数声明*/
/*计算字符串(不带括号的),计算的核心部分*/
char *Calculate_f(char *chpString);
/*主操作过程,输入式子串,返回double型结果*/

四则运算编程.doc

四则运算编程.doc

# include<stdio.h># include<stdlib.h># include<math.h>#define N 100typedef char typedata1;typedef int typedata2;//声明函数int Operator(char c);void convert(char s[N]);typedef struct charnode{ //字符、数字两个栈的定义typedata1 data;struct charnode *next;}charstack;typedef struct intnode{typedata2 data;struct intnode *next;}intstack;char s[100]; //定义数组charstack *creat(){ //字符指针函数charstack *head;head=(charstack *)malloc(sizeof(charstack));head->data='#';head->next=0;return head;}intstack *intcreat(){ //数字指针函数intstack *head;head=(intstack *)malloc(sizeof(intstack));head->data=0;head->next=0;return head;}charstack *push(charstack *top,char x){ //字符压栈指针函数charstack *p;p=(charstack *)malloc(sizeof(charstack));p->data=x;p->next=top;top=p;return top;}intstack *intpush(intstack *top,int x){ //数字压栈指针函数intstack *p;p=(intstack *)malloc(sizeof(intstack));p->data=x;p->next=top;top=p;return top;}typedata1 top(charstack *top){ //栈顶元素return top->data;}typedata2 inttop(intstack *top){return top->data;}char compare(char x1,char x2){ //比较函数><+char m;int m1,m2;if(x1=='+'||x1=='-') m1=0;else if(x1=='*'||x1=='/') m1=1;else if(x1=='(') m1=2;if(x2=='+'||x2=='-') m2=0;else if(x2=='*'||x2=='/') m2=1;else if(x2=='(') m2=2;if(m1>m2) m='>';else if(m1<m2) m='<';else m='=';return m;}charstack *popl(charstack *top){ //字符出栈指针函数charstack *p;if(top==0)printf("下溢!");else{p=top;top=top->next;free(p);return top;}}intstack *intpopl(intstack *top){ //数字出栈指针函数intstack *p;printf("下溢!");else{p=top;top=top->next;free(p);return top;}}void display(char m[],int n){ //显示后缀字符int i;for(i=0;i<=n;i++)printf("%c",m[i]);}int change(char a[],int n){ //将中缀转换成后缀表达式int i;int j=0;char x1,x2,bj;charstack *q;q=creat();for(i=0;i<n;i++){ //a[i]代表中缀s[j]代表后缀if(a[i]<='9'&&a[i]>='0'){s[j]=a[i];j=j+1;if(a[i+1]>'9'||a[i+1]<'0'){ //用空格区别两位数和一位数s[j]=' ';j=j+1;}}else{x1=a[i];x2=top(q);if(x2=='#')q=push(q,x1);else{if(x1==')'){while(top(q)!='('){ //找到)匹配,弹栈s[j]=top(q);j=j+1;q=popl(q);}q=popl(q);}bijiao:bj=compare(x1,x2); //x1为将要比较的数,x2为栈顶switch(bj){case '>':q=push(q,x1);break;case '<':if(x2!='('){s[j]=top(q);j=j+1;q=popl(q);x2=top(q);if(x2!='#') goto bijiao;else q=push(q,x1);break;}else q=push(q,x1);break;case '=':s[j]=top(q);j=j+1;q=popl(q);q=push(q,x1);break;}}}}}while(top(q)!='#'){//清空栈s[j]=top(q);j=j+1;q=popl(q);}return j;}//中缀转换前缀》=就进栈int f(char c) //判断运算符级别函数;{int f=-1;switch(c){case'+':case'-':f=1;break;case'*':case'/':f=2;break;default:f=0;break;}return f;}int Operator(char c) //判断字符是否为操作符{if(c=='+'||c=='-'||c=='*'||c=='/')return 1;elsereturn 0;}void convert(char s[N],char p[N]) //将中缀表达式转化为前缀表达式{char stack[N];int top=0,j=0, len=0;if(s[0]==')'){printf("对不起,您输入不合法,将退出当前的程序!");printf("\n\n");}else{while(s[len]!='\0'){len++;}for(int i=len-1;i>=0;){if(s[i]>=48 && s[i]<=57){while(s[i]>=48 && s[i]<=57){p[j]=s[i];j++;i--; //}p[j]=' ';j++;}if(s[i]==')') //假如是回括号,将它压栈。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
break;
else{
sum=Call(data.pop(),data.pop(),symbol.pop());
data.push(sum);
}
}
}
double jisuan::Calculate(char* buffer,double& sum) //字符串读入和各个函数调配
a++;
if(a>1)
return false;
return true;
}
int jisuan::pdyxj(char ch) //符号的优先极别
{
switch(ch)
for(i=0;i<strlen(ch);i++)
{
if(ch[i]!='.')
sumn=sumn*10+(ch[i]-'0');
else break;
}
if(i<strlen(ch))
}
double jisuan::Call(double sum,double data,char ch)
{
double ans=0.0;
switch(ch)
{
case '+':
ans=sum+data;
break;
private:
Type base[MAX];
int Size;
public:
STACK()
{
Size=0;
};
void push(Type a)
{
{
STACK<char> Temp;
int i;
for(i=pos;i<strlen(buffer);i++)
{
if(jckh(buffer[i])==1)
Temp.push('0');
if(jckh(buffer[i])==-1)
base[Size]=a;
Size++;
}
Type pop()
{
return base[--Size];
}
int size()
break;
case '#':
return 0;
default:ans=0.0;
break;
}
return ans;
}
int jisuan::ppkh(char* buffer,int pos) //利用栈找到匹配的括号
for(j=i+1;j<strlen(ch);j++)
sum=sum*10+(ch[j]-'0');
sum /= pow(10.0,(double)(strlen(ch)-1-i));
return (sum+sumn);
return 0;
default:
return -1;
}
}
double jisuan::ToData(char* ch) //将数字转化为数值
{
int i,j,sumn=0;
double sum=0.0;
if(!jcxsd(ch)) return 0.0;
{
cout<<" ===============================================\n"
<<" * 欢迎使用四则运算系统 *\n"
return (ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='#')?true:false;
}
int jisuan::jckh(char ch)
{
if(ch=='(')
return 1;
if(ch==')')
ans=sum/data;
else
{
cout<<"程序出现除0错误,终止!\n";
system("pause");
exit(1);
}
<<" * 学号:1109121101 *\n"
<<" * *\n"
};
bool jisuan::shuhanshu(char ch)
{
return ((ch>='0'&&ch<='9')||ch=='.')?true:false;
}
bool jisuan::fuhanshu(char ch)
{
{
double sum;
while(symbol.size()!=0)
{
char tem=symbol.pop();
int temp=pdyxj(tem);
symbol.push(tem);
if(temp<mark)
{
cout<<"这个表达式的结果为:"<<result<<endl;
}
private:
double result;
};
/*计算用的存储结构*/
template <class Type>
class STACK{
<<"请输入表达式,回车结束:";
}
};
/*输入模块*/
class Input
{
public:
Input()
{
for( int i = 0;i < MAX;i++ )
int jckh(char);
bool jcxsd(char *);
int pdyxj(char);
double ToData(char*);
{return Size;}
};
/*计算的模块*/
class jisuan
{
public:
bool shuhanshu(char);
bool fuhanshu(char);
{
public:
Output()
{
result = 0;
}
void getRes( double res )
{
result = res;
}
void printRes()
double Call(double,double,char);
int ppkh(char* buffer,int pos);
void Opr( STACK<char>&, STACK<double>&, int& );
double Calculate(char*, double& );
Str_input[i] = '\0';
}
char Str_input[MAX];
void inStr()
{
cin>>Str_input;
}
};
/*输出模块*/
class Output
{
case '+':
return 0;
case '-':
return 0;
case '*':
return 1;
case '/':
return 1;
case '#':
<<" ===============================================\n\n\n"
<<"=================================进入主程序====================================\n\n"
case '-':
ans=sum-data;
break;
case '*':
ans=sum*data;
break;
case '/':
if( data != 0.0 )
#include<iostream >
#include<cmath>
using namespace std;
const int MAX=1000;
/*欢迎模块*/
class Entry
{
public:
static void welcome()
{
Temp.pop();
if(Temp.size()==0) return i;
}
}
return -1;
}
void jisuan::Opr(STACK<char>& symbol,STACK<double>& data,int& mark)
return -1;
return 0;
}
相关文档
最新文档