点分十进制表示的字符串转换为 unsigned int 整型数值

本文介绍如何实现将点分十进制表示的字符串转换为无符号整型数值,例如"128.11.3.31"转换为2148205343,以及在遇到超出范围的情况如"128.399.2.12"时返回UINT_MAX。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*题目4:(简答题:10.0分)
实现函数将点分十进制表示的字符串转换为 unsigned int 整型数值
unsigned int my_ResDotDec(const char *strip);
参数说明:strip 点分十进制表示的字符串;
示例: strip =“128.11.3.31” ; 返回值: 2148205343;
strip =“128.399.2.12” ; 返回值为 UINT_MAX

#include <iostream>
#include <ctype.h>
#include <limits.h>
#include <stack>

using namespace std;


//处理单个atoi
int my_atoi_single(const char p)
{
   
    return p - '0';
}

//atoi的子函数,将进制,字符,符号传递进来
unsigned int my_atoi_Son(const char *p,int system)
{
   
    stack<char> sta;
    unsigned int sum = 0;
    unsigned int tmp = 0;
    while(isdigit(*p))
    {
   
        sta.push(*p);
        p++;
    }
    int n = sta.size();
    for(int i = 0; i < n;++i)
    {
   
        tmp = my_atoi_single(sta.top());
        sta.pop();
        for(int j = 0;j < i;++j)
        {
   
            tmp = tmp * system;
        }
        sum += tmp;
    }
    return sum;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值