怎么用C语言编写一个计算器程序? 15

只用基本的C语言,不能用C++,C#的语法,要求用数组,编写20位的计算器程序,能计算加减乘除,小弟我实在是不会了,有关期末考试……请各位大哥救命啊堆栈,链表什么的根本还... 只用基本的C语言,不能用C++,C#的语法,要求用数组,编写20位的计算器程序,能计算加减乘除,小弟我实在是不会了,有关期末考试……请各位大哥救命啊
堆栈,链表什么的根本还没学…………不能用的……
展开
 我来答
世镶柳009
2008-12-09 · TA获得超过3017个赞
知道答主
回答量:2928
采纳率:0%
帮助的人:2457万
展开全部
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <algorithm>

// 堆栈的数组实现,数组的大小固定。
template<class T>
class stack
{
private:
T *s; // 数组的首地址(栈底)
size_t N; // 指向栈顶第一个空闲块
const size_t size; // 堆栈的大小,固定不变

public:
stack(size_t n) : size(n)
{
s = new T[n]; // 可能抛出异常
N = 0; // 设置栈顶指针
}

~stack()
{
delete [] s;
}

bool empty() const
{
return N == 0;
}

bool full() const
{
return N == size;
}

void push(const T &object)
{
if (full())
{
throw "error: stack is full !";
}

s[N++] = object;
}

T pop()
{
if (empty())
{
throw "error: stack is empty !";
}

return s[--N];
}

T peek() const
{
if (empty())
{
throw "error: stack is empty !";
}

return s[N-1];
}

friend std::ostream& operator<<(std::ostream& os, const stack<T> &stk)
{
for (size_t i = 0; i < stk.N; i++)
{
std::cout << stk.s[i] << " ";
}

return os;
}
};

// 堆栈的链表实现
template<class T>
class STACK
{
private:
typedef struct node
{
T item;
node *next;
node(T x, node *t = NULL) : item(x), next(t) {}
}*link;

link head; // 指向栈顶第一个有效对象

public:
STACK(size_t n)
{
head = NULL;
}

~STACK() // 也可以用pop的方法删除,但效率低
{
link t = head;

while (t != NULL)
{
link d = t;
t = t->next;
delete d;
}
}

bool empty() const
{
return head == NULL;
}

bool full() const
{
return false;
}

void push(const T &object)
{
head = new node(object, head);
}

T pop()
{
if (empty())
{
throw "error: stack is empty !";
}

T v = head->item;
link t = head->next;
delete head;
head = t;
return v;
}

T peek() const
{
if (empty())
{
throw "error: stack is empty !";
}

return head->item;
}

friend std::ostream& operator<<(std::ostream& os, const STACK<T> &stk)
{
for (link t = stk.head; t != NULL; t = t->next)
{
std::cout << t->item << " ";
}

return os;
}
};

// 中缀表达式转化为后缀表达式,仅支持加减乘除运算、操作数为1位十进制非负整数的表达式。
char* infix2postfix(const char *infix, char *postfix)
{
const size_t N = strlen(infix);

if (N == 0 || postfix == NULL)
{
return postfix;
}

stack<char> opcode(N); // 堆栈存放的是操作符

for (size_t i = 0; i < N; i++)
{
switch (infix[i])
{
case '(': // 直接忽略左括号
break;

case ')': // 弹出操作符
*postfix++ = opcode.pop();
*postfix++ = ' ';
break;

case '+':
case '-':
case '*':
case '/':
opcode.push(infix[i]); // 压入操作符
break;

default:
if (isdigit(infix[i])) // 如果是数字,直接输出
{
*postfix++ = infix[i];
*postfix++ = ' ';
}
}
}

return postfix;
}

// 后缀表达式转化为中缀表达式,仅支持加减乘除运算、操作数为1位十进制非负整数的表达式。
char* postfix2infix(const char *postfix, char *infix)
{
const size_t N = strlen(postfix);

if (N == 0 || infix == NULL)
{
return infix;
}

*infix = '\0'; // 初始化输出字符串为空串
std::vector<std::string> v;

// 初始化,将所有有效字符放入容器
for (size_t i = 0; i < N; i++)
{
if (isdigit(postfix[i]) || postfix[i] == '+'
|| postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/')
{
v.push_back(std::string(1, postfix[i]));
}
}

// 处理每一个操作符
for (std::vector<std::string>::iterator b = v.begin(); b < v.end(); b++)
{
if (*b == "+" || *b == "-" || *b == "*" || *b == "/")
{
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
std::cout << "------------------------------------------------" << std::endl;

std::string opcode = *(b);
std::string oprand1 = *(b - 2);
std::string oprand2 = *(b - 1);
b = v.erase(b - 2, b + 1); // 删除原来的三个表达式,用一个新的表达式替换
b = v.insert(b, std::string("(") + oprand1 + opcode + oprand2 + std::string(")"));
}
}

for (std::vector<std::string>::iterator b = v.begin(); b < v.end(); b++)
{
strcat(infix, (*b).c_str());
}

return infix;
}

// 计算后缀表达式的值,仅支持加减乘除运算、操作数为非负整数的表达式。
int postfix_eval(const char * postfix)
{
const size_t N = strlen(postfix);

if (N == 0)
{
return 0;
}

STACK<int> operand(N); // 堆栈存放的是操作数

for (size_t i = 0 ; i < N; i++)
{
switch (postfix[i])
{
int op1, op2;

case '+':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 + op2);
break;

case '-':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 - op2);
break;

case '*':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 * op2);
break;

case '/':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 / op2);
break;

default:

if (isdigit(postfix[i])) // 执行类似atoi()的功能
{
operand.push(0);

while (isdigit(postfix[i]))
{
operand.push(10 * operand.pop() + postfix[i++] - '0');
}

i--;
}
}

std::cout << operand << std::endl; // 输出堆栈的内容
}

return operand.pop();
}

// 本程序演示了如何后缀表达式和中缀表达式的相互转换,并利用堆栈计算后缀表达式。
// 转换方向:org_infix --> postfix --> infix
int main(int argc, const char *argv[])
{
// const char *org_infix = "(5*(((9+8)*(4*6))+7))"; // section 4.3
const char *org_infix = "(5*((9*8)+(7*(4+6))))"; // exercise 4.12
std::cout << "原始中缀表达式:" << org_infix << std::endl;

char *const postfix = new char[strlen(org_infix) + 1];
infix2postfix(org_infix, postfix);
std::cout << "后缀表达式:" << postfix << std::endl;

char *const infix = new char[strlen(postfix) + 1];
postfix2infix(postfix, infix);
std::cout << "中缀表达式:" << infix << std::endl;

std::cout << "计算结果是:" << postfix_eval(postfix) << std::endl;
std::cout << "计算结果是:" << postfix_eval("5 9*8 7 4 6+*2 1 3 * + * + *") << std::endl; // exercise 4.13

delete []infix;
delete []postfix;
return 0;
}
ryw12403
2008-12-18 · TA获得超过1898个赞
知道大有可为答主
回答量:2501
采纳率:0%
帮助的人:2074万
展开全部
20位做不到,C 只能做到10位.

http://hi.baidu.com/ryw12403/blog/item/2e587e0a65c4361895ca6b9a.html
看下这个计算器,可能适合你,在我的空间里,
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wangbinwh
2008-12-10 · TA获得超过639个赞
知道大有可为答主
回答量:1367
采纳率:100%
帮助的人:513万
展开全部
VC的么?要windows界面么?
我资料里有联系方式
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式