简体   繁体   English

递归函数逆序打印

[英]Recursive function printing in reverse order

(I'll start by noting this lab is low level 1st year programing, so high level stuff isn't usable right now for us) (我会首先注意到这个实验室是低级别的第一年编程,所以高级别的东西现在对我们来说不可用)

I was given a lab to write a program in C that would accept a number between 1 and 36, six times, then print out those numbers as a bar graph, where the 'bar graph' is a number of # equal to the input number.我得到了一个实验室,用 C 语言编写一个程序,该程序将接受 1 到 36 之间的数字,六次,然后将这些数字打印为条形图,其中“条形图”是 # 等于输入数字的数字.

eg 5 would be:例如 5 将是:

So far I have this:到目前为止,我有这个:

#include <stdio.h> 

void graphCreate();

int main(void)
{
    graphCreate();
}


void graphCreate()
{
    static int chartLoop = 1;
    int graphLength = 0;
    int graphNumber = chartLoop;

    while(graphLength > 36 || graphLength < 1)
    {
        printf("How long is chart %d?\t", graphNumber);
        scanf("%d", &graphLength); 
    }

    if(chartLoop < 6)
    {
        chartLoop++;
        graphCreate();
    }

    printf("\n%d.\t%d|", graphNumber, graphLength);
    while(graphLength > 0)
    {
        printf("#");
        graphLength--;
    }

}

And it does the output as expected... mostly:它按预期执行输出......主要是:

How long is chart 1?    5
How long is chart 2?    10
How long is chart 3?    15
How long is chart 4?    20
How long is chart 5?    25
How long is chart 6?    30

6.      30|##############################        
5.      25|#########################
4.      20|####################
3.      15|###############
2.      10|##########
1.      5|#####

However, I need the final outputs (the bars) in 1 -> 6 order, and it's reversed.但是,我需要 1 -> 6 顺序的最终输出(条形),而它正好相反。 What am I doing wrong that it's in reverse?我做错了什么,它是相反的?

If using the recursive way to solve the problem is a requirement, you need to store the outputs and then print them in reverse order.如果需要使用递归方式解决问题,则需要存储输出,然后以相反的顺序打印它们。

If recursive is not a requirement, you can loop 6 times to get and store the input numbers and print the bars in sequence.如果递归不是必需的,您可以循环 6 次以获取和存储输入数字并按顺序打印条形。

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

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