我正在尝试创建一个字母金字塔。
我的代码编译,最初有效,但在要求用户输入字母的初始提示后不会打印。这是我的代码目前的样子:
intmain(void {
char letter = 'A';
char display;
char output;
int row;
char spaces = ' ';
printf ("Please enter a lowercase letter!");
scanf ("%c", &letter);
while ((letter < 96) || (letter > 122)) {
printf ("That's not lowercase.");
scanf ("%c", &letter);
}
else if ((letter > 96) || (letter < 122)) {
while (1) {
letter = letter - 32;
printf ("%c", letter -1);
printf ("\n\n");
spaces = letter - 1;
putchar = (spaces);
letter++
spaces++
}
}
return 0;
}
}
最终结果应如下所示。假设用户输入了E
:
----------A
---------ABA
--------ABCBA
-------ABCDCBA
------ABCDEDCBA
因此,输入的字母将是金字塔底部的中心字母。
答案 0 :(得分:1)
尝试一下,对我来说,它像这样:
#include <iostream>
#include <string>
char initialCharacter = 'A';
char characterToPrintTo = 'J';
int levels;
void printSpaces(int n)
{
int i;
for(i = 0; i < n; i++)
{
std::cout<<" ";
}
}
int main()
{
levels = characterToPrintTo - initialCharacter + 1;
int numberOfCharacters = 2 * levels - 1;
int maxChar, diff;
for(int level = 1; level <= levels; level++)
{
maxChar = 2 * level - 1;
if(maxChar < numberOfCharacters)
diff = numberOfCharacters - maxChar;
else diff = 0;
printSpaces(diff/2);
for(int i = 0; i < maxChar; i++)
{
if(i < maxChar / 2 + 1)
std::cout<< (char) (initialCharacter + i);
else
std::cout<< (char) (initialCharacter + maxChar - i - 1);
}
printSpaces(diff/2);
std::cout<<std::endl;
}
return 0;
}
这是输出:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
ABCDEFGHIHGFEDCBA
ABCDEFGHIJIHGFEDCBA
答案 1 :(得分:0)
#include <iostream>
#include <string>
int main()
{
std::string letters{};
std::cout << "Enter a string of letters to create a Letter Pyramid from it: ";
getline(std::cin, letters);
size_t num_letters = letters.length();
int position {0};
// for each letter in the string
for (char c: letters) {
size_t num_spaces = num_letters - position;
while (num_spaces > 0) {
std::cout << " ";
--num_spaces;
}
// Display in order up to the current character
for (size_t j=0; j < position; j++) {
std::cout << letters.at(j);
}
// Display the current 'center' character
std::cout << c;
// Display the remaining characters in reverse order
for (int j=position-1; j >=0; --j) {
// You can use this line to get rid of the size_t vs int warning
auto k = static_cast<size_t>(j);
std::cout << letters.at(k);
}
std::cout << std::endl; // Don't forget the end line
++position;
}
return 0;
}
答案 2 :(得分:0)
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
getline(cin,s);
for(int i=0;i<s.length();i++){
int spaces=s.length()-i-1;
for(;spaces>0;spaces--){
cout<<" ";
}
for(int j=0;j<=i;j++){
cout<<s.at(j);
}
for(int k=i-1;k>=0;k--){
cout<<s.at(k);
}
cout<<endl;
}
return 0;
}
答案 3 :(得分:-3)
一旦你意识到这封信只是数字,这个问题就很容易了。您可以像任何其他数字一样添加和减去它们。
int main(int argc, char* argv[]) {
char letter = 0;
cout << "Enter a letter between A and Z";
cin >> letter;
cout << endl;
for (int i = 'A'; i <= letter; i++) {
for (int j = 'A'; j < i; j++)
cout << (char)j;
while (j >= 'A') {
cout << (char)j;
j--;
}
cout << endl;
}
return 0;
}