更新:
char* DecToBin(int n){
char* ptr = new char[];
char* CharReturn = new char[9];
CharReturn[0] = n % 2;
CharReturn[1] = n % 3;
ptr = CharReturn;
return ptr;
}
得到了其他一切,但我无法得到这个char *来返回任何实际值。如何为char *(指针)分配内存。如何实现像(x& 1),(x>> 1)这样的位功能,以及如何使用(x == 0),就像for循环中的参数一样?
原始问题
char*
使用switch语句将二进制转换为十进制,十进制转换为二进制,到目前为止,我的代码非常粗糙,完全没有完成。我在理解使用char*
时遇到问题,程序必须使用函数
int BinToDec(char* s);
char* DecToBin(int n);
我是初级程序员,这是我的第三个学期,我从未用C ++编程。这个作业适用于我的CPS 260,在Assembly类中编程。我尽力做到最好,我主要想知道我是否正朝着正确的方向努力。我知道找到二进制和十进制的算法很粗糙。
CODE:
#include <iostream>
using namespace std;
int BinToDec(char* s);
char* DecToBin(int n);
int BinToDec(char* BinIn){
int intOut = 0;
intOut = intOut + BinIn[0] * 128;
intOut = intOut + BinIn[1] * 64;
intOut = intOut + BinIn[2] * 32;
intOut = intOut + BinIn[3] * 16;
intOut = intOut + BinIn[4] * 8;
intOut = intOut + BinIn[5] * 4;
intOut = intOut + BinIn[6] * 2;
intOut = intOut + BinIn[7] * 1;
return intOut;
}
char* DecToBin(){
unsigned int intInput, Holder;
char charReturn[7];
char* ptr;
cout << "Please Enter the num number you wish to convert to Binary. \n";
cin >> intInput;
Holder = intInput % 2;
charReturn[0] = Holder;
Holder = intInput / 2;
charReturn[1] = Holder % 2;
ptr = charReturn;
return ptr;
}
int main()
{
bool done = false;
unsigned short int intSelect;
while (!done)
{
cout << "Please select a conversion type: \n";
cout << "1. Convert from Binary to Decimal \n";
cout << "2. Convert from Decimal to Binary \n";
cout << "3. Exit the program. \n";
cin >> (intSelect);
switch (intSelect)
{
case 1: //How to call BinToDec()
{
char* Input;
cout << "Please enter the 8-bit binary:\n";
cin >> (Input);
BinToDec(Input);
break; }
case 2: //How to call DecToBin()
{cout << "case 2\n";
cout << DecToBin();
system("pause");
break; }
case 3: //Exit
{cout << "The Program will now exit\n";
system("pause");
done = true;
break; }
default: //Check others
{cout << "Invalid Entry try again. \n\n";
return 0;
}
}
}
cout << "\n\n";
return 0;
}
任何输入都是非常有建设性的,即使它说我是一个可怕的程序员并走向完全错误的方向。 提前感谢您,即使您没有发布回复。
P.S。我已经用Java和Visual Basic编程,没有C ++经验我仍然在使用函数。你必须先申报吗?
答案 0 :(得分:0)
假设二进制整数的长度不超过8位。
调用BinToDec()
char Input[9];
cout << "Please enter the 8-bit binary:\n";
cin >> (Input);
cout << BinToDec(Input);
你需要创建一个至少有8 + 1个元素的char数组,char数组字符串的最后一个元素总是为0。
int BinToDec(char* BinIn){
int intOut = 0;
intOut = intOut + (BinIn[0]-'0') * 128;
//convert '0' or '1' to 0 or 1.
intOut = intOut + (BinIn[1]-'0') * 64;
intOut = intOut + (BinIn[2]-'0') * 32;
intOut = intOut + (BinIn[3]-'0') * 16;
intOut = intOut + (BinIn[4]-'0') * 8;
intOut = intOut + (BinIn[5]-'0') * 4;
intOut = intOut + (BinIn[6]-'0') * 2;
intOut = intOut + (BinIn[7]-'0') * 1;
return intOut;
}
内部DecToBin()
unsigned int intInput, Holder;
//char charReturn[7];
//if you make it 7, you can only convert from 0-63
char* ptr;
char* charReturn = new char[9];
cout << "Please Enter the num number you wish to convert to Binary. \n";
cin >> intInput;
Holder = intInput % 2;
charReturn[0] = Holder+'0';
//Holder is 0 or 1, you need to convert it to '0' or '1'
Holder = intInput / 2;
charReturn[1] = Holder % 2+'0';
//...
charReturn[8] = 0; //last character is 0 in char* style string.
ptr = charReturn;
return ptr;
您需要使用new
或malloc
创建一个数组,以便在返回函数后将其存储在内存中。