如何在 C 中制作一个将 int 替换为 char 的程序(不使用 while/for)?

Posted

技术标签:

【中文标题】如何在 C 中制作一个将 int 替换为 char 的程序(不使用 while/for)?【英文标题】:How do I make a program that replaces int to char (without using while/for) in C? 【发布时间】:2022-01-20 10:17:39 【问题描述】:

我是编程新手,正在努力应对挑战。我正在尝试做的是一个程序,它读取整数并返回它用“。”替换偶数。和带有“-”的奇数(从单位开始,然后是十,然后是十万。所以像 8878 这样的数字被向后读取:8788)。

例如:

输入:

8878
2122
47

输出:

.-..
..-.
-.

我的两个问题如下:

如何使此代码仅转换位数。例如,我的程序中的“47”返回“-...”而不是“-”。这是我的目标。

如何让这段代码在完成 10 个输入之前始终要求下一个输入(并且不使用 while/for)?

#include <stdio.h>

int main() 
int number;

scanf("%d", &number);

int unit = number % 10;
int ten = number / 10 % 10;
int hundred = number / 100 % 10;
int thousand = number / 1000 % 10;

char even = '.';
char odd = '-';

// unit

if (unit % 2 == 0) 
    printf("%c", even);
 else if (unit % 2 != 0) 
    printf("%c", odd);
 else 
    printf("");


// ten

if (ten % 2 == 0) 
    printf("%c", even);
 else if (ten % 2 != 0) 
    printf("%c", odd);
 else 
    printf("");


// hundred

if (hundred % 2 == 0) 
    printf("%c", even);
 else if (hundred % 2 != 0) 
    printf("%c", odd);
 else 
    printf("");


// thousand

if (thousand % 2 == 0) 
    printf("%c", even);
 else if (thousand % 2 != 0) 
    printf("%c", odd);
 else 
    printf("");


return 0;

【问题讨论】:

如果你不能使用while/for,那么递归可能就是你需要的。 如何在我的代码中做到这一点?我是初学者 Recursive functions 是一个著名的编程概念。您可能可以谷歌并找到很多关于它的资源。 但是我该如何解决我的第一个疑问呢? @M.A.递归中的停止条件将决定代码何时停止。 【参考方案1】:

在编写代码之前,准确分析需求可能会有所帮助。我们有:

没有循环:好的,这暗示要使用递归 处理相反数中的数字:好的,我们可以使用数字模10然后将数字除以10来提取相反数中的数字 显示. 表示偶数,' 表示奇数:好的,最后一位是偶数,当且仅当数字是 - 是的,不需要取模... 我们将每行显示一个已处理数字:好的,在每个数字后写一个新行 只处理正数:好的,我们将使用unsigned int 类型

角盒

0 数字应显示为.,而它将是我们递归中的标记值:将处理拆分为一个测试 0 值的外部函数和一个处理数字的递归内部函数.

现在编写 C 代码变得微不足道:

#include <stdio.h>

// recursively discrimates even/odd digits
void do_describe(unsigned int n) 
    if (n == 0) return; // the sentinel aka the stop condition
    putchar(n % 2 ? '-' : '.');
    do_describe(n / 10);


// processes one number and displays it on its own line
void describe(unsigned int n) 
    // first the 0 corner case
    if (n == 0) 
        putchar('.');
    
    else 
        do_describe(n);
    
    putchar('\n');


int main() 
    // external loop: read integers one at a time
    for (;;) 
        unsigned int n;
        // stop when not a positive integer or on end of file
        if (scanf("%u", &n) != 1) break;
        describe(n);
    
    return 0;


在上面的代码中,main 仍然包含一个循环,因为它在 C 中更惯用,并且比递归更健壮/高效。但它可以很容易地转换为递归函数:

int recursive_loop(unsigned int max) 
    unsigned int n;
    if (max == 0) return 0;   // again the stop condition for recursion
    if (1 != scanf("%u", &n)) return 0;
    describe(n);
    return recursive_loop(max - 1);


int main() 
    // external recursive loop: read at most 10 integers
    recursive_loop(10);
    return 0;

【讨论】:

非常感谢您的回复!只是一个观察,我在帖子中说它总是要求一个新的输入,但我对其进行了编辑并将其设置为 10 个输入的限制。我怎么能这样做并删除代码中的“for”?你能编辑你的代码吗? @M.A. : 看看我的编辑... 你完美地回答了我所有的问题。非常感谢!

以上是关于如何在 C 中制作一个将 int 替换为 char 的程序(不使用 while/for)?的主要内容,如果未能解决你的问题,请参考以下文章

c语言:如何将字符串中指定的字符替换为另一个指定字符

如何将char * argv [1]转换为int并在C中重复打印而不发出警告

怎样将object转化为char

将 char 转换为 int [C]

Java - char、int 转换

如何在 c 中使用 void * 制作通用函数?