请我知道这些事情,只是我不知道应该添加什么循环语句以使其成为.as..requirments [英] Please I know these things just I don't know what loop statement I should add to make It .as..requirments

查看:67
本文介绍了请我知道这些事情,只是我不知道应该添加什么循环语句以使其成为.as..requirments的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以将此C程序转换为C ++,并使其不仅打印1到3的任何数量,例如输入1到AB C..Z,输入5到AAAAA ... BBBBB ..... ZZZZZ等这样.

Can anyone convert this C program into C++, and make it print any amount not only 1 to 3 like Enter 1 to A B C..Z , Enter 5 to AAAAA...BBBBB.....ZZZZZ, and other like this.

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
main()
{
    int i,j,k,n;
    

printf("Enter number(1-3)");

scanf("%d",&n);

if(n>=1 && n<=3)
{
    for(i=65;i<91;i++)
    {
         if(n==1)
         printf("\t %c",i);
    
        for(j=65;j<91;j++)
        {
           if(n==2)
           printf("\t %c%c",i,j);
               
               for(k=65;k<91;k++)
               {
                 if(n==3)
                 printf("\t %c%c%c",i,j,k);
                }
        }
      }
}
system("pause");
return 0;
}

推荐答案

与C ++无关.如果您真的不想在这里使用C,则将对printf的调用替换为std::cout,将对scanf的调用替换为std::cin.

并且只需确保包含适当的标头即可.真的没有什么了.
This has nothing to do with C++. If you really don''t want to use C here, then replace the calls to printf with std::cout and scanf with std::cin.

And just ensure the appropriate headers are included. There''s not anything more, really.


为什么你不停下来想一想你在问什么.您不需要递归来执行此操作(尽管我会花钱看一下您上交该代码并让您的老师请您解释一下). iostream.h不是C ++.不返回int的main()不是C ++.同样,如果您的老师给了您这些想法,那么您的课就很糟糕,您需要离开它.同上printf的使用.但是,要使C程序执行您想要的操作,只需停下来想一想您要问的是什么.您想输入一个数字,然后遍历字母并打印每个字母X次.查看您的代码,应该很清楚地知道为什么您的代码甚至没有针对数字1-3执行此操作.如果您无法解决问题,那么您肯定需要放弃编程.然后停下来思考.无需键入代码来处理您要支持的每个数字,而是您的代码中已有什么概念可让您执行某些代码特定次数?
Why don''t you stop and think about what you''re asking. You don''t need recursion to do this ( although I''d pay money to see you hand that code in and have your teacher ask you to explain it ). iostream.h is NOT C++. A main() that does not return an int is not C++. Again, if your teacher has given you these ideas, your class sucks and you need to leave it. Ditto the use of printf. However, to make a C program that does what you want, just stop and think about what you''re asking. You want to input a number, then work through the alphabet and print each letter X times. Looking through your code, it should be blatantly obvious why your code doesn''t even do that for the numbers 1-3. If you can''t work THAT out, you definitely need to give up on programming. Then stop and think. Instead of typing in code to deal with each number you want to support, what concept is ALREADY in your code that allows you to execute some code a specific number of times ?


''任何金额''有点自命不凡(您没有这样的计算能力...).无论如何,尽管您必须进行递归操作("要迭代是人类,要递归,要神圣"-L. Peter Deutsch),但您可能会做得比以前更好:
''Any amount'' is a bit pretentious (you''ve not such a computational power...). Anyway, you may do better than you did, though you''ve to go recursive ("To Iterate is Human, to Recurse, Divine" - L. Peter Deutsch):
void seq(char * str, unsigned int level, unsigned int depth)
{
  for (char c=''A''; c<=''Z''; c++)
  {
    str[level]=c;
    if ( level == depth-1 )
      printf("%s\t",str);
    else
      seq(str, level+1, depth);
  }
}
const int MAX_DEPTH = 10;
void main()
{
  char str[MAX_DEPTH+1] = {0};
  unsigned int depth;
  printf("enter a number between 1 and %d: ",  MAX_DEPTH );
  if (scanf("%u", &depth) != 1)
    return;

  depth %= MAX_DEPTH;
  seq(str,0, depth);
}



正如Rajesh已经指出的,在这里CC++之间的选择是不相关的.
:)



As Rajesh already noted, the choice between C or C++, is irrelevant here.
:)


这篇关于请我知道这些事情,只是我不知道应该添加什么循环语句以使其成为.as..requirments的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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