日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实现计算器功能

發布時間:2024/9/30 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现计算器功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.1point3acres.com/bbs/thread-12175-1-1.html


如何寫出一個簡單的4則運算的程序。
輸入 " 1 -2*3 + 6/3"?
輸出 -3

只用支持正數int的,+,-,*,/,不用寫出()的情況








要注意的問題是,空格。。。。


可以直接用逆波蘭式做,稍麻煩,用遞歸可以直接做


#include <iostream> #include <cstdio> using namespace std; char c; double num() {double ret = 0;while(c=getc(stdin)) {if(c >= '0' && c <= '9')ret = ret * 10 + c-'0';else break;}double dec = 0.1;if(c=='.') {while(c=getc(stdin)) {if(c >= '0' && c <= '9') {ret += dec*(c-'0');dec /= 10;}else break;}}return ret; } double calc_mul()//返回下一個數字或者返回后面乘除的值 {double ret = num();while(c=='*'||c=='/') {ret = c=='*'?ret*num():ret/num();}return ret; } double calc_add() {double ret = calc_mul();while(c=='+'||c=='-') {ret = c=='+'?ret+calc_mul():ret-calc_mul();}return ret; } int main() {cout << calc_add() << endl;return 0; }

int cal(int last, int idx ) {//idx指向當前的運算符,last指前一個數字if (idx >= n)return last;if (vt[idx] == "x")return cal(last * atoi(vt[idx+1].c_str()), idx + 2);if (vt[idx] == "/")return cal(last / atoi(vt[idx+1].c_str()), idx + 2);if (vt[idx] == "+"){if (idx + 2 >=N || vt[idx + 1] == "-" || vt[idx + 1] == "+")return cal(last + atoi(vt[idx+1].c_str()), idx +2);else return last + cal(atoi(vt[idx+1].c_str()), idx +2);}//"-"同理 }


總結

以上是生活随笔為你收集整理的实现计算器功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。