① 用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;
}
这是我翻家底找到的 .
② 用C语言做一个计算器,能实现加减乘除混合运算
是的,可以使用C语言编写一个计算器程序,能够实现加、减、乘、除等混合运算。下面是御困郑一个简单的示例程序:
```c
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
printf("Error: division by zero!\n");
return -1;
}
result = num1 / num2;
break;
default:
printf("Invalid operator!\n");
return -1;
}
printf("%.2f %c %.2f = %.2f\n", num1, operator, num2, result);
return 0;
}
```
此程序首先提示用户输入一个操作符(+、-、* 或 /),然后提示用户输入两个数字,最后根据操作符执镇颂行相应的计算并输出结果。注意,这个程序还包含了一些错误处理代码,例如当用户试尺睁图除以零时会给出错误提示。