数字系统转换器不适用于特定编译器 (Dev-C++)
Number systems converter doesn't work on a specific compiler (Dev-C++)
>选择数字 3(十进制到二进制) 具体来说,它适用于 Code:Blocks,还没有尝试过 Visual C++,但我们学校使用 Dev-C++,我无法弄清楚是什么导致了这个问题。我的意思是它在逻辑和语法上都是正确的。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define MAXDIGITS 100
void printmenu(void);
void getchoice(void);
void decitobinary(int str);
int main(void)
{
char choice;
printmenu();
getchoice();
system("cls");
while(1)
{
printf("nAgain? y/n: ");
scanf("n%c",&choice);
if(choice == 'y' || choice == 'Y')
{
system("cls");
main();
}
else if(choice == 'n' || choice == 'N')
exit(EXIT_SUCCESS);
else
{
printf("anInvalid input.n");
continue;
}
}
}
void printmenu(void)
{
printf("n3 - Decimal -> Binary");
printf("n19 - Exit Programn");
}
void getchoice(void)
{
int choice;
char number[MAXDIGITS];
int digits;
printf("nEnter choice: ");
scanf("n%d",&choice);
switch(choice)
{
case 3:
{
system("cls");
printf("Enter number: ");
scanf("n%d",&digits);
decitobinary(digits);
break;
}
case 19:
exit(EXIT_SUCCESS);
}
}
void decitobinary(int str)
{
int arraycntr = 0;
int number[arraycntr];
printf("n%d in binary is: ",str);
while(str > 0)
{
number[arraycntr] = str % 2;
str /= 2;
++arraycntr;
}
for(arraycntr -= 1;arraycntr >= 0;arraycntr--)
{
printf("%d",number[arraycntr]);
}
printf("n");
system("pause");
}
void decitobinary(int str)
{
int arraycntr = 0;
int number[arraycntr];
printf("n%d in binary is: ",str);
while(str > 0)
{
number[arraycntr] = str % 2;
str /= 2;
++arraycntr;
}
for(arraycntr -= 1;arraycntr >= 0;arraycntr--)
{
printf("%d",number[arraycntr]);
}
printf("n");
system("pause");
}
问题出在数字数组的初始化中。它应该是这样的:
int number[33];
33 位足以存储所有二进制数字。
这不是
答案。 只是需要考虑的事情。 对于相同的功能,您有大约 10 个相同的实现! 您是否考虑过将十进制和十进制的所有变体合并为一对函数:
void decitobase(int str, int base)
{
int arraycntr = 33;
int number[arraycntr] = {};
printf("n%d in base %d is: ",str, base);
while(str > 0)
{
number[arraycntr] = str % base;
str /= base;
++arraycntr;
}
for(arraycntr -= 1;arraycntr >= 0;arraycntr--)
{
printf("%d",number[arraycntr]);
}
printf("n");
system("pause");
}
void basetodeci(char str[], int base)
{
int arraycntr,digitcntr,power;
int value[MAXDIGITS];
int number;
arraycntr = 0;
digitcntr = 0;
number = 0;
power = 1;
while(str[arraycntr] != ' ')
{
value[arraycntr] = str[arraycntr] - '0'
++arraycntr;
}
digitcntr = arraycntr - 1;
for(digitcntr; digitcntr >= 0; digitcntr--)
{
number += value[digitcntr] * power;
power *= base;
}
printf("n%s base %d in decimal is: %ldn",str,base,number);
system("pause");
}
我不知道上面的代码是否比您提交的代码更正确,但我希望您了解我的意思。 另外不要忘记有一个 C 函数可以将字符串转换为您想要的任何基数中的整数:strtol 和 friends。
您使用的算法非常慢。相反,由于您正在处理二进制数,请使用按位运算符。
#include <stdio.h>
#include <stdlib.h>
#define BITS_N (sizeof(unsigned int) * 8)
#define MSB (1 << (BITS_N - 1))
void print_binary (unsigned int val)
{
for(size_t i=0; i<BITS_N; i++)
{
printf("%c", (val & MSB) ? '1' : '0' );
val <<= 1;
if( ((i+1)%8) == 0) // print a space every 8th digit
{
printf(" ");
}
}
}
int main()
{
unsigned int some_val = 0xBAADBEEF;
print_binary (some_val);
}
相关文章:
- C/C++编译器通常会删除重复的库吗
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- Win32编译器选项和内存分配
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 在线编译器中的分段C++没有打印消息
- 关于 PBC 库在 GCC 编译器在 DEV C++ 中使用
- 如何在 Windows 命令提示符下使用 Dev C++ 编译器编译 c++ 程序
- dev-c++ 编译器错误没有匹配函数
- 动态堆栈、编译时出错、dev编译器和g++
- 数字系统转换器不适用于特定编译器 (Dev-C++)
- dev 如何在C++中强制卸载库(Dev C++编译器)
- 链表中的线性搜索[语言:C++编译器:Dev Cpp 4.9.9.2]
- 如何将Dev-C++编译器设置为使用C库
- dev 我刚刚开始使用 DEV C++编译器。一些在涡轮编译器中运行良好的程序在开发编译器中没有运行。怎么办?
- Dev -Cpp编译器编译错误
- c++使用dev - c++编译器,我写了一个程序,从摄氏转换到华氏,但我一直得到错误的方程
- 递归的河内塔,访问冲突/分段错误,在dev c++编译器上
- 编译器构造-Dev-C++-[Build Error][main.o]错误1