static void test2(void)
{
    char a[] = "     010";
    char b[10] = {0};

    preprocess_str(a, b);
    printf("[%s], [%s]\n", a, b);
    printf("%d\n", myAtoi(a));
    printf("%d\n", myAtoi(b));
}
Пример #2
0
int main(int argc, char *argv[]) {

    char* strs[] = {
        "", "   ", // only contains space
        "123", "+123", "-123", // with positive and negtive symbol
        "-+123", "+-123", "--123", // with multiple +/- symbol
        "  123 ", "-12 3", " +123  ", // with space
        "123junk", "123abcdfg", // with junk character
        "2147483648", "-2147483649", // overflow
        "2147483647", "-2147483647", // not overflow
        "21474836300", "-21474836300" // another overflow
    };
    int results[] = {
        0, 0, // only space
        123, 123, -123, // with negtive symbol
        0, 0, 0, // with mutiple +/- symbol
        123, -12, 123, // skip prefix white space
        123, 123, // ignore junk letter
        2147483647, -2147483648, // overflow
        2147483647, -2147483647, // not overflow
        2147483647, -2147483648 //overflow
    };
    int count = sizeof(results)/sizeof(results[0]);
    int failed = 0;
    int result;
    int i;
    for (i = 0; i < count; i++) {
        result = myAtoi(strs[i]);
        if (result != results[i]) {
            failed++;
            printf("Test: %s, expect: %d, while returned: %d\n", strs[i], results[i], result);
        }
    }
    printf("Test %d cases, %d success, %d failed\n", count, (count-failed), failed);
}
Пример #3
0
int
main(int argc, char *argv[])
{
	char		string    [] = "-2147483648";
	printf("The atoi of %s is %d\n", string, myAtoi(string));
	return 0;
}
Пример #4
0
int cmd_settimer(int argc, char *argv[], FILE *ostrm) {
#else
int cmd_settimer(int argc, char *argv[]) {
#endif
    int res;
    int count;

    if (argc != 2) {
        res = efputs("usage: settimer [count]\r\n", ostrm);
        if (res == EOF) {
            return WRITE_ERROR;
        }
        return WRONG_NUMBER_ARGS;
    }

    count = myAtoi(argv[1]);
    if (count < 0 || count > 65535) {
        res = efputs("Duration must be between 0 and 65535\r\n", ostrm);
        if (res == EOF) {
            return WRITE_ERROR;
        }
        return INVALID_INPUT;
    }

    interrupt_fired = 0;
    svc_setTimer(count, my_timer_action);
    while(!interrupt_fired) {
        ;
    }

    efputs("Interrupt Fired\r\n", STDOUT);
    efflush(ostrm);
    return SUCCESS;
}
static void test4(void)
{
    char a[] = "    -12a48";

    printf("%d\n", myAtoi(a));
    printf("%d\n", atoi(a));
}
Пример #6
0
static int  parseEnum(char *text, ...)
{
va_list vlist;
char    *entries[64];
int     i, numEntries;

    va_start(vlist, text);
    for(i = 0; i < 64; i++){
        entries[i] = va_arg(vlist, char *);
        if(entries[i] == NULL)
            break;
    }
    numEntries = i;
    va_end(vlist);
    for(i = 0; i < numEntries; i++){
        if(strcasecmp(text, entries[i]) == 0)
            return i;
    }
    if(isdigit(*text)){
        return myAtoi(text);
    }
    fprintf(stderr, "Enum value \"%s\" not allowed. Allowed values are:\n", text);
    for(i = 0; i < numEntries; i++){
        fprintf(stderr, "  %s\n", entries[i]);
    }
    exit(1);
}
Пример #7
0
int main(int argc, const char * argv[])
{
    printf("%d\n",myAtoi("123"));//123
    printf("%d\n",myAtoi("-123"));//-123
    printf("%d\n",myAtoi("   -123"));//-123
    printf("%d\n",myAtoi("   -123a"));//-123
    printf("%d\n",myAtoi("-a123"));//0
    
    printf("%d\n",myAtoi2("123"));//123
    printf("%d\n",myAtoi2("-123"));//-123
    printf("%d\n",myAtoi2("   -123"));//-123
    printf("%d\n",myAtoi2("   -123a"));//-123
    printf("%d\n",myAtoi2("-a123"));//0
    
    return 0;
}
Пример #8
0
// Driver program to test above function
int main()
{
  char str[] = "-123";
  int val = myAtoi(str);
  printf ("%d ", val);
  return 0;
}
Пример #9
0
int main()
{
	char str[]="2147483648";
	int res = myAtoi(str);
	printf("%d\n",res);
	return 0;
}
Пример #10
0
int main()
{
    char s[1001];
    //scanf("%s", s);
    gets(s);
    int ans = myAtoi(s);
    printf("%d\n", ans);
}
Пример #11
0
int main(){
	char str[100];
	while(1){
		scanf("%s", str);
		printf("%d\n", myAtoi(str));		
	}
	return 0;
}
Пример #12
0
void test_atoi_not_good_string()
{
	clear_all();
	//not int value in string for atoi
	strcpy(a,"142c123");
	int x;
	int f = myAtoi(a,&x);
	assert(f==-1);
}
Пример #13
0
void test_atoi_empty()
{
	clear_all();
	//empty string 
	a[0]='\0';
	int x;
	int f=myAtoi(a,&x);
	assert(f==-1);
}
Пример #14
0
int main(int argc, const char *argv[])
{
	char* input="    +11191657170";
	int result=0;
	result=myAtoi(input);
	printf("result=''%d''\n", result);

	return 0;
}
Пример #15
0
int main() {
    char *s = malloc(256);
    printf("Enter your atoi input: ");
    scanf("%255s", s);

    int atoiNum = myAtoi(s);
    free(s);
    printf("%d is your atoi number\n", atoiNum);
    return 0;
}
Пример #16
0
int main(){
	//printf("space:%d\n",INT_MAX);
	char a[]="    -2929dblue";
	//char a[] = "2147483648";

	int ret = myAtoi(a);
	printf("ret:%d\n",ret);
	int rev = reverse(ret);
	printf("rev:%d\n",rev);
	puts("end of app");
}
Пример #17
0
void test_atoi_negative()
{
	clear_all();
	//negative int value in string for atoi
	a[0]='-';
	a[1]='2';
	int x;
	int f = myAtoi(a,&x);
	assert(x==-2);
	assert(f==0);
	//accepted
}
Пример #18
0
void test_atoi_positive()
{
	clear_all();
	//positive int value in string for atoi
	a[0]='+';
	a[1]='1';
	int x;
	int f = myAtoi(a,&x);
	assert(x==1);
	assert(f==0);
	//accepted
}
Пример #19
0
Файл: 8.c Проект: unasm/utils
int main(int argc, const char *argv[])
{
	//char test[] = "-23232";int expect = -23232;
	//char test[] = "     -23232";int expect = -23232;
	//char test[] = "23232";int expect = 23232;
	//char test[] = "-2147483649";int expect = -2147483648;
	//char test[] = "+-2";int expect = 0;
	//char test[] = " b11228552307";int expect = 0;
	char test[] = "9223372036854775809";int expect = 2147483647;
	//char test[] = "-2147483649";int expect = -2147483648;
	int ans;
	ans = myAtoi(test);
	if(ans == expect) {
		printf("yes\n");
	} else {
		printf("%d\n", ans);
	}
	return 0;
}
Пример #20
0
int
readlines(char *lineptr[], int maxlines)
{
	int len, nlines;
	char *p, line[MAXLEN];
	OPEN_FD();
	nlines = 0;
	while ((len = _getline(line, MAXLEN)) > 1)
		if (nlines >= MAXLINES )
		{
			CLOSE_FD();
			return nlines*(-1) - 1;
		}
		else
		if(!(p=malloc(len)))
		{
			CLOSE_FD();
			return nlines*(-1) - 1;
		}
		else {
			line[len - 1] = '\0';
			strcpy(p, line);
			int ans;
			int ff=myAtoi(p,&ans);
			if(numeric)
			{
				myItoa(ans,p);
			}
			if(numeric && ff==-1)
			{
				drop_error_string_format();
				CLOSE_FD();
				free(p);
				strcpy(line,"");
				return nlines*(-1) - 1;
			}
			lineptr[nlines++] = p;
		}
	CLOSE_FD();
	return nlines;
}
Пример #21
0
int main(int *argc, char *argv[])
{
    int ret = 0;

    /*ret = myAtoi("123");
    printf("%d\n", ret);

    ret = myAtoi("-123");
    printf("%d\n", ret);

    ret = myAtoi("0");
    printf("%d\n", ret);

    ret = myAtoi("-0");
    printf("%d\n", ret);

    ret = myAtoi("-aaaaaaa0");
    printf("%d\n", ret);*/


    ret = myAtoi("010");
        printf("%d\n",ret);
}
Пример #22
0
int main() {
  char a[100] = "G +9";

  int max_int = 0x7FFFFFFF;
  printf("max int = %d\n", max_int);

  unsigned char i = 1;
  for (i=1; i<128; i++) {
    a[0] = i;
    if (atoi(a) == 9) {
      printf("ans=%d\n", i);
    }
  }
  int x = atoi(a);

  /* strcpy(a, "2147483648");*/
  a[0] = '\0';
  x = atoi(a);
  printf("atoi: %d\n", x);
  x = myAtoi(a);
  printf("myAtoi: %d\n", x);
  return 0;
}
Пример #23
0
int _tmain(int argc, _TCHAR* argv[])
{
	printf("%d\n",myAtoi("+-4"));
	return 0;
}
Пример #24
0
int main(int argc, char **argv)
{
usb_dev_handle  *handle = NULL;
int             opt, len, action, argcnt;
char            *myName = argv[0], *s, *rxBuffer = NULL;
FILE            *fp;

    while((opt = getopt(argc, argv, "?hv:p:V:P:S:d:D:O:e:n:tbw")) != -1){
        switch(opt){
        case 'h':
        case '?':   /* -h or -? (print this help and exit) */
            usage(myName);
            exit(1);
        case 'v':   /* -v <vendor-id> (defaults to 0x%x, can be '*' for any VID) */
            vendorID = myAtoi(optarg);
            break;
        case 'p':   /* -p <product-id> (defaults to 0x%x, can be '*' for any PID) */
            productID = myAtoi(optarg);
            break;
        case 'V':   /* -V <vendor-name-pattern> (shell style matching, defaults to '*') */
            vendorNamePattern = optarg;
            break;
        case 'P':   /* -P <product-name-pattern> (shell style matching, defaults to '*') */
            productNamePattern = optarg;
            break;
        case 'S':   /* -S <serial-pattern> (shell style matching, defaults to '*') */
            serialPattern = optarg;
            break;
        case 'd':   /* -d <databytes> (data bytes for requests given on command line) */
            while((s = strtok(optarg, ", ")) != NULL){
                optarg = NULL;
                if(sendBytes != NULL){
                    sendBytes = realloc(sendBytes, sendByteCount + 1);
                }else{
                    sendBytes = malloc(sendByteCount + 1);
                }
                sendBytes[sendByteCount++] = myAtoi(s);
            }
            break;
        case 'D':   /* -D <file> (data bytes for request taken from file) */
            if((fp = fopen(optarg, "rb")) == NULL){
                fprintf(stderr, "error opening %s: %s\n", optarg, strerror(errno));
                exit(1);
            }
            fseek(fp, 0, SEEK_END);
            len = ftell(fp);
            fseek(fp, 0, SEEK_SET);
            if(sendBytes != NULL){
                sendBytes = realloc(sendBytes, sendByteCount + len);
            }else{
                sendBytes = malloc(sendByteCount + len);
            }
            fread(sendBytes + sendByteCount, 1, len, fp);   /* would need error checking */
            sendByteCount += len;
            fclose(fp);
            break;
        case 'O':   /* -O <file> (write received data bytes to file) */
            outputFile = optarg;
            break;
        case 'e':   /* -e <endpoint> (specify endpoint for some commands) */
            endpoint = myAtoi(optarg);
            break;
        case 't':   /* -t <timeout> (specify USB timeout in milliseconds) */
            usbTimeout = myAtoi(optarg);
            break;
        case 'b':   /* -b (binary output format, default is hex) */
            outputFormatIsBinary = 1;
            break;
        case 'n':   /* -n <count> (maximum number of bytes to receive) */
            usbCount = myAtoi(optarg);
            break;
        case 'c':   /* -c <configuration> (device configuration to choose) */
            usbConfiguration = myAtoi(optarg);
            break;
        case 'i':   /* -i <interface> (configuration interface to claim) */
            usbInterface = myAtoi(optarg);
            break;
        case 'w':   /* -w (suppress USB warnings, default is verbose) */
            showWarnings = 0;
            break;
        default:
            fprintf(stderr, "Option -%c unknown\n", opt);
            exit(1);
        }
    }
    argc -= optind;
    argv += optind;
    if(argc < 1){
        usage(myName);
        exit(1);
    }
    argcnt = 2;
    if(strcasecmp(argv[0], "list") == 0){
        action = ACTION_LIST;
        argcnt = 1;
    }else if(strcasecmp(argv[0], "control") == 0){
        action = ACTION_CONTROL;
        argcnt = 7;
    }else if(strcasecmp(argv[0], "interrupt") == 0){
        action = ACTION_INTERRUPT;
    }else if(strcasecmp(argv[0], "bulk") == 0){
        action = ACTION_BULK;
    }else{
        fprintf(stderr, "command %s not known\n", argv[0]);
        usage(myName);
        exit(1);
    }
    if(argc < argcnt){
        fprintf(stderr, "Not enough arguments.\n");
        usage(myName);
        exit(1);
    }
    if(argc > argcnt){
        fprintf(stderr, "Warning: only %d arguments expected, rest ignored.\n", argcnt);
    }
    usb_init();
    if(usbOpenDevice(&handle, vendorID, vendorNamePattern, productID, productNamePattern, serialPattern, action == ACTION_LIST ? stdout : NULL, showWarnings ? stderr : NULL) != 0){
        fprintf(stderr, "Could not find USB device with VID=0x%x PID=0x%x Vname=%s Pname=%s Serial=%s\n", vendorID, productID, vendorNamePattern, productNamePattern, serialPattern);
        exit(1);
    }
    if(action == ACTION_LIST)
        exit(0);                /* we've done what we were asked to do already */
    usbDirection = parseEnum(argv[1], "out", "in", NULL);
    if(usbDirection){   /* IN transfer */
        rxBuffer = malloc(usbCount);
    }
    if(action == ACTION_CONTROL){
        int requestType;
        usbType = parseEnum(argv[2], "standard", "class", "vendor", "reserved", NULL);
        usbRecipient = parseEnum(argv[3], "device", "interface", "endpoint", "other", NULL);
        usbRequest = myAtoi(argv[4]);
        usbValue = myAtoi(argv[5]);
        usbIndex = myAtoi(argv[6]);
        requestType = ((usbDirection & 1) << 7) | ((usbType & 3) << 5) | (usbRecipient & 0x1f);
        if(usbDirection){   /* IN transfer */
            len = usb_control_msg(handle, requestType, usbRequest, usbValue, usbIndex, rxBuffer, usbCount, usbTimeout);
        }else{              /* OUT transfer */
            len = usb_control_msg(handle, requestType, usbRequest, usbValue, usbIndex, sendBytes, sendByteCount, usbTimeout);
        }
    }else{  /* must be ACTION_INTERRUPT or ACTION_BULK */
        int retries = 1;
        if(usb_set_configuration(handle, usbConfiguration) && showWarnings){
            fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror());
        }
        /* now try to claim the interface and detach the kernel HID driver on
         * linux and other operating systems which support the call.
         */
        while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){
#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
            if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){
                fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror());
            }
#endif
        }
        if(len != 0 && showWarnings)
            fprintf(stderr, "Warning: could not claim interface: %s\n", usb_strerror());
        if(action == ACTION_INTERRUPT){
            if(usbDirection){   /* IN transfer */
                len = usb_interrupt_read(handle, endpoint, rxBuffer, usbCount, usbTimeout);
            }else{
                len = usb_interrupt_write(handle, endpoint, sendBytes, sendByteCount, usbTimeout);
            }
        }else{
            if(usbDirection){   /* IN transfer */
                len = usb_bulk_read(handle, endpoint, rxBuffer, usbCount, usbTimeout);
            }else{
                len = usb_bulk_write(handle, endpoint, sendBytes, sendByteCount, usbTimeout);
            }
        }
    }
    if(len < 0){
        fprintf(stderr, "USB error: %s\n", usb_strerror());
        exit(1);
    }
    if(usbDirection == 0)   /* OUT */
        printf("%d bytes sent.\n", len);
    if(rxBuffer != NULL){
        FILE *fp = stdout;
        if(outputFile != NULL){
            fp = fopen(outputFile, outputFormatIsBinary ? "wb" : "w");
            if(fp == NULL){
                fprintf(stderr, "Error writing \"%s\": %s\n", outputFile, strerror(errno));
                exit(1);
            }
        }
        if(outputFormatIsBinary){
            fwrite(rxBuffer, 1, len, fp);
        }else{
            int i;
            for(i = 0; i < len; i++){
                if(i != 0){
                    if(i % 16 == 0){
                        fprintf(fp, "\n");
                    }else{
                        fprintf(fp, " ");
                    }
                }
                fprintf(fp, "0x%02x", rxBuffer[i] & 0xff);
            }
            if(i != 0)
                fprintf(fp, "\n");
        }
    }
    usb_close(handle);
    if(rxBuffer != NULL)
        free(rxBuffer);
    return 0;
}
Пример #25
0
int main(){
    char a[] = "   - 321";
    printf ("stirng is %d \n", myAtoi(a));
    return 0;
}
static void test3(void)
{
    char a[] = "1000";

    printf("%d\n", myAtoi(a));
}
Пример #27
0
int main(int argc, char *argv[])
{
	printf("%d\n", myAtoi(argv[1]));
	return(0);
}
Пример #28
0
int main(int argc, char *argv[]) {
  char str[] = "2147483648";
  int result = myAtoi(str);
  printf("str: %s  result: %d", str, result);
  return 0;
}
Пример #29
0
int main(int argc, char *argv[]) {
	char s[]="123444";
	printf("%d",myAtoi(s));
	return 0;
}
Пример #30
0
int main () {
    printf("%d\n", myAtoi("    10522545459"));
    return 0;
}