用C语言怎样实现计算器加减乘除功能?
我学c++时写的
#include<iostream.h>
#include "string"
int count(int a,int b,char c)
{
if(c=='+') return a+b;
if(c=='-') return a-b;
if(c=='/') return a/b;
if(c=='*') return a*b;
}
void main()
{
char str[100];
cin>>str;
int number[10]={0};
char sign[10];
int i,j=0,k=0,m;
int strlong=strlen(str);//#include "string"
cout<<strlong;
for(i=0;i<strlong;i++)
{
if(str[i]>='0'&&str[i]<='9')
number[j]=number[j]*10+str[i]-48;
else
{j++;
sign[k]=str[i];
k++;}
}
j++;
//cout<<j<<k;
for(i=0;i<j;i++)
cout<<number[i]<<endl;
for(i=0;i<k;i++)
cout<<sign[i]<<endl;
for( i=0;i<k;i++)
{
if(sign[i]=='/'||sign[i]=='*')
{number[i]=count(number[i],number[i+1],sign[i]);
cout<<number[i];
for(m=i;m<k-i;m++)
{sign[m]=sign[m+1];number[m+1]=number[m+2];}
k--;i--;}
}
for( i=0;i<k;i++)
{ if(sign[i]=='+'||sign[i]=='-')
{number[i]=count(number[i],number[i+1],sign[i]);
cout<<number[i];
for(m=i;m<k-i;m++)
{sign[m]=sign[m+1];number[m+1]=number[m+2];}
k--;i--;}
}
for (i=0;i<3;i++)
{cout<<number[i];
}
cout<<k;
}
2.堆栈
#include "string"
#include "iostream"
#include<vector>
#include<list>
#include<cstdlib>
using namespace std;
//自定义类型 用于存储 两种数据类型
class newType
{
public:
bool flag;//true 为f false 为 c
union
{
float f;
char c;
}data;
};
//将字符串转换为 数字数组和字符数组 (通用提取字符串中数字)
bool couvert(string str,vector<float> &numbers,vector<char> &chars,vector<newType> &all)//这里要使用引用
{
int len=str.length();
bool flag=true;
int pos=0;
for(int i=0;i<len;i++)
{
if(str[i]>='0'&&str[i]<='9'||str[i]=='.')
{
if (flag)
{
string substr=str.substr(i,len) ;
float f=atof(substr.data());
numbers.push_back(f);
//添加f到all向量中
newType n;
n.data.f=f;
n.flag=true;
all.push_back(n);
}
flag=false;
}
else
{
//chars.push_back(str[i]);
chars.push_back(str[i]);
newType n;
n.data.c=str[i];
n.flag=false;
all.push_back(n);
flag=true;
}
}//for
return true;
}
//计算没有括号的表达式
bool calculate(vector<float> numbers,
vector<char> chars,float &value)
{
//计算四者表达式 无括号类型 1+2*3+4
int ii=0;
//先计算乘除
while(ii<chars.size())
//注意while(ii<(chars.size()-2)) 和while(ii<chars.size()-2) 区别
{
switch(chars[ii])
{
case '*':
numbers[ii]=numbers[ii]*numbers[ii+1];
numbers.erase(numbers.begin()+ii+1); //移除number[ii]后面的数
chars.erase(chars.begin()+ii); //移除chars[ii]
ii--;
break;
case '/':
numbers[ii]=numbers[ii]/numbers[ii+1];
numbers.erase(numbers.begin()+ii+1);
chars.erase(chars.begin()+ii);
ii--;
break;
}
ii++;
}
//只剩下加减 计算加减
ii=0;
while(ii<chars.size())
//注意while(ii<(chars.size()-2)) 和while(ii<chars.size()-2) 区别
{
switch(chars[ii])
{
case '+':
// cout<<"+::"<<numbers[ii]<<chars[ii]<<numbers[ii+1]<<endl;
numbers[ii]=numbers[ii]+numbers[ii+1];
numbers.erase(numbers.begin()+ii+1); //移除number[ii]后面的数
chars.erase(chars.begin()+ii); //移除chars[ii]
break;
case '-':
// cout<<"-::"<<numbers[ii]<<chars[ii]<<numbers[ii+1]<<endl;
numbers[ii]=numbers[ii]-numbers[ii+1];
numbers.erase(numbers.begin()+ii+1);
chars.erase(chars.begin()+ii);
break;
}
// ii++;
}
value=numbers[0];//得到值
return true;
}
//计算带括号的表达式
int calculate1( vector<newType> all,float &value)
{
int pos=0;
vector<float> numbers;
vector<char> chars;
float va=0;
for(int i=0;i<all.size();i++)
{
if (all[i].flag)//判断是数字还是字符
{
cout<<"数字"<<i<<":"<<all[i].data.f<<endl;
}
else
{
cout<<"字符"<<i<<":"<<all[i].data.c<<endl;
if (all[i].data.c==')') //如果是右括号 将之前的()之间的括号内容
{ //用calculate计算 并替换
for (int j=pos+1;j<i;j++) //参数转换
{
if (all[j].flag)
{
numbers.push_back(all[j].data.f);
} else{
chars.push_back(all[j].data.c);
}
}
calculate(numbers,chars,va);
numbers.clear();
chars.clear();
newType ne;
ne.data.f=va;
all.erase(all.begin()+pos,all.begin()+i+1);
all.insert(all.begin()+pos,ne);
i=pos+1;
}
else if (all[i].data.c=='(')
{
pos=i;//记录此时左括号的位置
}
}//else
}//for
for(int kk=0;kk<all.size();kk++)
{
if (all[kk].flag)
{
numbers.push_back(all[kk].data.f);
} else{
chars.push_back(all[kk].data.c);
}
}
for( i=0;i<all.size();i++)
{
if (all[i].flag)
{
cout<<all[i].data.f<<" ";
} else
{
cout<<all[i].data.c<<" ";
}
}
calculate(numbers,chars,value);
return 1;
}
void main()
{
//
string str="10+(2*3+8)+(10*2)";
vector<float> numbers;
vector<char> chars;
vector<newType> all;
couvert(str, numbers, chars,all);
for(int i=0;i<all.size();i++)
{
if (all[i].flag)
{
cout<<all[i].data.f<<" ";
} else
{
cout<<all[i].data.c<<" ";
}
}
float value,value1;
// calculate(numbers,chars,value);
calculate1(all,value1);
cout<<value1;
}
这是我翻家底找到的 .
2024-12-27 广告
/* 栈的链接存储----任意进制转换*/
#include <iostream.h>
#include <stdlib.h>
typedef int ElemType;
struct SNode {
ElemType data;
SNode* next;
};
void InitStack(SNode*& HS)
{
HS = NULL;
}
// 压栈 插入元素
void Push(SNode*& HS, const ElemType& item)
{
SNode* newptr = new SNode; /* 获取动态结点*/
newptr->data = item; /*给新分配的结点赋值*/
newptr->next = HS; /* 向栈顶压入新结点*/
HS = newptr;
}
//从栈中删除一个元素并返回该元素
ElemType Pop(SNode*& HS)
{
if(HS==NULL)
{
cerr<<"无法从空栈中删除元素,退出运行 !"<<endl;
exit(1);
}
SNode* p = HS;
HS = HS->next;
ElemType temp = p->data;
delete p;
return temp;
}
//读取栈顶元素
ElemType Peek(SNode* HS)
{
if(HS==NULL)
{
cerr<<"无法从空链栈中读取元素,退出运行 !"<<endl;
exit(1);
}
return HS->data;
}
bool EmptyStack(SNode* HS)
{
return HS == NULL;
}
void ClearStack(SNode*& HS)
{
SNode *mp, *np;
mp = HS;
while(mp!=NULL)
{
np = mp->next;
delete mp;
mp = np;
}
HS = NULL;
}
void Transform(long number, int r)
{
SNode *a;
InitStack(a);
while(number!=0)
{
int k = number%r;
Push(a,k);
number = number/r;
}
while(!EmptyStack(a))
{
if(r!=16) cout<<Pop(a);
else
{
int x = Pop(a);
if(x<10) cout<<x;
else
{
switch(x)
{
case 10: cout<<'A'; break;
case 11: cout<<'B'; break;
case 12: cout<<'C'; break;
case 13: cout<<'D'; break;
case 14: cout<<'E'; break;
case 15: cout<<'F'; break;
}
}
}
}
cout<<endl;
}
void main()
{
cout<<"请输入一个长整数(最长十位有效数字): ";
long n;
cin>>n;
cout<<"长整数 "<<n<<" 的十六进制数为: ";
Transform(n,16);
cout<<"长整数 "<<n<<" 的十进制数为: ";
Transform(n,10);
cout<<"长整数 "<<n<<" 的八进制数为: ";
Transform(n,8);
cout<<"长整数 "<<n<<" 的六进制数为: ";
Transform(n,6);
cout<<"长整数 "<<n<<" 的四进制数为: ";
Transform(n,4);
cout<<"长整数 "<<n<<" 的二进制数为: ";
Transform(n,2);
}
void main()
{
int x,y;
char op;
int d;
printf("第一个整数:");
scanf("%d",&x);
printf("第二个整数:");
scanf("%d",&y);
printf("运算符 (+,—,*,/,%%):");
getchar();
op=getchar();
switch(op)
{
case '+':
printf("%d+%d=%d\n",x,y,x+y);
break;
case '-':
printf("%d-%d=%d\n",x,y,x-y);
break;
case '*':
printf("%d*%d=%d\n",x,y,x*y);
break;
case '/':
if(y==0)
printf("溢出\n");
else
printf("%d/%d=%d\n",x,y,x/y);
break;
case '%':
if(y==0)
printf("溢出\n");
else
printf("%d%%%d=%d\n",x,y,x%y);
break;
default:
printf("输入错误!\n");
break;
}
scanf("%d\n",&d);
}
#include<stdio.h>
void main()
{
float a,b;
char d;
printf("请输入两个数a,b:");
scanf("%f,%f"&a,&b);
printf("请输入运算符号d");
scanf("%c",d);
switch(d)
{
case'+':printf("%f\n",a+b);break;
case'-':printf("%f\n",a-b);break;
case'*':printf("%f\n",a*b);break;
case'/':printf("%f\n",a/b);break;
default:printf("计算出错\n");
}
}
void main()
{
int a,b;
scanf("%d%d"&a,&b);
printf("%f\n",a+b);
printf("%f\n",a-b);
printf("%f\n",a*b);
printf("%f\n",a/b);
return 0;
}