-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
170 lines (147 loc) · 5.07 KB
/
main.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Benjamin White
// CS210 Project Three
// 20220215
#include <Python.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
#include <filesystem>
#define NOMINMAX
#undef NOMINMAX
#define RESET "\033[0m"
#define BOLDBLUE "\033[1m\033[34m"
using namespace std;
void CallProcedure(string pName)
{
char *procname = new char[pName.length() + 1];
std::strcpy(procname, pName.c_str());
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("import os");
PyRun_SimpleString("sys.path.append(os.getcwd())");
PyObject* my_module = PyImport_ImportModule("python_modules.PythonCode");
PyErr_Print();
PyObject* my_function = PyObject_GetAttrString(my_module, procname);
PyObject* my_result = PyObject_CallObject(my_function, NULL);
Py_Finalize();
delete[] procname;
}
int callIntFunc(string proc, string param)
{
char *procname = new char[proc.length() + 1];
std::strcpy(procname, proc.c_str());
char *paramval = new char[param.length() + 1];
std::strcpy(paramval, param.c_str());
PyObject *pName, *pModule, *pDict, *pFunc, *pValue = nullptr, *presult = nullptr;
// Initialize the Python Interpreter
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("import os");
PyRun_SimpleString("sys.path.append(os.getcwd())");
// Build the name object
pName = PyUnicode_FromString((char*)"python_modules.PythonCode");
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, procname);
if (PyCallable_Check(pFunc))
{
pValue = Py_BuildValue("(z)", paramval);
PyErr_Print();
presult = PyObject_CallObject(pFunc, pValue);
PyErr_Print();
}
else
{
PyErr_Print();
}
//printf("Result is %d\n", _PyLong_AsInt(presult));
Py_DECREF(pValue);
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
// clean
delete[] procname;
delete[] paramval;
return _PyLong_AsInt(presult);
}
int menu() {
// declaring and initializing variables for use in loop
int userChoice = 0;
int itemQuantity = 0;
string inputFilePath, userItem, itemName;
ifstream inputFile;
// Main Loop
while (userChoice != 4) { // loop while user has not entered 4
cout << endl;
// print menu
cout << "[1] Calculate the number of times each item appears" << endl;
cout << "[2] Calculate the frequency of a specific item" << endl;
cout << "[3] Create a histogram based on appearances of each item" << endl;
cout << "[4] Quit" << endl;
cout << endl;
cin >> userChoice; // getting user input
while (cin.fail()) { // if user input is invalid
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please input a valid selection: ";
cin >> userChoice; // get new user input
}
// switch statement to evaluate user input and call appropriate Python function
switch (userChoice) {
case 1:
CallProcedure("PrintList"); // calling Python function
cout << endl;
break;
case 2:
cout << endl;
cout << "Enter an item: \n";
cin >> userItem; // getting user input
cout << endl;
callIntFunc("PrintQuantity", userItem); // calling Python function
cout << endl;
break;
case 3:
CallProcedure("MakeList"); // calling Python function
cout << endl;
// read python file
inputFile.open("frequency.dat");
inputFile >> itemName; // store item name and quantity from frequency.dat separately
inputFile >> itemQuantity;
while (!inputFile.fail()) { // while frequency.dat is found
//Print item name and format for histogram
cout << setw(14) << left << itemName << setw(6);
//Print number of asterisks as item quantity
for (int i = 0; i < itemQuantity; i++) {
// format astericks
cout << right << BOLDBLUE << "*";
cout << RESET;
}
// new line + get next name and quantity
cout << endl;
inputFile >> itemName;
inputFile >> itemQuantity;
}
break;
case 4:
cout << endl;
cout << "Goodbye!" << endl; // exit loop if user enters 4
break;
default:
cout << "Please input a valid selection." << endl; // correct user input
break;
}
}
return 0;
}
int main()
{
menu();
return 0;
}