EE308 FZU 2022 Lab 1_2

832001325ZHOUYU 2022-10-27 23:16:18

The Link Your Class

https://bbs.csdn.net/forums/MUEE308FZU202201

 

The Link of Requirement of This Assignment

https://bbs.csdn.net/topics/608837246

 

The Aim of This Assignment

lab 1_2

 

MU STU ID and FZU STU ID

832001325 or 20122926

 

Personal Software Process Stages

Estimated time (minutes)

real time (minutes)

Planning

20

25

Estimate

10

5

Development

200

210

Analysis

30

40

Design Spec

30

40

Design Review

10

10

Coding Standard

30

25

Design

30

30

Coding

100

60

Code Review

10

10

Test and debug

30

30

Reporting

70

70

Size Measurement

10

10

Postmortem & Process Improvement Plan

20

30

Total

600

585

The link of github: https://github.com/ZYu158/lab-1_2/tree/main

In this experiment, we need to count the number of special characters. For this, my idea is mainly divided into four steps:

1. Open the file, read each word in each line, and determine whether the word is a keyword. In this process, look for the switch structure and look for the structure of the if_else statement.

2. When reading each word, we should make sure that the word starts with what and ends with what. For example, in my idea, each string starts with a letter and ends with a non-letter.

3. Determine whether this letter is a keyword. As we all know, there are 32 keywords in c++, we can build an array to store the keywords, count them, and then read them in the form of pointer or subscript in the code.

4. Then it is to record the number of occurrences of each string and output it in the main function.

The flow chart:

 

 

 

 

  1. But in the initial test, we found that the total number of keywords calculated was 36, which was not consistent with answer 35. After checking, it is found that the result is caused by the double calculation of do and double, so a judgment condition is added to solve the problem. In addition, I also learned that sometimes break would also cause counting errors. For advanced requirements, I start by looking for the switch keyword, count_switch, based on the basic requirement, and then create a count_case array, count_case [count_switch]+1, if a case happens once, count_switch+1, If a new switch occurs, then the number of cases corresponding to each switch can be counted. Finally, after improvement, the code for questions 1 and 2 is as follows:

void question1_2(string a[])

{

int i, j;

for (i = 0; i < cnt1; i++)

{

for (j = 0; j <= 31; j++)

{

if (a[i] == keyword[j])

{

keyword_num++;       

temp2[cnt2++] = a[i];

}

}

if (a[i] == "switch")

{

switch_num++;

case_cnt++;

}

if (a[i] == "case")

{

casenum[case_cnt]++;

}

}

}

 

  1. I decided to determine if there was a "if" between the "promotion requirement" and the "ultimate requirement." First of all, if there is an if, then the if-else structure is + 1. If there's an else If in the middle, then the number of if_else  -1, and then the number of if_elseif_else +1. Show my core code and some sample tests with the code prepared above:

 

 

void question3_4()

{

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_elseif_else_num++;

}

else

{

if_else_num++;

}

have_elseif.pop();

have_elseif.push(0);

while (s.top() != "if")

{

s.pop();

}

s.pop();

}

}

}

 

 

  1. Then we need to consider the output function and the main function. In the output function, we need to consider the input "level" value (1 to 4). For different values, we should output different results, the code is as follows:

 

void output(int Level)

{

if (Level == 1)

{

cout << "total num: " << keyword_num << endl;

}

if (Level == 2)

{

cout << "total num: " << keyword_num << endl;

cout << "switch num: " << switch_num << endl;

cout << "case num: ";

for (int i = 1; i <= case_cnt; i++)

{

cout << casenum[i] << " ";

}

cout << endl;

}

if (Level == 3)

{

cout << "total num: " << keyword_num << endl;

cout << "switch num: " << switch_num << endl;

cout << "case num: ";

for (int i = 1; i <= case_cnt; i++)

{

cout << casenum[i] << " ";

}

cout << endl;

cout << "if-else num: " << if_else_num << endl;

}

if (Level == 4)

{

cout << "total num: " << keyword_num << endl;

cout << "switch num: " << switch_num << endl;

cout << "case num: ";

for (int i = 1; i <= case_cnt; i++)

{

cout << casenum[i] << " ";

}

cout << endl;

cout << "if-else num: " << if_else_num << endl;

cout << "if-elseif-else num: " << if_elseif_else_num << endl;

}

}

 

  1. The last is the main function, in the main function, we should realize to read every word in the file, and should include the previous self defined function, the main function is as follows:

int main()

{

int level;

string filename, temp;

char buffer[1];

cout << "please input file path:";

cin >> filename;

ifstream fin(filename.c_str());

cout << "input level:";

cin >> level;

if (!fin)

{

cout << "File opening failure" << endl;

}

else

{

while (getline(fin, temp))

{           // Input from disk file, read in line by line, processed line by line

keywords(temp);

}

fin.close();    // close   

}

question1_2(temp1);  

question3_4();          

output(level);

}

 

Finally:

  

 

 

     This is the first time in my life that I use the PSP form. I find that the actual completion time is almost always longer than my estimated time, so I think my ability needs to be improved, which requires training. I use c++because I have a good grasp of it. The use of flow charts has brought me great benefits. I can clearly understand the logical relationship between the previous and subsequent steps. Finally, I learned how to use github and how important it is to look up data and revise it repeatedly as I write code 

 

...全文
55 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

285

社区成员

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

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