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 | 1. To achieve a program function 2. Extract keywords of different levels from the C++ code files that are read in 3. learn the use of github |
MU STU ID and FZU STU ID | 19103921_831902217 |
✔Content
文章目录
🎪The Links
https://github.com/yyx0115/EE308_LAB2/tree/main
📝PSP FORM
Personal Software Process stage | Estimated Time(min) | Actual Time(min) |
---|---|---|
Planning | – | – |
Estimate | 30 | 30 |
Development | – | – |
Analysis | 100 | 120 |
Design Spec | 60 | 100 |
Design Review | 15 | 15 |
Coding Standard | 30 | 30 |
Design | 180 | 200 |
Coding | 1200 | 1600 |
Code Review | 60 | 60 |
Test | 100 | 100 |
Reporting | – | – |
Test Report | 30 | 30 |
Size Measurement | 15 | 15 |
Postmortem & Process Improvement Plan | 60 | 60 |
Sum of Time | 1880 | 2360 |
🌋Description of problem-solving ideas
- Choose Language
- c++/c
- Python
- java
- Review the knowledge of C/C++
we need to know the keywords:
After last semester’s study, I acquired some knowledge about Java, C ++/ C. It’s a pity that I didn’t learn Python yet. After a summer vacation of “indulgence”, before starting this assignment, I need to review the “if-else” knowledge to wake up my memory
- rough thinking
-
The file is read in the specified address
-
Create a user input class
-
a search class to find the keywords
-
a class to calculate the number of keywords and the times of switches and cases
-
use pointer function probably
-
the number of if-else and if else if else
📜The Chart of the Key Functions
💫Code Description
- create a file named “in.txt” include⤵:
#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;
}
-
The input⤵:
#include<bits/stdc++.h> using namespace std; map<string,int> mp; string words[32]={"auto","case","char","const","do", "double","enum","extern","float","goto", "int","long","register","short","signed","static", "struct","typedef","union","unsigned","void","volatile","while" ,"break","continue","for","if","sizeof","switch","return","default","else"};
-
open and read the file⤵:
void getNum(char *s,int grade){
queue<int> q;
ifstream infile;
infile.open(s,ios::in);
if (!infile){
printf("file opened failure!!!");
return ;
}
- To find the switch and case numbers⤵:
string temp;
bool judgeswitch=false;
int cntcase;
while (!infile.eof()){
infile>>temp;
//cout<<temp<<endl;
for (int i=0;i<32;i++){
if (i<22){
if (temp==words[i]){
mp[temp]++;
if (temp=="case"&&judgeswitch) cntcase++;
}
}
else{
if (temp.substr(0,words[i].size())==words[i]){
if (temp.size()==words[i].size()){
mp[words[i]]++;
if (words[i]=="switch"){
judgeswitch=true;
cntcase=0;
}
else if (words[i]=="default"){
judgeswitch=false;
q.push(cntcase);
}
}
else if (temp[words[i].size()]=='('||temp[words[i].size()]==';'||temp[words[i].size()]==':'
||temp[words[i].size()]=='{'){
mp[words[i]]++;
if (words[i]=="switch"){
judgeswitch=true;
cntcase=0;
}
else if (words[i]=="default"){
judgeswitch=false;
q.push(cntcase);
}
}
}
}
}
}
- output and the main function⤵:
int total=0;
for (auto v:mp){
//output all the keywords
//cout<<v.first<<" : "<<v.second<<endl;
//calculate the sum
total+=v.second;
}
cout<<"total num:"<<total<<endl;
if (grade==2){
cout<<"switch num: "<<q.size()<<endl;
cout<<"case num: ";
while (!q.empty()){
cout<<q.front()<<" ";
q.pop();
}
}
}
int main(){
char url[100];
int grade;
cout<<"Enter the file path and judgment level:"<<endl; // in.txt level 2
cin>>url>>grade;
getNum(url,grade);
return 0;
}
- The output⤵
The entire code
#include<bits/stdc++.h>
using namespace std;
map<string,int> mp;
string words[32]={"auto","case","char","const","do",
"double","enum","extern","float","goto",
"int","long","register","short","signed","static",
"struct","typedef","union","unsigned","void","volatile","while"
,"break","continue","for","if","sizeof","switch","return","default","else"};
void getNum(char *s,int grade){
queue<int> q;
ifstream infile;
infile.open(s,ios::in);
if (!infile){
printf("file opened failure!!!");
return ;
}
string temp;
bool judgeswitch=false;
int cntcase;
while (!infile.eof()){
infile>>temp;
//cout<<temp<<endl;
for (int i=0;i<32;i++){
if (i<22){
if (temp==words[i]){
mp[temp]++;
if (temp=="case"&&judgeswitch) cntcase++;
}
}
else{
if (temp.substr(0,words[i].size())==words[i]){
if (temp.size()==words[i].size()){
mp[words[i]]++;
if (words[i]=="switch"){
judgeswitch=true;
cntcase=0;
}
else if (words[i]=="default"){
judgeswitch=false;
q.push(cntcase);
}
}
else if (temp[words[i].size()]=='('||temp[words[i].size()]==';'||temp[words[i].size()]==':'
||temp[words[i].size()]=='{'){
mp[words[i]]++;
if (words[i]=="switch"){
judgeswitch=true;
cntcase=0;
}
else if (words[i]=="default"){
judgeswitch=false;
q.push(cntcase);
}
}
}
}
}
}
int total=0;
for (auto v:mp){
//output all the keywords
//cout<<v.first<<" : "<<v.second<<endl;
//calculate the sum
total+=v.second;
}
cout<<"total num:"<<total<<endl;
if (grade==2){
cout<<"switch num: "<<q.size()<<endl;
cout<<"case num: ";
while (!q.empty()){
cout<<q.front()<<" ";
q.pop();
}
}
}
int main(){
char url[100];
int grade;
cout<<"Enter the file path and judgment level:"<<endl; // in.txt level 2
cin>>url>>grade;
getNum(url,grade);
return 0;
}
🎄Summarize this assignment
-
I gained new knowledge on how to use GitHub,created my own GitHub page. ♪(´▽`)
-
wrote code in the process of consolidating and reviewing the previous courses. After relearning, I found many deficiencies and did not grasp many details in place. ( •̀ ω •́ )y
-
Learned how to draw a flow chart with markdown and plan in advance with PSP form, which greatly improved the efficiency
-
I feel very guilty for not completing the last two levels of questions, and my ability still needs to be improved. `(>﹏<)′
-
The knowledge points of file flow need to be further strengthened. The reason why we choose to use C + + is that the knowledge of Java is not proficient enough and needs to be further study.