数字串输入----怎样把从一个连续数字串中提取出一个一个的数字

sunyuke 2009-02-23 09:10:53
如题
例子从键盘随便输入一串数字 比如2345654321
将数字一个一个读取 2、3、 4、5.......
希望得到c和c++ 两种答案
谢谢~~
...全文
216 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForestDB 2009-02-25
  • 打赏
  • 举报
回复

#include <stdio.h>

int main(void)
{
int c;

while((c = getchar()) != EOF)
{
printf("%d\n", c - '0');
}

return 0;
}


#include <iostream>

using namespace std;

int main()
{
char c;

while(!cin.eof())
{
cin >> c;
cout << c - '0' << endl;
}

return 0;
}

BuleRiver 2009-02-24
  • 打赏
  • 举报
回复

#include<iostream>
#include<string>

using namespace std;

int main()
{
char buf[100] = {0};
int nums[100] = {0};
int nCount = 0;
int i;
cout<<"input data: "<<endl;
cin.getline(buf, 100);
for (i = 0; buf[i] != '\0'; i++)
{
nCount++;
nums[i] = buf[i] - '0';
}

for (i = 0; i < nCount; i++)
{
cout<<nums[i]<<endl;
}

return 0;
}


sunnywyg 2009-02-23
  • 打赏
  • 举报
回复

#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "";
cin >> str;
int i = 0;
while(i++ < str.size())
{
cout << str[i]<< endl;
//获取数字:int iTemp = str[i] - '0';
}
}
fly-2024 2009-02-23
  • 打赏
  • 举报
回复
使用atoi啊
thesecretblue 2009-02-23
  • 打赏
  • 举报
回复
二楼用栈输出的是逆序哟!!!

1.用循环,将数组输出-----》楼主自己解决
2.楼主如果不用数组:读取 2、3、 4、5.......
科编程如下:
C++:
#include<iostream>
#include<string>
using namespace std;
int main()
{
char c;
while((c=getchar())!=EOF)
cout<<c<<" ";

return 0;
}
C:
#include<stdio.h>
int main()
{
char c;
while((c=getchar())!=EOF)
printf("%-3c",c);
return 0;
}
sunyuke 2009-02-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 elmnd 的回复:]
C/C++ code#include <cstdlib>
#include <iostream>
#define N 100
using std::cout;
using std::cin;
int main(void)
{
char a[N];
cin>>a;
cout<<"result:\n";
for(int i = 0; a[i] != '\0'; ++i)
{
cout<<a[i]<<" ";
}
system("pause");
return 0;
}



8L的兄弟, 俺是用DEV CPP的, 头文件你自己改改
[/Quote]
谢谢
用动态数组比较好
elmnd 2009-02-23
  • 打赏
  • 举报
回复
#include <cstdlib>
#include <iostream>
#define N 100
using std::cout;
using std::cin;
int main(void)
{
char a[N];
cin>>a;
cout<<"result:\n";
for(int i = 0; a[i] != '\0'; ++i)
{
cout<<a[i]<<" ";
}
system("pause");
return 0;
}

8L的兄弟, 俺是用DEV CPP的, 头文件你自己改改
sunyuke 2009-02-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 elmnd 的回复:]
貌似俺刚才写的头尾忘记调转回来。。。

C/C++ code#include <cstdlib>
#include <iostream>
#define N 100
using std::cout;
using std::cin;
int main(void)
{
int a[N];
int k, n = 0;
cin>>k;
while(k != 0)
{
a[n++] = k;
cin>>k;
}

n--;

cout<<"result:\n";
for(int i = 0; i <= n; ++i)
{
cout<<a[i]<<" ";
}
syst…
[/Quote]
VS2008调试错误。。。
feng4206yu 2009-02-23
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

int main()
{
int a[10];
int i=0,c;
while((c=getchar())!='\n')
{
a[i]=c-'0';
cout<<a[i++]<<endl;
}


return 0;
}
elmnd 2009-02-23
  • 打赏
  • 举报
回复
貌似俺刚才写的头尾忘记调转回来。。。
#include <cstdlib>
#include <iostream>
#define N 100
using std::cout;
using std::cin;
int main(void)
{
int a[N];
int k, n = 0;
cin>>k;
while(k != 0)
{
a[n++] = k;
cin>>k;
}

n--;

cout<<"result:\n";
for(int i = 0; i <= n; ++i)
{
cout<<a[i]<<" ";
}
system("pause");
return 0;
}
sunyuke 2009-02-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 elmnd 的回复:]
乱写的, 写的不好不要怪。。。

C/C++ code#include <cstdlib>
#include <iostream>
#include <stack>
using namespace std;

int main(void)
{
stack <int>aa;
int k;
cin>>k;
while(k != 0)
{
aa.push(k);
cin>>k;
}
cout<<"利用栈写:\n";
while(aa.empty() != true)
{
cout<<aa.top()<<" ";
aa.pop();
}
system("pau…
[/Quote]
问题倒是实现了
不过用栈的话 感觉用处不大
fairchild811 2009-02-23
  • 打赏
  • 举报
回复
2楼跑题了,lz是指一次性输入12346这个数字
1楼方法可行
elmnd 2009-02-23
  • 打赏
  • 举报
回复
#include <cstdlib>
#include <iostream>
#define N 100
using std::cout;
using std::cin;
int main(void)
{
int a[N];
int k, n = 0;
cin>>k;
while(k != 0)
{
a[n++] = k;
cin>>k;
}

n--;

cout<<"2楼说的:\n";
while(n >= 0)
{
cout<<a[n--]<<" ";
}
system("pause");
return 0;
}
elmnd 2009-02-23
  • 打赏
  • 举报
回复
乱写的, 写的不好不要怪。。。
#include <cstdlib>
#include <iostream>
#include <stack>
using namespace std;

int main(void)
{
stack <int>aa;
int k;
cin>>k;
while(k != 0)
{
aa.push(k);
cin>>k;
}
cout<<"利用栈写:\n";
while(aa.empty() != true)
{
cout<<aa.top()<<" ";
aa.pop();
}
system("pause");
return 0;
}
waizqfor 2009-02-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 sunyuke 的帖子:]
如题
例子从键盘随便输入一串数字 比如2345654321
将数字一个一个读取 2、3、 4、5.......
希望得到c和c++ 两种答案
谢谢~~
[/Quote]
简单啊 你用一个char []数组 存储 字符串 然后 读取的时候
做一个for()循环 一位一位的读取 C\C++都通用

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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