珞珈山水BBS电脑网络程序人生 → 单文区文章阅读

单文区文章阅读 [返回]
发信人: xiachaowen (awen), 信区: Programm
标  题: 程序查错!请!!!
发信站: BBS 珞珈山水站 (Thu Mar 17 11:01:28 2005)

下面这段c++程序有错,请找出!
#include "iostream.h"
#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
bool convert(const char *strDec,char **strOct);
void main()
{
    char str[10];
    puts("input a number string:");
    gets(str);
    int len=strlen(str);
    int i=0,err=0;
    do 
    {
        if(str[i]<48||str[i]>57)
        {
            err++;
        }
        i++;
    } while(str[i]!='\0');
    if(err>0)
    {
        printf("%d error...\n",err);
        return;
    }
    char *strOct=0;
    if(convert(str,&strOct))
    {
        printf("Decimal %s is converted to octal %s\n",str,strOct);
    }
    else
    {
        printf("Happen to error when converting...\n");
    }
}
bool convert(const char *strDec,char **strOct)
{
    
    int numDec=atoi(strDec);
    char *numOct=0;
    int count=0;
    int result=0;
    int midDec;
    while(1)
    {
        midDec=numDec;
        midDec=midDec/8;
        if(midDec==0)
        {
            result+=numDec*(int)pow(10,count);
            break;
        }
        else
        {
            result+=(numDec%8)*(int)pow(10,count);
            numDec=midDec;
        }
        count++;
    }
    _itoa(result,numOct,10);
    if(!numOct)
    {
        **strOct=*numOct;
        return true;
    }
    else
    {
        return false;
    }
}
--

※ 来源:·珞珈山水BBS站 http://bbs.whu.edu.cn·[FROM: 192.168.0.*]
[返回单文区目录]

武汉大学BBS 珞珈山水站 All rights reserved.
wForum , 页面执行时间:11.713毫秒