C++primer--Cp5

来源:互联网 发布:最简单的js幻灯片代码 编辑:程序博客网 时间:2024/06/06 09:34

5.2 factorial

int main(){    const int  size = 16;    array<long long, size>factorial;    factorial[0] = factorial[1] = 1LL;    for (int i = 2; i < size; i++ )    {        factorial[i] = i*factorial[i - 1];    }    for (int i = 0; i < size; i++)    {        cout << i << "! = " << factorial[i] << endl;    }        return 0;}

5.6 book selling

int main(){    string a[] = { "January "," February "," March "," April "," May "," June"," July "," August"," September "," October "," November "," December" };    double num[3][12];    for (int i = 0; i < 3; i++)    {        for (int j = 0; j < 12; j++)        {            cout << "please input this " << i +1<<"\tyear's\t"<<a[j]<<"\tmonth's\t"<< "total num \t\n";            cin >> num[i][j];        }    }    int as[3] = {0};    for (int i = 0; i < 3; i++)    {        for (int j = 0; j < 12; j++)        {            as[i] += a[i][j];        }    }    return 0;}

5.7 car information

Attention! compile result is wrong

I suspect the “new ” part has some problems, for I’m unfamiliar with that;

struct car{    string proname;    int proyear;};int main(){    int n = 0;    cout << "how many cars do you have?\t\n";    cin >> n;    car*p = new car[n];    cout << "rinima";    for (int i = 0; i < n; i++)    {        cout << "what's your car producer?\t\n";        cin >> p[n].proname;        cout << "what's your car proyear?\t\n";        cin >> p[n].proyear;    }    for (int i = 0; i < n; i++)    {        cout << p[n].proname << "\t";        cout << p[n].proyear << endl;    }    return 0;}

5.8 total words

int main(){    string yime;    int n=0;    cout << "Enter words(to stop,type the word'done')\n";    while (yime != "done")    {        cin >> yime;        n++ ;    }    cout << "you have total input " << n << "words";    return 0;}
原创粉丝点击