C++Primer Plus(第六版)第五章编程作业

Tips:

1.cin >> str //用于接收一个字符串时,遇到空格则停止
2.getling(cin,string) //用于接收一行字符串,可以接收空格,但是同样不会丢弃换行符
3.cin >> int/*在输入并读取之后,换行符依旧在队列中,此时若是继续用cin>>输入则会因为 >> 操作符的原因,自动跳过队列前空白。而如果使用getline()的系列方法来读取字符串,则会因为第一个字符是换行符而直接停止读取。*/

1——10题综合如下

#include<iostream>
#include<array>
#include<cstring>

using namespace std;

/*struct car
{
    string Productor;
    int Year_Of_Pro;
};*/

int main()
{
    // No.1
    /*int i,j,step = 0;
    cout << "input two numbers: ";
    cin >> i >> j;
    if(i<j)
    {
    for(i ; i <= j ; i++)
        step+=i;
    }
    else
    {
    for(j ; j <= i ; j++)
        step+=j;
    }
    cout<< "The amount of between num:"<<step<<endl;
    // N0.2
    array <long double,101>factorials;
    factorials[0] = factorials[1] = 1.0;
    for(int i = 2; i<101 ; i++)
        factorials[i] = factorials[i-1] * i;
    for(int i = 0; i<101 ; i++)
        cout<< i <<"! = "<<factorials[i]<<endl;

    cout<<"No.3"<<endl;
    int i,step = 0;
    cout << "input the number:";
    cin >> i;
    while(i!=0)
    {
        step += i;
        cout<<"multi plus:"<< step <<endl;
        cout<<"input the number:";
        cin>> i ;

    }
    cout << "the program is over"<<endl;
    cout << "No.4" <<endl;
    double D_Daphne = 100;
    double D_Cleo = 100;
    int year = 0;
    while(D_Cleo <= D_Daphne)
    {
         D_Daphne = 0.1 * 100 + D_Daphne;
         D_Cleo = 0.05 * D_Cleo + D_Cleo;
         year ++;
    }
   cout << "this is the "<<year<<" year."<<endl;
   cout << "Daphne's Deposit is:"<<D_Daphne<<endl;
   cout << "Cleo's Deposit is:"<<D_Cleo<<endl;
   cout << "No.5"<<endl;
   string Month[12] =
   {
       "January","February","March","April",
       "May", "June" ,"July","August","September",
       "October","November","December"
    };
    int Amount[12];
    int temp = 0;
    for(int i = 0; i < 12 ; i++)
    {
        cout <<"Selling Amount of "<<Month[i]<<" :";
        cin >> Amount[i];
        temp += Amount[i];
    }
    cout << "The Year Selling Amount is:"<<temp<<endl;

    cout<< "No.6"<<endl;
    string Month[12] =
   {
       "January","February","March","April",
       "May", "June" ,"July","August","September",
       "October","November","December"
    };
    int Amount[3][12];
    int i,j,temp1=0;
    int temp2 = 0;
    cout<<"initializing data:";
    for(i=0 ; i<12 ; i++)
    {
        for(j=0 ; j<3 ; j++)
        {
             cin>> Amount[j][i];
        }
    }
    cout<<"print detail of selling:"<<endl;
    for(i=0 ; i<12 ; i++)
    {
        cout << Month[i]<<":\t";
        for(j=0 ; j<3 ; j++)
        {
            cout << Amount[j][i]<<"\t";
            temp1 += Amount[j][i];
        }
        cout<<endl;
    }
    cout << "the full selling amount is:"<< temp1<<endl;
    for(i = 0; i<3 ;i++)
    {
        for(j = 0; j<12; j++)
        {
            temp2 += Amount[i][j];
        }
        cout <<"the Amount of "<<i+1<<" year:"<<temp2<<endl;
        temp2 = 0;
    }
    cout << "No.7"<<endl;
    cout<<"how many cars do you wish to catalog?"<<endl;
    int i;
    cin >> i;
    cin.get();
    car *C_num = new car[i];
    for(int j = 0 ; j<i ; j++)
    {
        cout << "car #"<<j+1<<endl;
        cout << "Please inter the make:";
        getline(cin,C_num[j].Productor);
        cout << "Please inter the made:";
        cin >> C_num[j].Year_Of_Pro;
        cin.get();
    }
    cout << "Here is your collection:"<<endl;
    for (int j = 0 ; j<i ; j++)
    {
        cout << C_num[j].Year_Of_Pro << " " << C_num[j].Productor <<endl;
    }

    cout << "No.8:"<<endl;
    char Word[20] = "none";
    int counting = 0;
    while(1)
    {
        cin >> Word;
        if(strcmp(Word,"done") != 0)
        counting++;
        else
            break;
    }
    cout << "entered a total of "<<counting << " words." << endl;

    cout << "No.9:" << endl;
    string Word = "none";
    int counting = 0;
    while(1)
    {
        cin >> Word;
        if(Word != "done")
        counting++;
        else
            break;
    }
    cout << "entered a total of "<<counting << " words." << endl;

    cout << "No.10" << endl;
    cout << "input how many stars do you need:";
    int Num;
    cin >> Num;
    char star[Num][Num];
    for(int i = 0 ; i < Num ; i++)
    {
        for(int j = 0 ; j<Num ; j++)
        {
            if( j < (Num -1 - i))
                star[i][j] = '.';
            else
                star[i][j] = '*';

        }
    }
    for(int i = 0 ; i < Num ; i++)
    {
        for(int j = 0 ; j < Num ; j++)
        {
            cout << star[i][j]<< "\t";
        }
        cout<<endl;
    }
    return 0;
*/
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 经过以下栈运算后,x的值是( )。 InitStack(s); Push(s,'a'); Push(s,'b'); Pop(s,x); Gettop(s,x); A. a B. b C. 1 D. 0 2.循环队列存储在数组A[0..m]中,则入队时的操作为( )。 A.rear=rear+1 B. rear=(rear+1) mod(m-1) C. rear=(rear+1)mod m D. rear=(rear+1) mod(m+1) 3. 栈和队列的共同点是( )。 A.都是先进先出 B.都是先进后出 C.只允许在端点处插入和删除元素 D.没有共同点 4. 若用一个大小为6的数组来实现循环队列,且当 rear 和 front 的值分别为 0 和 3。当从队列中删除一个元素,再插入两个元素后,rear 和 front 的值分别为:( )。 A.1 和 5 B.2 和 4 C.4 和 2 D.5 和 1 5. 程序填顺序循环队列的类型定义如下: typedef int ET; typedef struct{ ET *base; int Front; int Rear; int Size; }Queue; Queue Q; 队列 Q 是否“满”的条件判断为( C )。 A.(Q.Front+1)=Q.Rear B.Q.Front=(Q.Rear+1) C.Q.Front=(Q.Rear+1)% Q.size D.(Q.Front+1) % Q.Size=(Q.Rear+1)% Q.size 6. 若进栈序列为1,2,3,4,进栈过程中可以出栈,则( )不可能是一个出栈序列。 A.3,4,2,1 B.2,4,3,1 C.1,4,2,3 D.3,2,1,4 7. 向顺序存储的循环队列 Q 中插入新元素的过程分为三步: ( )。 A.进行队列是否空的判断,存入新元素,移动队尾指针 B.进行队列是否满的判断,移动队尾指针,存入新元素 C.进行队列是否空的判断,移动队尾指针,存入新元素 D.进行队列是否满的判断,存入新元素,移动队尾指针 8. 关于栈和队列,( )说法不妥。 A. 栈是后进先出表 B. 队列是先进先出表 C. 递归函数在执行时用到栈 D. 队列非常适用于表达式求值的算符优先法 9. 若用数组S[0..m]作为两个栈S1和S2的共同存储结构,对任何一个栈,只有当S全满时才不能作入栈操作。为这两个栈分配空间的最佳方案是( )。 A.S1的栈底位置为0,S2的栈底位置为m B.S1的栈底位置为0,S2的栈底位置为m/2 C.S1的栈底位置为1,S2的栈底位置为m D.S1的栈底位置为1,S2的栈底位置为m/2 二、程序填空题(没特别标注分数的空的为3分,共 23 分)。 1.下面的算法是将一个整数e压入堆栈S,请在空格处填上适当的语句实现该操作。 typedef struct{ int *base; int *top; int stacksize; }SqStack; int Push(SqStack S,int e) { if ( S.top- S.base>= S.stacksize ) { S.base=(int *) realloc(S.base,(S.stacksize+1)*sizeof(int)); if( !S.base ) { printf(“Not Enough Memory!\n”); return(0); } S.top= S.base+ S.stacksize ; S.stacksize= S.stacksize+1 ; } * S.top++=e ; return 1; } 2. 在表达式:6+5+3*7/(4+9/3-2)求值过程中,处理到2时刻,运算符栈的状态为: + / ( - (4分),操作数栈的内容为11,21,7,2(4分)。 3. 递调用时,处理参数及返回地址,要用一种称为 栈 的数据结构。 4. 设循环队列中数组的下标范围是1-n,其头尾指针分别为f和r,则其元素个数为_(r-f+n) mod n。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值