大学计算机c语言计算器源代码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
char *top;
char *base;
}stack_char;
stack_num S_num;//定义
stack_char S_char;//定义
char fu[18] = {'\n', ')', '+', '-', '*', '/', '%', '^',
'Q', 'L', 'C', 'S', 'T', 'c', 's', 't', '('};
shu[++p] = -8;
else if(line[i] == 'C')
shu[++p] = -9;
else if(line[i] == 'S')
shu[++p] = -10;
else if(line[i] == 'T')
shu[++p] = -11;
else if(line[i] == 'c')
typedef struct node//为了处理符号而建立的链表(如: 1+(-2))
{
char data;
node *next;
}node;
typedef struct stack_num//存储数的栈
{
double *top;
double *base;
}stack_num;
typedef struct stack_char//存储运算符号的栈
i++;
break;
}
if(flag1)
{
h = h * 10 + (line[i] - '0');
flag = 1;
i++;
if(biao_dian)
ge++;
}
else
break;
}
if(flag)
{
if(biao_dian)
{
int r = 1;
for(int k = 1; k <= ge; k++)
else if(c == '%')
{
int r0 = (int)x % (int)y;
r = double(r0);
}
return r;
}
double operate_one(double one, char cc)//对一个数运算(含单目运算符:如log(L), sin(S)等等){
double r;
i++;
}
else
{
if(m == -1)
c = '+';
else if(m == -2)
c = '-';
else if(m == -3)
c = '*';
else if(m == -4)
c = '/';
else if(m == -5)
c = '%';
else if(m == -6)
c = '^';
else if(m == -7)
{
* ++S_char.top = c;
}
double pop_num()//数字出栈
{
double m = *S_num.top;
S_num.top--;
return m;
}
char pop_char()//运算符号出栈
{
char cc = *S_char.top;
S_char.top--;
return cc;
if(dian == '/' && x == 0)//判断是否除了"零"
{
cout<<"由于您除了零,结果将是错误的"<<endl;
}
push_num(operate(x, dian, y));//把进行一次计算的结果入栈}
}
else
{
push_char(c);
i++;
}
}
}
double result = pop_num();//得到结果
else if(c == '+')
r = x + y;
else if(c == '/' && y != 0)
r = x / y;
else if(c == '*')
r = x * y;
else if(c == '^')
{
r = 1;
for(int i = 1; i <= y; i++)
r *= x;
}
return result;
int check_kuohao()//检查表达式括号是否匹配
{
int i, f = 0;
int kuo[SIZE], key = 1;
memset(kuo, 0, sizeof(kuo));
for(i = 0; line[i] != '\0'; i++)
{
if(line[i] == '(')
}
char get_top_char()//得到运算符号的栈中最顶端的运算符号
{
return *S_char.top;
}
double operate(double y, char c, double x)//对两个数计算(含是双目运算符:如*, /等等){
double r;
if(c == '-')
r = x - y;
break;
}
}
if(line[i] == '_')
{
break;
}
if(line[i] == '.')
{
i++;
ge = 0;
biao_dian = 1;
}
if(line[i] == 'P')
{
shu[++p] = pi;
i++;
break;
}
if(line[i] == 'E')
{
shu[++p] = e;
r *= 10;
h /= r;
}
shu[++p] = h;//把转化而来的数字存于数组
}
if(line[i] == '+')
shu[++p] = -1;
else if(line[i] == '-')
shu[++p] = -2;
else if(line[i] == '*')
shu[++p] = -3;
int compare[1000];//表现出各运算符号的优先级
double shu[1000];//存储"数"的数组
double dai_result;//运算的结果,是为了处理M运算(简介函数里有M的定义)
int biao = 0;//和dia_result一样,为了处理M运算
char line[SIZE];//输入的所要计算的表达式
if(cc == 'Q')
r = sqrt(one);
else if(cc == 'C')
r = cos(one);
else if(cc == 'S')
r = sin(one);
else if(cc == 'T')
r = tan(one);
else if(cc == 'c')
r = acos(one);
double two_L = pop_num();
char dian = pop_char();
push_num(operate_L(two_L, one_L, dian));
}
else
{
double x = pop_num();
double y = pop_num();
char dian = pop_char();
{
push_char(c);
i++;
}
}else if(ch == '(' && c == ')')
{
pop_char();
i++;
}
else if(compare[ch] >= compare[c] && ch != '(' && ch != '\n')
{
if(ch == 'Q' || ch == 'C' || ch == 'S'|| ch == 'T'
void init()//初始化
{
compare[fu[0]] = -2;//用数字的大小表现出符号的优先级
compare[fu[1]] = -1;
compare[fu[2]] = 2;
compare[fu[3]] = 2;
compare[fu[4]] = 4;
compare[fu[5]] = 4;
else if(cc == 's')
r = asin(one);
else if(cc == 't')
r = atan(one);
return r;
}
double operate_L(double a, double b, char dian)//求对数的值
{
double r = log(b) / log(a);
c = 'Q';
else if(m == -8)
c = 'L';
else if(m == -9)
c = 'C';
else if(m == -10)
c = 'S';
else if(m == -11)
c = 'T';
else if(m == -12)
c = 'c';
else if(m == -13)
c = 's';
if(biao)
push_num(dai_result);//把运算的结果先进栈,在这个结果的基础上继续进行运算biao = 0;
for(i = 0; line[i] != '\0';)//把表达式中的数字字符串转化成可计算的数字
{
int flag = 0;
int flag1 = 1;//标记是否是运算符号
shu[++p] = -12;
else if(line[i] == 's')
shu[++p] = -13;
else if(line[i] == 't')
shu[++p] = -14;
else if(line[i] == '(')
shu[++p] = -15;
else if(line[i] == ')')
shu[++p] = -16;
else if(line[i] == '\n')
shu[++p] = -17;
i++;
}
i = 1;
while(shu[i] != -17 || get_top_char() != '\n')
{
double m = shu[i];
if(m >= 0)
{
push_num(m);
|| ch == 'c' || ch == 's' || ch == 't')
{
double one = pop_num();
char dian = pop_char();
push_num(operate_one(one, dian));
}Βιβλιοθήκη Baidu
else if(ch == 'L')
{
double one_L = pop_num();
return r;
}
double compute()//对整个表达式的计算
{
char c;//表示运算符号
int p = 0;//用于shu[++p],先初始化
int i, j;
init();//进行初始化
push_char('\n');
line[strlen(line)] = '\n';
line[strlen(line)+1] = '\0';
kuo[++f] = 1;
C++
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
const double pi = 3.14159265;
const double e = 2.718281828459;
const int SIZE = 1000;
else if(m == -14)
c = 't';
else if(m == -15)
c = '(';
else if(m == -16)
c = ')';
else if(m == -17)
c = '\n';
char ch = get_top_char();//得到最顶端运算符号
if(compare[ch] < compare[c])//运算符号级别的比较
//int flag2 = 1;//标记是否出现'_';
double h = 0;
int ge;//位数
int biao_dian = 0;//是否是小数的类型
while(1)
{
flag1 = 1;
for(j = 0; j <= 16; j++)
{
if(line[i] == fu[j])
{
flag1 = 0;
compare[fu[6]] = 4;
compare[fu[7]] = 5;
for(int i = 8; i <= 15; i++)
compare[fu[i]] = 6;
compare[fu[16]] = 7;
S_num.base = (double*)malloc(sizeof(double)*SIZE);//为栈开辟空间
S_char.base = (char*)malloc(sizeof(char)*SIZE);//同上
S_num.top = S_num.base;
S_char.top = S_char.base;
}
void push_num(double n)//数字进栈
{
* ++S_num.top = n;
}
void push_char(char c)//运算符号进栈
else if(line[i] == '/')
shu[++p] = -4;
else if(line[i] == '%')
shu[++p] = -5;
else if(line[i] == '^')
shu[++p] = -6;
else if(line[i] == 'Q')
shu[++p] = -7;
else if(line[i] == 'L')
char *top;
char *base;
}stack_char;
stack_num S_num;//定义
stack_char S_char;//定义
char fu[18] = {'\n', ')', '+', '-', '*', '/', '%', '^',
'Q', 'L', 'C', 'S', 'T', 'c', 's', 't', '('};
shu[++p] = -8;
else if(line[i] == 'C')
shu[++p] = -9;
else if(line[i] == 'S')
shu[++p] = -10;
else if(line[i] == 'T')
shu[++p] = -11;
else if(line[i] == 'c')
typedef struct node//为了处理符号而建立的链表(如: 1+(-2))
{
char data;
node *next;
}node;
typedef struct stack_num//存储数的栈
{
double *top;
double *base;
}stack_num;
typedef struct stack_char//存储运算符号的栈
i++;
break;
}
if(flag1)
{
h = h * 10 + (line[i] - '0');
flag = 1;
i++;
if(biao_dian)
ge++;
}
else
break;
}
if(flag)
{
if(biao_dian)
{
int r = 1;
for(int k = 1; k <= ge; k++)
else if(c == '%')
{
int r0 = (int)x % (int)y;
r = double(r0);
}
return r;
}
double operate_one(double one, char cc)//对一个数运算(含单目运算符:如log(L), sin(S)等等){
double r;
i++;
}
else
{
if(m == -1)
c = '+';
else if(m == -2)
c = '-';
else if(m == -3)
c = '*';
else if(m == -4)
c = '/';
else if(m == -5)
c = '%';
else if(m == -6)
c = '^';
else if(m == -7)
{
* ++S_char.top = c;
}
double pop_num()//数字出栈
{
double m = *S_num.top;
S_num.top--;
return m;
}
char pop_char()//运算符号出栈
{
char cc = *S_char.top;
S_char.top--;
return cc;
if(dian == '/' && x == 0)//判断是否除了"零"
{
cout<<"由于您除了零,结果将是错误的"<<endl;
}
push_num(operate(x, dian, y));//把进行一次计算的结果入栈}
}
else
{
push_char(c);
i++;
}
}
}
double result = pop_num();//得到结果
else if(c == '+')
r = x + y;
else if(c == '/' && y != 0)
r = x / y;
else if(c == '*')
r = x * y;
else if(c == '^')
{
r = 1;
for(int i = 1; i <= y; i++)
r *= x;
}
return result;
int check_kuohao()//检查表达式括号是否匹配
{
int i, f = 0;
int kuo[SIZE], key = 1;
memset(kuo, 0, sizeof(kuo));
for(i = 0; line[i] != '\0'; i++)
{
if(line[i] == '(')
}
char get_top_char()//得到运算符号的栈中最顶端的运算符号
{
return *S_char.top;
}
double operate(double y, char c, double x)//对两个数计算(含是双目运算符:如*, /等等){
double r;
if(c == '-')
r = x - y;
break;
}
}
if(line[i] == '_')
{
break;
}
if(line[i] == '.')
{
i++;
ge = 0;
biao_dian = 1;
}
if(line[i] == 'P')
{
shu[++p] = pi;
i++;
break;
}
if(line[i] == 'E')
{
shu[++p] = e;
r *= 10;
h /= r;
}
shu[++p] = h;//把转化而来的数字存于数组
}
if(line[i] == '+')
shu[++p] = -1;
else if(line[i] == '-')
shu[++p] = -2;
else if(line[i] == '*')
shu[++p] = -3;
int compare[1000];//表现出各运算符号的优先级
double shu[1000];//存储"数"的数组
double dai_result;//运算的结果,是为了处理M运算(简介函数里有M的定义)
int biao = 0;//和dia_result一样,为了处理M运算
char line[SIZE];//输入的所要计算的表达式
if(cc == 'Q')
r = sqrt(one);
else if(cc == 'C')
r = cos(one);
else if(cc == 'S')
r = sin(one);
else if(cc == 'T')
r = tan(one);
else if(cc == 'c')
r = acos(one);
double two_L = pop_num();
char dian = pop_char();
push_num(operate_L(two_L, one_L, dian));
}
else
{
double x = pop_num();
double y = pop_num();
char dian = pop_char();
{
push_char(c);
i++;
}
}else if(ch == '(' && c == ')')
{
pop_char();
i++;
}
else if(compare[ch] >= compare[c] && ch != '(' && ch != '\n')
{
if(ch == 'Q' || ch == 'C' || ch == 'S'|| ch == 'T'
void init()//初始化
{
compare[fu[0]] = -2;//用数字的大小表现出符号的优先级
compare[fu[1]] = -1;
compare[fu[2]] = 2;
compare[fu[3]] = 2;
compare[fu[4]] = 4;
compare[fu[5]] = 4;
else if(cc == 's')
r = asin(one);
else if(cc == 't')
r = atan(one);
return r;
}
double operate_L(double a, double b, char dian)//求对数的值
{
double r = log(b) / log(a);
c = 'Q';
else if(m == -8)
c = 'L';
else if(m == -9)
c = 'C';
else if(m == -10)
c = 'S';
else if(m == -11)
c = 'T';
else if(m == -12)
c = 'c';
else if(m == -13)
c = 's';
if(biao)
push_num(dai_result);//把运算的结果先进栈,在这个结果的基础上继续进行运算biao = 0;
for(i = 0; line[i] != '\0';)//把表达式中的数字字符串转化成可计算的数字
{
int flag = 0;
int flag1 = 1;//标记是否是运算符号
shu[++p] = -12;
else if(line[i] == 's')
shu[++p] = -13;
else if(line[i] == 't')
shu[++p] = -14;
else if(line[i] == '(')
shu[++p] = -15;
else if(line[i] == ')')
shu[++p] = -16;
else if(line[i] == '\n')
shu[++p] = -17;
i++;
}
i = 1;
while(shu[i] != -17 || get_top_char() != '\n')
{
double m = shu[i];
if(m >= 0)
{
push_num(m);
|| ch == 'c' || ch == 's' || ch == 't')
{
double one = pop_num();
char dian = pop_char();
push_num(operate_one(one, dian));
}Βιβλιοθήκη Baidu
else if(ch == 'L')
{
double one_L = pop_num();
return r;
}
double compute()//对整个表达式的计算
{
char c;//表示运算符号
int p = 0;//用于shu[++p],先初始化
int i, j;
init();//进行初始化
push_char('\n');
line[strlen(line)] = '\n';
line[strlen(line)+1] = '\0';
kuo[++f] = 1;
C++
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
const double pi = 3.14159265;
const double e = 2.718281828459;
const int SIZE = 1000;
else if(m == -14)
c = 't';
else if(m == -15)
c = '(';
else if(m == -16)
c = ')';
else if(m == -17)
c = '\n';
char ch = get_top_char();//得到最顶端运算符号
if(compare[ch] < compare[c])//运算符号级别的比较
//int flag2 = 1;//标记是否出现'_';
double h = 0;
int ge;//位数
int biao_dian = 0;//是否是小数的类型
while(1)
{
flag1 = 1;
for(j = 0; j <= 16; j++)
{
if(line[i] == fu[j])
{
flag1 = 0;
compare[fu[6]] = 4;
compare[fu[7]] = 5;
for(int i = 8; i <= 15; i++)
compare[fu[i]] = 6;
compare[fu[16]] = 7;
S_num.base = (double*)malloc(sizeof(double)*SIZE);//为栈开辟空间
S_char.base = (char*)malloc(sizeof(char)*SIZE);//同上
S_num.top = S_num.base;
S_char.top = S_char.base;
}
void push_num(double n)//数字进栈
{
* ++S_num.top = n;
}
void push_char(char c)//运算符号进栈
else if(line[i] == '/')
shu[++p] = -4;
else if(line[i] == '%')
shu[++p] = -5;
else if(line[i] == '^')
shu[++p] = -6;
else if(line[i] == 'Q')
shu[++p] = -7;
else if(line[i] == 'L')