-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
81 lines (68 loc) · 2.72 KB
/
main.c
File metadata and controls
81 lines (68 loc) · 2.72 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
// --------------------------------------------------------
// ------------------------Headers:------------------------
//#include <Python.h>
#include "CYTHON_CODE.h" // Generado por Cython.
#include <stdio.h>
#include <string.h>
// --------------------------------------------------------
// Prototipo de la función externa
void solicitar_datos(char *name, int *a, int *b);
// --------------------------------------------------------
int main() {
Py_Initialize();
// Añadir el directorio actual al sys.path para asegurarse de que el módulo pueda ser encontrado
// PyObject *sysPath = PySys_GetObject("path");
// PyObject *path = PyUnicode_FromString(".");
// PyList_Append(sysPath, path);
// Py_DECREF(path);
// Solicitar datos al usuario
char name[100];
int a, b;
solicitar_datos(name, &a, &b);
// Cargar el módulo Cython
PyObject *pName = PyUnicode_FromString("CYTHON_CODE");
PyObject *pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
PyObject *pFunc = PyObject_GetAttrString(pModule, "cython_module");
if (pFunc && PyCallable_Check(pFunc)) {
// Crear una tupla para los argumentos de la función
PyObject *pArgs = PyTuple_New(3);
PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(name));
PyTuple_SetItem(pArgs, 1, PyLong_FromLong(a));
PyTuple_SetItem(pArgs, 2, PyLong_FromLong(b));
// Llamar a la función Cython con los argumentos
PyObject *pResult = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pResult != NULL) {
printf("La función se ejecutó correctamente.\n");
Py_DECREF(pResult);
} else {
PyErr_Print();
}
Py_DECREF(pFunc);
} else {
if (PyErr_Occurred()) PyErr_Print();
}
Py_DECREF(pModule);
} else {
PyErr_Print();
}
Py_Finalize();
return 0;
}
// --------------------------------------------------------
// Declaración de la función para solicitar datos
void solicitar_datos(char *name, int *a, int *b) {
printf("Ingrese su nombre: ");
fgets(name, 100, stdin); // Leer una línea de texto incluyendo espacios
name[strcspn(name, "\n")] = 0; // Eliminar el salto de línea
printf("Ingrese el primer número: ");
scanf("%d", a);
printf("Ingrese el segundo número: ");
scanf("%d", b);
// Limpiar el buffer de entrada (en caso de que queden caracteres extra)
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
}
// --------------------------------------------------------