实现atoi函数

要求:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

class Solution {
public:
    int myAtoi(string str) {
		if(str.empty())
			return 0;		
		int iters=0;
		for(; str[iters]==' '; ++iters);
		if(iters == str.size()) 
			return 0;
		
		long  ret=0;
		int fuHao=1;
		if(str[iters] == '+') {
			fuHao=1;
			++iters;
		} else if(str[iters] == '-') {
			fuHao=-1;
			++iters;
		} else if(str[iters]<'0'||str[iters]>'9'){
			return 0;
		}
		int high=iters;//iters points the loc of first digit
		while(str[high]>='0'&&str[high]<='9' &&high<str.size()) {
			++high;
		}
		//high--;
		int weight=10;		
		while(high > iters) {
			ret=ret*weight+str[iters]-'0';
			//		weight*=10;
			++iters;
			if(ret>INT_MAX && (fuHao==1)) {
				return INT_MAX;
			}
			if(-ret<INT_MIN && (fuHao==-1)) {
				return INT_MIN;
			}
		}
		return ret*fuHao;
    }
};





你可以在my_atoi函数中添加以下内容来实现atoi函数: ```c int my_atoi(const char* s) { assert(s != NULL); // 断言字符串不为空 int flag = 1; // 初始化符号标志为正 long long result = 0; // 初始化结果为0 // 跳过空白字符 while (isspace(*s)) { s++; } // 处理正负号 if (*s == '+') { flag = 1; s++; } else if (*s == '-') { flag = -1; s++; } // 逐个字符转换为数字并计算结果 while (*s != '\0') { if (isdigit(*s)) { result = result * 10 + (*s - '0'); s++; } else { break; } } // 根据符号标志返回最终结果 return flag * result; } ``` 这个函数会跳过字符串中的空白字符,处理正负号,并将字符串逐个字符转换为数字并计算结果。最后根据符号标志返回最终结果。请注意,这个函数还包含了一些特殊情况的处理,比如空指针、字符串长度为0、整型溢出以及含有其他字符的情况。 #### 引用[.reference_title] - *1* *2* [C语言简单实现atoi函数](https://blog.csdn.net/Rinki123456/article/details/121002329)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【C语言】模拟实现atoi函数](https://blog.csdn.net/m0_62179366/article/details/124066972)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值