用C语言编写一个计算器程序,实现加,减,乘,除,求平方根(正数),倒数等功能.

①要求根据用户从键盘输入的表达式:操作数1运算符op操作数2计算表达式的值,指定的算术运算符为加(+)、减(-)、乘(*)、除(/)等。②要求程序能进行浮点数的算术运算。... ①要求根据用户从键盘输入的表达式:
操作数1 运算符op 操作数2
计算表达式的值,指定的算术运算符为加(+)、减(-)、乘(*)、除(/)等。
②要求程序能进行浮点数的算术运算。
③如果要求输入的算术表达式中的操作数和运算符之间可以加入任意多个空白符,那么程序如何修改?
④程序要求可以连续做多次算术运算,每次运算结束后,程序都给出提示:
Do you want to continue(Y/N or y/n)?
用户输入Y或y时,程序继续进行其他算术运算;否则程序退出运行状态。
展开
 我来答
miraculouswxl
2014-06-17
知道答主
回答量:11
采纳率:0%
帮助的人:4万
展开全部
#include<iostream>
#include<cmath>
#include<string>
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')

i++;

}

i++;

}

if(ge >= 3)

return 0;

else

return 1;
}

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

{

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

cout<<result<<endl;
}

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

{

void introduce();

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

double result;//
结果

void input();//
定义

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

{

result = compute();

output(result);

cout<<"
输入一个字符
'M'

'D'

'F',
决定是否继续
: "<<endl;

while(cin>>cc)

{

if(cc == 'M')

{

system("cls");

introduce();

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

cout<<result<<endl;

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

dai_result = result;

biao = 1;

input();//
输入表达式

break;

}

else if(cc == 'D')

{

system("cls");

introduce();

cout<<"
计算器已清零
,
请输入您所要计算的表达式
"<<endl;

input();//
输入表达式

break;

}

else if(cc == 'F')

{

system("cls");

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

break;

}

else

{

cout<<"
所输入字符无效
,
请输入一个字符
'M'

'D'

'F'!"<<endl;

continue;

}

}

}

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

{

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

{

cout<<"
您所输入的表达式括号不匹配
,
请重新输入
:"<<endl;

input();//
输入表达式

}

else

{

cout<<"
您所输入的表达式不合法
,
请重新输入
:"<<endl;

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<<"
计算器简要介绍
"<<endl;

cout<<"C(cos)

S(sin)

T(tan)

a(arccos)

c(arcsin) "<<endl;

cout<<"7

8

9

/

on

t(arctan) "<<endl;

cout<<"4

5

6

*

%

L(log)"<<endl;

cout<<"1

2

3

-

M(M+)

Q(sqrt)

"<<endl;

cout<<"0

.

+

^(
乘方
) F(off)

Enter(=) "<<endl;

cout<<"
对于对数输入

L2_5
表示以
2
为底
5
的对数
"<<endl;

cout<<"M(
在前面结果的基础上继续计算,
如:
上次结果为
10

现输入
+10.5*2)"<<endl;

cout<<"D(
清零并继续输入
)"<<endl;

cout<<"F(
计算机关闭
)"<<endl;

cout<<"
输入

P
就代表输入圆周率
,
输入

E
代表输入自然对数
"<<endl<<endl;
}

void print()
{

system("color 2");

cout<<"

欢迎使用本计算器
"<<endl;

cout<<"
输入一个字符串

on,
计算器开始启动
"<<endl;
}

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

{

string start;

print();

while(cin>>start)

{

if(start != "on")

{

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

continue;

}

else

break;

}

if(start == "on")

{

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

system("cls");//
刷屏

}

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

cout<<"
现在
,
请输入您所要计算的表达式
"<<endl;

input();//
输入所要计算的表达式

}

int main()
{

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

return 0;
}

r = acos(one);

else if(cc == 's')

r = asin(one);

else if(cc == 't')

r = atan(one);
追问
没看懂  写简单点    按我上面要求弄下  大神 财富值不够在加
黎约未央31
2014-06-17 · TA获得超过239个赞
知道答主
回答量:145
采纳率:60%
帮助的人:55.8万
展开全部
把什么等掉了?
追问
自己看要求
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式