Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

How do I sum two integers in C?

Name: Anonymous 2010-05-01 17:42

subject.

Name: Anonymous 2010-05-01 17:46

+

next

Name: Anonymous 2010-05-01 17:46

>>1
You do it very carefully.

Name: Anonymous 2010-05-01 17:47

Forget it, it's NP Complete.

Name: Anonymous 2010-05-01 17:49

OP here.


#include <errno.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#define sum(x,y) ((x)+(y))

static void has_error(const long val)
{
    if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
        || (errno != 0 && val == 0)) {
        perror("strtol");
        exit(EXIT_FAILURE);
    }
}

static void digits_test(const char *const arg)
{
    if (arg == NULL) {
        fprintf(stderr, "No digits were found\n");
        exit(EXIT_FAILURE);
    }
}

int main(const int argc, const char *const *const argv)
{
    if (argc != 3) {
        fprintf(stderr, "Usage: %s integer integer\n",
            (argv[0] == NULL) ? "sum" : argv[0]);
        exit(EXIT_FAILURE);
    }
    errno = 0;
    long val = strtol(argv[1], NULL, 10);
    has_error(val);
    digits_test(argv[1]);
    int32_t a = val;
    val = strtol(argv[2], NULL, 10);
    has_error(val);
    digits_test(argv[2]);
    int32_t b = val;
    printf("sum = %" PRId64 "\n", (int64_t) sum(a, b));
    return EXIT_SUCCESS;
}

Name: Anonymous 2010-05-01 17:49

I dont know about C, but in Java, you should use a Sum class created via a SumFactory

Name: Anonymous 2010-05-01 21:44

I am sorry but this feature is not currently part of the C standard, but our plan to implement it soon. We apologize for this inconvenience.

Name: Anonymous 2010-05-01 22:52

Leah Culver quality:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int sum(int a, int b)
{
    int na, nb, nm;
    char *ba, *bb, *br;
    int i, v, r;
    char carry = 0;
    if (~(a ^ (~a)) == a) return b;
    if (!b) return a;
    na = (int)log10(a) + 1;
    nb = (int)log10(b) + 1;
    nm = na > nb ? na : nb;
    ba = malloc(nm + 1);
    bb = malloc(nm + 1);
    br = malloc(nm + 2);
    itoa(a, ba + (nm - na), 10);
    itoa(b, bb + (nm - nb), 10);
    for (i = 0; i < nm - na; ++i) ba[i] = 0x30;
    for (i = 0; i < nm - nb; ++i) bb[i] = 060;
    for (i = 0; i <= nm; ++i) br[i] = 48;
    br[nm - 1] = '\0';
    for (i = nm; i > 0; --i)
    {
        v = (ba[i - 1] + bb[i - 1] + carry) - '0';
        carry = v > 071;
        if (carry) v -= 10;
        br[i] = v;
    }
    br[0] = carry + '0';
    r = atoi(br);
    free(ba);
    free(bb);
    free(br);
    return r;
}


Extending the implementation to support negative integers and call itself recursively is left as an exercise to the reader.

Name: Anonymous 2010-05-02 0:11

SMUG C++ WEENIE quality:
#include <stdlib.h>
#include <stdarg.h>

typedef struct CContainer_VTable_s {
    void *(*foldr)(void*, void*, void *(*)(void*, void*));
    void *(*foldr1)(void*, void *(*)(void*, void*));
    int (*count)(void*);
} CContainer_VTable;

typedef struct CContainer_s {
    CContainer_VTable *vtable;
} CContainer;

#define DEFLL(t) \
struct LinkedList_s$##t { \
    CContainer super; \
    t value; \
    struct LinkedList_s$##t *next; \
}; \
typedef struct LinkedList_s$##t LinkedList$##t; \
\
CContainer_VTable VTABLE_LinkedList$##t; \
\
int LinkedList$##t_count(void *p) \
{ \
    LinkedList$##t *w = p; \
    return w ? 1 + LinkedList$##t_count(w->next) : 0; \
} \
\
void *LinkedList$##t_foldr(void *p, void *z, void *(*f)(void*, void*)) \
{ \
    LinkedList$##t *w = p; \
    return w ? (*f)(&w->value, LinkedList$##t_foldr(w->next, z, f)) : z; \
} \
void *LinkedList$##t_foldr1(void *p, void *(*f)(void*, void*)) \
{ \
    LinkedList$##t *w = p; \
    return LinkedList$##t_foldr(w->next, &w->value, f); \
} \
\
LinkedList$##t *vnew_LinkedList$##t(int count, va_list args) \
{ \
    LinkedList$##t *w = NULL; \
    if (count) \
    { \
        w = malloc(sizeof(LinkedList$##t)); \
        w->super.vtable = &VTABLE_LinkedList$##t; \
        w->value = va_arg(args, t); \
        w->next = vnew_LinkedList$##t(count - 1, args); \
    } \
    return w; \
} \
LinkedList$##t *new_LinkedList$##t(int count, ...) \
{ \
    va_list args; \
    LinkedList$##t *w = NULL; \
    va_start(args, count); \
    w = vnew_LinkedList$##t(count, args); \
    va_end(args); \
    return w; \
} \
void delete_LinkedList$##t(LinkedList$##t *w) \
{ \
    if (w) \
    { \
        delete_LinkedList$##t(w->next); \
        free(w); \
    } \
}

#define INITLL(t) do { \
VTABLE_LinkedList$##t.foldr = &LinkedList$##t_foldr; \
VTABLE_LinkedList$##t.foldr1 = &LinkedList$##t_foldr1; \
VTABLE_LinkedList$##t.count = &LinkedList$##t_count; \
} while (0)

#define LL(t) LinkedList$##t
#define NEWLL(t) new_LinkedList$##t
#define DELETELL(t) delete_LinkedList$##t

DEFLL(int);

void *add(void *p, void *q)
{
    int *a = p, *b = q, *r = malloc(sizeof(int));
    *r = *a + *b;
    return r;
}

int sum(int a, int b)
{
    int *r;
    LL(int) *l = NEWLL(int)(2, a, b);
    r = l->super.vtable->foldr1(l, &add);
    DELETELL(int)(l);
    return *r;
}


Note that you have to call INITLL(int); somewhere before calling NEWLL(int)(...);. Implementing an add which doesn't leak memory is left as an exercise to the reader.

Name: Anonymous 2010-05-02 0:20

>>9
Heh, I've writen some similar crap myself once, it's never pretty, but at least it can be useful.


If I were to solve OP's "problem" in a nice way, I'd probably try to write a verilog or vhdl or maybe raw schematics and compile it to NAND gates, then write a very tiny emu which emulates the circuits. Alas, I'm a bit lazy to do this now, but it'd probably be the most obfuscated example yet.

Name: Anonymous 2010-05-02 0:23

#include <main.cpp>
#include <vector>
using gayspace.exe

int main()
{
  cout << x ++ y == z;
  cin >> z;
  return -1;
}

Name: Anonymous 2010-05-02 5:41

Perhaps now we can discuss how to multiply two numbers:
http://en.wikipedia.org/wiki/Karatsuba_algorithm#Algorithm

Name: Anonymous 2010-05-02 5:52

>>9
This is some excellent use of the cpp, well done sir

Name: Anonymous 2010-05-02 7:21

Recursive functions implementing Peano Arithmetic.

Gotta start from the basics.

Name: Anonymous 2010-05-02 18:30

>>8
int + int = long int

fail.

Name: Anonymous 2010-05-02 19:02

mpz_add

Name: Anonymous 2010-05-02 19:10

CURSES

Name: Anonymous 2010-05-02 19:11

[code]#include <stdio.h>
using namespace std;

int add(int x, int y)
{
  while (y>0)
  {
    x++
    y--;
  }

  return x;
}

int main()
{
  cin >> x;
  cin >> y;
  cout << add(x,y);

  return 0;
}

Name: Anonymous 2010-05-02 19:12



    #include <stdio.h>
    using namespace std;

    int add(int x, int y)
    {
      while (y>0)
      {
        x++
        y--;
      }

      return x;
    }

    int main()
    {
      cin >> x;
      cin >> y;
      cout << add(x,y);

      return 0;
    }

Name: Anonymous 2010-05-02 19:17

[aa]

[code]#include <stdio.h>
using namespace std;

int add(int x, int y)
{
  while (y>0)
  {
    x++
    y--;
  }

  return x;
}

int main()
{
  cin >> x;
  cin >> y;
  cout << add(x,y);

  return 0;
}

[/aa]

Name: Anonymous 2010-05-02 19:49

>>9
Implying that smug C++ weenies use #define

Name: Anonymous 2010-05-02 19:59

>>21
Actually, they use nothing BUT #define. At times they may use static inline, but that's it.

Name: Anonymous 2010-05-02 20:53

>>22
Smug C++ weenies use templates for things like compile-time factorials and other such useless esoterica.

Name: Anonymous 2012-07-15 4:06

int a = 5;
int b = 6;

int c = a + b;

Name: Anonymous 2012-07-15 4:13

>>9
Ahaha. What a story Mark.

Name: Anonymous 2012-07-15 4:13

unsigned add(unsigned x, unsigned y) {
     return x == 0 ? y : add(--x, ++y);
}

Name: Anonymous 2012-07-15 15:06

>>22
No, C++ aims to reduce the use of the preprocessor. It's only C niggers that still use it.

Name: Anonymous 2012-07-15 15:30

int addition(int a, int b) {
    int x = a & b;
    do {
        a ^= b;
        b = x << 1;
        x = a & b;
    } while(x != 0);
    return a | b;
}

Name: Anonymous 2012-07-15 15:44

>>28
Yet they still use #includes and header files instead of a proper module system.

Name: Anonymous 2012-07-15 16:13

#include <limits.h>
int plus[INT_MAX-INT_MIN+1][INT_MAX-INT_MIN+1] = /* left as an exercise for the reader */;
int add(int a, int b) {
     return plus[a-INT_MIN][b-INT_MIN];
}

Name: VIPPER 2012-07-15 16:27

Im suprised nobody has referenced SICP yet.

Name: Anonymous 2012-07-15 17:58

>>32

AND THEN THIS FAGGOT CAME UP

Name: Anonymous 2012-07-15 18:01

Name     bitadd ;
PartNo   00 ;
Date      ;
Revision 01 ;
Designer  ;
Company   ;
Assembly None ;
Location  ;
Device   g22v10 ;
PIN 1 = a ;
PIN 2 = b ;
PIN 14 = t1 ;
PIN 15 = t22 ;
PIN 16 = xor ;
t1 = !a & b ;
t2 = a & !b ;
xor = term1 # term2 ;

Name: Anonymous 2012-07-15 18:04

lvalue = value1 + value2
lvaule += value2

Name: Anonymous 2012-07-15 18:06

int a, b, c;
c = a /* left as an exercise for the reader */ b;

Name: Anonymous 2012-07-15 19:24

>>9
$##t
HIBT

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List