-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
213 lines (183 loc) · 5.12 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <Python.h>
#include <iostream>
#include <Windows.h>
#include <cmath>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
/*
Description:
To call this function, simply pass the function name in Python that you wish to call.
Example:
callProcedure("printsomething");
Output:
Python will print on the screen: Hello from python!
Return:
None
*/
void CallProcedure(string pName)
{
char* procname = new char[pName.length() + 1];
std::strcpy(procname, pName.c_str());
Py_Initialize();
PyObject* my_module = PyImport_ImportModule("CS210_Starter_PY_Code");
PyErr_Print();
PyObject* my_function = PyObject_GetAttrString(my_module, procname);
PyObject* my_result = PyObject_CallObject(my_function, NULL);
Py_Finalize();
delete[] procname;
}
/*
Description:
To call this function, pass the name of the Python functino you wish to call and the string parameter you want to send
Example:
int x = callIntFunc("PrintMe","Test");
Output:
Python will print on the screen:
You sent me: Test
Return:
100 is returned to the C++
*/
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();
// Build the name object
pName = PyUnicode_FromString((char*)"CS210_Starter_PY_Code");
// 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);
}
/*
Description:
To call this function, pass the name of the Python functino you wish to call and the string parameter you want to send
Example:
int x = callIntFunc("doublevalue",5);
Return:
25 is returned to the C++
*/
int callIntFunc(string proc, int param)
{
char* procname = new char[proc.length() + 1];
std::strcpy(procname, proc.c_str());
PyObject* pName, * pModule, * pDict, * pFunc, * pValue = nullptr, * presult = nullptr;
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyUnicode_FromString((char*)"CS210_Starter_PY_Code");
// 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("(i)", param);
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;
return _PyLong_AsInt(presult);
}
int main()
{
string num1;
int choice;
string printH;
ifstream inFS("frequency.dat");
string itemName;
int itemQuantity;
do {
cout << "\n1: Create a list of all items purchased with quantities" << endl;
cout << "2: Create a number of how many times an item was purchased" << endl;
cout << "3: Create a histogram of all items purchased in a day" << endl;
cout << "Enter your selection as a number 1, 2, 3, or 4 to exit." << endl;
cin >> choice; // accepting user input
switch (choice)
{
case 1:
CallProcedure("ReadFile"); // pulling python function
break;
case 2:
cout << "Select an item from the list, please select following option for list of items" << endl;
cin >> num1; // user input
num1[0] = toupper(num1[0]); // fixes user input to have first letter capitalized
cout << callIntFunc("ItemCount", num1); // utilizes function and takes userinput
break;
case 3:
CallProcedure("Histogram"); // pulls from function
inFS.open("frequency.dat"); //Open frequency.dat
if (inFS.is_open()) // test condition to see file is open
{
cout << "File is good to read!\n press option three again" << endl;
while ((getline(inFS, itemName, ' ') && (inFS >> itemQuantity)))
{
cout << itemName << " "; // prints(myfile, first item line with space in between
for (int i = 0; i < itemQuantity; i++) // loop to get histogram
{
cout << "*";
}
cout << endl;
}
{
}
inFS.close(); // closing the file
break;
case 4:
cout << "Exiting program" << endl;
exit(3);
return 3; // exits program
default:
cout << "Invalid selection" << endl; // telling the user they selected a wrong input
break;
}
}
} while (choice > 0 || choice <= 3);
// validates user input
return 0;
}