zoukankan      html  css  js  c++  java
  • 第03次作业-栈和队列

    第03次作业-栈和队列


    1.学习总结


    2.PTA实验作业

    2.1题目一 7-3 表达式转换(25 分)

    2.2 设计思路(伪代码或流程图)

        定义变量result[100]存储需要输出的式子。
        定义变量str[100]存储输入的表达式
        for i=0 to strlen(str)
        if(str[i] 为 '-' 并且 str[i-1]不是数字)
            then  result[r++]=str[i];
            elseif  (str[i]不是数字)
               then
               while str[i]为数字或为"."
                   do  result[r++]=str[i];
                   i++;
                   end
                elseif  str[i]为"+" 并 str[i-1] 为"("
                 then  continue;
            elseif  str[i]为")"   
               then
                while st.top()不为'('
                   do 把括号之间存在栈的元素吐出放到result里  
                   end
                把"("出栈
            elseif  str[i]为'('或栈为空)
                  then  str[i]进栈
            else
                then
                 if(栈顶运算符优先级大于str[i])
                   then  str[i]进栈
                 else
                  then while( 栈不为空或者栈顶不是()
                       do  吐出栈内元素到result
                       end
                       str[i]进栈    
           end for 
    

    2.3 代码截图



    2.4 PTA提交列表说明。

    • 运算数前有正负号

    错误原因写代码时没考虑正号,没有想到
    解决方法增添如下语句即可

    else   if(str[i] == '+' && str[i-1] == '(')
              continue;
    
    • 运算数超过1位整数且有非整数出现

      错误原因题目中若是出现一位以上的数字如123,不能输出1 2 3,要输出123,中间不能有空格而且可能存在小数的情况。
      解决方法 加下面语句

    else  if(isNum(str[i])) //遇到数字
            {
                while(isNum(str[i])||str[i]=='.')
                {
                    result[r++]=str[i];
                    i++;
                }
                i--;//if语句结束后,i还会再加一次!
                result[r++]=' ';//最后再加空格
            }
    
    • 嵌套括号 段错误
      错误原因: 在运算符小于栈顶元素时,在弹出栈内元素时没有想到嵌套括号,在遇到"("或栈为空时都应该停止出栈!
      解决方法:
     while( !st.empty())
    

    改为

    while( !st.empty()&& st.top() !='(')
    

    2.1题目二 7-2 银行业务队列简单模拟(25 分)

    2.2 设计思路(伪代码或流程图)

        queue <int> q1;//建队列q1,q2
        queue <int> q2;
        for i=0 to n-1
          输入客户p[i]
            if p[i] 为偶数
            then 进q1
            else
               then 进q2
        end for
       if p[0]为偶数
       then
           while(q1,q2均不为空)
             do  输出q1的队首
                输出q2的前两个元素
             end
          if q1不空
           then  while(q1不空)
             do  输出q1内元素
             end
          if q2不空
            then  while(q2不空)
                  do  输出q2内元素
                   end
        else
        then
           while(q1,q2均不为空)
             do  输出q2的前两个元素
                 输出q1的队首
             end
         ....接下来同上
    

    2.3 代码截图



    2.4 PTA提交列表说明

    • 最大N,随机 段错误
      错误原因:存储客户的数组不够大。
      解决方法
    int p[100];
    

    改成

    int p[1000];
    
    • 最小N 段错误
      错误原因: 如果只有一个客户,就直接输出这个客户,如果没有客户直接return 0
      解决方法:增加下列语句
    if(n==0)
     return 0;
    if(n==1)
    {
        printf("%d",p[0]);
         return 0;
     }
    

    2.1题目三 7-3 银行排队问题之单队列多窗口服务(25 分)

    2.2 设计思路(伪代码或流程图)

    typedef struct pnode//定义两个结构体,顾客和窗口
    {
        int atime;//到达时间
        int ctime;//处理时间
        int wtime;//等待时间
    }pnode ;  
    typedef struct wnode
    {
        int num;//处理顾客数
        int endtime;//结束时间
    }wnode;
       queue <pnode> q;//建队列q
        for i=0 to n-1
          输入顾客信息,并入队
          end for
      for i=0 to k-1
        初始化num,endtime 为0;
        end for
        if 顾客<=窗口数
          then
             for i=0 to n-1
               wtime为0;
               windows[i].num++;
               windows[i].endtime为atime+ctime;
             end for
        else
           while q不空
           do
             for j=0 to k-1
             寻找最小结束时间的窗口标记为flag。
             end for
        
         if 顾客来的晚
                then  windows[flag].num++;
                      windows[flag].endtime为atime+ctime;
                     wtime为0
           else  
               then     
                    wtime 等于 endtime 减 atime
                    windows[flag].endtime+=q.front().ctime;
                    windows[flag].num++;
                }
                sumtime累加wtime
            end
    

    2.3 代码截图




    2.4 PTA提交列表说明。

    • 本题在提交前在codeblocks测试修改过,提交上去一次过
      在codeblocks上修改了两处。
    • 第一处:
      错误原因
      题目中要求“这里假设每位顾客事务被处理的最长时间为60分钟。 ”
      言下之意是如果处理时间大于60也把它当成60,刚开始理解为输入的数据会自动<60.
      解决方法
                if(p.ctime>60)
                p.ctime=60;
    
    • 第二处
      错误原因输出的平均等待时间小数与样例相反
      解决方法 sumtime 也要定义成double 型

    3.截图本周题目集的PTA最后排名

    3.1 栈PTA排名

    3.2 队列PTA排名

    3.3 我的总分:2.5分

    必做题做完选做做部分。

    4. 阅读代码

    代码截图:
    包含注释





    代码优点:利用栈来实现计算器功能,能够实现更为复杂的运算式子,带有括号,带有负数,带有优先级的判断,功能比之前学的更强大。
    代码地址 https://gitee.com/adressad/codes/9ystlgoh6b413n5vfupa839

    5. 代码Git提交记录截图

  • 相关阅读:
    分页功能
    四个内置对象的作用范围
    include和application
    jsp中的session
    IDC机房的相关常识
    使用dm-cache组合SSD与HDD实现高性价比存储
    负载均衡基本原理与lvs
    秒级别执行脚本的方法
    Tomcat调优
    Nginx调优
  • 原文地址:https://www.cnblogs.com/huangqingqing/p/8727906.html
Copyright © 2011-2022 走看看