183
社区成员
发帖
与我相关
我的任务
分享| 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 Assignment | Git/GitHub Using & Keywords Capturing |
| MU STU ID and FZU STU ID | 19105185 / 831901317 |
| Personal Software Process Stages | Estimated time(min) | Time(min) |
|---|---|---|
| Planning | 30 | 70 |
| Estimate | 200 | 700 |
| Development | - | - |
| Analysis | 180 | 200 |
| Design Spec | 10 | 15 |
| Design Review | 10 | 15 |
| Coding Standard | 30 | 40 |
| Design | 40 | 60 |
| Coding | 340 | 580 |
| Code Review | 60 | 110 |
| Test | 60 | 290 |
| Test Report | 60 | 70 |
| Postmortem & Process Improvement Plan· Design Review | 50 | 60 |
| Summary | 1070 | 2210 |
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
My source code https://github.com/1615639948/Lab2/blob/main/Final.cpp


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

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:

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
}
}
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:

**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 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
}
}
}
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 **:

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();
}
}
}
The code implement can be seen in github.
Now we use the example to test:

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.
