View Code of Problem 3689

#include<stdio.h>
#include<string.h>
struct bigint {
	int d[1000];
	int len;
};
bigint change(char a[]) {
	bigint c;
	c.len = 0;
	for (int i = 0; i < 1000; i++) {
		c.d[i] = 0;
	}
	for (int i = 0; i < strlen(a); i++) {
		c.d[c.len++] = a[strlen(a) - i - 1];
	}
	return c;
}
bigint sub(bigint a, bigint b) {
	bigint c;
	c.len = 0;
	for (int i = 0; i < a.len || i < b.len; i++) {
		if (a.d[i] < b.d[i]) {
			a.d[i + 1]--;
			a.d[i] += 10;
		}
		c.d[c.len++] = a.d[i] - b.d[i];
	}
	while (c.len - 1 >= 1 && c.d[c.len-1] == 0) {
		c.len--;
	}
	return c;
}
int main() {
	int t;
	scanf("%d", &t);
	for(int k=1;k<=t;k++){	
		char a[101], b[101],s[100];
		scanf("%s", a);
		scanf("%s", b);
		printf("Case #%d:\n", k);
		if (strcmp(a, b) < 0) {
			strcpy(s, a);
			strcpy(a, b);
			strcpy(b, s);
			printf("-");
		}
		bigint x, y;
		x = change(a);
		y = change(b);
		bigint c = sub(x, y);
		for (int i = c.len - 1; i >= 0; i--) {
			printf("%d", c.d[i]);
		}
		printf("\n");
	}
}
/*
Main.c:7:1: error: unknown type name 'bigint'; use 'struct' keyword to refer to the type
 bigint change(char a[]) {
 ^~~~~~
 struct 
Main.c: In function 'change':
Main.c:8:2: error: unknown type name 'bigint'; use 'struct' keyword to refer to the type
  bigint c;
  ^~~~~~
  struct 
Main.c:9:3: error: request for member 'len' in something not a structure or union
  c.len = 0;
   ^
Main.c:11:4: error: request for member 'd' in something not a structure or union
   c.d[i] = 0;
    ^
Main.c:14:4: error: request for member 'd' in something not a structure or union
   c.d[c.len++] = a[strlen(a) - i - 1];
    ^
Main.c:14:8: error: request for member 'len' in something not a structure or union
   c.d[c.len++] = a[strlen(a) - i - 1];
        ^
Main.c: At top level:
Main.c:18:1: error: unknown type name 'bigint'; use 'struct' keyword to refer to the type
 bigint sub(bigint a, bigint b) {
 ^~~~~~
 struct 
Main.c:18:12: error: unknown type name 'bigint'; did you mean 'int'?
 bigint sub(bigint a, bigint b) {
            ^~~~~~
            int
Main.c:18:22: error: unknown type name 'bigint'; did you mean 'int'?
 bigint sub(bigint a, bigint b) {
                      ^~~~~~
                      int
Main.c: In function 'main':
Main.c:47:3: error: unknown type name 'bigint'; use 'struct' keyword to refer to the type
   bigint x, y;
   ^~~~~~
   struct 
Main.c:50:3: error: unknown type name 'bigint'; use 'struct' keyword to refer to the type
   bigint c = sub(x, y);
   ^~~~~~
   struct 
Main.c:50:14: warning: implicit declaration of function 'sub' [-Wimplicit-function-declaration]
   bigint c = sub(x, y);
              ^~~
Main.c:51:17: error: request for member 'len' in something not a structure or union
   for (int i = c.len - 1; i >= 0; i--) {
                 ^
Main.c:52:18: error: request for member 'd' in something not a structure or union
    printf("%d", c.d[i]);
                  ^
Main.c:50:10: warning: variable 'c' set but not used [-Wunused-but-set-variable]
   bigint c = sub(x, y);
          ^
*/

Double click to view unformatted code.


Back to problem 3689