c语言字符型计算器,C语言字符计算器

这又是以前的一篇文章,觉得有纪念价值。就发过来了。

去年暑假自己下了C语言实战105例,看了几个基础的,其中有一个是关于字符计算器的

我看起来蛮简单的,不过自己做起来我觉得还是做得少了,懵懵懂懂的。

现在想起那个觉得蛮有趣的(想想以前跟老大学做按钮计算器没做成,失败啊!~~~)

于是又重新写了一下,不过还是忍不住看了下源码,呵呵,真菜

不过发现里面有一点点错误,例如:不能计算 6 /4;只能计算 6/4 。

也许作者当时只想把原理展示出来,而把更多的细节留给了我们,不错的办法。

然后我自己也尝试着赶紧了一下:

#include

#include

#include

char token; //保存操作符

int error = 1;//error 为0表示计算中有错误

int mid();

int high();

int Match(char c)

{

//匹配成功则读入下一个字符并返回1

if(c == token)

{

while((token = getchar()) == ' ')

;

return 1;

}

//失败则返回0

else

return -1;

}

int low()

{

int result;

result = mid(); //读入操作数

while('+' == token || '-' == token) //计算多个加法与减法

if('+' == token) //计算加法

{

Match('+');

result += mid();

}

else if('-' == token) //计算减法

{

Match('-');

result -= mid();

}

return result;

}

int mid()

{

int result;

int div; //除数

result = high(); //读入操作数

while('*' == token || '/' == token) //计算多个乘法和除法

if('*' == token) //乘法

{

Match('*'); //匹配获取下一个字符

result *=high();

}

else if('/' == token) //除法

{

Match('/');

div = high(); //先读入除数

if(div) //除数不能为0

{

result /= div;

}

else

error = 0;

}

return result; //返回结果

}

int high()

{

int result;

if('(' == token)

{

Match('('); //获取输入中的下一个字符

result = low(); //计算括号内的表达式

Match(')'); //获取输入中的下一个字符

}

else if(token >= '0' && token <= '9')

{

ungetc(token,stdin); //如果token是数字,返还给输入流

scanf("%d",&result); //从输入中获取数字

while((token = getchar()) == ' ')

; // 消除空格符并读入下一个字符

}

else

error = 0;

return result; //返回结果

}

int main()

{

int result;

char title[] = "小小程序菜鸟野兔 qq:371514614 欢迎大家加我"; //做个小广告

SetConsoleTitle(title); //设置窗口标题

printf("*****************************************\n");

printf("**Welcome to use this simple calculator**\n");

printf("**Please input a multinomial like **\n");

printf("** 6-3*(5-1)/2+14/7 **\n");

printf("*****************************************\n");

while((token = getchar()) == ' ')

; //载入第一个字符

result = low(); //从低到高进行计算

if('\n' == token && (error != 0)) //按下回车则开始计算结果并判断计算过程中是否出错

printf("The answer is:%d\n",result);

else

printf("出错了\n");

getch(); //让屏幕停留一会

return 0;

}

我很佩服写出这个程序的人,刚开始拿到这程序的时候,我根本

就不相信它可以正确执行,后来直到自己运行了一下。然后我又想着它的工作

原理是什么呢(没办法,我就喜欢刨根问底。嘻嘻)。就自己看源码,根据注释慢慢看

。可那时候我刚学,递归都不是很懂,遇到不理解的,就把这里改一下在运行

看看有什么不同。就这样跌跌撞撞的弄懂了其中的原理,也让我自己成了一回计算机,

执行了一下这个程序。不过我真觉得自己模拟计算机执行代码很好。很能锻炼能力。

看看以前写的代码,心里还是挺有感触的。以前自己不怕死,不弄明白饭也不吃。

现在呢?我跟自己说想通了,不会就不会呗。锐气也就被磨掉了。

带着这样的想法,很久没有接触过编程了。昨天在群里被他们说成是“大牛”其实我

只是一个小菜鸟。

有个朋友说他以前是这个专业的,现在要重新拾起来。我也是这么想的,重新拾起来。

加油,大笨兔!!! :)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 用C语言编写计算器的方法有很多种,但是最简单的方式是使用C语言中的标准库函数,比如printf()和scanf()函数,以及其他数学函数。另外,还可以使用预先定义的函数,如add(),subtract(),multiply()和divide()等函数来实现计算器的功能。 ### 回答2: 使用C语言编写一个计算器,首先需要定义基本的数据结构和函数。 1. 定义一个结构体用于保存操作数和操作符: ``` typedef struct { double operand1; double operand2; char operator; } Calculator; ``` 2. 定义一个函数来接收输入的操作数和操作符,并进行相应的计算: ``` double calculate(Calculator calculator) { switch(calculator.operator) { case '+': return calculator.operand1 + calculator.operand2; case '-': return calculator.operand1 - calculator.operand2; case '*': return calculator.operand1 * calculator.operand2; case '/': return calculator.operand1 / calculator.operand2; default: printf("错误的运算符\n"); return 0; } } ``` 3. 主函数中进行输入和调用计算函数的操作: ``` int main() { double operand1, operand2; char operator; printf("请输入第一个操作数: "); scanf("%lf", &operand1); printf("请输入运算符(+, -, *, /): "); scanf(" %c", &operator); printf("请输入第二个操作数: "); scanf("%lf", &operand2); Calculator calculator; calculator.operand1 = operand1; calculator.operand2 = operand2; calculator.operator = operator; double result = calculate(calculator); printf("结果: %.2lf\n", result); return 0; } ``` 这样就完成了一个简单的四则运算的计算器。用户可以通过依次输入操作数和操作符,程序将会输出结果。例如,输入"2 + 3"将会输出结果"5.00"。 ### 回答3: 要用C语言写一个计算器,可以按照以下步骤进行: 1. 首先,我们需要定义变量来存储用户输入的数值和操作符。可以使用整、浮点字符的变量。 2. 接下来,使用printf函数向用户展示可以进行的操作,如加法、减法、乘法和除法等。 3. 使用scanf函数来获取用户输入的操作符和数值,并将它们存储到相应的变量中。 4. 根据用户选择的操作符,使用if-else语句或switch语句来执行相应的操作。例如,当用户选择加法时,将存储的两个数值相加,并将结果打印出来。 5. 可以使用循环语句来让计算器持续运行,直到用户选择退出。 以下是一个简单的示例代码: ``` #include <stdio.h> int main() { char operator; float num1, num2; while (1) { printf("\nEnter an operator (+, -, *, /) or q to quit: "); scanf("%c", &operator); if (operator == 'q') { break; } printf("Enter two numbers: "); scanf("%f %f", &num1, &num2); switch (operator) { case '+': printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2); break; case '-': printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2); break; case '*': printf("%.2f * %.2f = %.2f\n", num1, num2, num1 * num2); break; case '/': if (num2 != 0) { printf("%.2f / %.2f = %.2f\n", num1, num2, num1 / num2); } else { printf("Error: Division by zero\n"); } break; default: printf("Invalid operator\n"); break; } getchar(); // Removes the trailing newline character from the input buffer } return 0; } ``` 以上代码实现了一个基本的计算器,用户可以选择不同的操作符来执行相应的数学计算,并得到结果。当用户选择退出时,程序会终止运行。请注意,此示例代码仅作为参考,你可以根据自己的需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值