| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 363 人关注过本帖
标题:快疯了。。。为啥这个代码运行不正常??跪拜求助!!!
收藏  订阅  推荐  打印 
duanjingyu
Rank: 1
等级:新手上路
帖子:4
积分:144
注册:2008-3-22
快疯了。。。为啥这个代码运行不正常??跪拜求助!!!

高手帮忙看一下!


第23行      printf("%d",isdigit(xt));
运行时发现如果输入的x位数为4位以下的话,isdigit(xt)的值总是打印为0,也就是说,只有输入四位数或更大的数才不为0。。。难道3位数就不是数字了吗?我输入9、99、999都不行!为啥啊谁能告诉我!!!
拜托了!4月就考三级了啊啊啊啊啊


#include  <stdio.h>
#include  <ctype.h>

int op;

float x,y,res;

int error_detect(int x);

main()
{
   int xt=0;
   puts("This is a calculator program.");
   puts("Tip: To do subtration or division work, the first \
number \nwill be used as minuend or dividend, and \nthe second \
number as subtrahend or divisor.");
   do
   {
      puts("Enter the first number:");
      scanf("%f",&x);
      xt=(long)x;
      printf("%d",xt);
      printf("%d",isdigit(xt));
      if(error_detect(xt)==0)
         continue;
      puts("Enter an operation please:");
      puts("1. Add\n2. Minus\n3. multiply\n4. Divide\n5. Quit");
      scanf("%d",&op);
      if(error_detect(op)==0)
         continue;
      puts("Enter the second number:");
      scanf("%f",&y);
      if(error_detect(y)==0)
         continue;
      switch(op)
      {
         case 1:
         {
            res=x+y;
            break;
         }
         case 2:
         {
            res=x-y;
            break;
         }
         case 3:
         {
            res=x*y;
            break;
         }
         case 4:
         {

            if(y!=0.00)
            {
               res=x/y;
            }else{
               puts("Error. Please input a divisor other than zero.");
            };
            break;
         }
         case 5:
         {
            exit(0);
            break;
         }
         default:
         {
            puts("Input Error.");
            break;
         }
      }
      printf("The result is %f",res);
   }while(1);
}

int error_detect(int temp)
{
   int err=0;
   if(isdigit(temp))
   {
       err=1;
   }else{
       puts("Input Error. Please input numbers.");
       err=0;
   };
   return err;
}

第23行      printf("%d",isdigit(xt));
运行时发现如果输入的x位数为4位以下的话,isdigit(xt)的值总是打印为0,也就是说,只有输入四位数或更大的数才不为0。。。为啥啊谁能告诉我!!!
拜托了!4月就考三级了啊啊啊啊啊

[ 本帖最后由 duanjingyu 于 2008-3-22 21:04 编辑 ]
搜索更多相关主题的帖子: int  跪拜  代码  位数  
2008-3-22 20:59
qfyzy
Rank: 3Rank: 3
来自:安徽合肥
等级:中级会员
威望:1
帖子:344
积分:3728
注册:2008-2-17

程序我仔细看,好象isdigit只能检测一个字符吧..

当对C的经验增加时,它会显的很好用.----Dennis M Ritche如是说
2008-3-22 21:28
moonwalker
Rank: 4
等级:高级会员
威望:1
帖子:907
积分:9698
注册:2007-3-2

你的函数用法不对,你看看函数原型
int isdigit(int ch);
所以输入参数是整形,你输入字符型的当然出错
这个函数的作用就是判断一个整型数是不是在0~9中

“视频教程网”免费提供教学资源
C不限制你的自由!
条件是自己承担滥用自由的恶果!
2008-3-22 22:09
duanjingyu
Rank: 1
等级:新手上路
帖子:4
积分:144
注册:2008-3-22

啊,原来是这样啊,我试试!谢谢这位大人!
2008-3-23 10:13
hyq1122
Rank: 1
等级:新手上路
帖子:23
积分:338
注册:2008-3-24
isalnum()

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char *argv[])
{
  char ch[]={'a','b','c','d','A','B','C','D'};
  int i=0;
  for (i=0;i<8;i++)
  {
      if(isalnum(ch[i]))printf( "Isdigit:%c,and it's digit number is %3d,and it's hex number is %x \n",ch[i],ch[i],ch[i]);   
  }
  system("PAUSE");    
  return 0;
}

[ 本帖最后由 hyq1122 于 2008-3-25 10:59 编辑 ]
2008-3-25 10:53
kissme
Rank: 1
等级:新手上路
帖子:8
积分:180
注册:2006-4-21

45318591  C/C++,VC爱好者  欢迎加入
2008-3-25 11:40
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.072837 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved