用C语言编写可以进行加减乘除整数运算混合运算的计算器,要求写思路,越详细越好,初学者,不要很复杂的。

实现整数的算术运算(加、减、乘、除)。
程序只接受界面上显示的字符输入。用户每按一个数值或一个运算符后按回车键,最后用户按 = 键后输出运算结果。运算需先乘除后加减。输入时要检查输入数据的合法性,滤除一切不合法的输入。
如:
12(输入)
+(输入)
34(输入)
-5(输入)
=(输入)
41(输出)

#include <cstdlib>

#include <iostream>

using namespace std;

int main()

{

int a,b;//a是输出结果,b是临时输入数据

char x;//x是标点符号输入

cin>>a;//先输入第一个数

while(1)//由于不知道运算式一共多长,所以用一个死循环不断读取

{

cin>>x;//输入运算符

if(x=='=')//'='特殊处理,输出结果

{

cout<<a<<endl;

break;//退出循环,跳到return 0;

}

else//如果是运算符就输入下一个数

{

cin>>b;

switch(x)//判断符号类型,并进行相应计算

{

case '+':a+=b;break;//每个case后面必须加break;否则将后面所有运算式全走一遍

case '-':a-=b;break;

case '*':a*=b;break;

case '/':a/=b;break;

}

}

}

    return 0;

汗,又改变条件了,这次你的要求和原来要求可是截然不同的程序啊,涉及到很多算法的,二叉树,堆栈等,我如果重写了初学者不一定能看懂了。我下面就给你贴一下差不多的代码吧。只是这个不需要输入等号,回车自动计算。如果需要去掉那些繁琐的代码估计没人为了这几个虚拟分给你去掉的。

  #include<iostream>   

  #include<string>   

  #include<stack>   

  #include "Tree.h"

  #include<windows.h>

  bool   isok(string   exp)                                               //此函数验证式子是否正确,即是否符合运算规则。   

{   

char   check;   

int   error=0;   

int   lb=0;   

int   rb=0;   

if(exp.size()==1 && exp[0]!='-')return   false; 

else   if((IsOperator(exp[0])&& exp[0]!='-' ||IsOperator(exp[exp.size()-1]))&&exp[0]!='('&&exp[exp.size()-1]!=')')         //此处若不加,在遇到某些式子时,会出现非法操作。   

return   false;   

for(int   m=0;m<exp.size();m++)   

{   

check=exp[m]; 

if(m==0 && check=='-' && (isdigit(exp[1])!=0 || exp[1]=='(' ) )check=exp[++m];

if(IsOperand(check));                         //如果是数字,跳过,不管。   

else   if(IsOperator(check))   

{   

if(check==')')   

{   

rb++;   

if(IsOperator(exp[m+1])&&(exp[m+1]=='+'||exp[m+1]=='-'||exp[m+1]=='*'||exp[m+1]=='/'||exp[m+1]=='^'||exp[m+1]==')'))   

{   

m++;   

if(exp[m]==')')   

rb++;   

}   

else   if(IsOperator(exp[m+1]))   

error++;   

}

else   if(check=='(')   

{   

lb++;   

if(exp[m+1]=='(')   

{   

m++;   

lb++;   

}   

else   if(IsOperator(exp[m+1])&&exp[m+1]!='-')   

error++;   

}   

else   

{   

if(IsOperator(exp[m+1])&&exp[m+1]=='(')   

{   

m++;   

lb++;   

}   

else   if(IsOperator(exp[m+1]))   

error++;   

}   

}   

else   

error++;   

}   

if(error==0&&lb==rb)   

return(true);   

else   

return(false);   

}   

  int   main()

  {

  HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 

  SetConsoleTitle("四则运算器二叉树版");

  SetConsoleTextAttribute(hOut,BACKGROUND_GREEN+FOREGROUND_BLUE);

  binary_tree   etree;   

  stack<binary_tree>NodeStack;   

  stack<char>OpStack;   

  string   infix;   

  char   choice='y'; 

  system("cls");

  cout<<"*******************************************************************"<<endl;

  cout<<"*                                                                 *"<<endl;

  cout<<"*   十进制四则运算计算器       ※※※※※※※※※※※※    *"<<endl;

  cout<<"*                       ※                    ※    *"<<endl;

  cout<<"*        (二叉树版)               ※※※※※※※※※※※※    *"<<endl;

    cout<<"*                                                                 *"<<endl;

  cout<<"*******************************************************************"<<endl;

  char   c;   

  while(choice=='y'||choice=='Y')   

{   

cout<<"\n请输入表达式,不要带空格:\n";   

        cin>>infix;   

        cout<<"--------------------------------------------------------------------------------"<<'\n';   

        cout<<"表达式为:         "<<infix<<'\n';   

if(isok(infix))   

{   

for(int   i=0;i<infix.size();i++)   

{   

c=infix[i]; 

if(i==0 && c=='-') //若开始为负,则把零压入运算数栈,把'-'压入运算符栈

{

binary_tree   temp;   

temp.root=build_node("0");  

NodeStack.push(temp); 

OpStack.push('-');

}

else

if(IsOperand(c))   

{   

string   tempstring;   

tempstring=tempstring+c; 

while(i+1<infix.size()&&IsOperand(infix[i+1]))   

{   

tempstring+=infix[++i];   

}   

binary_tree   temp;   

temp.root=build_node(tempstring);   

NodeStack.push(temp);   

}   

else   if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')   

{   

if(OpStack.empty())   

OpStack.push(c);   

else   if(OpStack.top()=='(')   

OpStack.push(c);   

else   if(TakesPrecedence(c,OpStack.top()))   

OpStack.push(c);   

else   

{   

while(!OpStack.empty()&&(TakesPrecedence(OpStack.top(),c)||addition(OpStack.top(),c)))   

{   

binary_tree   temp_tree;   

string   thisstring="";   

thisstring=thisstring+OpStack.top();   

OpStack.pop();   

etree.root=build_node(thisstring);   

copy(temp_tree.root,NodeStack.top().root);   

NodeStack.pop();   

etree.root->right_child=temp_tree.root;   

temp_tree.root=NULL;   

copy(temp_tree.root,NodeStack.top().root);   

etree.root->left_child=temp_tree.root;   

NodeStack.pop();   

temp_tree.root=NULL;   

copy(temp_tree.root,etree.root);   

NodeStack.push(temp_tree);   

etree.root=NULL;   

}   

OpStack.push(c);   

}   

    

}   

else   if(c=='(')    //若中间遇到括号,则判断下一位是否为'-'

{OpStack.push(c);

 if(infix[i+1]=='-')

{

binary_tree   temp;

temp.root=build_node("0"); 

NodeStack.push(temp);

OpStack.push('-');

++i;

}

}   

else   if(c==')')   

{   

while(OpStack.top()!='(')   

{   

binary_tree   temp_tree;   

string   thisstring="";   

thisstring=thisstring+OpStack.top();   

OpStack.pop();   

etree.root=build_node(thisstring);   

copy(temp_tree.root,NodeStack.top().root);   

NodeStack.pop();   

etree.root->right_child=temp_tree.root;   

temp_tree.root=NULL;   

copy(temp_tree.root,NodeStack.top().root);   

etree.root->left_child=temp_tree.root;   

NodeStack.pop();   

temp_tree.root=NULL;   

copy(temp_tree.root,etree.root);   

NodeStack.push(temp_tree);   

etree.root=NULL;   

}   

OpStack.pop();   

}   

}   

  ////////////////////////////////////////////////////////   

while(!OpStack.empty())   

{   

        binary_tree   temp_tree;   

        string   thisstring="";   

        thisstring=thisstring+OpStack.top();   

        OpStack.pop();   

        etree.root=build_node(thisstring);   

        copy(temp_tree.root,NodeStack.top().root);   

        NodeStack.pop();   

        etree.root->right_child=temp_tree.root;   

        temp_tree.root=NULL;   

        copy(temp_tree.root,NodeStack.top().root);   

        etree.root->left_child=temp_tree.root;   

        NodeStack.pop();   

        temp_tree.root=NULL;   

        copy(temp_tree.root,etree.root);   

        NodeStack.push(temp_tree);   

        if(!OpStack.empty())   

{   

etree.root=NULL;   

}   

}   

cout<<"打印结点如下:         ";   

etree.print();   

cout<<'\n';   

cout<<"结点个数为:"<<etree.counter()<<'\n';   

cout<<"以下是,中间的计算结果:"<<'\n';

etree.evaluate();

cout<<'\n';

cout<<"结果是:     ";

cout<<etree.root->data<<'\n';

    cout<<'\n'<<"--------------------------------------------------------------------------------"<<'\n';   

cout<<"\n\n是否要重新运行?输入<Y/N>: ";

cin>>choice;

  }   

  else

{   

cout<<"************************************************"<<'\n';   

cout<<"错误:输入的表达试有误!"<<'\n';   

cout<<"************************************************"<<'\n';   

cout<<"\n\n是否要重新运行?输入<Y/N>: ";   

cin>>choice;   

}   

  }   

  return   0;   

  } 

课程设计报告

设计题目:十进制四则运算计算器

实习目的

通过实习,了解并初步掌握设计、实现较大系统的完整过程,包括系统分析、编码设计、系统集成、以及调试分析,熟练掌握数据结构的选择、设计、实现以及操作方法,为进一步的应用开发打好基础。

二.问题描述

在以二叉树表示算术表达式的基础上,设计一个十进制的四则运算的计算器。[设计要求]实现整数浮点数的四则运算。

三.需求分析

   该程序实现的是实数型的四则运算,并在此运算上又加入了幂”^”运算,该程序用一二叉树表示整个输入的算术表达式:

(1)实现对结点的打印,便于结果分析;

(2)实现对结点的统计;

(3)实现中间结果的显示,可以看打印的结点,验证运算结果的正确与否。

四.概要设计

系统用到的抽象数据类型定义:

1.ADT node_type{

   数据对象V:一个集合,该集合中的所有元素具有相同的特性

   数据关系R:R={VR}

               VR={<x,y>|P(x,y)^(x,y属于V)}

   基本操作:

(1) node_type(string   k);

操作结果:对结点进行初始化

}ADT node_type

     2.ADT binary_tree{

           数据对象D:一个集合,该集合中的所有元素具有相同的特性

           数据关系R:若D为空,则为空树。若D中仅含有一个数据元素,则R为空集,否则R={H},H为如下二元关系:

(1) 在D中存在唯一的称为根的数据元素root,它在关系H中没有前驱

(2) 除root以外,D中每个结点在关系H下有且仅有一个前驱。

           基本操作:

(1) print(node_type   *r)CopyTimeTree(p,q);

操作结果:对结点进行打印

(2) evaluate(node_type   *prt);

操作结果:对一二叉树进行计算

(3) counter();

操作结果:计算一棵二叉树中的结点个数

}ADT binary_tree

 系统中子程序及功能要求:

1. ADT node_type  build_node(string   x):建立一个结点

2. addition(char   OperatorA,char   OperatorB):判断两操作符是否相等,若相等返回True

3. TakesPrecedence(char OperatorA,char OperatorB):判别符号的优先级。A>B,返回为TRUE

4. copy(ADT node_type *&r1, ADT node_type *r2):拷贝整个二叉树

5. isok(string   exp):验证表达式是否输入正确,若正确返回TRUE

五.测试分析(运行结果)

第一个表达式:10-(-3)*(((21+3/5)*8/3)*(-2))

第二个表达式:-(32.7-3210.3)/((8.0+0.9)*8.9)+4.4

依次把运算符和操作数放入堆栈中,过程中依次把中间运算结果打印出来

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-18
你这个就是用c语言玩玩,
#include <cstdlib>
#include <stdio.h>
void main()
{
double a,b;//a是输出结果,b是临时输入数据
char x;//x是标点符号输入
scanf(%lf,&a);//先输入第一个数
while(1)//由于不知道运算式一共多长,所以用一个死循环不断读取
{
scanf("%c",&c);//输入运算符
if(x=='=')//'='特殊处理,输出结果
{
printf(”%c“,&a);
break;//退出循环,跳到return 0;
}
else//如果是运算符就输入下一个数
{
scanf(%lf,&b);/判断符号类型,并进行相应计算
switch(x)
{
case '+':a+=b;break;//每个case后面必须加break;否则将后面所有运算式全走一遍
case '-':a-=b;break;
case '*':a*=b;break;
case '/':a/=b;break;
}
}
}
return 0;
} 把第一个改成c语言的,,会么?
第2个回答  2012-06-19
运行结果:
12
+
34
-5
=
41

思路:
保存最后一次的算符(lastOperator, +或-),保存上一次读入的数字(lastNumber)。
如果当前算符operator的优先级低(+或-), 更新数据(data = data +或- lastNumber)
如果当前算符operator的优先级高(*或/), 更新数字(lastNumber = lastNumber *或/ number).
当前表达式为:
data lastOperator(+或-) lastNumber operator(+,-,* 或/) number

C语言代码(不是C++代码,保存为.c编译):
#include <stdio.h>
#include <stdlib.h>
void clearLine()
{
int ch;
while(((ch = getchar())!='\n')&&(ch != EOF));
}
double getNumber()
{
double f;
int n;
while((n = scanf("%lf", &f))!=1){
if(n == EOF) exit(1);
printf("请输入一个数\n");
clearLine();
}
clearLine();
return f;
}
char getOperator()
{
int c;
while((c = getchar())!=EOF){
clearLine();
switch(c){
case '+':
case '-':
case '*':
case '/':
case '=':
return c;
default:
printf("请输入+-*/\n");
break;
}
}
exit(1);
}
double calc(double data, char operator, double number)
{
switch(operator){
case '+':
data += number;
break;
case '-':
data -= number;
break;
case '=':
data += 0;
break;
default:
abort();
break;
}
return data;
}
int main()
{
int lastOperator = '+';
int operator = '+';
double lastNumber = 0;
double number = 0;
double data = 0;
/* data lastOperator(+-) lastNumber operator number ... */
while(1){
number = getNumber();
/* update lastOperator, lastNumber */
switch(operator){
case '+':
case '-':
data = calc(data, lastOperator, lastNumber);
lastOperator = operator;
lastNumber = number;
break;
case '*':
lastNumber *= number;
/* keep lastOperator */
break;
case '/':
lastNumber /= number;
/* keep lastOperator */
break;
default:
abort();
break;
}
operator = getOperator();
if(operator == '='){
switch(lastOperator){
case '+':
data += lastNumber;
break;
case '-':
data -= lastNumber;
break;
default:
abort();
}
printf("%.15g\n", data);
printf("输入回车退出\n");
getchar();
break;
}
}
return 0;
}