注册 登录
编程论坛 数据结构与算法

大家帮忙找一下错误

stupidfishsj 发布于 2010-10-28 11:20, 471 次点击
帮忙看一下 我自己写的代码  但运行出错了 找不到错误的源头……
#include<iostream>
#include<stack>
#include<math.h>
#include<string>
using namespace std;
class scale
{
private:
    int m;
    string str;
public:
    scale(string &pt,int &j):str(pt),m(j){}
    int translate_ten()
    {
        int tmp=0;
        int i,j;
        for( i=0,j=str.size();i<j;++i)
        {
            if(str[i]>='A'&&str[i]<='F')
                tmp+=(str[i]-'A'+10)*pow(m,j-i-1);
            if(str[i]>='0'&&str[i]<='9')
                tmp+=(str[i]-'0')*pow(m,j-i-1);
        }
        return tmp;
    }
    ~scale(){}
};
void translate_m(int &tmp,int &m)
{
    int t;
    stack <char> M;
    while(tmp)
    {
        t=tmp%m;
        if(t<10)
            M.push(t);
        else
        {
            t=t-10+'A';
            M.push(t);
        }
        tmp/=m;
    }
    while(!M.empty())
    {
        printf("%c",M.top());
        M.pop();
    }
}
void main()
{
    int m;
    int result;
    scanf("%d",&m);
    if(m<2||m>16)
        return;
    string num1,num2;
    scanf("%s%s",&num1,&num2);
    scale t1(num1,m);
    scale t2(num2,m);
    result=t1.translate_ten()/t2.translate_ten();
    translate_m(result,m);
    printf("\n");
    result=t1.translate_ten()%t2.translate_ten();
    translate_m(result,m);
}
2 回复
#2
m21wo2010-10-28 13:45
程序代码:
#include<iostream>
#include<stack>
#include<cmath>
#include<string>
using namespace std;
class scale
{
private:
    int m;
    string str;
public:
    scale(string &pt,int &j):str(pt),m(j){}
    int translate_ten()
    {
        int tmp=0;
        int i,j;
        for( i=0,j=str.size();i<j;++i)
        {
            if(str[i]>='A'&&str[i]<='F')
                tmp+=(str[i]-'A'+10)*pow(double(m),double(j-i-1));
            if(str[i]>='0'&&str[i]<='9')
                tmp+=(str[i]-'0')*pow(double(m),double(j-i-1));
        }
        return tmp;
    }
    ~scale(){}
};
void translate_m(int &tmp,int &m)
{
    int t;
    stack <int> M;
    while(tmp)
    {
        t=tmp%m;
        if(t<10)
            M.push(t);
        else
        {
            t=t-10+'A';
            M.push(t);
        }
        tmp/=m;
    }
    while(!M.empty())
    {
        cout<<M.top();
        M.pop();
    }
}
void main()
{
    int m;
    int result;
    cin>>m;
    if(m<2||m>16)
        return;
    string num1,num2;
    cin>>num1>>num2;
    scale t1(num1,m);
    scale t2(num2,m);
    result=t1.translate_ten()/t2.translate_ten();
    translate_m(result,m);
    cout<<endl;
    result=t1.translate_ten()%t2.translate_ten();
    translate_m(result,m);
}
#3
stupidfishsj2010-10-31 10:17
回复 2楼 m21wo
呵呵 我自己也找出来了  。但POW函数在6.0编译器是可以这样用的   相信你是用08的吧   分数给你了
1