百韵网 >>  正文

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

来源:www.baiyundou.net   日期:较早时间

#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<<"
请输入表达式,不要带空格:
";   

        cin>>infix;   

        cout<<"--------------------------------------------------------------------------------"<<'
';   

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

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<<'
';   

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

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

etree.evaluate();

cout<<'
';

cout<<"结果是:     ";

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

    cout<<'
'<<"--------------------------------------------------------------------------------"<<'
';   

cout<<"

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

cin>>choice;

  }   

  else

{   

cout<<"************************************************"<<'
';   

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

cout<<"************************************************"<<'
';   

cout<<"

是否要重新运行?输入<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

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



你这个就是用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语言的,,会么?

运行结果:
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;
}

怎样用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器?~

用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器的方法:
1、打开visual C++ 6.0-文件-新建-文件-C++ Source File;

2、输入预处理命令和主函数:
#include /*函数头:输入输出头文件*/
void main()/*空类型:主函数*/

3、定义变量:
int a,b,d; /*定义变量的数据类型为整型*/
char c;/*定义变量的数据类型为字符型*/

4、输入四则运算式:
printf("输入如“3*4”或“5+2”的四则运算式:");/*输出文字提示*/
scanf("%d%c%d",&a,&c,&b);/*输入四则运算式*/

5、判断运算符号:
switch(c) /*判断运算符号*/
{
case'+':d=a+b;break;/*进行加法运算*/
case'-':d=a-b;break;/*进行减法运算*/
case'*':d=a*b;break;/*进行乘法运算*/
case'/':d=a/b;break; /*进行除法运算*/
}

6、输出结果:
printf("%d%c%d=%d
",a,c,b,d);/*输出结果*/

完整的源代码:
#include /*函数头:输入输出头文件*/
void main()/*空类型:主函数*/
{
int a,b,d;/*定义变量的数据类型为整型*/
char c;/*定义变量的数据类型为字符型*/
printf("输入如“3*4”或“5+2”的四则运算式:");/*输出文字提示*/
scanf("%d%c%d",&a,&c,&b);/*输入四则运算式*/
switch(c)/*判断运算符号*/
{
case'+':d=a+b;break;/*进行加法运算*/
case'-':d=a-b;break;/*进行减法运算*/
case'*':d=a*b;break;/*进行乘法运算*/
case'/':d=a/b;break;/*进行除法运算*/
}
printf("%d%c%d=%d
",a,c,b,d);/*输出结果*/
}

用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器的方法:
1、打开visual C++ 6.0-文件-新建-文件-C++ Source File;

2、输入预处理命令和主函数:
#include /*函数头:输入输出头文件*/
void main()/*空类型:主函数*/


3、定义变量:
int a,b,d; /*定义变量的数据类型为整型*/
char c;/*定义变量的数据类型为字符型*/


4、输入四则运算式:
printf("输入如“3*4”或“5+2”的四则运算式:");/*输出文字提示*/
scanf("%d%c%d",&a,&c,&b);/*输入四则运算式*/


5、判断运算符号:
switch(c) /*判断运算符号*/
{
case'+':d=a+b;break;/*进行加法运算*/
case'-':d=a-b;break;/*进行减法运算*/
case'*':d=a*b;break;/*进行乘法运算*/
case'/':d=a/b;break; /*进行除法运算*/
}


6、输出结果:
printf("%d%c%d=%d
",a,c,b,d);/*输出结果*/


完整的源代码:
#include /*函数头:输入输出头文件*/
void main()/*空类型:主函数*/
{
int a,b,d;/*定义变量的数据类型为整型*/
char c;/*定义变量的数据类型为字符型*/
printf("输入如“3*4”或“5+2”的四则运算式:");/*输出文字提示*/
scanf("%d%c%d",&a,&c,&b);/*输入四则运算式*/
switch(c)/*判断运算符号*/
{
case'+':d=a+b;break;/*进行加法运算*/
case'-':d=a-b;break;/*进行减法运算*/
case'*':d=a*b;break;/*进行乘法运算*/
case'/':d=a/b;break;/*进行除法运算*/
}
printf("%d%c%d=%d
",a,c,b,d);/*输出结果*/
}


相关要点总结:

19150969571:用C语言编写可以进行加减乘除整数运算混合运算的计算器,要求写思路,越...
徒姣答:用C语言编写可以进行加减乘除整数运算混合运算的计算器,要求写思路,越详细越好,初学者,不要很复杂的。200 实现整数的算术运算(加、减、乘、除)。程序只接受界面上显示的字符输入。用户每按一个数值或一个运算符后按回车键,最后用户按=键后输出运算结果。运算需先乘除后加减。输入时要检查... 实现整数的算术运...

19150969571:用c语言设计一个简单的加减乘除计算器
徒姣答:1、打开visual C++ 6.0-文件-新建-文件-C++ Source File。2、输入预处理命令和主函数:#include /*函数头:输入输出头文件*/,void main()/*空类型:主函数*/。3、定义变量:int a,b,d; /*定义变量的数据类型为整型*/,char c;/*定义变量的数据类型为字符型*/。4、输入四则运算式:pri...

19150969571:c语言 生成两个随机数随机加减乘除 并且相减相除时前面的数要比后面...
徒姣答:两个随机数 x,y, 随机加减乘除 z ..程序如下。include <stdio.h> include int main(){ int x,y,z,t,i;srand(time(0));for (i=0;i<8;i++){ z = rand()%4;x = rand()%100+1;y=rand()%100+1;if (x<y){t=x;x=y;y=t;};switch(z){ case 0: printf("%d+...

19150969571:输入两个整数,进行加减乘除四则运算的c语言程序怎么写啊,拜托了~
徒姣答:运行截图 分析C语言中的加减乘除和数学中的加减乘除一样,不同在于符号的表示问题,乘号需要用“*”表示。除号需要用“/”表示。新手学习C语言,很容易把除号和取余好混淆,强调一下,取余号是“%”,百分号就是取余的意思。因此在输入两个整数以后,按照数学方法就可以直接输出结果,整数的输入用scanf...

19150969571:用C语言做一个计算器,能实现加减乘除混合运算?
徒姣答:是的,可以使用C语言编写一个计算器程序,能够实现加、减、乘、除等混合运算。下面是一个简单的示例程序:```c include <stdio.h> int main() { char operator;double num1, num2, result;printf("Enter an operator (+, -, *, /): ");scanf("%c", &operator);printf("Enter two ...

19150969571:怎么写C语言简单的加减乘除
徒姣答:C语言可以帮助我们快速的进行加减乘除运算,那么如何操作呢?下面小编给大家分享一下。1、首先打开Code Blocks软件,新建一个C语言文件,如下图所示 2、接下来在C语言文件中编写加减乘除运算代码,如下图所示 3、然后点击构建菜单下面的编译当前文件选项,如下图所示 4、最后运行程序就可以得到加减乘除运算...

19150969571:输入两个整数,进行加减乘除四则运算的c语言程序怎么写啊,拜托了~
徒姣答:void function(int a, int b){ printf("%d add %d = %d\n",a, b, a+b); //加法运算 printf("%d sub %d = %d\n",a, b, a-b);//加法运算 printf("%d mul %d = %d\n",a, b, a*b);//乘法运算 printf("%d div %d = %d\n",a, b, a/b);//除法运算 } int ...

19150969571:...刚学c语言。谁能帮我写一个能运行两个数加减乘除的程序?我们书上说...
徒姣答:int i,j,d;char c;scanf("%d%c%d",&i,&c,&j);switch(c){ case '+':d=i+j;break;case '-':d=i-j;break;case '*':d=i*j;break;case '/':if(j!=0)d=i/j;else { printf("Can not divided by 0!\n");return 1;} } printf("%d%c%d=%d\n",i,c,j,d);system...

19150969571:谁能用C语言给我写一个可以计算加减乘除的程序(只用算整数就OK)
徒姣答:int a,b,result;char operator ;printf("请输入两个数,输入的两个数用空格分隔\n");scaf("%d %d",&a,&b);printf("请指定你要输入的运算+或1表示加,-或2表示减,*或3表示乘,/或4表示除!\n") ;scanf("%c",&operator) ;switch operator { case ‘1’:‘+’result=a+b;break ...

19150969571:C语言 编译计算器可以分别计算加减乘除
徒姣答:while(scanf("%f", &num1) != 1 )//确保输入数据正确 { printf("输入有误,请重新输入第一个数\n"); CLR_INTPUT; } getchar();//清除空格符 while(scanf("%c", &ch) == 1) { if(ch != '+' && ch != '-' && ch != '*' && ch != '/'...

(编辑:本站网友)
相关推荐
关于我们 | 客户服务 | 服务条款 | 联系我们 | 免责声明 | 网站地图
@ 百韵网