字符串转换为数字

今天面试的时候,主考官问到了这样一个问题。所以私下通过查找资料整理了一下。

题目:

输入一个表示整数的字符串,把该字符串转换成整数并输出,例如输入字符串"345", 则输出整数 345。

给定函数原型 int StrToInt(const char *str) ,完成函数 StrToInt,实现字符串转换成整数的功能,不得用库函数 atoi

/*************************************************************************
	> File Name: strToInt.c
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年07月05日 星期二 14时24分07秒
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include "strToInt.h"
/*
 * res = 0;输入含有特殊字符
 * res = -1;输入为空
 * res = 数字;输入正常
 * */

int strToInt(const char* str)
{
	int res = 0;
	int i = 0;
	int signal = '+';
	int cur;
	
	if (!str)
	{
		res = -1;
		printf("func strToInt() err:%d\n",res);
		return res;
	}
	//skip backspace
	while (isspace(str[i]))
		i++;

	//skip signal
	if (str[i] == '+' || str[i] == '-')
	{
		signal = str[i];
		i++;
	}

	//get result
	while (str[i] != '\0')
	{
		if (str[i] >= '0' && str[i] <= '9')
		{
			cur = str[i] - '0';

			//judge overlap or not
			if ((signal == '+') && (cur > INT_MAX - res*10))
			{
				res = INT_MAX;
				break;
			}
			else if ((signal == '-') && (cur > INT_MAX - res*10))
			{
				res = INT_MIN;
				break;
			}
			res = res*10 + cur;
			i++;
		}
		else
		{
			break;
		}
		
	}

	return (signal == '-') ? -res : res;
}
/*************************************************************************
	> File Name: strToInt.h
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年07月05日 星期二 14时39分31秒
 ************************************************************************/

#ifndef _STRTOINT_H
#define _STRTOINT_H

int strToInt(const char* str);

#endif

/*************************************************************************
	> File Name: test.c
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年07月05日 星期二 14时40分12秒
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "strToInt.h"

void Test(char *TestName, const char *str, int expected)
{
	if (strToInt(str) == expected)
	{
		printf("%s:%s->%d pass!\n",TestName, str, expected);
	}
	else
		printf("%s:%s->%d fail!\n", TestName, str, expected);
}

void Test1()
{
	const char *str = "-123";
	Test("Test1", str, -123);
}
void Test2()
{
	const char *str = "+123";
	Test("Test2", str, 123);
}
void Test3()
{
	const char *str = "abc";
	Test("Test3", str, 0);
}
void Test4()
{
	const char *str = NULL;
	Test("Test4", str, -1);
}
void Test5()
{
	const char *str = "    123";
	Test("Test5", str, 123);
}
void Test6()
{
	const char *str = "00000123";
	Test("Test6", str, 123);
}
void Test7()
{
	const char *str = "2147483648";
	Test("Test7", str, 2147483647);
}
void Test8()
{
	const char *str = "-2147483648";
	Test("Test8", str, -2147483648);
}
int main()
{
	Test1();
	Test2();
	Test3();
	Test4();
	Test5();
	Test6();
	Test7();
	Test8();
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yiluohan0307

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值