EE308_Lab1-2

盾子192 2022-10-28 06:04:55

The Link Your Class

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

The Link of Requirement of This Assignment

https://bbs.csdn.net/topics/608734907
The Aim of This AssignmentAchive a program function which can extract keywords
GitHub repositoryhttps://github.com/BlueP0118/Git-hub1
MU STU ID and FZU STU ID

20124287_832002122

1.PSP FORM

Personal Software Process StagesESTIMATE TIME/MINUTESREAL TIME(MINUTE)
Planning3040
Estimate

20

20
Development3020
Analysis6050
Design Spec4040
Design Review5040
Coding Standard6030
Design6060
Coding60120
Code Review6030
Test3020
Reporting6040
Test Report2020
Size Measurement4040
Postmortem&Process Improvement Plan3030
Total650600

2.Problem-solving ideas

Since I am familiar with C++, I chose C++ as the programming language for this lab. After getting the questions, we could write a method for each level due to the grading requirements.

3.Design and implementation process

 

4.Code description

Level 1:

void level_1(vector <string> s) {
	int total_num = 0;
	for (int i = 0; i < int(s.size()); i++) {
		for (int j = 0; j < 32; j++) {
			if (s[i] == keyword[j])
				total_num++;
		}
	}
	cout << "total num: " << total_num << endl;
}

Level 2:

void level_2(vector <string> s) {
	int switch_num = 0;
	int case_num = 0;
	vector <int> case1;
	for (int i = 0; i < int(s.size()); i++) {
		if (s[i] == "switch") {
			switch_num += 1;
			for (int j = i + 1; j < int(s.size()); j++) {

				if (s[j] == "case") {
					case_num += 1;
				} else if (s[j] == "switch" ) {
					i = j - 1;
					case1.push_back(case_num);
					case_num = 0;
					break;
				} else if (j == int(s.size() - 1)) {
					i = j;
					case1.push_back(case_num);
					break;
				}
			}
		}
	}
	cout << "switch num: " << switch_num << endl;
	cout << "case num: ";
	for (int i = 0; i < (int)case1.size(); i++) {
		cout << case1[i] << " ";
	}
	cout << endl;
}

 

Level 3:

void level_3(vector <string> s) {
	int if_else_num = 0;
	int if_elseif_else_num = 0;
	bool c = true;//Check if the following content is else if or if
	for (int i = 0; i < (int)s.size(); i++) {
		if (s[i] == "if") {
			for (int j = i + 1; j < (int)s.size(); j++) {
				if (s[j] == "if") {
					i = j;
				} else if (s[j] == "else" && s[j + 2] == "if") {
					if (c == true)
						c = false;
				} else if (s[j] == "else" && s[j + 2] != "if") {
					if (c == true)
						if_else_num++;
					else if (c == false)
						if_elseif_else_num++;
				}
			}
		}
	}
	cout << "if-else num: " << if_else_num << endl;
}

Level 4:

void level_4(vector <string> s) {
	int if_else_num = 0;
	int if_elseif_else_num = 0;
	bool c = true;//Check if the following content is else if or if
	for (int i = 0; i < (int)s.size(); i++) {
		if (s[i] == "if") {
			for (int j = i + 1; j < (int)s.size(); j++) {
				if (s[j] == "if") {
					i = j;
				} else if (s[j] == "else" && s[j + 2] == "if") {
					if (c == true)
						c = false;
				} else if (s[j] == "else" && s[j + 2] != "if") {
					if (c == true)
						if_else_num++;
					else if (c == false)
						if_elseif_else_num++;
				}
			}
		}
	}
	cout << "if-else num: " << if_elseif_else_num << endl;
}

keyword and main function:

vector<string> keyword = {"char", "double", "enum", "float", "int", "long",
                          "short", "signed", "struct", "union", "unsigned", "void", "for",
                          "do", "while", "break", "continue", "if", "else", "goto", "switch",
                          "case", "default", "return", "auto", "extern", "register", "static",
                          "const", "sizeof", "typedef", "volatile"
                         };




int main() {
	int level;
	string filename;
	ifstream inFile;
	cout << "Please input the path of the code file:" << endl;
	cin >> filename;
	cout << "Please input the completion level(from 1 to 4):" << endl;
	cin >> level;
	inFile.open(filename.c_str());
	if (inFile.fail()) {
		cout << "The file can not open rightly" << endl;
		exit(0);
	}
	vector <string> str;
	string str1;
	while (getline(inFile, str1)) {
		int i = 1;
		int j = 1;
		while (i < int(str1.size()) && j < int(str1.size())) {
			if (isalpha(str1[i])) {
				if (!isalpha(str1[j] )) {
					string s = str1.substr(i, j - i);
					str.push_back(s);
					i = j + 1;
					j = i;
				} else {
					j++;
				}
			} else {
				i++;
				j = i;
			}
		}
	}

	if (level == 1) {
		level_1(str);
	}
	if (level == 2) {
		level_1(str);
		level_2(str);
	}
	if (level == 3) {
		level_1(str);
		level_2(str);
		level_3(str);
	}
	if (level == 4) {
		level_1(str);
		level_2(str);
		level_3(str);
		level_4(str);
	}
}

5.TEST

Level 1:

 Level 2:

Level 3:

 

Level 4:

 

Fail to read:

 

 

6.Summarize

 

In fact, my code is very complicated, and there are many parts that can be simplified. For example, level3 and 4 can be merged and output separately, but unfortunately I haven't done too much optimization due to time. At the same time, my relatively weak basic knowledge led to frequent errors in my code, and I spent a lot of time to revise and correct it. For my harvest, I used github to build a repository and uploaded my code.

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

285

社区成员

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

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