【发布时间】:2021-09-10 21:18:02
【问题描述】:
我写了这个程序:
#include <iostream>
#include <string>
#include <ctime>
#include <sstream>
#include <time.h>
#include "TextTable.h"
using namespace std;
int command();
void new_car();
void print();
int c=0;
int c1=0;
char f;
int i=0;
int size1=0;
TextTable t( '-', '|', '*' );
struct car
{
string car_name;
string car_owner_name;
int year;
string car_paint;
string car_performance;
string car_problem;
int time;
};
car *a = NULL;
car *p;
int main ()
{
cout<<"welcome to car repair shop program. to help , press h."<<endl;
command();
}
int command(){
cout<<"admin@car_repair_shop_program # ";
cin>>f;
switch(f)
{
case 'n':
new_car();
break;
case 'h':
cout<<endl<<"help"<<endl<<"p : print"<<endl<<"n : new"<<endl<<"h : help"<<endl<<"q : quit"<<endl;
command();
break;
case 'p':
print();
break;
case 'q':
char tmp;
cout<<"Are you sure you want to quit? (y or n): ";
cin>>tmp;
switch(tmp){
case 'y':
delete [] a;
delete [] p;
return 0;
break;
case 'n':
command();
break;
default:
cout << "error! Please try again"<<endl;
command();
}
default:
cout << "error! Please try again"<<endl;
command();
}
}
void new_car()
{
c++;
string car_name;
string car_owner_name;
int year;
string car_paint;
string car_performance;
string car_problem;
int time;
p = new car[++size1];
if (c==1){
a = new car [size1-1];
}
cout<<"enter car name: ";
cin>>car_name;
cout<<endl<<"enter car owner name: ";
cin>>car_owner_name;
cout<<endl<<"enter car paint: ";
cin>>car_paint;
cout<<endl<<"enter car performance: ";
cin>>car_performance;
cout<<endl<<"enter car problem: ";
cin>>car_problem;
cout<<endl<<"enter time: ";
cin>>time;
cout<<endl<<"enter year: ";
cin>>year;
for(int i = 0 ; i < size1-1 ; ++i)
{
p[i].car_name = a[i].car_name;
p[i].car_owner_name = a[i].car_owner_name;
p[i].car_paint = a[i].car_paint;
p[i].car_performance = a[i].car_performance;
p[i].car_problem = a[i].car_problem;
p[i].time = a[i].time;
p[i].year = a[i].year;
}
delete [] a;
a = p;
a[size1-1].car_name=car_name;
a[size1-1].car_owner_name=car_owner_name;
a[size1-1].car_paint=car_paint;
a[size1-1].car_performance=car_performance;
a[size1-1].car_problem=car_problem;
a[size1-1].time=time;
a[size1-1].year=year;
cout<<"OK!"<<endl;
command();
}
void print()
{
c1++;
if (c1 == 1){
t.add( " car name " );
t.add( " car owner name " );
t.add( " car paint " );
t.add( " car performance " );
t.add( " car problem " );
t.add( " time " );
t.add( " year " );
t.endOfRow();
}
string tmp;
for (;i<size1;){
t.add(p[i].car_name);
t.add(p[i].car_owner_name);
t.add(p[i].car_paint);
t.add(p[i].car_performance);
t.add(p[i].car_problem);
tmp = to_string(p[i].time);
t.add(tmp);
tmp = to_string(p[i].year);
t.add(tmp);
t.endOfRow();
t.setAlignment( i, TextTable::Alignment::LEFT );
i+=1;
}
cout << t;
command();
}
但我不能做这部分项目: “检查 C/C++ 库提供的哪些合适的数据类型可以用来在上面的程序中存储时间和日期信息,并使用这些工具重写你的程序”我需要获取时间和年份变量。 TextTable.h 文件的文本还包含以下内容:
#pragma once
#include <iostream>
#include <map>
#include <iomanip>
#include <vector>
#include <string>
#ifdef TEXTTABLE_ENCODE_MULTIBYTE_STRINGS
#include <clocale>
#ifndef TEXTTABLE_USE_EN_US_UTF8
#define TEXTTABLE_USE_EN_US_UTF8
#endif
#endif
class TextTable {
public:
enum class Alignment { LEFT, RIGHT };
typedef std::vector< std::string > Row;
TextTable() :
_horizontal( '-' ),
_vertical( '|' ),
_corner( '+' ),
_has_ruler(true)
{}
TextTable( char horizontal, char vertical, char corner ) :
_horizontal( horizontal ),
_vertical( vertical ),
_corner( corner ),
_has_ruler(true)
{}
explicit TextTable( char vertical ) :
_horizontal( '\0' ),
_vertical( vertical ),
_corner( '\0' ),
_has_ruler( false )
{}
void setAlignment( unsigned i, Alignment alignment )
{
_alignment[ i ] = alignment;
}
Alignment alignment( unsigned i ) const
{ return _alignment[ i ]; }
char vertical() const
{ return _vertical; }
char horizontal() const
{ return _horizontal; }
void add( std::string const & content )
{
_current.push_back( content );
}
void endOfRow()
{
_rows.push_back( _current );
_current.assign( 0, "" );
}
template <typename Iterator>
void addRow( Iterator begin, Iterator end )
{
for( auto i = begin; i != end; ++i ) {
add( * i );
}
endOfRow();
}
template <typename Container>
void addRow( Container const & container )
{
addRow( container.begin(), container.end() );
}
std::vector< Row > const & rows() const
{
return _rows;
}
void setup() const
{
determineWidths();
setupAlignment();
}
std::string ruler() const
{
std::string result;
result += _corner;
for( auto width = _width.begin(); width != _width.end(); ++ width ) {
result += repeat( * width, _horizontal );
result += _corner;
}
return result;
}
int width( unsigned i ) const
{ return _width[ i ]; }
bool has_ruler() const { return _has_ruler;}
int correctDistance(std::string string_to_correct) const
{
return static_cast<int>(string_to_correct.size()) - static_cast<int>(glyphLength(string_to_correct));
};
private:
const char _horizontal;
const char _vertical;
const char _corner;
const bool _has_ruler;
Row _current;
std::vector< Row > _rows;
std::vector< unsigned > mutable _width;
std::vector< unsigned > mutable _utf8width;
std::map< unsigned, Alignment > mutable _alignment;
static std::string repeat( unsigned times, char c )
{
std::string result;
for( ; times > 0; -- times )
result += c;
return result;
}
unsigned columns() const
{
return _rows[ 0 ].size();
}
unsigned glyphLength( std::string s ) const
{
unsigned int _byteLength = s.length();
#ifdef TEXTTABLE_ENCODE_MULTIBYTE_STRINGS
#ifdef TEXTTABLE_USE_EN_US_UTF8
std::setlocale(LC_ALL, "en_US.utf8");
#else
#error You need to specify the encoding if the TextTable library uses multybyte string encoding!
#endif
unsigned int u = 0;
const char *c_str = s.c_str();
unsigned _glyphLength = 0;
while(u < _byteLength)
{
u += std::mblen(&c_str[u], _byteLength - u);
_glyphLength += 1;
}
return _glyphLength;
#else
return _byteLength;
#endif
}
void determineWidths() const
{
_width.assign( columns(), 0 );
_utf8width.assign( columns(), 0 );
for ( auto rowIterator = _rows.begin(); rowIterator != _rows.end(); ++ rowIterator ) {
Row const & row = * rowIterator;
for ( unsigned i = 0; i < row.size(); ++i ) {
_width[ i ] = _width[ i ] > glyphLength(row[ i ]) ? _width[ i ] : glyphLength(row[ i ]);
}
}
}
void setupAlignment() const
{
for ( unsigned i = 0; i < columns(); ++i ) {
if ( _alignment.find( i ) == _alignment.end() ) {
_alignment[ i ] = Alignment::LEFT;
}
}
}
};
inline std::ostream & operator<<( std::ostream & stream, TextTable const & table )
{
table.setup();
if (table.has_ruler()) {
stream << table.ruler() << "\n";
}
for ( auto rowIterator = table.rows().begin(); rowIterator != table.rows().end(); ++ rowIterator ) {
TextTable::Row const & row = * rowIterator;
stream << table.vertical();
for ( unsigned i = 0; i < row.size(); ++i ) {
auto alignment = table.alignment( i ) == TextTable::Alignment::LEFT ? std::left : std::right;
// std::setw( width ) works as follows: a string which goes in the stream with byte length (!) l is filled with n spaces so that l+n=width.
// For a utf8 encoded string the glyph length g might be smaller than l. We need n spaces so that g+n=width which is equivalent to g+n+l-l=width ==> l+n = width+l-g
// l-g (that means glyph length minus byte length) has to be added to the width argument.
// l-g is computed by correctDistance.
stream << std::setw( table.width( i ) + table.correctDistance(row[ i ])) << alignment << row[ i ];
stream << table.vertical();
}
stream << "\n";
if (table.has_ruler()) {
stream << table.ruler() << "\n";
}
}
return stream;
}
可以使用 C/C++ 库提供的哪些合适的数据类型?
【问题讨论】:
-
您是在问“可以使用 C/C++ 库提供的合适的数据类型”吗?还是您在问如何“使用这些工具重写您的程序”?
-
可以使用C/C++库提供的合适的数据类型
-
例如 print : 输入时间: 并输入到时钟如 10:00 并保存为数据类型。
-
仅仅保存是没有意义的。您打算以后如何处理存储的时间和日期?直接输出?您可以使用
char的数组。但如果你想做更复杂的事情,比如确定从那时到今天的天数,事情就会变得更复杂。