Function:

 /// <summary>
/// ToInt
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int atoi(string str)
{
string strNew = str.Trim();
string ch = strNew.Substring(0, 1);
string result = "";
int bl = 0;
double re;
bool flag = true;
for (int i = 1; i < strNew.Length; i++)
{
string s = strNew.Substring(i, 1);
if (int.TryParse(s, out bl))
{
if (Convert.ToInt32(s) == 0)
continue;
switch (ch)
{
case "0":
result = result + s.ToString();
break;
case "+":
result = result + s.ToString();
break;
case "-":
flag = false;
result = result + s.ToString();
break;
default:
if (i == 1)
result = result + ch.ToString();
result = result + s.ToString();
break;
}
}
else
break;
}
if (!string.IsNullOrEmpty(result))
{
re = Convert.ToDouble(result);
if (!flag)
re = -re;
if (re < int.MinValue || re > int.MaxValue)
re = 0;
}
else
re = 0;
return (int)re;
}

控制台展示:

static void Main(string[] args)
{
Console.WriteLine("请输入要转化的字符串:");
string str = Console.ReadLine();
int re = atoi(str);
if (re != 0)
Console.WriteLine("转化后:" + re);
else
Console.WriteLine("该字符串不能转换为整数!");
Console.ReadKey();
}

  

leetcode系列---atoiFunction C#code的更多相关文章

  1. leetcode系列---3Sum C#code

    Function: public static List<int[]> SumSet(int[] array) { List<int[]> result = new List& ...

  2. CRL快速开发框架系列教程一(Code First数据表不需再关心)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  3. 1 翻译系列:什么是Code First(EF 6 Code First 系列)

    原文链接:http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx EF 6 Code-First系列文章目录 ...

  4. leetcode 系列文章目录

    leetcode 系列文章目录 0. 两数之和1. 两数相加 2. 无重复字符的最长子串 3. 寻找两个有序数组的中位数 4. 最长回文子串 5. Z 字形变换 6. 整数反转 7. 字符串转换整数 ...

  5. C#刷遍Leetcode系列连载 索引

    C#刷遍Leetcode系列文章 索引 索引(陆续发布中,请保持关注) C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - ...

  6. Leetcode系列之两数之和

    Leetcode系列之两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你 ...

  7. leetcode系列---Two Sum C#code

    /// <summary> /// 方法一:双循环 /// </summary> /// <param name="array"></pa ...

  8. 【一天一道LeetCode】#89. Gray Code

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...

  9. 8天掌握EF的Code First开发系列之2 Code First开发系列之领域建模和管理实体关系

    本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 理解Code First及其约定和配置 创建数据表结构 管理实体关系 三种继承模式 本章小结 本人的实验环境是V ...

随机推荐

  1. Spring MVC 梳理 - handlerMapping和handlerAdapter分析

    参考图片 综上所述我们来猜测一下spring mvc 中根据URL找到处理器Controller中相应方法的流程 ①:获取Request的URL ②:从UrlLookup这个map中找到相应的requ ...

  2. 【IT技术概念】WebAPI与传统的WebService有哪些不同?

    在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下,你有很多的选择来构建一个HTTP Services.我分享一下我对 ...

  3. Vue躬行记(1)——数据绑定

    Vue.js的核心是通过基于HTML的模板语法声明式地将数据绑定到DOM结构中,即通过模板将数据显示在页面上,如下所示. <div id="container">{{c ...

  4. aircrack-ng wifi密码破解

    wifi密码破解 步骤1:查看网卡信息 ifconfig 找到你要用到的网卡 步骤2:启动网卡监听模式 airmon-ng start wlan0 我的是wlp2s0 步骤三:查看网卡变化 wlan0 ...

  5. 创建新镜像-从已创建的容器中更新镜像并提交镜像(以Nginx为例)

    目标:现在我们主要是修改nginx的index.html,然后做一个新镜像 1.基于nginx:1.12运行一个容器 docker run -d -p 8080:80 --name nginx ngi ...

  6. 【Java】获取当前操作系统桌面路径

    //当前用户桌面 File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory(); String desktopPat ...

  7. Nebula 架构剖析系列(一)图数据库的存储设计

    摘要 在讨论某个数据库时,存储 ( Storage ) 和计算 ( Query Engine ) 通常是讨论的热点,也是爱好者们了解某个数据库不可或缺的部分.每个数据库都有其独有的存储.计算方式,今天 ...

  8. unittest中diascover批量执行用例

    # case_dir='./'#当前脚本的路径 # discover=unittest.defaultTestLoader.discover(case_dir,pattern='unittest_fr ...

  9. Vue 全家桶,深入Vue 的世界

    内容简介: Vue 实例上的属性 Vue 生命周期 Vue 数据绑定 computed 计算属性 watch 监听器 Vue 组件 Vue 组件 extend Vue 组件高级属性 Vue 的rend ...

  10. Sieve of Eratosthenes时间复杂度的感性证明

    上代码. #include<cstdio> #include<cstdlib> #include<cstring> #define reg register con ...