我在C中创建了一个itob版本但是没有用 [英] I have created a version of itob in C but doesnt work

查看:60
本文介绍了我在C中创建了一个itob版本但是没有用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建itob,一个将整数转换为a到b基数表示的函数,但无论何时我尝试运行它,它都会崩溃。有什么想法吗?

I tried creating itob, a function that turns an integer to a into a b base number representation but anytime i try to run it, it crashes. Any ideas?

#include <stdio.h>


void itob(int j, int k[], int z);

int main(void)
{
	int a, b, i;
	int c[5];
	a = 5;
	b = 10;
	itob(a, c, b);
	printf("%s", c);
	return 0;
}

void itob(int n, int s[], int b)
{
	int i=0;
	do
	{
		if (n%b>=10)
		    s[i++]=(n%b-10)+'a';
		else
		    s[i++]=n%b-'0';
	}
	while (n/=b>0);
}





我的尝试:



我尝试过使用getchar和scanf给a和b但是在我给出了第一个数字后它就停止了工作。



What I have tried:

I have tried using getchar and scanf to give a and b but after I gave the first number it stopped working.

推荐答案

嗯......因为没有检查你是否已超出输出数组的范围,并且它只有5宽,这可能会导致它。但即使你停止崩溃它也不会正常工作,因为:

Well...since there is no checking to see if you have gone outside the bounds of your output array, and it's only 5 wide, that might cause it. But it's not going to work properly even if you stop it crashing, given that:
printf("%s", c);



尝试将整个整数数组打印为字符串,它不是 - 你需要一个循环依次打印每个元素。



但这是一个简单的问题:只需使用调试器并找出发生了什么on。

我不知道你试图使用什么系统,所以我不能给你明确的指示,但是在 main 并逐步执行代码,查看变量内容以确切了解发生的情况。


Is trying to print your whole array of integers as a string, which it isn;t - you will need a loop to print each element in turn.

But this is a simple problem: just use the debugger and find out what is going on.
I have no idea what system you are trying to use so I can't give you explicit instructions, but put a breakpoint on the first line of main and step through your code, looking at the variable contents to find out exactly what is happening.


从代码中看,更符合逻辑s 作为一组字符。

这与前一行更加一致

From the code, it look more logical to have s as an array of chars.
This would be more consistent with previous line
s[i++]=n%b+'0';





对于匹配的那两行来说,这是一个很好的做法。



It is considered a good practice to those 2 lines that match

void itob(int j, int k[], int z);
void itob(int n, int s[], int b)





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



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

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

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



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候您接近一个错误。



建议:拿一张纸并尝试手工完成,您的程序应该使用相同的程序。



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[^]

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.


itob中的逻辑不正确。要找到一个基数,你可以继续找到余数并除以或找到该基数和除法的所有幂。

主要的是在编程之前你需要记下并考虑过程和问题的不同情况。在这种情况下,2-10之间很容易,但在10之后你将需要更多的字符,例如HEX。



The logic in itob is incorrect. To find a base number you can either keep finding the remainder and dividing OR find all powers of that base number and divide.
The main thing is before programming you need to write down and think about the process and different cases of the problem. In this case, between 2-10 is easy but after 10 you will need more characters eg HEX.

#include <stdio.h>
#include <string.h>
#define N 100
int itob(int decimal, char *basenumber_out, int base, int size);

int main(void)
{
	int input, base, result;
	char c[N];
	memset(c,0,N);//make all null character
	input = 5;
	base = 2;
	result=itob(a, c, b, N);
	if(x<0) return -1;
	printf("%s", c);
	return 0;
}

int itob(int decimal, char *basenumber_out, int base, int size)
{
    if(base<=0) return -1;//impossible
    char backwards[N];
    int count=0;
    memset(backwards,0,N);//make all null character
    if(base<=10)
    {
        // find remainder
        // divide by base
        // store in backwards array
        // increment count
    }
    else
    {
        // if base>10
        // need alphabet characters instead of 0-9
    }

    //copy string in reverse to array
	return 0;
}


这篇关于我在C中创建了一个itob版本但是没有用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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