团队编程

Github链接:https://github.com/Robot-Blitzcrank/wordCount

模块设计

本次的代码分为以下几个模块:

  • 统计字符数

int count_characters(string s){	



	int num = 0;

	for(int i=0;i<s.length();i++){

		if(s[i]>=0&&s[i]<=127)

		num++;

	}

	return num;

}
  • 统计文件的有效行数

class testfile

{

public:

    testfile countcha(char *, testfile);//�����ַ���

    testfile countword(char *, testfile);//���㵥����

    testfile countline(char *, testfile);//��������

    int getcharacters();

    int getlines();

    int getwords();

    char *content;//����ı��ļ����� 

    void init();

private:

    int characters;

    int words;

    int lines;



};



void testfile::init()

{

    characters = 0;

    words = 0;

    lines = 0;

    content = (char*)malloc(sizeof(char*)*MAXN);

}





	//ͳ���ı��ļ����ַ����Լ����� 

testfile testfile::countcha(char *t, testfile f1)

{

    int i = 0;

    ifstream myfile;

    myfile.open(t);

    if (!myfile.is_open())

    {

        cout << "�ļ���ʧ��" << endl;

    }

    char c;

    myfile >> noskipws;//ǿ�ƶ���ո�ͻ��з�

    while (!myfile.eof())

    {

        myfile >> c;

        if (myfile.eof())

            break;//��ֹ���һ���ַ��������

        i++;

    }

    f1.characters = i;

    myfile.close();

    return f1;

}



testfile testfile::countline(char *t, testfile f1)

{

    ifstream myfile;

    myfile.open(t, ios::in);

    int i = 0;

    string temp;//��Ϊgetline����ʹ��

    if (!myfile.is_open())

    {

        cout << "�ļ���ʧ��" << endl;

    }

    while (getline(myfile, temp))

    {

        if(temp.empty()) continue;

        i++;

    }

    f1.lines = i;

    myfile.close();

    return f1;

}
  • 统计单词数

int sum_word(){//
	int sum = 0;
	for(int i = 0;i < (str.size()-4);i++){
		int k = 0;
		if(checkA(str[i])){
			if(checkA(str[i+1]) && checkA(str[i+2]) && checkA(str[i+3])){ //检测前四位是否为字母 
				for(int j = i + 3;j < str.size();j++){
					if(str[j] == ' '||str[j] == '\n' || (!checkA(str[i])&&!checkB(str[i])) ){
						k = j;
						break;
					}
		        	k = str.size()-1;
				}
				string temp;
				temp = str.substr(i,k-i+1);
				struct word_node temp_node;
				temp_node.word = temp;
				temp_node.num = 1;
				words.push_back(temp_node);
				sum++;
				i = k;
			}
		}else if(checkB(str[i])){
			for(int j = i;j<str.size();j++){
				if(str[j] == ' '||str[j] == '\n' || (!checkA(str[j])&&!checkB(str[j])) ){
					k = j;
					break;
					
				}
				k = str.size()-1;
			}
			i = k;
		}
	}
	return sum;
}
  • 统计词组(单词)及其词频

void count_word(){//统计单词频率
	for(int i =0;i<words.size();i++){
		transform(words[i].word.begin(), words[i].word.end(), words[i].word.begin(), ::tolower);
	}
	for(int i = 0;i < (words.size()-1);i++){
		for(int j = i+1;j < words.size();j++){
			if(words[i].word == words[j].word){
				words[j] = words[(words.size()-1)];
				words.pop_back();
				words[i].num++;
			}
		}
	}
	sort_word();
} 
  • 命令行输入、文件输入输出

int main(int argc, char *argv[])
{
     int in, out, m = 0, n = 10, i, get, count=0;//count统计共有多少个换行符
     int characters = 0, words = 0, lines = 0;
     char ch;
     char* ff;
     string strp, strpp;//输入后单词存入strp中,其间以‘ ’分隔
 /*    for (i = 1; i < argc; i++) {
          cout << argv[i] << endl;
     }
 *///测试用码
     for (i = 1; i < argc; i++) {
          ff = argv[i];
          if ((get = strcmp("-i", ff)) == 0) {
               in = i + 1;
          }
          if ((get = strcmp("-o", ff)) == 0) {
               out = i + 1;
          }
          if ((get = strcmp("-m", ff)) == 0) {
               m = atoi(argv[i + 1]);
          }
          if ((get = strcmp("-n", ff)) == 0) {
               n = atoi(argv[i + 1]);
          }
     }
     fstream infile(argv[in], ios::in);       //指定输入文件流对象
     fstream outfile(argv[out], ios::out);     //指定输出文件流对象.
     //连续读入单词,中间以空格分割
     infile >> strp; 
     while (infile >> strpp) {
          strp = strp + ' ' +  strpp;
     }

     infile.clear();
     infile.seekg(0, ios::beg);
     while (1) {
          if ((ch = infile.get()) == '\n') {
               count++;
          }
          else if (ch == EOF) {
               break;
          }
     }
     cout << strp << endl;//用于在cmd中查看结果
     cout << count;//输出共有几行
     //文件输出
     outfile << "characters:" << characters << endl;
     outfile << "words:" << words << endl;
     outfile << "lines:" << lines << endl;
     for (i = 0; i <= n; i++) {
          outfile << '<' << "str" << '>:' << "times";
     }     
}

组员职责分工

组员 具体分工
031702327何云鹏 文件输入输出
031702142林德辉 统计字符数
031702130张成德 统计文件的有效行数
031702101王茜葶 统计单词数
031702108叶心言 统计词组(单词)及其词频
031702140彭文泽 github汇总
031402112李中瑾 命令行输入
031702409余泓 博客,代码汇总

程序运行截图

提交日志截图

posted @ 2019-10-19 14:10  灯塔。  阅读(376)  评论(0编辑  收藏  举报