python写一个类600行代码_C++语言编写一个大概300~600行的程序解决一个实际问题。(必须使用类)...

展开全部

从今天e68a843231313335323631343130323136353331333337626237凌晨3点多,一直写代码写到现在,除了吃饭没休息过。希望这个程序对你有所帮助。如果有任何疑惑的地方,可以继续提问、交流。附件里有源代码文件。

代码已经超过600行,这里只有部分代码,因为已经超过字数限制。其余代码可在源文件里查看。

9be4202fa3f8b7cf8fb1e7fd9a989fcc.png

eb951f356a45013ba55a2d9f5cb9ebaa.png

17a6d19940591d13c8d889133068e3d7.png

66dc3531f8174b8fbdad99ced498bb71.png

#include 

#include 

#include 

using namespace std;

#define Mc_MaxByte_MenuItem       61  //菜单项字符串长度最多61字节

#define Mc_MaxBufferSize          17  //缓存最大字节数。因为该程序只支持输入小于等于2字节的整型,而2字节整型的二进制位数有16位,所以需要17个字节保存二进制字符串,最后一个为结束符。

#define Mc_Back                  (-1) //返回上一级菜单

#define Mc_Exit                  (-2) //退出程序

#define Mc_Error                 (-3) //输入错误

#define Mc_Binary                 0   //二进制

#define Mc_Octonary               1   //八进制

#define Mc_Decimalism             2   //十进制

#define Mc_Hexadecimal            3   //十六进制

class Calculator //计算器类

{

protected:

char         chr1Buffer[Mc_MaxBufferSize]; //用于接收用户输入的数学算数运算式,如:“23+6*5-400/20=”

bool         Chr1SameAsInt(char* chrPt,int number); //判断2个整数是否相等。前一个是用字符串表示的整数,后一个就是int型的整数。

int          Chr1ToInt(const char* chrPt); //将用字符串表示的整数转换为int型整数

int          Menu(char* chrPtMenuTitle,char chr2MenuItem[][Mc_MaxByte_MenuItem], int intItemCount,bool bCreateBackItem); //显示一个菜单。最后一个参数表示:是否创建“返回”选项

void         ClearScreen();            //清屏

void         ShowInf(const char* chrInf);    //显示一条信息

void         NextLine(int count);      //换行

int          NumberFormTransitionUI(char* title); //“整数的进制转换”功能界面

void         InputToBuffer(const char* title);

const char*  GetBuffer();

void         (Calculator::*funPt)(const char* chrSource,char* chrResult); //函数指针

int          NumberFormTransition(int source,int target); //进制转换函数。source源数据进制形式,target目标进制形式

void         BinaryToOctonary(const char* chrPtBinary,char* chrPtOctonaryResult); //二进制转八进制。得出的结果将用字符串表示,且用chrPtOctonaryResult接收转换后的结果

void         BinaryToDecimalism(const char* chrPtBinary,char* chrPtDecimalismResult); //二进制转十进制

void         BinaryToHexadecimal(const char* chrPtBinary,char* chrPtHexadecimalResult); //二进制转十六进制

void         OctonaryToDecimalism(const char* chrPtOctonary,char* chrPtDecimalismResult); //八进制转十进制

void         OctonaryToBinary(const char* chrPtOctonary,char* chrPtBinaryResult); //八进制转二进制

void         OctonaryToHexadecimal(const char* chrPtOctonary,char* chrPtHexadecimalResult); //八进制转十六进制

void         DecimalismToBinary(const char* chrPtDecimalism,char* chrPtBinaryResult); //十进制转二进制

void         DecimalismToOctonary(const char* chrPtDecimalism,char* chrPtOctonaryResult); //十进制转八进制

void         DecimalismToHexadecimal(const char* chrPtDecimalism,char* chrPtHexadecimalResult); //十进制转十六进制

void         HexadecimalToDecimalism(const char* chrPtHexadecimal,char* chrPtDecimalismResult); //十六进制转十进制

void         HexadecimalToOctonary(const char* chrPtHexadecimal,char* chrPtOctonaryResult); //十六进制转八进制

void         HexadecimalToBinary(const char* chrPtHexadecimal,char* chrPtBinaryResult); //十六进制转二进制

public:

int          UI();                   //计算器界面

};

int Calculator::UI()

{

int code = !Mc_Exit, CodeTmp = 1;

char chr2Menu[][Mc_MaxByte_MenuItem] = { "数的进制转换" };

while (code != Mc_Exit)

{

code = Menu("计算器", chr2Menu, 1, false);

switch (code)

{

case 1:

ClearScreen();

CodeTmp = NumberFormTransitionUI(chr2Menu[0]);

break;

case Mc_Error:

ShowInf("\n  \n  ");

getch();

ClearScreen();

}

if (CodeTmp == Mc_Exit)

code = Mc_Exit;

}

return code;

}

bool Calculator::Chr1SameAsInt(char* chrPt,int number)

{

int len = strlen(chrPt) - 1;

while(number && len >= 0)

{

if (chrPt[len] - 48 != number % 10)

break;

len --, number /= 10;

}

if (len 

return true;

return false;

}

int Calculator::Chr1ToInt(const char* chrPt)

{

int len = strlen(chrPt), num = 0, i;

for (i = 0; len > 0; len --, i ++) //将用字符串表示的数字转换成int整数

num += (chrPt[i] - 48) * (int)pow(10, len - 1);

return num;

}

int Calculator::Menu(char* chrPtMenuTitle,char chr2MenuItem[][Mc_MaxByte_MenuItem],int intItemCount,bool bCreateBackItem)

{

int index, choice = 0, len, i;

cout<"<

for (index = 1; index <= intItemCount; index ++)

cout<

if (bCreateBackItem)

cout<

cout<

InputToBuffer("请输入相应选项的序号:");

if (bCreateBackItem)

{

if (Chr1SameAsInt(chr1Buffer, index - 1))

return Mc_Back;    //用户选择了返回选项

}

if (Chr1SameAsInt(chr1Buffer, index))

return Mc_Exit;    //用户选择了退出选项

len = strlen(chr1Buffer);

for (i = 0; len > 0; len --, i ++) //将用字符串表示的数字转换成int整数

choice += (chr1Buffer[i] - 48) * (int)pow(10, len - 1);

if (choice  index)

return Mc_Error;

return choice;

}

void Calculator::ClearScreen()

{

system("cls");

}

void Calculator::ShowInf(const char* chrInf)

{

cout<

}

void Calculator::NextLine(int count)

{

while (count-- > 0)

{

cout<

}

}

void Calculator::InputToBuffer(const char* title)

{

cout<

cin>>chr1Buffer;

}

const char* Calculator::GetBuffer()

{

return chr1Buffer;

}

void Calculator::BinaryToOctonary(const char* chrPtBinary,char* chrPtOctonaryResult)

{

int index, x, numTmp = 0;

bool  bBreak;

const char* chrPt;

char chr1Tmp[6]; //十进制65535等于八进制177777,这个最大值八进制数有6位数,所以至少需要6个字节。这个临时数组所保存的字符串不需要结束符。

chrPt = chrPtBinary + strlen(chrPtBinary) - 1; //让chrPt指向最后一个字符

index = 0, bBreak = false;

while (bBreak == false)

{

for (x = 0, numTmp = 0; x 

{ // 01011011 -->每3位二进制数对应一个八进制数。从最低位,右边开始:011=3,011=3,01=1。则该二进制数的八进制形式为133(最后结果需要反序)

if (chrPt > chrPtBinary)

numTmp += (*chrPt - 48) * (int)pow(2, x);

else

{

numTmp += (*chrPt - 48) * (int)pow(2, x);

bBreak = true;

break;

}

}

chr1Tmp[index ++] = numTmp + 48;

}

if (index > 1)

{

for (-- index; index > 0; index --)

if (chr1Tmp[index] != '0')

break;

}

else

{

chr1Tmp[0] = '0';

index = 0;

}

for (; index >= 0; chrPtOctonaryResult ++, index --)

*chrPtOctonaryResult = chr1Tmp[index];

*chrPtOctonaryResult = '\0'; //给返回参数加上结束符

}

void Calculator::DecimalismToBinary(const char* chrPtDecimalism,char* chrPtBinaryResult) //十进制转二进制

{

int num = Chr1ToInt(chrPtDecimalism), index = 0;

char chr1Tmp[16];

while (num)

{

chr1Tmp[index ++] = num % 2 + 48;

num /= 2;

}

if (!index)

{

chr1Tmp[0] = '0';

index = 1;

}

for(-- index; index >= 0; chrPtBinaryResult ++, index --)

*chrPtBinaryResult = chr1Tmp[index];

*chrPtBinaryResult = '\0';

}

void Calculator::DecimalismToHexadecimal(const char* chrPtDecimalism,char* chrPtHexadecimalResult) //十进制转十六进制

{

int num = Chr1ToInt(chrPtDecimalism), index = 0, tmp;

char chr1Tmp[16];

while (num)

{

tmp = num % 16;

num /= 16;

switch (tmp)

{

case 10:

chr1Tmp[index ++] = 'A';

break;

case 11:

chr1Tmp[index ++] = 'B';

break;

case 12:

chr1Tmp[index ++] = 'C';

break;

case 13:

chr1Tmp[index ++] = 'D';

break;

case 14:

chr1Tmp[index ++] = 'E';

break;

case 15:

chr1Tmp[index ++] = 'F';

break;

default:

chr1Tmp[index ++] = tmp + 48;

}

}

if (!index)

{

chr1Tmp[0] = '0';

index = 1;

}

for(-- index; index >= 0; chrPtHexadecimalResult ++, index --)

*chrPtHexadecimalResult = chr1Tmp[index];

*chrPtHexadecimalResult = '\0';

}

void Calculator::HexadecimalToDecimalism(const char* chrPtHexadecimal,char* chrPtDecimalismResult) //十六进制转十进制

{

int x, index, NumTmp = 0;

const char* chrPt = chrPtHexadecimal + strlen(chrPtHexadecimal) - 1;

char chr1Tmp[4];

for ( x = 0; chrPt > chrPtHexadecimal; chrPt --)

{

switch (*chrPt)

{

case 'a':

case 'A':

NumTmp += 10 * (int)pow(16, x ++);

break;

case 'b':

case 'B':

NumTmp += 11 * (int)pow(16, x ++);

break;

case 'c':

case 'C':

NumTmp += 12 * (int)pow(16, x ++);

break;

case 'd':

case 'D':

NumTmp += 13 * (int)pow(16, x ++);

break;

case 'e':

case 'E':

NumTmp += 14 * (int)pow(16, x ++);

break;

case 'f':

case 'F':

NumTmp += 15 * (int)pow(16, x ++);

break;

default:

NumTmp += (*chrPt - 48) * (int)pow(16, x ++);

}

}

switch (*chrPt)

{

case 'a':

case 'A':

NumTmp += 10 * (int)pow(16, x);

break;

case 'b':

case 'B':

NumTmp += 11 * (int)pow(16, x);

break;

case 'c':

case 'C':

NumTmp += 12 * (int)pow(16, x);

break;

case 'd':

case 'D':

NumTmp += 13 * (int)pow(16, x);

break;

case 'e':

case 'E':

NumTmp += 14 * (int)pow(16, x);

break;

case 'f':

case 'F':

NumTmp += 15 * (int)pow(16, x);

break;

default:

NumTmp += (*chrPt - 48) * (int)pow(16, x ++);

}

for (index = 0; NumTmp; index ++)

{

chr1Tmp[index] = NumTmp % 10 + 48;

NumTmp /= 10;

}

if (!index)

{

chr1Tmp[0] = '0';

index = 1;

}

for (-- index; index >= 0; chrPtDecimalismResult ++, index --)

*chrPtDecimalismResult = chr1Tmp[index];

*chrPtDecimalismResult = '\0';

}

void Calculator::HexadecimalToOctonary(const char* chrPtHexadecimal,char* chrPtOctonaryResult) //十六进制转八进制

{

char chr1Tmp[6];

HexadecimalToDecimalism(chrPtHexadecimal, chr1Tmp);  //先将十六进制转化为十进制

DecimalismToOctonary(chr1Tmp, chrPtOctonaryResult);  //再将十进制转化为八进制

}

void Calculator::HexadecimalToBinary(const char* chrPtHexadecimal,char* chrPtBinaryResult) //十六进制转二进制

{

char chr1Tmp[6];

HexadecimalToDecimalism(chrPtHexadecimal, chr1Tmp);  //先将十六进制转化为十进制

DecimalismToBinary(chr1Tmp, chrPtBinaryResult);      //再将十进制转化为二进制

}

int main()

{

Calculator cal;

cal.UI();

return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值