C语言设计 ( 1) 设计一个程序来模拟一个简单的手持计算器。程序支持算术运算+、-、*、⼀、=、以及C(清除)

2025-04-28 23:27:29
推荐回答(3个)
回答1:

我给你说一下思路可以吗?我现在比较忙,所以没有那么多的时间再去写代码。
1、你 用函数 window(left,riht,top,bottom); 画出输入窗口;该函数的原型在 conio.h 里;
2、用textbackground(color)函数设置背景色,用textcolor()设置前景色;
3、用highvideo() 和 lowcideo() 控制字符的亮度显示;
4、很多教材上都有简单计算器程序或者你从网上搜一下,然后与前面结合起来就可以了
如果想让我写的话,我得三天后才可能有时间,真的很抱歉哈!
对了,还有清除功能对不对,用delline(); 函数可以清除整行,这样就可以与A结合实现按一次A全清除,按C一个字符一个字符往前清除更好说

回答2:

C语言版的,希望对你有帮助!

#define push(p,c) *p=c;p++;
#define pop(p,c) p--;c=*p;
#include
int calculate(int data1,char theta, int data2)
{
switch(theta)
{
case '+':return data1+data2;break;
case '-':return data1-data2;break;
case '*':return data1*data2;break;
case '/':return data1/data2;break;
default: printf("wrong theta:%c!",theta);
}
}

int compare(char c1,char c2)
{
if(c1=='#')
return -1;
if(c1=='+'||c1=='-')
if(c2=='*'||c2=='/')
return -1;
else return 1;
if(c1=='*'||c1=='/')
return 1;
}

void main()
{
int opnd[40],*pnd,data,data1,data2;
char optr[40],*ptr,ch,theta;
ptr=optr;pnd=opnd;
printf("please input a calculation expression:\n");
ch=getchar();
if(ch!='#')
putchar(ch);
push(ptr,'#');
while(ch!='#'||*(ptr-1)!='#')
{
if(!(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='#'))
{
data=0;
while(ch>='0'&&ch<='9')
{
data=data*10+ch-'0';
ch=getchar();
if(ch!='#')
putchar(ch);
}
push(pnd,data);
}
else switch(compare(*(ptr-1),ch))
{
case 1: pop(pnd,data1);pop(pnd,data2);pop(ptr,theta);
data=calculate(data2,theta,data1);
push(pnd,data); break;
case -1:push(ptr,ch);ch=getchar();
if(ch!='#') putchar(ch);
}

}

printf("=%d\n",*(pnd-1));

}

回答3:

LZ您好,一下是我自己写的用C#软件编译实现的功能,窗口自己加进去,贴下我的以下代码即可实现如上功能,不过需要自己调试一下喽,望LZ学习进步!~
private void addValues()
{
int lhs = int.Parse(lhsOperand.Text);
int rhs = int.Parse(rhsOperand.Text);
int outcome;
outcome = lhs + rhs;
expression.Text = lhsOperand.Text + " + " + rhsOperand.Text;
result.Text = outcome.ToString();
}

private void subtractValues()
{
int lhs = int.Parse(lhsOperand.Text);
int rhs = int.Parse(rhsOperand.Text);
int outcome;
outcome = lhs - rhs;
expression.Text = lhsOperand.Text + " - " + rhsOperand.Text;
result.Text = outcome.ToString();
}

private void multiplyValues()
{
int lhs = int.Parse(lhsOperand.Text);
int rhs = int.Parse(rhsOperand.Text);
int outcome;
outcome = lhs * rhs;
expression.Text = lhsOperand.Text + " * " + rhsOperand.Text;
result.Text = outcome.ToString();
}

private void divideValues()
{
float lhs = float.Parse(lhsOperand.Text);
float rhs = float.Parse(rhsOperand.Text);
float outcome;
outcome = lhs / rhs;
expression.Text = lhsOperand.Text + " / " + rhsOperand.Text;
result.Text = outcome.ToString();
}

private void remainderValues()
{
float lhs = float.Parse(lhsOperand.Text);
float rhs = float.Parse(rhsOperand.Text);
float outcome;
outcome = lhs % rhs;
expression.Text = lhsOperand.Text + " % " + rhsOperand.Text;
result.Text = outcome.ToString();
}