您的当前位置:首页正文

C++语言计算器源代码

2023-10-27 来源:钮旅网
.

C++语言编写。。

#include

#include

#include

using namespace std;

const double pi = 3.14159265;

const double e = 2.718281828459;

const int SIZE = 1000;

typedef struct node//为了处理符号而建立的链表(如: 1+(-2))

{

char data;

node *next;

}node;

.

.

typedef struct stack_num//存储 数 的栈

{

double *top;

double *base;

}stack_num;

typedef struct stack_char//存储 运算符号 的栈

{

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', '('};

int compare[1000];//表现出各运算符号的优先级

double shu[1000];//存储 \"数\" 的数组

double dai_result;//运算的结果,是为了处理 M 运算(简介函数里有M的定义)

int biao = 0;//和dia_result 一样,为了处理 M 运算

char line[SIZE];//输入的所要计算的表达式

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;

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)//运算符号进栈

{

* ++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;

}

char get_top_char()//得到运算符号的栈中最顶端的运算符号

{

return *S_char.top;

}

double operate(double y, char c, double x)//对两个数计算(含是双目运算符:如 *, / 等等)

{

double r;

if(c == '-')

.

.

r = x - y;

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;

}

.

.

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;

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);

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);

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';

if(biao)

push_num(dai_result);//把运算的结果先进栈, 在这个结果的基础上继续进行运算

biao = 0;

for(i = 0; line[i] != '\\0';)//把表达式中的数字字符串转化成可计算的数字

{

int flag = 0;

int flag1 = 1;//标记是否是运算符号

// 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;

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;

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++)

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;

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')

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')

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);

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)

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';

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])//运算符号级别的比较

{

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'

|| ch == 'c' || ch == 's' || ch == 't')

{

double one = pop_num();

char dian = pop_char();

push_num(operate_one(one, dian));

}

else if(ch == 'L')

.

.

{

double one_L = pop_num();

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();

if(dian == '/' && x == 0)//判断是否除了\"零\"

{

.

.

cout<<\"由于您除了零,结果将是错误的\"<}

push_num(operate(x, dian, y));//把进行一次计算的结果入栈

}

}

else

{

push_char(c);

i++;

}

}

}

double result = pop_num();//得到结果

.

.

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] == '(')

kuo[++f] = 1;

else if(line[i] == ')')

{

.

.

if(kuo[f] == 1)

{

kuo[f] = 0;

f--;

}

else

{

key = 0;

break;

}

}

}

if(key && f == 0)

.

.

return 1;

else

return 0;

}

int check_char()//检查运算符号是否合法(如: 1 +* 4)

{

int i, ge;

for(i = 0; line[i] != '\\0'; )

{

ge = 0;

while(line[i] == '+' || line[i] == '-' || line[i] == '*'

|| line[i] == '/' || line[i] == '%' || line[i] == '^'

|| line[i] == 'Q' || line[i] == 'L' || line[i] == 'S'

.

.

|| line[i] == 'C' || line[i] == 'T' || line[i] == 's'

|| line[i] == 'c' || line[i] == 't')

{

ge++;

i++;

}

i++;

}

if(ge >= 3)

return 0;

else

return 1;

}

.

.

void output(double result)//打出结果

{

printf(\"所得结果是: \");

cout<}

void check()//检查表达式是否合法

{

void introduce();

char cc;//决定计算器按哪种功能进行计算

double result;//结果

void input();//定义

if( check_kuohao() && check_char() )//看是否合法, 合法则计算

{

.

.

result = compute();

output(result);

cout<<\"输入一个字符'M'或'D'或'F', 决定是否继续: \"<while(cin>>cc)

{

if(cc == 'M')

{

system(\"cls\");

introduce();

printf(\"您上次所得结果为: \");

cout<cout<<\"在上次计算结果的基础上, 请继续输入想计算的表达式\"<dai_result = result;

.

.

biao = 1;

input();//输入表达式

break;

}

else if(cc == 'D')

{

system(\"cls\");

introduce();

cout<<\"计算器已清零, 请输入您所要计算的表达式\"<input();//输入表达式

break;

}

else if(cc == 'F')

.

.

{

system(\"cls\");

cout<<\"计算器关闭, 谢谢使用!\"<break;

}

else

{

cout<<\"所输入字符无效, 请输入一个字符'M'或'D'或'F'!\"<continue;

}

}

}

else//不合法,分两种不合法

.

.

{

if(check_kuohao() == 0 && check_char() == 1)

{

cout<<\"您所输入的表达式括号不匹配, 请重新输入:\"<input();//输入表达式

}

else

{

cout<<\"您所输入的表达式不合法, 请重新输入:\"<input();//输入表达式

}

}

}

.

.

void tackle_fuhao()//处理负号

{

node *root, *head, *p, *q, *p1;

root = head = new node;

head->next = NULL;

int i;

for(i = 0; line[i] != '\\0'; i++)//建立链表

{

p = new node;

p->data = line[i];

p->next = head->next;

head->next = p;

head = p;

.

.

}

// delete p;

q = (node*)malloc(sizeof(node));

head = root;

if(root->next->data == '+' || root->next->data == '-')//处理第一个字符

{

p = new node;

p->data = '0';

p->next = head->next;

head->next = p;

}

if(root->next != NULL)

{

.

.

for(q = root->next; q; q = q->next)

{

if(q->data == '(' && (q->next->data == '-' || q->next->data == '+'))

{

p = new node;

p->data = '0';

p->next = q->next;

q->next = p;

}

}

}

// delete q;

p1 = new node;

.

.

int qi = -1;

for(p1 = root->next; p1; p1 = p1->next)

{

line[++qi] = p1->data;

}

line[++qi] = '\\0';

}

void input()//输入

{

cin>>line;

if(biao == 0)

tackle_fuhao();//处理负号

check();//检查表达式是否合法

.

.

}

void introduce()//对计算器的符号功能的简要介绍

{

cout<<\"计算器简要介绍\"<cout<<\"C(cos) S(sin) T(tan) a(arccos) c(arcsin) \"<cout<<\"7 8 9 / on t(arctan) \"<cout<<\"4 5 6 * % L(log)\"<cout<<\"1 2 3 - M(M+) Q(sqrt) \"<cout<<\"0 . + ^(乘方) F(off) Enter(=) \"<cout<<\"对于对数输入 L2_5 表示以2为底5的对数\"<cout<<\"M(在前面结果的基础上继续计算,如:上次结果为10,现输入+10.5*2)\"<cout<<\"D(清零并继续输入)\"<cout<<\"F(计算机关闭)\"<.

.

cout<<\"输入 P 就代表输入圆周率, 输入 E 代表输入自然对数\"<}

void print()

{

system(\"color 2\");

cout<<\" 欢迎使用本计算器\"<cout<<\"输入一个字符串 on, 计算器开始启动\"<}

void if_start()//是否启动计算器

{

string start;

print();

while(cin>>start)

.

.

{

if(start != \"on\")

{

cout<<\"您所输入的字符无效, 请按照介绍的继续输入:\"<continue;

}

else

break;

}

if(start == \"on\")

{

system(\"color 5\");//颜色的处理

system(\"cls\");//刷屏

.

.

}

introduce();//对计算器的简要介绍

cout<<\"现在,请输入您所要计算的表达式\"<input();//输入所要计算的表达式

}

int main()

{

if_start();//调用是否启动计算器函数

return 0;

}

.

因篇幅问题不能全部显示,请点此查看更多更全内容