[ create a new paste ] login | about

Link: http://codepad.org/RntigZhR    [ raw code | output | fork ]

C++, pasted on Feb 3:
#include <stdio.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <iostream>
#include <iomanip>
#include <vector>

struct Row
{
    float degrees;
    float radians;
    float sin;
    float cos;

    Row(float degrees)
        : degrees(degrees)
        , radians(degrees * (M_PI / 180.0f))
        , sin(::sin(radians))
        , cos(::cos(radians))
    {      
    }
};

std::vector<Row> MakeData()
{
    std::vector<Row> data;

    const int COUNT = 11;
    for (int i=0; i<COUNT; ++i)
    {
        float t = (float)i / (float)COUNT;

        data.push_back(Row(t * 360.0f));
    }

    return data;
}

void PrintUsingStdio(const std::vector<Row>& data)
{
    printf("+---------+---------+-------+-------+\n");
    printf("| Degrees | Radians |  Sin  |   Cos |\n");
    printf("+---------+---------+-------+-------+\n");

    for (std::vector<Row>::const_iterator it = data.begin(); it != data.end(); ++it) 
    {
        printf("| %7.1f | %7.4f | %5.2f | %5.2f |\n", 
            it->degrees,
            it->radians,
            it->sin,
            it->cos);
    }

    printf("+---------+---------+-------+-------+\n");
}

void PrintUsingIostreams(const std::vector<Row>& data)
{
    using namespace std;

    cout << "+---------+---------+-------+-------+" << endl;
    cout << "| Degrees | Radians |  Sin  |   Cos |" << endl;
    cout << "+---------+---------+-------+-------+" << endl;

    for (std::vector<Row>::const_iterator it = data.begin(); it != data.end(); ++it) 
    {
        cout << "| " 
             << fixed << setw(7) << setprecision(1) << it->degrees 
             << " | " 
             << setw(7) << setprecision(4) << it->radians 
             << " | " 
             << setw(5) << setprecision(2) << it->sin 
             << " | " 
             << setw(5) << setprecision(2) << it->cos 
             << " |" << endl;
    }

    cout << "+---------+---------+-------+-------+" << endl;
}

int main()
{
    std::vector<Row> data = MakeData();

    printf("Using stdio....\n");
    PrintUsingStdio(data);

    printf("\nUsing iostreams...\n");
    PrintUsingIostreams(data);

    return 0;
}


Output:
Using stdio....
+---------+---------+-------+-------+
| Degrees | Radians |  Sin  |   Cos |
+---------+---------+-------+-------+
|     0.0 |  0.0000 |  0.00 |  1.00 |
|    32.7 |  0.5712 |  0.54 |  0.84 |
|    65.5 |  1.1424 |  0.91 |  0.42 |
|    98.2 |  1.7136 |  0.99 | -0.14 |
|   130.9 |  2.2848 |  0.76 | -0.65 |
|   163.6 |  2.8560 |  0.28 | -0.96 |
|   196.4 |  3.4272 | -0.28 | -0.96 |
|   229.1 |  3.9984 | -0.76 | -0.65 |
|   261.8 |  4.5696 | -0.99 | -0.14 |
|   294.5 |  5.1408 | -0.91 |  0.42 |
|   327.3 |  5.7120 | -0.54 |  0.84 |
+---------+---------+-------+-------+

Using iostreams...
+---------+---------+-------+-------+
| Degrees | Radians |  Sin  |   Cos |
+---------+---------+-------+-------+
|     0.0 |  0.0000 |  0.00 |  1.00 |
|    32.7 |  0.5712 |  0.54 |  0.84 |
|    65.5 |  1.1424 |  0.91 |  0.42 |
|    98.2 |  1.7136 |  0.99 | -0.14 |
|   130.9 |  2.2848 |  0.76 | -0.65 |
|   163.6 |  2.8560 |  0.28 | -0.96 |
|   196.4 |  3.4272 | -0.28 | -0.96 |
|   229.1 |  3.9984 | -0.76 | -0.65 |
|   261.8 |  4.5696 | -0.99 | -0.14 |
|   294.5 |  5.1408 | -0.91 |  0.42 |
|   327.3 |  5.7120 | -0.54 |  0.84 |
+---------+---------+-------+-------+


Create a new paste based on this one


Comments: