① 用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;
}
```
此程序首先提示用戶輸入一個操作符(+、-、* 或 /),然後提示用戶輸入兩個數字,最後根據操作符執鎮頌行相應的計算並輸出結果。注意,這個程序還包含了一些錯誤處理代碼,例如當用戶試尺睜圖除以零時會給出錯誤提示。