帮助一个简单的C语言编程练习 [英] Help with a simple C programming exercise

查看:117
本文介绍了帮助一个简单的C语言编程练习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C编程,有一点与编程动作难度的,我敢肯定,这是简单的人谁知道C,不幸的是你必须通过这次演习的规则行事。

下面是本练习:


  

有一个程序要求用户
  输入大写字母。使用嵌套
  循环,以产生一个金字塔图案
  像这样的:

  A   ABA  ABCBA ABCDCBAABCDEDCBA


  
  

这个模式应该延伸到
  输入的字符。例如,该
  preceding模式将导致从
  E.提示的输入值:使用一个外
  循环处理中的行。用三
  在一排内部循环,一个处理
  的空间,一个用于打印字母
  以升序,和一个用于
  印刷字母降序排列。


所以,我得到了这个远:

 的#include<&stdio.h中GT;诠释主要(无效){ 诠释行;
 INT空间; 焦炭ASC;
 字符说明;
 字符输入; 的printf(请输入一个大写字母:);
 scanf函数(%C,&安培;输入); 为(行='A';行< =输入;行++){
  对于(空格=输入;空间>的行; spaces--){
   的printf();
  }
  对于(ASC ='A'; ASC< =行; ASC ++){
   的printf(%C,ASC);
  }
  对于(DESC = ASC - 2;递减> =行; desc--){
   的printf(%C,DESC);
  }
  的printf(\\ n);
 }
 返回0;
}


解决方案

您是非常接近:

 为(DESC = ASC  -  2;递减> ='A'; desc--){

请注意,第二个内循环后,递增行+ 1 。那么你初始化说明行 - 1 。你应该能够明白为什么方式> =行是错误的,会导致没有迭代

正确的条件仅仅是方式> ='A'

I'm new to C programming, having a bit of difficulty with a programming exercise, I'm sure this is simple for anyone who knows C, unfortunately you have to play by the rules of the exercise.

Here's the exercise:

Have a program request the user to enter an uppercase letter. Use nested loops to produce a pyramid pattern like this:

    A 

   ABA

  ABCBA

 ABCDCBA

ABCDEDCBA

The pattern should extend to the character entered. For example, the preceding pattern would result from an input value of E. Hint: Use an outer loop to handle the rows. Use three inner loops in a row, one to handle the spaces, one for printing letters in ascending order, and one for printing letters in descending order.

So I got this far:

#include <stdio.h>

int main(void) {

 int rows;
 int spaces;

 char asc;
 char desc;
 char input;

 printf("Please enter an uppercase letter: ");
 scanf("%c", &input);

 for (rows = 'A'; rows <= input; rows++) {
  for (spaces = input; spaces > rows; spaces--) {
   printf(" ");
  }
  for (asc = 'A'; asc <= rows; asc++) {
   printf("%c", asc);
  }
  for (desc = asc - 2; desc >= rows; desc--) {
   printf("%c", desc);
  }
  printf("\n");
 }
 return 0;
}

解决方案

You're very close:

for (desc = asc - 2; desc >= 'A'; desc--) {

Note that after the second inner loop, asc is rows + 1. You're then initializing desc to rows - 1. You should be able to see why >= rows is wrong, and will result in no iterations.

The correct condition is simply >= 'A'.

这篇关于帮助一个简单的C语言编程练习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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