请教:怎么把字符型数据转换成整型数据

bonn 2003-10-16 08:14:02
老师布置的作业:怎么把一串字符型数据string[]="1-5,34,45,101-126"转换为整型数据输出1,2,3,4,5,34,45,101,102……126?
...全文
1000 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
brxren 2003-10-21
  • 打赏
  • 举报
回复
我给你个程序,我做的!
#include <iostream>
using namespace std;
#include <vector>
#include <string>

void intToChar(vector <int> save,vector <char> &output)
{
int tiaoyue=1;//tiaoyue是用来解决读取几个不连续的值的问题,此鉴别方法有待改进!!!
int i;
if(save[0]/10!=0)
output.push_back(char(save[0]/10+48));
output.push_back(char(save[0]%10+48));
for(i=1;i<save.size();i++)
{
if((save[i]-save[i-1])==1)
{
if(i==(save.size()-1))
{
output.push_back('-');
if(save[i]/10!=0)
output.push_back(char(save[i]/10+48));
output.push_back(char(save[i]%10+48));
}
tiaoyue=0;
}
else
{
tiaoyue++;
if(tiaoyue==1)
{
output.push_back('-');
if(save[i-1]/10!=0)
output.push_back(char(save[i-1]/10+48));
output.push_back(char(save[i-1]%10+48));
}
output.push_back(',');
if(save[i]/10!=0)
output.push_back(char(save[i]/10+48));
output.push_back(char(save[i]%10+48));
}
}

}


int stringToDeci(vector <char> buf)
{
int i=0;
int sum=0;
while(i<buf.size()) sum=sum*10+buf[i++]-48;
return sum;
}

void perform(vector <char> start,vector <char> end,vector <int> &save)
{
int i;
int min=stringToDeci(start);
int max=stringToDeci(end);
for(i=min;i<=max;i++)
{
save.push_back(i);
}
}

void ReadThrough(vector <int> &save,string input)//用来挨个读取input里的内容
{
int flag=0;//flag是用来记录读取的值是表示一个区间的最小值还是表示一个区间的最大值,
//flag=0表示是最小值,flag=1表示是最大值
vector <char> start; //存储表示最小值的字符串
vector <char> end;//存储表示最大值的字符串
char *p;
p=&input[0];
while(*p!='\0')
{
if(flag==0)
{
start.push_back(*p);
p++;
}

if(flag==1)
{
end.push_back(*p);
p++;
}

if(*p=='-')
{
p++;
flag=1;
}
if(*p==',')
{
if(end.size()==0)
perform(start,start,save);
else
perform(start,end,save);
p++;
start.clear();
end.clear();
flag=0;
}
}
if(end.size()==0)
perform(start,start,save);
else
perform(start,end,save);
}

void main()
{
string input;
cin>>input;
vector <int> save;//save是用来存储整数形式的值
ReadThrough(save,input);

int i;
for(i=0;i<save.size();i++) cout<<save[i]<<' ';cout<<endl;
vector <char> output;
intToChar(save,output);
i=0;
while(i<output.size())
{
cout<<output[i];
i++;
}
cout<<endl;

}
bonn 2003-10-21
  • 打赏
  • 举报
回复
我都不知道怎么给分了,这么多兄弟捧场
jakeye 2003-10-19
  • 打赏
  • 举报
回复
‘字符’-‘0’就是整型数据了
取字符串中的每一个字符然后一字符‘0’,就可以了....。简单吧


接分喽!!!
bonn 2003-10-19
  • 打赏
  • 举报
回复
倒~~~其实是要你输入此类任意类型的字符型数据,然后进行转换
yeyuboy 2003-10-17
  • 打赏
  • 举报
回复
agree with scale8292
Andy84920 2003-10-17
  • 打赏
  • 举报
回复
agree to pxwzd123(tajore) .

end.
TianGuangZao 2003-10-17
  • 打赏
  • 举报
回复
#include <stdio.h>

int main()
{
char *str[33]= {"1","2","3","4","5","43","45",
"101","102","103","104","105","106","107",
"108","109","110","111","112","113","114",
"115","116","117","118","119","120","121",
"122","123","124","125","126"};

int iarr[33] = {0};
int i;
for ( i = 0; i < 33; i++)
{
sscanf( str[i], "%d", &iarr[i]);
printf( "%d\n", iarr[i]);
}
return 0;
}

用 sscanf 也可以。
bapldc 2003-10-17
  • 打赏
  • 举报
回复
请问函数的递归怎么用的?
ssbull 2003-10-17
  • 打赏
  • 举报
回复
atoi()函数的使用方法很简单。()中填入你要进行转换的字符串就可以了,注意,这个参数要是const char *型的
pxwzd123 2003-10-17
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>

void main( void )
{
char *s; double x; int i; long l;

s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}
ccwwbb 2003-10-16
  • 打赏
  • 举报
回复
#include <string>
#include <iostream>
using namespace std;
void main()
{
char *str[]={"1","2","3","4","5","43","45",
"101","102","103","104","105","106","107",
"108","109","110","111","112","113","114",
"115","116","117","118","119","120","121",
"122","123","124","125","126"};
char **p;
p=str;
for(int i=0;i<33;i++)
cout<<atoi(*(p+i))<<" ";
}
ccwwbb 2003-10-16
  • 打赏
  • 举报
回复
少了最后一个
};
ccwwbb 2003-10-16
  • 打赏
  • 举报
回复
#include <string>
#include <iostream>
using namespace std;
void main()
{
char *str[]={"1","2","3","4","5","43","45",
"101","102","103","104","105","106","107",
"108","109","110","111","112","113","114",
"115","116","117","118","119","120","121",
"122","123","124","125","126"};
char **p;
p=str;
for(int i=0;i<33;i++)
cout<<atoi(*(p+i))<<" ";
bonn 2003-10-16
  • 打赏
  • 举报
回复
那请问一下,具体这个atoi的函数是怎么应用,我没有看到过!
buaaaladdin 2003-10-16
  • 打赏
  • 举报
回复
用strtok分割字符串,用atoi转换成整数。

遇到‘-’的时候就把左边和右边之间的数字打印出来就可以了,否则就直接打印。
lingar 2003-10-16
  • 打赏
  • 举报
回复
‘字符’-‘0’就是整型数据了
scale8292 2003-10-16
  • 打赏
  • 举报
回复
用atoi()函数试试
ssbull 2003-10-16
  • 打赏
  • 举报
回复
#include <string>
#include <iostream>
using namespace std;
void main() {
string str[]={"1","2","3","4"};
int i[4];
for(int j=0;j<4;j++){
i[j]=atoi(str[j].c_str())+1;
cout<<i[j]<<" ";
}
cout<<endl;
}
output:
2 3 4 5
以上就把字符串转化为整型数据

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧