简体   繁体   English

回文中的回文数检查

[英]Palindrome number checking in c

Here is my code, I have many things to improve in my code, but my concern right now is to stop the program from crashing. 这是我的代码,我的代码中有很多要改进的地方,但是现在我要担心的是阻止程序崩溃。 Thanks in advance! 提前致谢!

#include <stdio.h>

int IsPalindrome(int a);
int dec[20];
int a = 0;
int main()
{

    printf("Please insert your number:\n");
    scanf("%d", a);
    IsPalindrome(a);
  return 0;

}

int IsPalindrome(int a)
{
    int temp = 0;
    int count = 1;
    int p = 1;
    temp = a;
        while(temp > 10)
        {
            dec[count] = temp % 10;
            count = count+1;
            temp = temp / 10;
            printf("%d", count);
        }
    for(int i = 1; i < count; i++)
    {
            if (dec[i] != dec[count-i])
            {
                printf("Your number is not a Palindrome");
                return 1;
            }
    }
}

Side questions: 附带问题:

  • How do i support a number larger than 20 digits( or -how do I create an array w/o specifying it's size in advance, and I could still set values in it). 我如何支持大于20位的数字(或-如何创建一个不预先指定其大小的数组,仍然可以在其中设置值)。
  • Should my function be void or int (or some other parameter)? 我的函数应该为void还是int(或其他一些参数)?
  • int a = 0; scanf("%d", a); will lead to crush because it means it should store the data to somewhere which is invalid. 会导致崩溃,因为这意味着它将数据存储到无效的地方。
  • Use string to support large number. 使用字符串支持大数。
  • If you won't use what your function returns, make the return type of function void . 如果您不使用函数的返回值,请将函数的返回类型设置为void Note that the return type of main() should be int according to the standard. 请注意,根据标准, main()的返回类型应为int

Try this: 尝试这个:

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

#define BUFFER_SIZE 512

char* ReadNumber(void);
void IsPalindrome(const char* a);
int main(void)
{
    char* a;
    printf("Please insert your number:\n");
    a = ReadNumber();
    IsPalindrome(a);
    free(a);
    return 0;

}

char* ReadNumber(void) {
    char* ret = malloc(BUFFER_SIZE);
    size_t allocatedSize = BUFFER_SIZE;
    size_t readLen = 0;
    if (ret == NULL)
    {
        perror("malloc");
        exit(1);
    }
    for (;;)
    {
        int c = getchar();
        if (!isdigit(c))
        {
            if (c != EOF) ungetc(c, stdin);
            break;
        }
        ret[readLen++] = c;
        if (readLen >= allocatedSize)
        {
            ret = realloc(ret, allocatedSize += BUFFER_SIZE);
            if (ret == NULL)
            {
                perror("realloc");
                exit(1);
            }
        }
    }
    ret[readLen] = '\0';
    return ret;
}

void IsPalindrome(const char* a)
{
    size_t count = strlen(a);
    /* can't write as i < count - i - 1 because size_t may be unsigned */
    for(size_t i = 0; i + i + 1 < count; i++)
    {
            if (a[i] != a[count - i - 1])
            {
                printf("Your number is not a Palindrome");
                return;
            }
    }
}

Another solution, would be to read the number as a string. 另一种解决方案是将数字读取为字符串。 Then reverse the string and see if the reversed string is lexicographically equal to the original string. 然后反转该字符串,看看反转后的字符串在字典上是否等于原始字符串。

void strrev(char *s)
{
    char *start, *end;

    start = s;
    end = s + strlen(s) - 1;

    for (; end > start; --end, ++start) {
        char tmp;
        tmp = *start;
        *start = *end;
        *end = tmp;
    }
}
/* checks if string is a number. Only positive integers, or 0 */
int isnum(const char *s)
{
    int i;

    for (i = 0; s[i]; ++i)
        if (!isdigit(s[i]))
            return 0;

    return 1;
}
int main()
{
    char num[16], rev[16];
    fgets(num, 16, stdin);

    if (!isnum(num)) {
        fprintf(stderr, "not number\n");
        return 1;
    }

    strcpy(rev, num);
    strrev(rev);

    if (strcmp(rev, num) == 0)
        fprintf(stderr, "Palindrome\n");
    else fprintf(stderr, "No\n");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM