Solutions of Calculator - MarisaOJ: Marisa Online Judge

Solutions of Calculator

Select solution language

Write solution here.


ducyn    Created at    1 likes

**Hint**: - double a, b — lưu 2 số thực. - char t — lưu ký tự phép toán (+, -, *, /). - double res — lưu kết quả tính toán. - bool c — kiểm tra xem phép toán hợp lệ không. ------- **Xử lý phép toán:** - Nếu t == '+' → cộng. - Nếu t == '-' → trừ. - Nếu t == '*' → nhân. - Nếu t == '/': - Nếu b == 0 → phép toán sai (c = false). - Ngược lại → chia. - Nếu t không thuộc 4 phép toán trên → phép toán sai (c = false) *Đừng quên fixed và setprecision(3) để làm tròn theo yêu cầu của để bài nhé!!* -------- Code tham khảo: ```cpp #include <bits/stdc++.h> #define ll long long #define name "TASK" #define TIME (1.0 * clock() / CLOCKS_PER_SEC) using namespace std; void solve(){ double a, b, res=0; char t; cin>>a>>t>>b; bool c=true; if(t == '+'){ res = a + b; } else if(t == '-'){ res = a - b; } else if(t == '*'){ res = a * b; } else if(t == '/') { if (b == 0) { c = false; } else{ res=a/b; } } else c=false; if(!c){ cout<<"ze"; } else{ cout<<fixed<<setprecision(3)<<res; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if(fopen(name".INP","r")) { freopen(name ".INP","r",stdin); freopen(name ".OUT","w",stdout); } solve(); cerr << '\n' << "Time collapsed: " << TIME << '\n'; return 0; } ```