Name: Anonymous 2010-05-01 17:42
subject.
#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;
}
#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;
}#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;
}INITLL(int); somewhere before calling NEWLL(int)(...);. Implementing an add which doesn't leak memory is left as an exercise to the reader.
#include <main.cpp>
#include <vector>
using gayspace.exe
int main()
{
cout << x ++ y == z;
cin >> z;
return -1;
}
mpz_add
#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;
}
unsigned add(unsigned x, unsigned y) {
return x == 0 ? y : add(--x, ++y);
}
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;
}
#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 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 ;
lvalue = value1 + value2
lvaule += value2
int a, b, c;
c = a /* left as an exercise for the reader */ b;