-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
87 lines (77 loc) · 2.55 KB
/
Copy pathmemory.cpp
File metadata and controls
87 lines (77 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <array>
#include <cctype>
using namespace std;
class Memory {
public:
static const int size = 256;
array<string, size> mem;
Memory() {
for (int i = 0; i < size; i++) {
mem[i] = "xy"; // Initialize memory cells to "xy"
}
}
string getcell(int address) {
if (address < 0 || address >= size) { // Corrected bounds check
cout << "Out of bounds. ";
return "00"; // Return a default value for out of bounds
} else {
return mem[address];
}
}
void setcell(int address, const string& value) {
bool flag = true; // Reset flag for each call
// Validate hex input
for (char c : value) {
if (!isxdigit(c)) { // Check if character is not a valid hex digit
flag = false;
break;
}
}
// Store valid two-character hex value
if (value.size() == 2 && flag) {
if (address >= 0 && address < size) {
mem[address] = value;
} else {
cout << "Address out of bounds." << endl;
}
return;
}
// Handle values longer than 2 characters
if (value.length() > 2) {
for (size_t i = 0; i < value.length(); i += 2) {
if (i + 1 < value.length()) {
string pair = value.substr(i, 2);
if (address >= 0 && address < size) {
mem[address++] = pair; // Store the pair in memory
} else {
cout << "Out of bounds for address." << endl;
return;
}
}
}
// Handle any remaining single character
if (value.length() % 2 != 0 && address < size) {
string remaining(1, value.back());
mem[address++] = remaining; // Store remaining character
}
} else if (!flag) {
cout << "Invalid hex input." << endl;
}
}
};
int main() {
Memory m;
m.setcell(0, "aa");
cout << m.getcell(0) << endl;
m.setcell(200, "abc");
cout << m.getcell(200) << endl;
cout << m.getcell(201) << endl;
m.setcell(150, "bb");
cout << m.getcell(300) << endl;
m.setcell(100, "2ab4");
cout << m.getcell(100) << endl;
cout << m.getcell(101) << endl;
return 0;
}