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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

ZZ的计算器

發布時間:2023/12/1 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

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

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