EE308 Lab2

厦大打工人 2021-09-23 23:51:52

EE308 Lab2 Individual programing work

The Link Your Class https://bbs.csdn.net/forums/MUEE308FZ?category=0
The Link of Requirement of This Assignment https://bbs.csdn.net/topics/600798588
The Aim of This AssignmentGit/GitHub Using & Keywords Capturing
MU STU ID and FZU STU ID19105185 / 831901317

PSP

Personal Software Process StagesEstimated time(min)Time(min)
Planning3070
Estimate200700
Development--
Analysis180200
Design Spec1015
Design Review1015
Coding Standard3040
Design4060
Coding340580
Code Review60110
Test60290
Test Report6070
Postmortem & Process Improvement Plan· Design Review5060
Summary10702210

Stating with Git

Git installation tutorial https://www.cnblogs.com/ximiaomiao/p/7140456.html


Github tutorial https://www.bilibili.com/video/BV1Xx411m7kn?spm_id_from=333.999.0.0

GitHub Repository

My source code https://github.com/1615639948/Lab2/blob/main/Final.cpp

img

img

Ideas of solving the problem

  • Choice of language :The first is the problem of language selection. In our freshman year, we majored in electronic information engineering learned the language python. In our sophomore year, we learned Arduino C. in the second semester of our sophomore year, we learned C + +. Here, I first sent C language, because the C language I involved is only some simple single-chip microcomputer programming, which can not complete this task at all. However, python has been learning for too long and forgotten too much, so I can only choose C + + for programming. However in the process of writing, I obviously feel that C + + is cumbersome. In the later programming, I should learn more about python.

  • General idea flow chart

img

Text input :To be honest, I spent a lot of time on text input, because I didn't use C + + for a long time. At the beginning, I learned how to read text. At first, I was worried that I could only read .txt files instead of .cpp files, but after testing, it was found that it could be successfully implemented.

c++读取TXT文件内容 - 张成的博客 - 博客园 (cnblogs.com) https://www.cnblogs.com/nkzhangcheng/p/7722568.html

  • I first used the reading of the whole text. A txt variable of string type is created to receive the content of text, and then a vector < string > text is created to receive the elements in each txt as a quantity in the string array.

    void ReadFile(InputInformation input,vector<string> text){
        ifstream file(input.file_address);
        for (string txt; file >> txt;) {
            text.push_back(txt);
        }
        for (int i = 0; i < text.size(); ++i) {
            cout << text[i];
        }
    }
    

    This is the result of code testing:

img

  • However, I found it difficult to read the full text above after 3 and 4 questions, so I modified it to read line by line, that is, the form of the final code.

    int main()
    {
        int level;
        string filename,temp;
        cout<<"input the text address: ";
        cin>>filename;
        ifstream file(filename.c_str());
        if ( ! file)
        {
            cout << "File cannot be opened" <<endl;
        }else
        {
            while (getline(file,temp))
            {           // Input from disk file, read in line by line, processed line by line
                TakeKeywords(temp);
            }
            file.close();    // Close the file input stream
        }
    }
    

Text Processing :

  • Use the TakeKeywords function to convert the text. First, convert the string to an array of char, then process the char array, remove symbols, and concatenate adjacent letters, so that you can get a number of strings, and then compare the string with the keywords in the lexicon, you can pick out the correct string And store it in an array for questions 1 and 2.

  • In addition, vector if_elseif_else is used to store '{', '}', 'if', 'else if', 'else' during reading, which is used to solve problems 3 and 4. However, else_if is not easy to judge in the implementation process, and it will be pressed repeatedly in the process. Finally, whether else is pressed in the previous one is used to judge the condition.

    Flow chart of text conversion:

img

**This is my code implementation:**
void TakeKeywords(string s) {
    string str;
    int len = s.length();
    int symbol = 0; //Check if non-alphabetic characters are '{' or '}' and mark them
    const char* p = s.data();  //将string转化为char *[]
    for (int i = 0; i < len; ++i) {
        if (p[i] == '{')
        {
            symbol = 1;
        }
        if (p[i] == '}')
        {
            symbol = 2;
        }

        if ((p[i] >= 'a'&&p[i] <= 'z'||p[i] >= 'A'&&p[i] <= 'Z'))
        {
            str += p[i];
        }
        else
        {
            if (str == "")//No letters are encountered, but parentheses may be encountered, check, if any, press into IEE
            {
                if (symbol == 1){
                    if_elseif_else.push_back("{");
                }
                if (symbol == 2){
                    if_elseif_else.push_back("}");
                }
                symbol = 0;
                continue;
            }
            temp1[countNum] = str;
            countNum++;
            if (str == "else") //If we find an else if we find an else if we find an if after n Spaces
            {
                int j = i;
                while (p[j] == ' ' && j <= len){
                    j++;
                }
                if (p[j] == 'i' && p[j+1] == 'f'){
                    if_elseif_else.push_back("else_if");
                    i = j+2;
                    keyword_num++;
                }else{
                    if_elseif_else.push_back("else");
                }
            }
            if (str == "if"){  //I find if and I tell you if the previous one is else, if it's not, I push if in
                if(if_elseif_else.back() != "else"){
                    if_elseif_else.push_back("if");
                }
            }
            if (symbol == 1){
                if_elseif_else.push_back("{");
            }
            if (symbol == 2){
                if_elseif_else.push_back("}");
            }
            symbol = 0;
            str = "";
            continue;
        }
    }
}

The completion function of task 1 and 2:

The TakeKeywords() function completes all the preparations for the completion of subsequent tasks. I think task one or two is relatively simple in difficulty, but task three or four is more difficult.

Calculate12(). Get the required results through the use of arrays:

void Calculate12(string a[]){
    int i,j;
    for(i = 0;i < countNum;i++)
    {
        for(j = 0;j < 32;j++)//Decide if the word is the desired keyword
        {
            if(a[i] == key_word[j])
            {
                keyword_num++;
            }
        }
        if(a[i] == "switch")
        {
            switch_num++;
            case_count++;
        }
        if(a[i] == "case")
        {
            case_num[case_count]++;//Read from the first
        }
    }
}

The completion function of task 3 and 4:

I encountered great difficulties in solving the three four questions, because I couldn't use the C + + I learned to solve it well, so I consulted the relevant materials and finally decided to use the stack to solve the three four tasks.

The idea of matching parentheses, using the access stack, complete task 3. 4

**The flow chart of task three and four is as follows **:

img

The code of task three and four is as follows:

void Calculate34()
{
    stack <string> s;
    stack <int> have_elseif;
    for (int i = 0;i < if_elseif_else.size();i++)
    {
        if (if_elseif_else[i] == "{")
        {
            s.push("{");
            have_elseif.push(0);
        }
        else if (if_elseif_else[i] == "}")
        {
            while (s.top() != "{")
            {
                s.pop();
            }
            s.pop();
            have_elseif.pop();
        }
        else if (if_elseif_else[i] == "if")
        {
            s.push("if");
            have_elseif.pop();
            have_elseif.push(0);
        }
        else if (if_elseif_else[i] == "else_if")
        {
            have_elseif.pop();
            have_elseif.push(1);
        }
        else if (if_elseif_else[i] == "else")
        {
            if (have_elseif.top())
            {
                if_else_else_num++;
            }
            else
            {
                if_else_num++;
            }
            have_elseif.pop();
            have_elseif.push(0);
            while (s.top() != "if")
            {
                s.pop();
            }
            s.pop();
        }
    }
}

Final code test

The code implement can be seen in github.
Now we use the example to test:

img

  • Deficiency and reflection

    The {} determination mechanism is used to determine three or four tasks, but in the syntax of C and C + +, there can be no {} when writing ifelse statements, so I can't solve this situation.

Task test

img

Summary

  1. Use git for the first time, learn the basic git command, create a warehouse on GIT and upload this job.
  2. Through this assignment, I found the gap between myself and others' code level. When writing code, many ideas could not be realized in code. It was very distressing, but I gained a lot.
  3. When reading and writing files, we read a lot of other people's code, but we have many difficulties in implementing it ourselves. Therefore, when learning code, we should try more by ourselves instead of just listening to knowledge.
  4. Many small details are not handled well, making your code always wrong. It takes a lot of time in the process of continuous debugging, or your ideas are not comprehensive enough.
  5. Many students write in Python. The writing difficulty is much lower than that of C + +. I'm going to attack Python later and master more languages.
...全文
278 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

183

社区成员

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

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