急!!帮忙改一下代码,进制转换来的

linguanghuisue 2010-07-28 04:38:25
#include<iostream.h>
#include<string>

class num{

public:
char f1(char a);
int f2();
int f3();
};

int num::f1(char a){
/*16进制转换成10进制*/
int n=0;
for(int i=0;a[i]!='\0';i++)
{
if(a[i]>='0'&&a[i]<='9')
n=n*10+a[i]-'0';
if(a[i]>='a'&&a[i]<='z')
n=n*16+a[i]-'a'+10;
if(a[i]>='A'&&a[i]<='Z')
n=n*16+a[i]-'A'+10;
}
return n;
}
int num::f2(){
/*10进制转换成16进制*/

int len=0;
int t;
char b[50];

for(int k = 0; n != 0; k++)
{
t = n % 16;
if(t >= 0 && t < 10)
{
b[k] = t + '0';
}
else if(t >= 10 && t < 16)
{
b[k] = 'A' + t - 10;
}
len++;
n = n / 16;
}
for(int j = len - 1; j >= 0; j--)
{
cout<<b[j];
}
cout<<endl;
cout<<"len="<<len<<endl;
}
int num::f3(){
//10进制转换成2进制
int c[50];
int len1=0;
for(int l = 0; n1 != 0; l++)
{
t = n1 % 2;
c[l]=t;
len1++;
n1 = n1 / 2;
}
for( j = len1 - 1; j >= 0; j--)
{
cout<<c[j];
}
cout<<endl;
cout<<"len1="<<len1<<endl;

}

void main()
{
num n;
char a1[20];
cin>>a1;
n.f1(a1);
n.f2(n);
n.f3(n;
}
...全文
100 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
linguanghuisue 2010-07-31
  • 打赏
  • 举报
回复
出现问题了
输入af,输出b=FA烫烫烫烫烫烫,c=烫烫烫烫烫烫烫烫af,只是怎么回事

#include<iostream.h>
int n=0;
int f1(char *a);
char f2(int n);
void f3(int n1);
int f1(char *a){//字符串转换成10进制
for(int i=0;a[i]!='\0';i++)
{
if(a[i]>='0'&&a[i]<='9')
n=n*10+a[i]-'0';
if(a[i]>='a'&&a[i]<='z')
n=n*16+a[i]-'a'+10;
if(a[i]>='A'&&a[i]<='Z')
n=n*16+a[i]-'A'+10;
}
return n;
}
char f2(int m){//10进制转换成16进制
int t;
int len=0;
char b[50];
for(int k = 0; m != 0; k++)
{
t = m % 16;
if(t >= 0 && t < 10)
{
b[k] = t + '0';
}
else if(t >= 10 && t < 16)
{
b[k] = 'A' + t - 10;
}
len++;
m = m / 16;
}

cout<<"len="<<len<<endl;
cout<<"b="<<b<<endl;
char c[50];
int i=0;
for(int j = len - 1; j >= 0; j--)
{
c[i]=b[j];
i++;
}
//cout<<"c="<<c<<endl;
return* c;
}

void f3(int n1){//10转2;

int c[50];
int len1=0;
int t,j;
for(int l = 0; n1 != 0; l++)
{
t = n1 % 2;
c[l]=t;
len1++;
n1 = n1 / 2;
}
for( j = len1 - 1; j >= 0; j--)
{
cout<<c[j];
}
cout<<endl;
cout<<"len1="<<len1<<endl;
//return d;
}

void main()
{
char a[20];
char c[20];
cin>>a;
f1(a);
cout<<"n="<<n<<endl;
f2(n);
cout<<"c="<<c<<endl;
f3(n);



}
elegant87 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 slsnake 的回复:]

C/C++ code

#include<iostream.h>
#include<string>

class num{

public:
int f1(char *a);
void f2(int n);
void f3(int n1);
};

int num::f1(char *a){
/*16进制转换成10进制*/
int n=0;
for(int i=0;a[i]!='\0'……
[/Quote]
UP 解决了就顶下
SLSnake 2010-07-28
  • 打赏
  • 举报
回复

#include<iostream.h>
#include<string>

class num{

public:
int f1(char *a);
void f2(int n);
void f3(int n1);
};

int num::f1(char *a){
/*16进制转换成10进制*/
int n=0;
for(int i=0;a[i]!='\0';i++)
{
if(a[i]>='0'&&a[i]<='9')
n=n*10+a[i]-'0';
if(a[i]>='a'&&a[i]<='z')
n=n*16+a[i]-'a'+10;
if(a[i]>='A'&&a[i]<='Z')
n=n*16+a[i]-'A'+10;
}
return n;
}
void num::f2(int n){
/*10进制转换成16进制*/

int len=0;
int t;
char b[50];

for(int k = 0; n != 0; k++)
{
t = n % 16;
if(t >= 0 && t < 10)
{
b[k] = t + '0';
}
else if(t >= 10 && t < 16)
{
b[k] = 'A' + t - 10;
}
len++;
n = n / 16;
}
for(int j = len - 1; j >= 0; j--)
{
cout<<b[j];
}
cout<<endl;
cout<<"len="<<len<<endl;
}
void num::f3(int n1){
//10进制转换成2进制
int c[50];
int len1=0;
int t,j;
for(int l = 0; n1 != 0; l++)
{
t = n1 % 2;
c[l]=t;
len1++;
n1 = n1 / 2;
}
for( j = len1 - 1; j >= 0; j--)
{
cout<<c[j];
}
cout<<endl;
cout<<"len1="<<len1<<endl;

}

void main()
{
num n;
int m;
char a1[20];
cin>>a1;
m = n.f1(a1);
n.f2(m);
n.f3(m);
}

VC6编译通过
太乙 2010-07-28
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
class A
{
public:
static int _16To10(char* a, int len);
static void _10To16(int n, char* b);
};
int A::_16To10(char* a, int len)
{
int res = 0;
for (int i = 0; i <len && a[i] != '\0'; ++i)
{
if (a[i] >= '0' && a[i] <= '9')
res = res*10 + a[i] - '0';
else if (a[i] >= 'a' && a[i] <= 'z')
res = res*16 + a[i] - 'a' + 10;
else if (a[i] >= 'A' && a[i] <= 'Z')
res = res*16 + a[i] - 'A' + 10;
}
return res;
}
void A::_10To16(int n, char* b)
{
int k = 0;
for(; n != 0; k++)
{
int t = n % 16;
if(t >= 0 && t < 10)
{
b[k] = t + '0';
}
else if(t >= 10 && t < 16)
{
b[k] = 'A' + t - 10;
}
n = n / 16;
}
b[k] = '\0';
}
int main()
{
char a[10];
int n=0;
int t;
cin>>a;
cout<<"a number="<<a<<endl;
cout<<"n=" << (n = A::_16To10(a, 10)) <<endl;
char b[50];
A::_10To16(n, b);
cout << b << endl;
return 0;
}
SLSnake 2010-07-28
  • 打赏
  • 举报
回复
你f2和f3申明的时候根本没有输入参数,后面使用的时候缺带参数
大哥,好好看看基础的东西吧.....
SLSnake 2010-07-28
  • 打赏
  • 举报
回复
char f1(char a);

改成char *a;

你定义的是char,但是在函数里当作数组用,当然出错了..
linguanghuisue 2010-07-28
  • 打赏
  • 举报
回复
我用的c++来的
错误:
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(12) : error C2556: 'int __thiscall num::f1(char)' : overloaded function differs only by return type from 'char __thiscall num::f1(char)'
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(7) : see declaration of 'f1'
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(12) : error C2371: 'f1' : redefinition; different basic types
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(7) : see declaration of 'f1'
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(15) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(17) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(17) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(18) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(19) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(19) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(20) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(21) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(21) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(22) : error C2109: subscript requires array or pointer type
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(33) : error C2065: 'n' : undeclared identifier
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(58) : error C2065: 'n1' : undeclared identifier
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(60) : error C2065: 't' : undeclared identifier
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(65) : error C2065: 'j' : undeclared identifier
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(79) : error C2664: 'f1' : cannot convert parameter 1 from 'char [20]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(80) : error C2660: 'f2' : function does not take 1 parameters
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(81) : error C2143: syntax error : missing ')' before ';'
C:\Documents and Settings\pzftt.PZF-40675D33299.000\桌面\16.cpp(81) : error C2660: 'f3' : function does not take 1 parameters
vanchristin 2010-07-28
  • 打赏
  • 举报
回复
有什么问题吗?
怎么光有代码
建议你去C版

64,676

社区成员

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

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