gpt4 book ai didi

c++ - 如果字段中的字符数不同,则输出时分隔符会丢失

转载 作者:行者123 更新时间:2023-12-02 10:20:15 25 4
gpt4 key购买 nike

我在格式化方面有问题。我的程序收集有关学生的信息,然后它应该输出用户输入的信息。
结论应该精确地出现在这些列中(我指的是表格和“||”)。
问题在于,对于不同大小的字段,这些分隔符“||”为我迷路了,总的来说,这看起来很难看。可以做些什么,以使所有内容都显示精美。即使某些字段中的字符数不匹配?

我将如何在Pascal上解决此问题,例如:

write(first.surname:8:6);

输出:
Information you entered >                                                                                                        

Surname < Trump || Obama |
|
Group < rz-1 || rpds-5 |
|
Course < 2 || 3 |
|
Physics < 3 || 4 |
|
Programming < 5 || 4 |
|
Math < 3 || 3 |
|
Name< Donald || Barack |
|
Patronymic < Donalldd || Bakkaa |
|
Average < 3.66667 || 3.66667 |
|

码:
#include <iostream>
using namespace std;
float sr_mark;
struct student
{
char surname[21];
char group[21];
int course;
int physics;
int programming;
int math;
float mark;
// Student Information
char WorkersName[21]; // student name
char JobTitle[21]; // middle name of student
};
int main()
{
setlocale(0, "");
sr_mark = 0;
cout << "\n";
student first;
cout << "\t" << "Enter information about the first student < " << endl;
cout << "\n";
cout << " Enter surname > ";
cin >> first.surname;
cout << "\n";
cout << " Enter group > ";
cin >> first.group;
cout << "\n";
cout << " Enter course > ";
cin >> first.course;
cout << "\n";
cout << " Enter physics grade > ";
cin >> first.physics;
cout << "\n";
cout << " Enter a programming grade > ";
cin >> first.programming;
cout << "\n";
cout << " Enter math grade > ";
cin >> first.math;
cout << "\n";
cout << " Enter student name > ";
cin >> first.WorkersName;
cout << "\n";
cout << " Enter the middle name of the student > ";
cin >> first.JobTitle;
cout << "\n";
sr_mark += first.math;
sr_mark += first.programming;
sr_mark += first.physics;
sr_mark /= 3;
first.mark = sr_mark;
sr_mark = 0;

cout << "\n";
cout << "\t" << "Enter data about the second student < " << endl;
cout << "\n";
student *second = new student;
cout << " Enter surname > ";
cin >> second -> surname;
cout << "\n";
cout << " Enter group > ";
cin >> second -> group;
cout << "\n";
cout << " Enter course > ";
cin >> second -> course;
cout << "\n";
cout << " Enter physics grade > ";
cin >> second -> physics;
cout << "\n";
cout << " Enter a programming grade > ";
cin >> second -> programming;
cout << "\n";
cout << " Enter math grade > ";
cin >> second -> math;
cout << "\n";
cout << " Enter student name > ";
cin >> second -> WorkersName;
cout << "\n";
cout << " Enter the middle name of the student > ";
cin >> second -> JobTitle;
cout << "\n";
sr_mark += second -> math;
sr_mark += second -> programming;
sr_mark += second -> physics;
sr_mark /= 3;
second -> mark = sr_mark;
sr_mark = 0;

cout << "\n";
cout << "Information you entered >" << endl;
cout << "\n";
cout << " Surname < " << "\t" <<first.surname << "\t\t\t\t" << "||" << "\t\t" << second -> surname << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Group < " << "\t" <<first.group << "\t\t\t\t" << "||" <<"\t\t" << second -> group << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Course < " << "\t" <<first.course << "\t\t\t\t" << "||" <<"\t\t" << second -> course << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Physics < " << "\t" <<first.physics << "\t\t\t\t" << "||" <<"\t\t" << second -> physics << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Programming < " << "\t" <<first.programming << "\t\t\t\t" << "||" <<"\t\t" << second -> programming << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Math < " << "\t" <<first.math << "\t\t\t\t" << "||" <<"\t\t" << second -> math << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Name<" << "\t" <<first.WorkersName << "\t\t\t\t" << "||" <<"\t\t" << second -> WorkersName << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Patronymic < " << "\t" <<first.JobTitle << "\t\t\t\t" << "||" <<"\t\t" << second -> JobTitle << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Average < " << "\t" <<first.mark << "\t\t\t\t" << "||" <<"\t\t" << second -> mark << "\t\t\t\t" << "||" << "\t\t\n";

system("pause");
}

最佳答案

这是一项复杂的任务,没有神奇的解决方案:您需要测量字段的大小(以字符为单位),然后相应地添加空格,依此类推。
更不用说您可能遇到的utf-8问题(以字节为单位的字符串大小可能与以字符为单位的大小不同)。

您可以使用std::setw(),但不能解决所有问题。

您最好使用一个库。
如果您有C++ 17,tabulate似乎是一个很好的解决方案。
可能会过分满足您的要求?

(免责声明:我自己没有使用过。)

关于c++ - 如果字段中的字符数不同,则输出时分隔符会丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60489019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com