欢迎使用CSDN-markdown编辑器

来源:互联网 发布:串口收到的数据都是0 编辑:程序博客网 时间:2024/06/08 02:34

5.1

#include<iostream>int main(){    using namespace std;    int a,b;    cout<<"input the first number:"<<endl;    cin>>a;    cout<<"input the second number:"<<endl;    cin>>b;    int sum=0;    for(int i=a;i<=b;i++)        sum=sum+i;    cout<<"the sum is "<<sum<<endl;    return 0;}

5.2

#include <iostream>#include <array>const int ArrSize=101;int main(){    using namespace std;    array<long double,ArrSize> arr;    arr[0]=arr[1]=1l;    for (int i=2;i<ArrSize;i++)        arr[i]=arr[i-1]*i;    for (int i=0;i<ArrSize;i++)        cout<<i<<"! = "<<arr[i]<<endl;    return 0;}

5.3

#include <iostream>using namespace std;int main(){    int sum=0;    int a;    cin>>a;    while(a!=0)    {        sum=sum+a;        cout<<"sum= "<<sum<<endl;        cin>>a;    }    return 0;}

5.4

#include <iostream>using namespace std;int main(){    double a=100;    double b=100;    double n=1;    a=(1+0.1)*100;    b=(1+0.05)*b;    while(a>b)    {        a=a+0.1*100;        b=b*(1+0.05);        n=n+1;    }    cout<<"years "<<n<<endl;    cout<<"D "<<a<<endl;    cout<<"C "<<b<<endl;}

5.5

#include <iostream>#include <string>int main(){    using namespace std;    string arr[12]={"1","2","3","4","5","6","7","8","9","10","11","12"};    string *c=arr;    int num[12];    int sum=0;    for (int i=0;i<12;i++)    {        cout<<"the "<< *c<<" month "<<endl;        cin>>num[i];        sum=sum+num[i];        c++;    }    cout<<"sum "<<sum<<endl;    return 0;}

5.6

#include <iostream>using namespace std;const int years=3;const int months=12;int main(){    int arr[years][months];    for (int i=0;i<years;i++)    {        for (int j=0;j<months;j++)        {            cout<<"please input the "<<i+1<<"th year, the "<<j+1<<"th month's data: ";            cin>>arr[i][j];        }    }    int total=0;    for (int i=0;i<years;i++)    {        int sum=0;        for (int j=0;j<months;j++)        {            sum=sum+arr[i][j];        }        cout<<"the "<<i+1<<"th year's sum is "<<sum<<endl;        total=total+sum;    }    cout<<"the "<<years<<"years' sum is "<<total<<endl;\    return 0;}

5.7

#include <iostream>#include <string>using namespace std;struct car_informatiom {    string name;    int year;};int main(){    cout<<"How many cars do you wish to catalog?";    int num;    cin>>num;    car_informatiom *a=new car_informatiom [num];    for(int i=0;i<num;i++)    {        cout<<"Car #:"<<i+1<<endl;        cout<<"Please enter the make: ";        cin>>a->name;        cout<<endl;        cout<<"Please enter the year made: ";        cin>>a->year;        a++;    }    a=a-num;    cout<<"Here is your collection: "<<endl;    for (int i=0;i<num;i++)    {        cout<<(*a).year<<" "<<a->name<<endl;        a++;    }/*  delete [] a;*/    return 0;}

5.8

#include <iostream>#include <cstring>const char test[]="done";using namespace std;int main(){    char test_word[20];    int sum=0;    cout<<"Enter words (to stop,type the word done):"<<endl;    do     {        cin>>test_word;        if (strcmp(test,test_word)==0)            break;        else             sum++;    } while (1);    cout<<"num: "<<sum;    return 0;}

5.9

#include <iostream>#include <string>using namespace std;int main(){    string test_word;    string test="done";    int sum=0;    cout<<"Enter words (to stop,type the word done):"<<endl;    do     {        cin>>test_word;        if (test==test_word)            break;        else             sum++;    } while (1);    cout<<"num: "<<sum;    return 0;}

5.10

#include <iostream>using namespace std;int main(){    cout<<"Enter the number of rows: "<<endl;    int rows;    cin>>rows;    for (int i=0;i<rows;i++)    {        for (int j=0;j<rows;j++)        {            if (j<rows-i-1)            {                cout<<".";            }            else                cout<<"*";        }        cout<<endl;    }    return 0;}
0 0