用c语言编一个简单的能计算加减乘除的小计算器,要是能把思路附上最好,谢啦! 怎样用C语言编写一个简单的可以进行加减乘除运算混合运算的计算...

www.zhiqu.org     时间: 2024-06-02
#include <stdio.h>
#include<conio.h>
int main()
{
int i,num1,num2,sum,count;
char ch,op;
printf("1 2 3 + - \n");
printf("4 5 6 * / \n");
printf("7 8 9 = # \n\n");
{
printf("0");
ch=getch();
while(ch<'0' || ch>'9')//输入的不是数字,重新输入
ch=getch();
printf("\b%c",ch);
count=1;
num1=ch-'0';
while(ch=getch(),ch>='0' && ch<='9')
{
putchar(ch);
num1=num1*10+ch-'0';
count++;
}
while(1)
{
op=ch;//符号
ch=getch();
while(ch<'0' || ch>'9')//输入的不是数字,重新输入
ch=getch();
for(i=0;i<count;i++)//回到行首
putchar('\b');
for(i=0;i<count;i++)//用空格覆盖
putchar(' ');
for(i=0;i<count;i++)//回到行首
putchar('\b');
putchar(ch);
count=1;
num2=ch-'0';
while(ch=getch(),ch>='0' && ch<='9')
{
putchar(ch);
num2=num2*10+ch-'0';
count++;
}
switch(op)
{
case '+':
sum=num1+num2;
break;
case '-':
sum=num1-num2;
break;
case '*':
sum=num1*num2;
break;
case '/':
sum=num1/num2;
break;
default :
break;
}
for(i=0;i<count;i++)//回到行首
putchar('\b');
for(i=0;i<count;i++)//用空格覆盖
putchar(' ');
for(i=0;i<count;i++)//回到行首
putchar('\b');
printf("%d",sum);
num1=sum;
count=1;
while(sum/=10)
count++;
if(ch=='#')
break;
}
}
}

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

#include
#include
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
#include
#include
#include "Tree.h"
#include
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;
stackNodeStack;
stackOpStack;
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<<"结果是: ";
coutdata<<'
';
cout<<'
'<<"--------------------------------------------------------------------------------"<<'
';
cout: ";
cin>>choice;
}
else
{
cout<<"************************************************"<<'
';
cout<<"错误:输入的表达试有误!"<<'
';
cout<<"************************************************"<<'
';
cout: ";
cin>>choice;
}
}
return 0;
}
课程设计报告
设计题目:十进制四则运算计算器
实习目的
通过实习,了解并初步掌握设计、实现较大系统的完整过程,包括系统分析、编码设计、系统集成、以及调试分析,熟练掌握数据结构的选择、设计、实现以及操作方法,为进一步的应用开发打好基础。
二.问题描述
在以二叉树表示算术表达式的基础上,设计一个十进制的四则运算的计算器。[设计要求]实现整数浮点数的四则运算。
三.需求分析
该程序实现的是实数型的四则运算,并在此运算上又加入了幂”^”运算,该程序用一二叉树表示整个输入的算术表达式:
(1)实现对结点的打印,便于结果分析;
(2)实现对结点的统计;
(3)实现中间结果的显示,可以看打印的结点,验证运算结果的正确与否。
四.概要设计
系统用到的抽象数据类型定义:
1.ADT node_type{
数据对象V:一个集合,该集合中的所有元素具有相同的特性
数据关系R:R={VR}
VR={|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语言编写一个简单的可以进行加减乘除运算混合运算的计算器的方法:
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语言编一个计算器程序,能够实现基本的加减乘除,能够输出运算对象,运算符,运算结果.谢谢啦! -
(19217842507): #include <stdio.h> int main(void) { float a,b,result; char c; printf("请输bai入du计zhi算dao式回:答"); scanf("%f%c%f",&a,&c,&b); switch(c) { case '+': result=a+b; break; case '-': result=a-b; break; case '*': result=a*b; break; case '/': result=a/b; break; } printf("%f%c%f=%f\n",a,c,b,result); return 0; }

#毋光风# C语言要编写一个计算器的简单程序! 急需急需
(19217842507): #include <stdio.h> int main() { float a,b; char c,i='a'; for(;i!='0';) { printf("请输入要运算的表达式:"); scanf("%f%c%f",&a,&c,&b); switch(c) { case '+':printf("运算的结果是:%f\n",a+b);break; case '-':printf("运算的结果是:%f\n",a-...

#毋光风# C语言 要求编写一个简单计算器的程序 -
(19217842507): #include<stdio.h> void add(int a,int b,int c) { c=a+b; printf("%d+%d = %d",a,b,c); printf("\n"); } void minus(int a,int b,int c) { c=a-b; printf("%d-%d=%d",a,b,c); printf("\n"); } void multiplication(int a,int b,int c) { c=a*b; printf("%d*%d=%d...

#毋光风# 求大神给一个C语言的程序代码做简易计算器? -
(19217842507): 更好看的代码 #include <stdio.h> #include <math.h> double sum(double a, double b) { return a + b; } double minu(double a, double b) { return a - b; } double mult(double a, double b) { return a * b; } double div(double a, double b) { return a / b; } int mod...

#毋光风# 用c语言编写一个计算器 -
(19217842507): 先简单给你写了一下,你先看看行不行,太晚了,我得睡啦,要是不明白,明天追问就行:#include int main() { int a,b; char c; while( //这里是逗号表达式,如果看起来别扭可以改到while循环外 printf("请输入1个式子(输入q退出):\n"), ...

#毋光风# 用c语言编写 设计一个1~7的简易计数器 -
(19217842507): //一个具有两个数加减乘除功能的计算器#include <stdio.h> void main() { int a,b,c; char ch,ch1; printf("请输入表达式如 5+6= 然后按回车键:"); scanf("%d%c%d%c",&a,&ch,&b,&ch1); switch(ch) { case '+': c=a+b; printf("%d+%d=%d\n",...

#毋光风# 如何使用C语言做一个简单的计算器 -
(19217842507): #include <iostream> using namespace std; int main() { int a,b; char c; cin>>a; cin>>c; cin>>b; switch(c) { case '+':printf("%d+%d=%d\n",a,b,a+b);break; case '-':printf("%d-%d=%d\n",a,b,a-b);break; case '*':printf("%d*%d=%d\n",a,b,a*b);...

#毋光风# 利用你现有的c语言知识 设计开发一个简易计算器,可进行加、减、乘、除、求余运算. -
(19217842507): #include <stdio.h> float numA = 0; float numB = 0; float temp = 0; void calc(){ printf("\n"); printf("======欢迎使用计算器====="); printf("\n"); printf("请输入第一个数:"); scanf("%lf",&numA) printf("请输入第二个数:")...

#毋光风# 求用C语言编写一简单计算器程序,要求:实现简单地加减乘除就行了 -
(19217842507): #include <stdio.h> int jisuan(int a,int b,char fu) { if(fu=='+') return a+b; if(fu=='-') return a-b; if(fu=='*') return a*b; if(fu=='/') return a/b; } int fun(char *ss,int n) { int i,flag=0; if(n==1) return ss[0]-'0'; for(i=0;i<n;i++) { e68a84e8a2ad62616964757a686964616f...

#毋光风# 利用 C语言,设计开发一个“简单计算器”提供加减乘除运算功能 -
(19217842507): 退出系统你手动关闭行不?#includevoid main() {int a,b,c; char d;do {printf("输入:"); scanf("%d%c%d",&a,&d,&%b); switch(d) {case'+':c=a+b;printf("%d+%d=%d",a,b,c);\*后面的一样*/default:printf("错误)";}}while(1);}