将数字字符串转化为数字

来源:互联网 发布:随机数预测算法 编辑:程序博客网 时间:2024/05/17 03:59
<span style="font-family:Microsoft YaHei;font-size:18px;">//字符串转换为数字#include<iostream>using namespace std;int strtonum(char *p){int temp=0;int flag=0;if(*p=='-'){p++;flag=1;}while(*p!=0){temp=temp*10+(*p-'0');p++;}if(flag)return -temp;elsereturn temp;}int main(){char p[100];cout<<"请输入数字字符串:";cin>>p;int num;num=strtonum(p);cout<<num;return 0;}</span>

0 0