1127: 第三章:再見,林靜!

1127: 第三章:再見,林靜!
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 112 Solved: 33
[Submit][Status][Web Board]
Description

鄭薇的專業是土木工程,理工科的女生原本就是珍稀動物,而且大多數都長得比較抽象。想她鄭微雖然不是什麼絕代美女,跟她漂亮的媽媽相比也有一定差距,但她有一張討喜的圓臉,小巧的尖下巴,大而靈動的眼睛,秀氣挺直的鼻子,尤其是皮膚白皙無瑕——這是媽媽也承認自己年輕的時候也比不上的。因此,根據鄭微自己無數次攬鏡自照的鑑定結果,她絕對稱得上是人見人愛、花見花開的美少女,簡直就是瓊瑤阿姨筆下的女主角。雖然瓊瑤阿姨的小說已經落伍幾個世紀了,但阿姨的審美觀還是歷久彌新的,看她挑中的連續劇女主角一個比一個紅就知道了。就連一向很少夸人的林靜也曾說過鄭微不說話的時候還是相當有迷惑性的,稱得上“靜若處子”。當然,鄭微很自覺地過濾掉了他後半句“動若瘋兔”的評價,完全當做他對她的肯定。如今想起林靜,她的臉上只是微微一笑。

生性豁達的鄭薇,埋藏起自己的愛情,開始過上大學時代的忙碌生活。

土木工程的數學題:給定一些沒有括號的四則運算表達式,求其結果。

Input

輸入數據中含有一些表達式(數量≤1000,長度按含有的運算計,運算符≤30),表達式的運算符只含有加、減、乘、除。表達式中每個數的精度範圍在double型內,表達式中沒有任何其他運算符,沒有括號。

Output

對每個表達式,計算其結果。按科學計數法輸出,精度按6位小數,每個結果應占獨立一行。如果表達式發生除0的情況,則對該表達式直接輸出“DivByZero”。

Sample Input

3+5.0
6-27
6-2/0
3+5
6+1
3+5+17
1+2-3
4+5/6-43211+2+3+4+5

Sample Output

8.000000e+00
-8.000000e+00
DivByZero
3.400000e+01
1.500000e+01
-1.816667e+01

HINT

輸出結果請使用printf("%e\n",ans);或者cout<<scientific<<ans<<endl;

Source
/*
這題我寫了好多個版本,但是除了Ac了的這個版本(參考了大佬的),其他的版本(純自己寫的)問題暫時還沒有找出來誒
ac版本學習涉及到的stringsream,學習入口:https://blog.csdn.net/jnxxhzz/article/details/53581179
*/
ac_code:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    while(cin>>s)
    {
        double num[55];
        char oper[50];
        stringstream st(s);
        double n;
        char c;
        int k = 0,j = 0,flag = 1;
        st>>n;
        num[k++] = n;
        while(st>>c>>n)
        {
            if(c == '*'|| c == '/')
            {
                if(c == '*')
                    num[k-1] *= n;
                else
                {
                    if(n == 0)
                    {
                        flag = 0;
                        break;
                    }
                    num[k-1] /= n;
                }
            }
            else
            {
                oper[j++] = c;
                num[k++] = n;
            }
        }
        if(flag)
        {
            double ans = num[0];
            for(int i = 0; i < j; i++)
                oper[i] == '+' ? ans += num[i+1] : ans -= num[i+1];
            cout<<scientific<<ans<<endl;
        }
        else
            cout<<"DivByZero"<<endl;
    }
    return 0;
}

(待發現)問題版本:
我寫的第一個版本:
//學數據結構,棧的應用就涉及到了表達式的計算
/*
這個版本是我按我們自己算表達式的方式寫的,由於最後計算部分頻繁的push和pop,時間超限,同時內存很耗,不過這讓我我發現不用棧還更好做(下面那個版本就是根據和這個版本改進的)
*/

#include <bits/stdc++.h>
using namespace std;
int main()
{
    stack<double>num,num1;
    stack<char>oper,oper1;
    double n;
    char p,q;
    while(cin>>n)
    {
        num.push(n);
        double x,y;
        int flag = 1;
        while(1)
        {
            p = getchar();
            if(p == '\n')
            {
                while(!oper.empty())
                {
                    oper1.push(oper.top());
                    oper.pop();
                }
                while(!num.empty())
                {
                    num1.push(num.top());
                    num.pop();
                }
                while(!oper1.empty()&&num1.size()>=2)
                {
                    x = num1.top();
                    num1.pop();
                    y = num1.top();
                    num1.pop();
                    q = oper1.top();
                    oper1.pop();
                    q == '+' ? x += y : x -= y;
                    num1.push(x);
                }
                if(flag)
                    cout<<scientific<<num1.top()<<endl;
                else
                    cout<<"DivByZero"<<endl;
                num1.pop();
                break;
            }
            cin>>y;
            if(p=='*'||p=='/')
            {
                x = num.top();
                num.pop();
                if(p == '*')
                    x *= y;
                else
                {
                    if(!y)
                        flag = 0;
                    x /= y;
                }
                num.push(x);
            }
            else
            {
                oper.push(p);
                num.push(y);
            }
        }
    }
    return 0;
}

/*
第一個版本派生出的,根據學長給我的這題的測試數據測試,沒有發現問題,也沒看出數組越界,不知道爲什麼一直說訪問了不該訪問的內存區域,心塞!!
*/

#include <bits/stdc++.h>
using namespace std;
double num[105];
char oper[50];
int main()
{
    char p;
    double n;
    while(cin>>n)
    {
        int k = 0,j = 0;
        num[k++] = n;
        double ans,y;
        int flag = 1;
        while(1)
        {
            p = getchar();
            if(p == '\n')
            {
                if(flag)
                {

                    ans = num[0];
                    for(int i = 0; i < j; i++)
                    {
                        oper[i] == '+' ? ans += num[i+1] : ans -= num[i+1];
                    }
                    cout<<scientific<<ans<<endl;
                }
                else
                    cout<<"DivByZero"<<endl;
                break;
            }
            cin>>y;
            if(p=='*'||p=='/')
            {
                if(p == '*')
                    num[k-1] *= y;
                else
                {
                    if(y == 0)
                        flag = 0;
                    if(flag)
                        num[k-1] /= y;
                }
            }
            else
            {
                oper[j++] = p;
                num[k++] = y;
            }
        }
    }
    return 0;
}

/*
第三個正規的STL中stack方法(未知 Why Wrong answer)
W A 沒關係,學到了東西就是王道!
*/

#include <bits/stdc++.h>
using namespace std;
double Num(string s, int &k)
{
    int flag = 0;
    double x = 0,y = 0.1;
    while((s[k] >= '0'&&s[k] <= '9')||s[k] == '.')
    {
        if(s[k] >= '0'&&s[k] <= '9')
        {
            if(!flag)
                x = x*10 + s[k] - '0';
            else
                x = x + y * (s[k] - '0');
        }
        else
            flag = 1;
        k++;
    }
    return x;
}
int priority(char c)
{
    int p = 0;
    switch(c)
    {
    case '*':
        p = 2;
        break;
    case '/':
        p = 2;
        break;
    case '+':
        p = 1;
        break;
    case '-':
        p = 1;
        break;
    }
    return p;
}
int main()
{
    stack<double>num;
    stack<char>oper;
    string s;
    while(cin>>s)
    {
        oper.push('\0');
        int k = 0,flagE = 1,flagD = 1;
        char c,op;
        double x ,y,z;
        while(flagE)
        {
            c = s[k];
            if((c >= '0'&&c <= '9')||c == '.')
            {
                num.push(Num(s,k));
            }
            else if(c == '\0'&&oper.top() == '\0')
            {
                flagE = 0;
            }
            else if(priority(c) > priority(oper.top()))
            {
                oper.push(c);
                k++;
            }
            else if(priority(c) <=  priority(oper.top()))
            {
                x = num.top();
                num.pop();
                y = num.top();
                if(!x) flagD = 0;
                num.pop();
                op = oper.top();
                oper.pop();
                switch(op)
                {
                case '+':
                    z = x + y;
                    break;
                case '-':
                    z = y - x;
                    break;
                case '*':
                    z = x * y;
                    break;
                case '/':
                    z = y / x;
                    break;
                }
                num.push(z);
            }
        }
        if(flagD)
            cout<<scientific<<num.top()<<endl;
        else
      

      cout<<"DivByZero"<<endl;
        num.pop();
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章