帮助创建列 [英] Help with creating columns

查看:65
本文介绍了帮助创建列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建名片,我希望有两列5张牌。这就是我到目前为止所拥有的。我仍然是c ++的新手,所以如果有人能告诉我我应该开始研究什么才能使我成为可能。


//名片

#include < iostream>

#include< string>



使用命名空间std;



const string NAME =" John Smith" ;;
//名称的字符串

const string ADDRESS ="1234 Road Street City,CA"; //地址的字符串

const string NUMBER ="(123)456-7890" ;;
// String电话号码

const string EMAIL =" name@gmail.com" ;; //电子邮件地址的字符串

const string LINE =" ____________________________________" ;; //用于创建分隔名片的简洁线的字符串

int main()

{

cout<< LINE<< endl;
//名片行

cout<< NAME<< endl;
//显示名称的行

cout<< ADDRESS<< ENDL; //显示地址的行

cout<< NUMBER<< endl;
//显示数字的行

cout<< EMAIL<< ENDL; //显示电子邮件的行

cout<< LINE<< endl;
//名片行

cout<< NAME<< endl;
//显示名称的行

cout<< ADDRESS<< ENDL; //显示地址的行

cout<< NUMBER<< endl;
//显示数字的行

cout<< EMAIL<< ENDL; //显示电子邮件的行

cout<< LINE<< endl;
//名片行

cout<< NAME<< endl;
//显示名称的行

cout<< ADDRESS<< ENDL; //显示地址的行

cout<< NUMBER<< endl;
//显示数字的行

cout<< EMAIL<< ENDL; //显示电子邮件的行

cout<< LINE<< endl;
//名片行

cout<< NAME<< endl;
//显示名称的行

cout<< ADDRESS<< ENDL; //显示地址的行

cout<< NUMBER<< endl;
//显示数字的行

cout<< EMAIL<< ENDL; //显示电子邮件的行

cout<< LINE<< endl;
//名片行





cin.get();

返回0 ;

}

解决方案

如果你想做相同的任务五次,最好是代码一次通过任务并将其嵌入循环中,重复五次。 这种方法的优点之一是b $ b    您可以轻松更改迭代次数。
    您只需要在一个地方进行更改。


要生成两列,您需要复制第二列中第一列的内容。 


    首先确定第二列缩进的距离。 这可能是行长的一半,也可能比最长的单行略多。

    对于您打算打印的每一行:将内容存储在字符串中;通过用空格填充将字符串大小调整为所需的缩进;将该行附加到此字符串的末尾。


您可以在迭代循环时动态构建每一行,或者在进入循环之前为每一行构建单独的字符串。


I'm creating business cards and I want have two columns with 5 cards. This is what I have so far. I am still new to c++ so if anyone could tell me what I should start looking into it in order for me to make this possible.

// Business Cards
#include <iostream>
#include <string>

using namespace std;

const string NAME = "John Smith"; // String for name
const string ADDRESS = "1234 Road Street City, CA"; // String for address
const string NUMBER = "(123) 456-7890"; // String for phone number
const string EMAIL = "name@gmail.com"; // String for email address
const string LINE = "____________________________________"; // String to create a clean line seperating the business cards
int main()
{
cout << LINE << endl; // Line for business card
cout << NAME << endl; // Line for displaying name
cout << ADDRESS << endl; // Line for displaying address
cout << NUMBER << endl; // Line for displaying number
cout << EMAIL << endl; // Line for displaying email
cout << LINE << endl; // Line for business card
cout << NAME << endl; // Line for displaying name
cout << ADDRESS << endl; // Line for displaying address
cout << NUMBER << endl; // Line for displaying number
cout << EMAIL << endl; // Line for displaying email
cout << LINE << endl; // Line for business card
cout << NAME << endl; // Line for displaying name
cout << ADDRESS << endl; // Line for displaying address
cout << NUMBER << endl; // Line for displaying number
cout << EMAIL << endl; // Line for displaying email
cout << LINE << endl; // Line for business card
cout << NAME << endl; // Line for displaying name
cout << ADDRESS << endl; // Line for displaying address
cout << NUMBER << endl; // Line for displaying number
cout << EMAIL << endl; // Line for displaying email
cout << LINE << endl; // Line for business card


cin.get();
return 0;
}

解决方案

If you want to do the same task five times, it would be better to code one pass through the task and imbed that in a loop that iterates five times.  Among the advantages of this approach
     You can change the number of iterations easily
     You only need to make a change in one place.

To produce two columns, you need to duplicate the content of column one in column two. 
     First decide how far you want column two indented.  This could be half the line length or it could be slightly more than the longest single line.
     For each line you intend to print: store the content in a string; resize the string to the desired indent by padding with blanks; append the line to the end of this string.

You could build each line on the fly as you iterate through the loop or you build a separate string for each line prior to entering the loop.


这篇关于帮助创建列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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