演员明楼个人资料:C++改错,求援!!!

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/30 00:49:04
#include<iosream.h>
#include<stdlib.h>
int Dec(char);

void main(void)
{
int i=0;char t[20];
cout<<"Input:";
cin.getline(t,20);
cout<<"Output:"<<Dec(t)<<endl;
}

int Dec(char s[])
{
int i,n;
for(i=0;s[i];i++;)
{
if (s[i]=='')break;
if (s[i]>='0'&&s[i]<='9') n=n*16+s[i]-'0';
else
if(s[i]>='a'&&s[i]<='f') n=n*16+s[i]-'a'+10;
else
if (s[i]>='A'&&s[i]<='F') n=n*16[i]-'A'+10;
else
{ cout <<"非法十六进制数!"<<endl; exit(1);
}
}
return n;
}
还有没有简单一点的答案啊,我是编程菜鸟啊,看不懂啊!

你的程序基本上没有问题,几个小的拼写和笔误,改了,试试吧
有一个地方要注意,就是溢出,20位的数超过int的表示范围了。

#include<iostream.h>
#include<stdlib.h>
int Hex(char[]);

void main(void)
{
int i=0;
char t[20];
cout<<"Input:";
cin.getline(t,20);
cout<<"Output:"<<Hex(t)<<endl;
}

int Hex(char s[])
{
int i,n=0;
for(i=0;s[i];i++)
{
if (s[i]>='0'&&s[i]<='9') n=n*16+s[i]-'0';
else
if(s[i]>='a'&&s[i]<='f') n=n*16+s[i]-'a'+10;
else
if (s[i]>='A'&&s[i]<='F') n=n*16+s[i]-'A'+10;
else
{ cout <<"·Ç·¨Ê®Áù½øÖÆÊý!"<<endl; exit(1);
}
}
return n;
}

先说一下,你的程序极多低级错误!!!
比如第一句include!!!
格式极其不规范!比如分号应该结束该行!
变量名用错也很离谱!16进制是Hex!Dec是十进制!
强烈建议看看《C++变成规范》或者《代码大全》!

先修改你的程序,把你错误的地方和用得不好的地方修正了,
包括你的方法名!比如16进制是Hex不是Dec!你的格式我也相应做了修改。
把程序列出如下:(注意平台的不同,比如,你在6.0环境下可能要把#include<iostream>改成#include<iostream.h>等等,有问题可以联系我,邮箱)
/**************************************************************************************************

调试平台: Windows Sever 2003 Enterprise Edition sp1 with .Net 2.0
编写环境: Visual Studio 2005 Team Edition
错误报告: KimiaZhu@sina.com

**************************************************************************************************/

#include<iostream>
#include<stdlib.h>
using namespace std;

int ConvertToHex(char s[]);

int _tmain(int argc, _TCHAR* argv[])
{
int i=0;char t[20];
cout<<"Input:";
cin.getline(t,20);
cout<<"Output:"<<ConvertToHex(t)<<endl;
return 0;
}

int ConvertToHex(char s[])
{
int i,n=0;
for(i=0;s[i]!='\0';i++)
{
//if (s[i]=='\0')
// break;
/*else */if(s[i]>='0'&&s[i]<='9')
n=n*16+s[i]-'0';
else if(s[i]>='a'&&s[i]<='f')
n=n*16+s[i]-'a'+10;
else if (s[i]>='A'&&s[i]<='F')
n=n*16+s[i]-'A'+10;
else
{
cout <<"非法十六进制数!"<<endl;
exit(-1);
}
}
return n;

}

//------------------------------------------------------------------------------

以下我给你提供另一个参考程序,若有兴趣可以看看,
基本同样的功能,可以接受8进制,10进制或16进制输入。
示例:你输入08表示八进制数字8(按照约定,8进制前加数字0),
你输入0x8a表示十六进制8a(按照约定,8进制前加数字0和字母x);
你直接输入数字表示10进制的,所有输入都将表示成十进制形式,你也可以输入PI,pi或E,e表示常量,也将得到该常量。
注意这里没有提供错误处理,你自己可以加上。

/**************************************************************************************************

问题描述: 写一个atoi(const char*)函数,它以一个包含数字的C风格字符串为参数并返囬与之对应的int值
备注: 错误处理有待增加
作者: KimiaZhu
调试平台: Windows Sever 2003 Enterprise Edition sp1 with .Net 2.0
编写环境: Visual Studio 2005 Team Edition
完成日期: 2006-6-20
错误报告: KimiaZhu@sina.com

**************************************************************************************************/

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
enum NUMBER_TYPE{ DEC, HEX, OCT, CONSTANT, OTHER};
NUMBER_TYPE type = DEC;

//*************************************************************************************************

namespace MySpace
{
double atoi(const char * ch);
NUMBER_TYPE Judge(const char * ch);
void Print(NUMBER_TYPE, double, const char *);
}

//*************************************************************************************************

double MySpace::atoi(const char * ch)
{
type = Judge(ch);
double num;
int i;
if(type == DEC || type == OCT)
{
num = *ch - '0';
i = 1;

while(ch[i] != '\0')
num = num * 10 + ch[i++] - '0';
return num;
}
else if(type == HEX)
{
int n = 0;
i = 3;
if(ch[2] - '0' > 9)
num = ch[2] - 'a'+ 10;
else
num = ch[2] - '0';
while(ch[i] != '\0')
{
if(ch[i] - '0' > 9)
n = ch[i++] - 'a'+ 10;
else
n = ch[i++] - '0';
num = num * 16 + n;
}
return num;
}
else if(type == CONSTANT)
{
if(strcmp(ch, "PI") == 0 || strcmp(ch, "pi") == 0)
{
return 3.141592653f;
//return Math::PI;
}
if(*ch == 'E' || *ch == 'e')
return 2.718281828;
}
else
return NULL;
return NULL;
}

//*************************************************************************************************

NUMBER_TYPE MySpace::Judge(const char * ch)
{
if(ch[0] < 58 && ch[0] > 48)
return DEC;
else if(ch[0] == '0' && ch[1] == 'x')
return HEX;
else if(ch[0] == '0' && ch[1] != 'x')
return OCT;
else if(strcmp(ch, "PI") == 0 || strcmp(ch, "E") == 0
|| strcmp(ch, "pi") == 0 || strcmp(ch, "e") == 0 )
return CONSTANT;
else
return OTHER;
}

//*************************************************************************************************

void MySpace::Print(NUMBER_TYPE typ, double no, const char * ch)
{
if(typ == OCT)
{
cout << "你输入的是八进制数:";
cout << std::oct << no << endl;
return;
}
if(typ == HEX)
{
cout << "你输入的是十六进制数:";
cout << std::hex << no << endl;
return;
}
if(typ == DEC)
{
cout << "你输入的是十进制数:";
cout << no << endl;
return;
}
if(typ == CONSTANT)
{
cout << "你所输入的是常量:";
cout << ch << " = " << no << endl;
}
else
{
cout << "Error!" << endl;
return;
}
}
//*************************************************************************************************
int main()
{
char * ch = new char[128];
cout << "请输入一个数,它将按炤char类型被读入:";
cin >> ch;
double i = MySpace::atoi(ch);
MySpace::Print(type, i, ch);
delete[] ch;
system("pause");
return 0;
}