Jadvalning formatli chiqishi uchun C ++ ochiq manba kutubxonasi

Jadval formatlash uchun ochiq manba kodli C ++ kutubxonasi bormi?

Menga yozadigan narsalar

Table t;// Table is a class provided by the library
t.column(2);
t.name(0,"x");
t.name(1,"x^2");
t.type("x",Table::Integer);
t.type("x^2",Table::Integer);
t.width("x",1);
t.width("x^2",2);
for( size_t i = 0; i < 10; i++ ) {
 t.set("x",i);
 t.set("x^2",i*i);
}
std::cout << t;

va bu matnli chiqishni oling

0  0
1  1
2  4
3  9
4 16
5 25
6 36
7 49
8 64
9 81

Faqatgina tamsayılar emas, balki suzuvchi nuqtali raqamlarni boshqarish imkoniga ega bo'lishi kerak. Rahmat.

2

2 javoblar

I am not aware of any libraries.
But you can overload the << operator for your class Table and you can achieve the same.

EDIT:
To clarify on your comment of Table being an Library class, It does not affect overloading the << because it is overloaded as an non-member free function.

Kod misoli:

std::ostream& operator<<(std::ostream& os, const Table& obj) 
{ 
 //write obj to stream in whichever way you like
  return os;
} 
2
qo'shib qo'ydi
Jadval kutubxonadan taqdim etilgan sinf bo'lishi kerak. Men savolni aniqlab beraman.
qo'shib qo'ydi muallif Alessandro Jacopson, manba
Ehtimol, men muammomni etarli darajada tushuntirmadim. Menda faqat i va i ^ 2 raqamlari bor, men izlayotgan kutubxona esa stol kabi bir sinfni berishi kerak.
qo'shib qo'ydi muallif Alessandro Jacopson, manba
@uvts_cvs: << odatda a'zo bo'lmagan funktsiya sifatida haddan tashqari ko'tariladi, shuning uchun muammo bo'lmasligi kerak.
qo'shib qo'ydi muallif Alok Save, manba

Ushbu turdagi narsalarni biladigan oldindan qurilgan kutubxonalar asosan GUIga yo'naltirilgan bo'lib, shuning uchun ma'lumotlar matn kabi matn o'rniga elektron jadvallar kabi ko'rinadi.

Kutubxonadan qanday foydalanish haqida fikringizni tekshirib ko'rsangiz, u meni C ++ ga o'xshatib, juda yomon. Shuni esda tutingki, C ++ da statik matn terish qo'llaniladi, bu erda t.type ("x", "Table :: integer") dinamik terishni bildiradi (ya'ni, vaqtni kompilyatsiya qilish).

Shaxsiy sifatida, ehtimol, bir obyekti uchun formatlash ma'lumotlarini saqlaydigan formatlash obyekti nuqtai nazaridan o'ylayman. Keyin formatlash obyekti va biriktirilgan ma'lumotlarning vektori bo'lgan "ustun" bor. Nihoyat, siz o'zboshimchalik bilan ustunlar qatoriga ega bo'lgan "jadval" ga egasiz. Buning aksariyati (ayniqsa, turlari) shablon argumentlari sifatida belgilanadi, shuning uchun quyidagi o'xshash narsalar bo'lishi mumkin:

class format { 
    int w, p;
public:
    format(int width = 0, int precision = 0) : w(width), precision(p) {}

    friend std::ostream &operator<<(std::ostream &os, format const &f) { 
        return os << std::setw(f.w) << std::setprecision(f.p);
    }
};

template  >
class Column {
    format f;
    std::string label;
    container data;
public:
    Column(std::string const &l, format const &fmt) : f(fmt), label(l) {}
    void push_back(T const &d) { data.push_back(d); }
};

format fmt1(1);
format fmt2(2);

Column c1("x", fmt1);
Column c2("x^2", fmt2);

table<2> my_table(c1, c2);

I'd probably then design the table to act roughly like a vector >, so to add the values, you'd do something like:

for (int i=0; i<10; i++)
    my_table.push_back(make_tuple(i, i*i));

The method you've used (based on the name of the column instead of the position) could probably be done, but the obvious way (a map) would only work if all the columns had the same type. To support multiple column types, you could do something like map, but I'd stay away from that if at all reasonable.

1
qo'shib qo'ydi
+1 javobingiz uchun rahmat. Sizning takliflaringiz asosida biror narsani kodlashga harakat qilaman deb o'ylayman.
qo'shib qo'ydi muallif Alessandro Jacopson, manba