Welcome, guest | Sign In | My Account | Store | Cart

A simple calculator that works with whole numbers written in C/Python.

The purpose of this implementation is only to show how a simple extension C/Python that can help people who are starting with C/Python.

In this cookbook show the two codes in C, the first is the pure C and the second is the implementation for Python.

I hope this cookbook possar help.

-- Until more and good luck, Nycholas de Oliveira and Oliveira.

Python, 186 lines
  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
/** START - First code written in C (calc.c). **/
/**
 *  A simple calculator that works with whole numbers written in C.
 *  Copyright (C) 2008 by Nycholas de Oliveira e Oliveira <nycholas@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301, USA.
 */

#include <stdlib.h>
#include <stdio.h>

int calc_sum(int, int);
int calc_subtract(int, int);
int calc_multiplies(int, int);
int calc_divides(int, int);

int main(void) {
    int a = 6;
    int b = 2;
    printf(" a = %d; b = %d;\n", a, b);
    printf("==============\n\n");
    printf(" + sum: %d\n", calc_sum(a, b));
    printf(" + subtract: %d\n", calc_subtract(a, b));
    printf(" + multiplies: %d\n", calc_multiplies(a, b));
    printf(" + divides: %d\n", calc_divides(a, b));
    return 0;
}

int calc_sum(int a, int b) {
    return a + b;
}

int calc_subtract(int a, int b) {
    return a - b;
}

int calc_multiplies(int a, int b) {
    return a * b;
}

int calc_divides(int a, int b) {
    return a / b;
}
/** END - First code written in C (calc.c). **/

/** START - According code written in C / Python (pycalc.c). **/
/**
 *  A simple calculator that works with whole numbers written in C/Python.
 *  Copyright (C) 2008 by Nycholas de Oliveira e Oliveira <nycholas@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301, USA.
 */

#include <stdlib.h>
#include <python2.5/Python.h>

static PyObject *pycalc_sum(PyObject *self, PyObject *args) {
    int a, b;
    if (!PyArg_ParseTuple(args, "ii", &a, &b))
        return NULL;
    return Py_BuildValue("i", (a + b));
}

static PyObject *pycalc_subtract(PyObject *self, PyObject *args) {
    int a, b;
    if (!PyArg_ParseTuple(args, "ii", &a, &b))
        return NULL;
    return Py_BuildValue("i", (a - b));
}

static PyObject *pycalc_multiplies(PyObject *self, PyObject *args) {
    int a, b;
    if (!PyArg_ParseTuple(args, "ii", &a, &b))
        return NULL;
    return Py_BuildValue("i", (a * b));
}

static PyObject *pycalc_divides(PyObject *self, PyObject *args) {
    int a, b;
    if (!PyArg_ParseTuple(args, "ii", &a, &b))
        return NULL;
    return Py_BuildValue("i", (a / b));
}

static PyMethodDef pycalc_methods[] = {
    {"sum", pycalc_sum, METH_VARARGS, "Sum two integers."},
    {"subtract", pycalc_subtract, METH_VARARGS, "Subtracts two integers."},
    {"multiplies", pycalc_multiplies, METH_VARARGS, "Subtracts two integers."},
    {"divides", pycalc_divides, METH_VARARGS, "Divide two integers."},
    {NULL, NULL, 0, NULL}
};

void initpycalc(void) {
    PyObject *m;
    m = Py_InitModule("pycalc", pycalc_methods);
    if (m == NULL)
        return;
}

int main(int argc, char *argv[]) {
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    initpycalc();
    return 0;
}
/** END - According code written in C / Python (pycalc.c). **/

## START - Code setup.py. ##
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#   Script `setup` the install/build Simple calculator.
#   Copyright (C) 2008 by Nycholas de Oliveira e Oliveira <nycholas@gmail.com>
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; either version 2
#   of the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#   MA  02110-1301, USA.
#
"""A simple calculator that works with whole numbers written in C / Python."""

from distutils.core import setup, Extension

def run():
    setup(name='pycalc',
          version='0.1',
          author='Nycholas de Oliveira e Oliveira',
          author_email='nycholas@gmail.com',
          license='GNU General Public License (GPL)',
          description="""A simple calculator that works with """
                      """whole numbers written in C/Python.""",
          platforms=['Many'],
          ext_modules=[Extension('pycalc', sources = ['pycalc.c'])]
    )

# Commands:
#
# ++ clean up temporary files from 'build' command
# ./setup.py clean -a
#
# ++ build everything needed to install
# ./setup.py build
#
# ++ install everything from build directory
# ./setup.py install -c -O2
#

if __name__ == "__main__":
    run()
## END - Code setup.py. ##

3 comments

Nikolay Dyankov 11 years, 9 months ago  # | flag

I tested this and it works. However, I don't quite understand how it happens to give any results at all since there are no explicit invocations to functions in calc.c from methods in pycalc.c. Maybe this is something that the Python import mechanism handles...

Nikolay Dyankov 11 years, 9 months ago  # | flag

It appears, you're not using calc.c in any way... I guess this wasn't the original intent of the example.

:D, Actually, in this example calc.c for mere illustration.

Created by Nycholas Oliveira e Oliveira on Fri, 16 May 2008 (PSF)
Python recipes (4591)
Nycholas Oliveira e Oliveira's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks