计算一个数字中有多少个X数 [英] Counting how many X numbers there are in a number

查看:139
本文介绍了计算一个数字中有多少个X数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现一个代码,其中输出是用户输入的数字的频率,也是由数字输入的数字。我相信我的代码几乎是正确的,字符声明查询可能正在发生。

这是代码



谢谢



我尝试过:



  #include   <   stdio.h  >  
#include < stdlib.h >

int main( void ){
int number,elements,count = 0 ;
char *字母,查询;
printf( 输入一个数字,我们可以算:);
scanf( %d,& number);
printf( 这个数字有多少个数字?:);
scanf( %d,& elements);
printf( 您想要计算哪个数字?:);
scanf( %c,& query);

// 分配一个字符串,这样我们就可以完成循环并计算
// 查询频率
letters =( char *)malloc(elements * sizeof char ));
sprintf(letters, %d,number);


for int i = 0 ; i< elements + 1; i ++)
if (letters [i] == query)
count ++;
}

printf( 有%d个数字\ n,count);

}

解决方案

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



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

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

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



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


尝试下面的代码:

  #include   <   stdio.h  >  
#include < stdlib.h >

int main( void
{
char * cpLetters;
char cQuery;
int iNumber;
int iDigits;
int iCount;
int iIndex;

// 输入数字
printf( 键入一个数字,以便我们可以计算:);
scanf( %d,& iNumber);
printf( 这个数字有多少个数字?:);
scanf( %d,& iDigits);
printf( 您想要计算哪个数字?:);
scanf( %c,& cQuery);
// 将数字转换为字符串
cpLetters =( char *)malloc(iDigits + 1 );
sprintf(cpLetters, %d,iNumber);
iCount = 0 ;
for (iIndex = 0 ; iIndex< iDigits + 1 ; iIndex ++)
{
if (cpLetters [iIndex] == cQuery)
iCount ++;
}
免费(cpLetters);
// 显示结果
printf( 有%d个数字与查询'%c'\ n,iCount,cQuery匹配);
}



请注意,在动态分配用于保存字符串的内存时,必须再允许一个字节的内存空间,因为它们都有一个0x00 null终止字符字符串的结尾。



在我的系统上,我无法获得scanf(%c,& cQuery);工作,但没有专业人士会使用scanf()来获取数据到应用程序,因为它是如此脆弱和不可移植。



另请注意for()循环必须递增索引,以便if可以看到下一个字符。



我希望这会有所帮助。

:)


I have to implement a code where the output is the frequency of a number typed by the user in a number also typed by the . I believe my code is almost right, there's just probably something going on with the character declaration "query".
Here is the code

Thank you

What I have tried:

#include <stdio.h>
#include <stdlib.h>

int main (void){
	int number, elements, count=0;
	char *letters, query;
	printf("Type a number so we can count: ");
	scanf("%d", &number);
	printf("How many numbers does this number have?: ");
	scanf("%d", &elements);
	printf("Which number do you want to count?: ");
	scanf("%c", &query);

	//allocate a string so we can go through a loop and count
	//the frequency of query
	letters = (char*)malloc(elements * sizeof(char));
	sprintf(letters, "%d", number);


	for (int i=0; i<elements+1; i++)
		if (letters[i] == query)
			count++;
	}

	printf("there are %d numbers\n", count);

}

解决方案

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.


Try the following code:

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
     char *    cpLetters ;
     char      cQuery ;
     int       iNumber ;
     int       iDigits ;
     int       iCount ;
     int       iIndex ;

// Input numbers
     printf ("Type a number so we can count: ");
     scanf ("%d", & iNumber) ;
     printf ("How many numbers does this number have?: ");
     scanf ("%d", & iDigits) ;
     printf ("Which number do you want to count?: ");
     scanf ("%c", & cQuery) ;
// Convert number to string
     cpLetters = (char *) malloc (iDigits + 1) ;
     sprintf (cpLetters, "%d", iNumber) ;
     iCount = 0 ;
     for (iIndex = 0 ; iIndex < iDigits + 1 ; iIndex ++)
     {
          if (cpLetters [iIndex] == cQuery)
               iCount ++ ;
     }
     free (cpLetters) ;
// Show result
     printf ("There are %d digits which match the query '%c'\n", iCount, cQuery) ;
}


Notice you have to allow one more byte of memory space when dynamically allocating memory which is to hold strings because they all have a 0x00 null terminating character at the end of the string.

On my system I had trouble getting "scanf ("%c", & cQuery) ;" to work, but then no professional would use scanf() to get data into an application because it's so flakey and non-portable.

Also notice that the for() loop must increment the index so the "if" can see the next character.

I hope this helps.
:)


这篇关于计算一个数字中有多少个X数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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