SE308_lab1_2

Adrian不爱卷 2022-10-27 12:38:05

Catalogue

  • Lab1_2
  • GitHub repository
  • 1.PSP
  • 2.Description of problem-solving ideas
  • 3.Design and implementation process
  • 4.test file
  • 5.Test output
  • 6.Summary

Lab1_2

The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZU202201?category=0
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608734907
The Aim of This AssignmentLab1_2_solution
MU STU ID and FZU STU ID20122268 and 832001214

GitHub repository

https://github.com/xingwang133/SE308-lab1_2

1.PSP

Personal Software Process StagesEstimated time (minutes)real time (minutes)
Planning2025
Estimate55
Development150200
Analysis3030
Design Spec3030
Design Review1010
Coding Standard2520
Design3030
Coding90100
Code Review1010
Test and debug3040
Reporting5060
Size Measurement1010
Postmortem & Process Improvement Plan2025
Total510595

2.Description of problem-solving ideas

Since we used c++ in our course last semester, I am quite familiar with it now, so I decided to use c++ to complete this experiment. Solve the problem in stages by breaking it down.
1.Need to read the c file.
2.In order to meet the different Program Requirements, it is necessary to make corresponding code design for four different levels of tasks.

3.Design and implementation process

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<sstream>
using namespace std;

string Keyword[32]=
{
    "auto","break","case","char","const","continue","default","double",
    "do","else","enum","extern","float","for","goto","if","int","long",
    "register","return","short","signed","sizeof","static","struct",
    "switch","typedef","union","unsigned","void","volatile","while"
};
vector<int> Case_num;
int total_num = 0;
int switch_num = 0;
int case_num = 0;
int ifelse_num = 0;
int if_elseif_else_num = 0;
int stack[ 1000 ] = { 0 };
int top = -1;//Define the required variables

Define the function corresponding to the keyword

nt match( string a , string b )
{
    int correspond( char str );
    int pos = a.find( b , 0 );
    int len = b.length();    
    if( pos != string::npos )
    {
        if( pos == 0 ) 
        {
            if(correspond( a[ pos + len ] ) == 0 )
            {
                return 1;
            }
            else 
            {
                return 0;  
            }      
        }
        else
        {
            if(correspond( a[ pos + len] ) == 0 && correspond( a[ pos - 1] ) == 0 )
            {
                return 1;
            }
            else
            {
                return 0;
            }    
        }
    }
    return 0;
}
int correspond( char str )
{
    if( ( str <= 'z' && str >= 'a' ) || ( str <= 'Z' && str >= 'A' ) )
    {
        return 1;
    }
    else
    {
        return 0;    
    }    
}

Use keywords to count the text in the test file

void sort_1(string str)
{
    for(int i = 0; i < 32; i++ )
    {
        if(match( str , Keyword[i] ) == 1)
        {
            if( match( str , "switch" ) )
            {
                Case_num.push_back( case_num );
                switch_num++;
                case_num = 0;
            }
            if( match( str , "case" ))
            {
                case_num++;
            }
            total_num++;
            break;
        }
    }
}

void sort_2( string str )
{
    if( match( str , "else if" ) )
    {
        top++;
        stack[ top ] = 2;
    }    
    else
    {
        if( match( str , "else" ) )
        {
            if( stack[ top ] == 1 )
            {
                ifelse_num++;
                top--;
            }
            else
            {
                if( stack[ top ] == 2 )
                {
                    if_elseif_else_num++;
                    top--;
                }
            }
        }
        else
        {
            if(match( str ,"if" ) )
            {
                top++;
                stack[ top ] = 1;
            }
        }
    }
}

Output the count result

int main()
{
    int level;
    string file,temp;
    cout << "Enter the file path: " << endl;
    cin >> file;
    cout << "Enter the completion level: " << endl;
    cin >> level;
    ifstream File( file.c_str() );
    int correspond( string a,string b );
    int correspond( char str);
    void sort_1( string str );
    void sort_2( string str );
    while ( getline( File ,temp ))
    {
        istringstream istr ( temp );
        string str;
        if( level >= 3)
        {
            sort_2( temp );
        }
        while( istr >> str ) 
        {
            sort_1( str );
        }
    }
    if( level >= 1 )  //level1 and above
    {
        cout << "total num: " << total_num << endl;
    }
    if( level >= 2 ) //level2 and above
    {
        cout << "switch num: " << switch_num << endl;
        if( !Case_num.empty() )
        {
            Case_num.push_back(case_num);
        }
        else
        {
            Case_num.push_back(0);
        }
        cout << "case num: ";
        for ( int i = 1; i <= switch_num; i++)
        {
            cout<< Case_num[i] << " ";
        }
        cout << endl;
    }
    if( level >=3 ) //level3 and above
    {
        cout << "if_else num: " << ifelse_num << endl;
    }
    if( level >=4 )  //level4 and above
    {
        cout << "if_elseif_else num: " <<if_elseif_else_num << endl;
    }
}

4.test file

#include <stdio.h>  
int main(){  
    int i=1;  
    double j=0;  
    long f;  
    switch(i){  
        case 0:  
        break;  
        case 1:  
        break;  
        case 2:  
        break;  
        default:  
        break;  
    }  
    switch(i){  
        case 0:  
        break;  
        case 1:  
        break;  
        default:  
        break;  
    }  
    if(i<0){  
        if(i<-1){}  
        else{}  
    }  
    else if(i>0){  
        if (i>2){}  
        else if (i==2) {}  
        else if (i>1) {}  
        else {}  
    }  
    else{  
        if(j!=0){}  
        else{}  
    }  
    return 0;  
}

5.Test output

img

6.Summary

  1. Learn how to use github and find solutions to problems
  2. Write a blog for the first time to explain your thoughts and strategies for solving the problem
...全文
200 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

285

社区成员

发帖
与我相关
我的任务
社区描述
福州大学 梅努斯国际工程学院 软件工程(2022秋) 教学
软件工程 高校
社区管理员
  • LinQF39
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧