View Code of Problem 3689

#include <stdio.h>
#include <string.h> 
struct bigInt{
	int d[1000];
	int len;
};
bigInt change(char str[]){
	bigInt bI;
	bI.len=strlen(str);
	for(int i=0;i<1000;i++){
		bI.d[i]=0;
	}
	for(int i=0;i<bI.len;i++){
		bI.d[i] = str[bI.len-i-1]-'0';
	}
	return bI;
}
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];
		c.len++;
	}
	while(c.len-1>=1&&c.d[c.len-1]==0)c.len--;
	return c;
}
int main(int argc, char *argv[])
{
	int t;
	scanf("%d",&t) ;
	for(int i=1;i<=t;i++){
		char stra[100];
		char strb[100];
		scanf("%s",stra);
		scanf("%s",strb);
		bigInt a = change(stra);
		bigInt b = change(strb);
		bigInt c;
		printf("Case #%d:\n",i);
		if(a.len>b.len||(a.len==b.len&&a.d[a.len-1]>b.d[b.len-1])){
			 c = sub(a,b);
			
		}else{
			 c = sub(b,a);
			printf("-");
		}
		
		for(int i=c.len-1;i>=0;i--){
			printf("%d",c.d[i]);
		}
		printf("\n");
		 
	}	
	return 0;
}
/*
Main.c:2:20: warning: extra tokens at end of #include directive
 #include <string.h> 
                    ^
Main.c:7:1: error: unknown type name 'bigInt'; use 'struct' keyword to refer to the type
 bigInt change(char str[]){
 ^~~~~~
 struct 
Main.c: In function 'change':
Main.c:8:2: error: unknown type name 'bigInt'; use 'struct' keyword to refer to the type
  bigInt bI;
  ^~~~~~
  struct 
Main.c:9:4: error: request for member 'len' in something not a structure or union
  bI.len=strlen(str);
    ^
Main.c:11:5: error: request for member 'd' in something not a structure or union
   bI.d[i]=0;
     ^
Main.c:13:18: error: request for member 'len' in something not a structure or union
  for(int i=0;i<bI.len;i++){
                  ^
Main.c:14:5: error: request for member 'd' in something not a structure or union
   bI.d[i] = str[bI.len-i-1]-'0';
     ^
Main.c:14:19: error: request for member 'len' in something not a structure or union
   bI.d[i] = str[bI.len-i-1]-'0';
                   ^
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:21: error: unknown type name 'bigInt'; did you mean 'int'?
 bigInt sub(bigInt a,bigInt b){
                     ^~~~~~
                     int
Main.c: In function 'main':
Main.c:41:3: error: unknown type name 'bigInt'; use 'struct' keyword to refer to the type
   bigInt a = change(stra);
   ^~~~~~
   struct 
Main.c:42:3: error: unknown type name 'bigInt'; use 'struct' keyword to refer to the type
   bigInt b = change(strb);
   ^~~~~~
   struct 
Main.c:43:3: error: unknown type name 'bigInt'; use 'struct' keyword to refer to the type
   bigInt c;
   ^~~~~~
   struct 
Main.c:45:7: error: request for member 'len' in something not a structure or union
   if(a.len>b.len||(a.len==b.len&&a.d[a.len-1]>b.d[b.len-1])){
       ^
Main.c:45:13: error: request for member 'len' in something not a structure or union
   if(a.len>b.len||(a.len==b.len&&a.d[a.len-1]>b.d[b.len-1])){
             ^
Main.c:45:21: error: request for member 'len' in something not a structure or union
   if(a.len>b.len||(a.len==b.len&&a.d[a.len-1]>b.d[b.len-1])){
                     ^
Main.c:45:28: error: request for member 'len' in something not a structure or union
   if(a.len>b.len||(a.len==b.len&&a.d[a.len-1]>b.d[b.len-1])){
                            ^
Main.c:45:35: error: request for member 'd' in something not a structure or union
   if(a.len>b.len||(a.len==b.len&&a.d[a.len-1]>b.d[b.len-1])){
                                   ^
Main.c:45:39: error: request for member 'len' in something not a structure or union
   if(a.len>b.len||(a.len==b.len&&a.d[a.len-1]>b.d[b.len-1])){
                                       ^
Main.c:45:48: error: request for member 'd' in something not a structure or union
   if(a.len>b.len||(a.len==b.len&&a.d[a.len-1]>b.d[b.len-1])){
                                                ^
Main.c:45:52: error: request for member 'len' in something not a structure or union
   if(a.len>b.len||(a.len==b.len&&a.d[a.len-1]>b.d[b.len-1])){
                                                    ^
Main.c:46:9: warning: implicit declaration of function 'sub' [-Wimplicit-function-declaration]
     c = sub(a,b);
         ^~~
Main.c:53:14: error: request for member 'len' in something not a structure or union
   for(int i=c.len-1;i>=0;i--){
              ^
Main.c:54:17: error: request for member 'd' in something not a structure or union
    printf("%d",c.d[i]);
                 ^
Main.c:43:10: warning: variable 'c' set but not used [-Wunused-but-set-variable]
   bigInt c;
          ^
*/

Double click to view unformatted code.


Back to problem 3689