ZZ的計算器

Problem Description

ZZ自從上大學以來,腦容量就是以SB計算的,這個吃貨竟然連算術運算也不會了,可是當今的計算機可是非常強大的,作為ACMer, 幾個簡單的算術又算得了什么呢?可是該怎么做呢?ZZ只想算簡單的加減乘除,由于大腦鈍化,ZZ連小數都沒有概念了!

Input

有多組測試數據,每組測試數據為一個字符串,字符串為合法的算術運算,運算符號只包含+、-、*、/,數據為整數.

Output

請輸出正確的結果,除數為0的時候,輸出impossible。(結果保證在長整形之內)

Sample Input

1+1*2
2/2+1

Sample Output

3
2
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define LL long long
 4 
 5 int main()
 6 {
 7     LL d[10000] , temp , cnt;
 8     LL i , j;
 9     char ch[200];
10     while(gets(ch))
11     {
12         cnt=temp=0;
13         for(j=0 ; j<strlen(ch) ; j++)
14         {
15             if(ch[j]>='0'&&ch[j]<='9') temp = temp*10+ch[j]-'0';
16             else break;
17         }
18         d[cnt++]=temp;
19         for(i=j ; i<strlen(ch) ; i++)
20         {
21             temp=0;
22             for(j=i+1 ; ch[j]>='0'&&ch[j]<='9' ; j++) temp = temp*10+ch[j]-'0';
23             if(ch[i]=='*') d[cnt-1] *= temp;
24             else if(ch[i]=='/') d[cnt-1] /= temp;
25             else if(ch[i]=='+') d[cnt++] = temp;
26             else d[cnt++] = (-1)*temp;
27             i=j-1;
28         }
29         for(i=1 ; i<cnt ; i++) d[0]+=d[i];
30         printf("%I64d\n",d[0]);
31     }
32     return 0;
33 }
數組模擬
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stack>
 4 using namespace std;
 5 
 6 stack<long long> stk;
 7 stack<char> op;
 8 
 9 int main()
10 {
11     char str[100];
12     bool bl ;
13     int len, i, j, k;
14     long long a, b;
15     while (scanf("%s",str)!=EOF)
16     {
17         bl = true;
18         while (!stk.empty()) stk.pop();
19         while (!op.empty()) op.pop();
20         i = 0;
21         if (str[0] == '-') i++;
22         len = strlen(str);
23         for(; i<len; i++)
24         {
25             a = str[i++]-'0';
26             while(i < len && str[i] >='0' && str[i] <='9')
27             {
28                 a *= 10;
29                 a += str[i++] -'0';
30             }
31             if (!op.empty())
32             {
33                 if (op.top() == '*')
34                 {
35                     b = stk.top();
36                     stk.pop();
37                     a *=b;
38                     op.pop();
39                 }
40                 else if (op.top() == '/')
41                 {
42                     b = stk.top();
43                     stk.pop();
44                     if (a == 0)
45                     {
46                         puts("impossible");
47                         bl = false;
48                         break;
49                     }
50                     a = b / a;
51                     op.pop();
52                 }
53             }
54             stk.push(a);
55             if (i < len) op.push(str[i]);
56         }
57         if (!bl) continue;
58         long long sum=0;
59         while (!op.empty())
60         {
61             a = stk.top();
62             stk.pop();
63             if (op.top() == '+')
64             {
65                 sum += a;
66                 op.pop();
67             }
68             else
69             {
70                 sum -= a;
71                 op.pop();
72             }
73         }
74 
75         if (str[0] == '-') sum -= stk.top();
76         else sum += stk.top();
77         printf("%I64d\n", sum);
78 
79     }
80     return 0;
81 }
兩個棧模擬

?

轉載于:https://www.cnblogs.com/contestant/p/3209633.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378050.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378050.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378050.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

kotlin 覆蓋屬性_Kotlin程序| 方法覆蓋的示例

kotlin 覆蓋屬性方法重載 (Method Overriding) Method overriding allows derived class has the same function name and signature as the base class 方法重寫允許派生類具有與基類相同的函數名稱和簽名 By method overriding we can provide different implementation into…

對視頻中的特征顏色物體(青色水杯)進行跟蹤

方法一&#xff1a;目標物體白色&#xff0c;其余黑色 import cv2 import numpy as npdef extrace_object():capture cv2.VideoCapture("G:/Juptyer_workspace/study/data/yy.mp4")while(True):ret,frame capture.read()if retFalse:breakhsv cv2.cvtColor(frame…

Android實現號碼歸屬地查詢

我們通過發送XML訪問 WebService就可以實現號碼的歸屬地查詢&#xff0c;我們可以使用代理服務器提供的XML的格式進行設置&#xff0c;然后請求提交給服務器&#xff0c;服務器根據請求就會返回給一個XML&#xff0c;XML中就封裝了我們想要獲取的數據。 發送XML 1.通過URL封裝路…

如何從 Datagrid 中獲得單元格的內容與 使用值轉換器進行綁定數據的轉換IValueConverter...

一、如何從 Datagrid 中獲得單元格的內容 DataGrid 屬于一種 ItemsControl, 因此&#xff0c;它有 Items 屬性并且用ItemContainer 封裝它的 items. 但是&#xff0c;WPF中的DataGrid 不同于Windows Forms中的 DataGridView。 在DataGrid的Items集合中&#xff0c;DataGridRow…

【C++ grammar】常量、指針、Usage of using, typedef, and #define

目錄1、常量 &#xff08;Constant&#xff09;2、指針&#xff08;Pointer&#xff09;3、Usage of using, typedef, and #define1、常量 &#xff08;Constant&#xff09; 常量是程序中一塊數據&#xff0c;這個數據一旦聲明后就不能被修改了。 如果這塊數據有一個名字&am…

斯威夫特山地車_斯威夫特| 兩個數字相加的程序

斯威夫特山地車In this program, we will have an idea - how two numbers can be added and displayed as the output on the screen? 在此程序中&#xff0c;我們將有一個想法- 如何將兩個數字相加并顯示為屏幕上的輸出 &#xff1f; Open XCode terminal and type the fol…

四、色彩空間

一、色彩空間 1、什么是色彩空間&#xff1f; 色彩空間是定義的顏色范圍。 2、常見的色彩空間有哪些&#xff1f; ①RGB ②HSV 在OpenCV中&#xff0c;Hue的值為0~180&#xff0c;之所以不是360是因為&#xff0c;8位存不下&#xff0c;故進行歸一化操作&#xff0c;使得H…

Oracle LOB 詳解

一. 官方說明Oracle 11gR2 文檔&#xff1a;LOB Storagehttp://download.oracle.com/docs/cd/E11882_01/appdev.112/e18294/adlob_tables.htm#ADLOB45267Oracle 10gR2 文檔&#xff1a;LOBs in Tableshttp://download.oracle.com/docs/cd/B19306_01/appdev.102/b14249/adlob_t…

FIFA的完整形式是什么?

國際足聯&#xff1a;國際足球聯合會 (FIFA: Federation Internationale de Football Association) FIFA is an abbreviation of the "Federation Internationale de Football Association" in French. It is also known as the International Federation of Associa…

POJ 1654 Area

題意&#xff1a;從原點出發&#xff0c;沿著8個方向走&#xff0c;每次走1個點格或者根號2個點格的距離&#xff0c;最終回到原點&#xff0c;求圍住的多邊形面積。分析&#xff1a;直接記錄所經過的點&#xff0c;然后計算多邊形面積。注意&#xff0c;不用先保存所有的點&am…

【C++ grammar】重載、內聯、變量作用域、帶默認參數的函數

目錄1、變量的作用域1. 變量的作用域分類2. Unary Scope Resolution (一元作用域解析運算符)2、重載函數3、帶有默認參數值的函數4、重載函數 VS 帶有默認參數值的函數5、內聯函數&#xff08;Inline Function&#xff09;1. 普通函數的優缺點2. 使用內聯函數3. 定義內聯函數4.…

五、像素運算

一、相關概念 1、算術運算 Ⅰ加減乘除 Ⅱ調節亮度 Ⅲ調整對比度 2、邏輯運算 Ⅰ與或非 Ⅱ遮罩層控制 二、圖像算術運算(加減乘除均值方差) 其中圖像的加減乘除需要保證兩張圖像的大小相同 import cv2 import numpy as npdef add(src1,src2):dst cv2.add(src1,src2)cv2.im…

創建bootstrap項目_使用Bootstrap創建第一個網頁

創建bootstrap項目使用Bootstrap創建第一個網頁 (Create First Webpage with Bootstrap) In the previous article, we learned "how to setup bootstrap?" for a web project. If you haven’t gone through that, it is recommended to read it. Now, in this art…

Chaikin Curve(球面插值)

在兩條折線間完成平滑的過渡是 用畫布做UI 或者做類似地圖編輯器一類的工作的 很常見的任務。 怎么樣化方為圓是決定工作效率的很重要的因素。&#xff08;當需要編輯的曲線多起來&#xff0c; 復雜起來的時候&#xff0c;這會是件相當繁重的工作&#xff09; 最容易想到的莫非…

【2020 電設G題 圖像題解】

博主聯系方式: QQ:1540984562 QQ交流群:892023501 群里會有往屆的smarters和電賽選手,群里也會不時分享一些有用的資料,有問題可以在群里多問問。 2022.3.10新增訂閱通知 近期把此專欄設置為了付費模式,可以直接花9.9買這個專欄,買了就可以直接這個專欄的所有文章了。后…

六、ROI和泛洪填充

一、ROI ROI&#xff1a;region of interest&#xff0c;即感興趣區域。 一般主要通過numpy來獲取ROI 將某區域轉變為灰色圖片再覆蓋原圖像 import cv2 import numpy as npsrc cv2.imread(r"G:\Juptyer_workspace\study\opencv\opencv3\a1.jpg") cv2.imshow(&quo…

VS2005 there is no source code available for the current location 解決方案

1.首先試最常規的方法&#xff1a;Clean and then rebuild solution&#xff0c;但是沒有解決 2.進入Tools>Options,選擇Debugging>General 卻掉 Enable address-level debugging 選項&#xff0c;在去掉 Require source files to exactly match the original version. O…

django 靜態數據_如何在Django中使用靜態數據?

django 靜態數據Static Data means those data items that we cannot want to change, we want to use them as it is like audio, video, images, files etc. 靜態數據是指我們不想更改的數據項&#xff0c;我們想像音頻&#xff0c;視頻&#xff0c;圖像&#xff0c;文件等那…

Leetcode226. 翻轉二叉樹(遞歸、迭代、層序三種解法)

目錄題目1、層序法&#xff1a;2、遞歸法&#xff1a;1、先序遍歷&#xff08;中左右&#xff09;2、后序遍歷&#xff08;左右中&#xff09;3、遞歸中序遍歷為什么不行&#xff08;左中右&#xff09;3、迭代法&#xff1a;1、先序遍歷2、中序遍歷3、后序遍歷為什么迭代法的中…

Asp.net 獲取當前目錄的三種方法

方法一&#xff1a; string sPath System.IO.Path.GetDirectoryName(Page.Request.PhysicalPath) 方法二&#xff1a; string sPath System.Web.HttpContext.Current.Request.MapPath("/") 方法三&#xff1a; string s…