atoi()函数的实现



    上一篇博客讲的是atoi()函数的功能及举例,现在呢,就自己写写代码(根据atoi()的功能)来表示atoi()函数的实现。我在这里先把atoi()函数的功能贴出来,也好有个参考啊~~~

    atoi()函数的功能:将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)。

    atoi()函数实现的代码:

    上一篇博客讲的是atoi()函数的功能及举例,现在呢,就自己写写代码(根据atoi()的功能)来表示atoi()函数的实现。我在这里先把atoi()函数的功能贴出来,也好有个参考啊~~~

    atoi()函数的功能:将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)。

    atoi()函数实现的代码:

  1. /* 
  2. * name:xif 
  3. * coder:xifan@2010@yahoo.cn 
  4. * time:08.20.2012 
  5. * file_name:my_atoi.c 
  6. * function:int my_atoi(char* pstr) 
  7. */  
  8.   
  9. int my_atoi(char* pstr)  
  10. {  
  11.     int Ret_Integer = 0;  
  12.     int Integer_sign = 1;  
  13.       
  14.     /* 
  15.     * 判断指针是否为空 
  16.     */  
  17.     if(pstr == NULL)  
  18.     {  
  19.         printf("Pointer is NULL\n");  
  20.         return 0;  
  21.     }  
  22.       
  23.     /* 
  24.     * 跳过前面的空格字符 
  25.     */  
  26.     while(isspace(*pstr) == 0)  
  27.     {  
  28.         pstr++;  
  29.     }  
  30.       
  31.     /* 
  32.     * 判断正负号 
  33.     * 如果是正号,指针指向下一个字符 
  34.     * 如果是符号,把符号标记为Integer_sign置-1,然后再把指针指向下一个字符 
  35.     */  
  36.     if(*pstr == '-')  
  37.     {  
  38.         Integer_sign = -1;  
  39.     }  
  40.     if(*pstr == '-' || *pstr == '+')  
  41.     {  
  42.         pstr++;  
  43.     }  
  44.       
  45.     /* 
  46.     * 把数字字符串逐个转换成整数,并把最后转换好的整数赋给Ret_Integer 
  47.     */  
  48.     while(*pstr >= '0' && *pstr <= '9')  
  49.     {  
  50.         Ret_Integer = Ret_Integer * 10 + *pstr - '0';  
  51.         pstr++;  
  52.     }  
  53.     Ret_Integer = Integer_sign * Ret_Integer;  
  54.       
  55.     return Ret_Integer;  
  56. }  
/*
* name:xif
* coder:xifan@2010@yahoo.cn
* time:08.20.2012
* file_name:my_atoi.c
* function:int my_atoi(char* pstr)
*/

int my_atoi(char* pstr)
{
	int Ret_Integer = 0;
	int Integer_sign = 1;
	
	/*
	* 判断指针是否为空
	*/
	if(pstr == NULL)
	{
		printf("Pointer is NULL\n");
		return 0;
	}
	
	/*
	* 跳过前面的空格字符
	*/
	while(isspace(*pstr) == 0)
	{
		pstr++;
	}
	
	/*
	* 判断正负号
	* 如果是正号,指针指向下一个字符
	* 如果是符号,把符号标记为Integer_sign置-1,然后再把指针指向下一个字符
	*/
	if(*pstr == '-')
	{
		Integer_sign = -1;
	}
	if(*pstr == '-' || *pstr == '+')
	{
		pstr++;
	}
	
	/*
	* 把数字字符串逐个转换成整数,并把最后转换好的整数赋给Ret_Integer
	*/
	while(*pstr >= '0' && *pstr <= '9')
	{
		Ret_Integer = Ret_Integer * 10 + *pstr - '0';
		pstr++;
	}
	Ret_Integer = Integer_sign * Ret_Integer;
	
	return Ret_Integer;
}

    现在贴出运行my_atoi()的结果,定义的主函数为:int  main  ()

  1. int main()  
  2. {  
  3.     char a[] = "-100";  
  4.     char b[] = "456";  
  5.     int c = 0;  
  6.       
  7.     int my_atoi(char*);   
  8.   
  9.     c = atoi(a) + atoi(b);  
  10.       
  11.     printf("atoi(a)=%d\n",atoi(a));  
  12.     printf("atoi(b)=%d\n",atoi(b));  
  13.     printf("c = %d\n",c);  
  14.   
  15.     return 0;  
  16. }  
int main()
{
	char a[] = "-100";
	char b[] = "456";
	int c = 0;
	
	int my_atoi(char*);	

	c = atoi(a) + atoi(b);
	
	printf("atoi(a)=%d\n",atoi(a));
	printf("atoi(b)=%d\n",atoi(b));
	printf("c = %d\n",c);

	return 0;
}


    运行结果:


`atoi` 函数是 C 语言中的标准库函数,用于将字符串转换为整数。在 Visual Studio Code (VSCode) 中使用 `atoi` 函数实现基本的加减乘除运算需要结合一些编程逻辑。以下是一个简单的 C 语言示例代码,展示如何使用 `atoi` 函数结合运算符实现基本的数学运算: ```c #include <stdio.h> #include <stdlib.h> int main() { char expression[100]; printf("请输入一个简单的数学表达式(例如:12+34):"); scanf("%s", expression); // 提取运算符 char operator = expression[0]; // 提取操作数 int operand1 = atoi(&expression[1]); int operand2 = atoi(&expression[expression[0] - '0']); int result = 0; // 根据运算符进行相应的运算 switch (operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': // 除数不能为0,需要检查 if (operand2 != 0) { result = operand1 / operand2; } else { printf("错误:除数不能为0。\n"); return 1; } break; default: printf("错误:未知的运算符。\n"); return 1; } printf("结果是:%d\n", result); return 0; } ``` 在这段代码中,用户输入的表达式首先被读取到 `expression` 字符数组中。然后,程序通过字符串操作提取运算符和操作数,并使用 `atoi` 函数将操作数转换为整数。接下来,根据提取到的运算符,程序通过 `switch` 语句选择相应的运算,并将结果存储在 `result` 变量中。最后,程序输出运算结果。 请注意,在使用这段代码时,输入的表达式格式需要遵循代码中的假设,例如,加减乘除运算符后面紧跟着操作数,且不考虑运算符优先级和括号等复杂情况。在实际应用中可能需要更复杂的解析算法来处理不同的数学表达式。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值