Upload files to "/"
This commit is contained in:
commit
6fd7cd476c
51
4.31 tempat terbatas MUHAMMAD RAFI GHAZI AL QARY.cpp
Normal file
51
4.31 tempat terbatas MUHAMMAD RAFI GHAZI AL QARY.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Fungsi untuk melakukan operasi sesuai operator
|
||||
int operasi(int a, char op, int b) {
|
||||
switch (op) {
|
||||
case '+': return a + b;
|
||||
case '-': return a - b;
|
||||
case '*': return a * b;
|
||||
default: return 0; // Jika operator tidak dikenal
|
||||
}
|
||||
}
|
||||
|
||||
// Fungsi untuk memproses satu baris ekspresi
|
||||
int prosesEkspresi(const string& ekspresi) {
|
||||
stringstream ss(ekspresi);
|
||||
vector<string> tokens;
|
||||
string token;
|
||||
|
||||
while (ss >> token) {
|
||||
tokens.push_back(token);
|
||||
}
|
||||
|
||||
int hasil = stoi(tokens[0]);
|
||||
for (size_t i = 1; i < tokens.size(); i += 2) {
|
||||
char op = tokens[i][0];
|
||||
int angka = stoi(tokens[i + 1]);
|
||||
hasil = operasi(hasil, op, angka);
|
||||
}
|
||||
|
||||
return abs(hasil);
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<string> soal = {
|
||||
"4 + 2 - 0 * 0",
|
||||
"1 + 1 + 1 + 1",
|
||||
"10 - 10 * 100 - 5"
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < soal.size(); ++i) {
|
||||
int hasil = prosesEkspresi(soal[i]);
|
||||
cout << "h#" << i + 1 << ": " << hasil << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user