如何使文本对齐啊!
void output(struct BOOK* s)
{
int t = 0;
system("cls");
struct BOOK* p = (struct BOOK*)malloc(sizeof(struct BOOK));
p = s;
gotoxy(5, 6); printf("------------------------------------------------------------------------------------------------------------------------------\n");
gotoxy(10, 8); printf("图书登录号 书名 作者 分类号 出版单位 出版时间 图书价格 图书数量\n");
gotoxy(5, 10); printf("------------------------------------------------------------------------------------------------------------------------------\n");
while (p->next != NULL)
{
gotoxy(10, 11 + t); printf("%s", p->next->ISBN);
gotoxy(25, 11 + t); printf("%s", p->next->bookname);
gotoxy(38, 11 + t); printf("%s", p->next->writer);
gotoxy(58, 11 + t); printf("%s", p->next->ICS);
gotoxy(70, 11 + t); printf("%s", p->next->publisher);
gotoxy(87, 11 + t); printf("%s", p->next->data);
gotoxy(99, 11 + t); printf("%g", p->next->price);
gotoxy(110, 11 + t); printf("%d\n", p->next->num);
t = t + 2;
p = p->next;
}
}
使用格式串输出。如:
%-10s
-代表左对齐,10表示字段宽度,s是字符串格式
%7.2f
f表示浮点数,整个占7字符宽,2位小数
标题和列表内容,使用相同的格式串。更多内容可参考:https://blog.csdn.net/xpamdroid/article/details/121632221
构造函数是在创建对象的时候进行调用,主要用来对对象成员变量进行初始化赋初值。一个对象可以有多个构造函数,可以重载构造函数(根据参数列表的不同)。
析构函数是在对象生命周期结束时释放对象所占用的资源。析构函数是特殊的类成员函数,他的名字与类名相同,没有返回值,没有参数不可以随意调用与重载,只由系统自动调用。
回答:
在C++图书信息管理系统中如何实现文本对齐?要实现文本对齐,需要确定每个字段的宽度。可以计算每个字段中字符的数量,然后根据最长字段的字符数量设置每个字段的宽度。可以使用setw()函数设置字段宽度,如下所示:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout << left << setw(20) << "Book Name" << setw(20) << "Author" << setw(10) << "Price" << setw(10) << "Quantity" << endl;
cout << setfill('_') << setw(60) << "" << endl;
//假设有三本书
cout << left << setw(20) << "C++ Primer" << setw(20) << "Stanley B. Lippman" << setw(10) << "35.0" << setw(10) << "3" << endl;
cout << left << setw(20) << "Effective C++" << setw(20) << "Scott Meyers" << setw(10) << "40.0" << setw(10) << "5" << endl;
cout << left << setw(20) << "The C++ Programming Language" << setw(20) << "Bjarne Stroustrup" << setw(10) << "50.0" << setw(10) << "2" << endl;
return 0;
}
使用setw()函数来设置每个字段的宽度,使用setfill()来设置每个字段填充的字符。通过如上代码即可实现C++图书信息管理系统中的文本对齐。