不使用函数在C中进行二进制到八进制转换 [英] Binary to octal conversion in C without using function

查看:121
本文介绍了不使用函数在C中进行二进制到八进制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在c中使用二进制到八进制转换程序而不使用函数但我认为我的逻辑是不正确的。



我尝试了什么:



I have tried to make a program on binary to octal conversion in 'c' 'without using function but I think my logic is incorrect.

What I have tried:

//binary to octal conversion
#include<stdio.h>
#include<math.h>
int main()
{
	int binary,i=0,rem,c,count=0,x=0;
	printf("enter a binary number for conversion to decimal\n");
	scanf("%d",&binary);
	while(count<=3);
	{
	rem=binary%10;
	c=pow(2,i);
	x+=rem*c;
	i++;
	count++;
	}
	printf("the converted binary number to octal is:  %d",x);
	getch();
	return 0;
}

推荐答案

哦,是的,这是不正确的。

这是你的功课,所以没有代码!



但是......想一想。什么是八达?什么是八进制数?

答案是:基数8.八进制数只包含数字'0'到'7'(含),与Decimal(基数10)只包含数字'0'到'9的方式相同'包含,二进制(Base 2)只包含数字'0'和'1'。



您的代码甚至不尝试使用Octal ...



我强烈建议您花一些时间研究如何使用调试器 - 谷歌会帮助你,只需谷歌'C',这个名字你正在使用的C IDE和调试器,它应该给你一个解释。当你到达这个阶段时,它会为你节省很多头发!
Oh yes, it's incorrect.
This is your homework, so no code!

But...think about it. What is Octal? What are Octal numbers?
The answer is: base 8. Octal numbers contain only the digits '0' to '7' inclusive, in the same way that Decimal (base 10) contains only the digits '0' to '9' inclusive, and Binary (Base 2) contains only the digits '0' and '1'.

Your code doesn't even try to use Octal...

I'd strongly suggest that you invest some time in working out how to use the debugger - Google will help you there, just google 'C', the name of the C IDE you are using, and "Debugger" and it should give you an explanation. It will save you a lot of hair pulling when you get to this stage!


你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较做。

当代码没有达到预期的效果时,你就接近一个bug。



建议:拿一张论文并尝试手工完成,你的程序应该使用相同的程序。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.


这篇关于不使用函数在C中进行二进制到八进制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆