C高手请进

jasmine 2011-04-14 10:52:37
题目:将一个正整数逆序输出
以下是我写的程序
但是输出有问题
请教高手 帮忙修改一下
#include <stdio.h>
main()
{int x,digit;

scanf("%d",&x);
while(x!=0){
digit=x%10;
x=x/10;
printf("%d\n",digit);

}

system("pause");
}
...全文
423 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjoksky 2011-04-18
  • 打赏
  • 举报
回复
system("pause")函数不在#include<stdio.h>里面,用C语言编译器的话加#include<stdlib.h>,用C++编译器的话加#include<iostream.h>就行了,还有输出的数据不在一行(去掉\n),main函数最好给个返回值,哪怕是void也行,编程风格很重要!加油
jasmine 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 csiyou 的回复:]
C/C++ code
#include <stdio.h>
main()
{long x,digit; /*把int改成long,在TC中int最大只有35767,VC中int和long都是四个字节*/

scanf("%d",&x);
while(x!=0){
digit=x%10;
x=x/10;
printf("%d\n",digit);

……
[/Quote]
你的上面貌似也有问题哦
printf("%d\n",digit);
当中是不是应该改成 printf("%ld\n",digit);
嘻嘻 我也发现了不少大家回帖中的错误哦
jasmine 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 luozenghui529480823 的回复:]
#include <stdio.h>

void main()
{
int x,digit;
scanf("%d",&x);
while(x!=0)
{
digit=x%10;
x=x/10;
printf("%d",digit);
}
}
[/Quote]
请问一下 你这个程序与我的有什么差别吗 兄弟
你也太敷衍我了
jasmine 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 pathuang68 的回复:]
要是觉得竖着排不爽,那就横着排:

C/C++ code

#include <stdio.h>

void int_reverse(int a)
{
int b;
b = a / 10;
if(!b)
{
printf("%d",a);
return;
}
printf("%d",a ……
[/Quote]
不好意思 哈 大哥
我的要求是随意输入一个数字
要求它逆序输出
leighjian 2011-04-18
  • 打赏
  • 举报
回复
没看出有什么问题,输出样式是1行输出1数字字符不是连续1串数字输出,建议吧\n写到while语句外
jasmine 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 pathuang68 的回复:]
我来个C++版的:)

C/C++ code

#include <iostream>
#include <stack>
using namespace std;

#define N 10

int main()
{
int a = 123456789;
char s[N];
_itoa_s(a, s, 10);

stack……
[/Quote]
我要的是C版的 大哥
jasmine 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 anyidan 的回复:]
printf("%d\n",digit); --> printf("%d",digit);

注意判断正负号, 或在 scan() 前加一点提示
[/Quote]
你说的方法冒得用 美女
guohao_c 2011-04-18
  • 打赏
  • 举报
回复
你就直接加一个头文件就可以了#include <stdlib.h>
lovebaby999 2011-04-18
  • 打赏
  • 举报
回复
用sprintf,把你这个整数转化成字符串,然后再倒着把字符串输出。我这儿只说一下关键的语句了。

sprintf(char*,"%d",int);
i=strlen(char*)
for(n=i-1;n>=0;n--)
{
printf("%c", char*[n])
}
herbaini2 2011-04-17
  • 打赏
  • 举报
回复
什么问题啊
asmst 2011-04-17
  • 打赏
  • 举报
回复
tc的int是2字节的
sboas 2011-04-17
  • 打赏
  • 举报
回复
没有问题的,你是用什么软件编译的,如果用最新的Microsoft visual studio 需要在main 前 加上 void。
666233 2011-04-17
  • 打赏
  • 举报
回复
#include <stdio.h>

void main(void)
{
unsigned long x,digit;

scanf("%ld",&x);

while(x!=0)
{
digit=x%10;
x=x/10;
printf("%ld",digit);
}

printf("\n");
}
hzc543806053 2011-04-15
  • 打赏
  • 举报
回复
加个#include<stdlib.h>
guolicong 2011-04-15
  • 打赏
  • 举报
回复
这个感觉语法不对经
pathuang68 2011-04-15
  • 打赏
  • 举报
回复
我来个C++版的:)

#include <iostream>
#include <stack>
using namespace std;

#define N 10

int main()
{
int a = 123456789;
char s[N];
_itoa_s(a, s, 10);

stack<char> stk;
int i = 0;
do
{
stk.push(s[i++]);
}
while(i < (int)strlen(s));

memset(s, 0, 10);

int stksize = (int)stk.size();
for(i = 0; i < stksize; i++)
{
s[i] = stk.top();
stk.pop();
}

int result = atoi(s);
cout << result << endl;
return 0;
}
bullbat 2011-04-15
  • 打赏
  • 举报
回复
没发现有什么逻辑错误
jasmine 2011-04-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qq576240289 的回复:]
我改成了这个就可以了呢?

#include <stdio.h>

void main(void)
{
int x,digit;

scanf("%d",&x);

while(x!=0)
{
digit=x%10;
x=x/10;
printf("%d\n",digit);
}

}


我用的VC6.0++

而且我觉得你的程序……
[/Quote]
我按照你的修改了一下
还是有问题
我输入54321
输出却为
-5
-1
-2
-1
-1

但是如果我输入4321
结果又是正确的
是不是因为我们用的编译软件不同
我用的是wintc
你说的的vc6.0++
在我的win7系统里头用不好
qq576240289 2011-04-15
  • 打赏
  • 举报
回复
你输入的54321已经超出了int数据类型(-32768到32767)
  • 打赏
  • 举报
回复
加上头文件#include<stdlib.h>
加载更多回复(16)

69,371

社区成员

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

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