fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. string kalkulator(char op, vector<string> data) {
  7. if (op == '+') {
  8. long long total = 0;
  9. for (string s : data) {
  10. total += stoll(s); // Ubah string ke long long, lalu jumlahkan
  11. }
  12. return to_string(total); // Ubah kembali ke string sebelum dikembalikan
  13. }
  14. else if (op == '*') {
  15. long long total = 1; // Perkalian dimulai dari 1
  16. for (string s : data) {
  17. total *= stoll(s); // Ubah string ke long long, lalu kalikan
  18. }
  19. return to_string(total); // Ubah kembali ke string sebelum dikembalikan
  20. }
  21. else {
  22. return "KESALAHAN";
  23. }
  24. }
  25.  
  26. // Fungsi main() di bawah tidak boleh diubah!
  27. int main() {
  28. cout << kalkulator('+', {"1234567890", "0", "987654321", "314159265"}) << endl;
  29. cout << kalkulator('+', {"123", "456", "789", "111"}) << endl;
  30. cout << kalkulator('*', {"123", "456", "789", "111"}) << endl;
  31. cout << kalkulator('*', {"17", "8", "1945"}) << endl;
  32. cout << kalkulator('?', {"3", "2", "1"}) << endl;
  33. }
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
2536381476
1479
4912130952
264520
KESALAHAN