zoukankan      html  css  js  c++  java
  • 编程作业

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/computer-science-class4-2018
    这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-science-class4-2018/homework/11880
    这个作业的目标 <编程>
    其他参考文献 菜鸟教程

    一,Gitee地址

    gitee

    二,时间

    PSP2.1 Personal Software Process Stages 预估耗时(分钟)
    Planning 计划 0.5h
    Estimate 估计这个任务需要多少时间 15h
    Development 开发 3h
    Analysis 需求分析(包括学习新技术) 2h
    Design Spec 生成设计文档 0.5h
    Design Review 设计复审 0.5h
    Coding Standard 代码规范 (为目前的开发制定合适的规范) 0.1h
    Design 具体设计 0.5h
    Coding 具体编码 3h
    Code Review 代码复审 1h
    Test 测试(自我测试,修改代码,提交修改) 2h
    Reporting 报告 0.1h
    Test Repor 测试报告 0.1h
    Size Measurement 计算工作量 1.2h
    Postmortem & Process Improvement Plan 事后总结, 并提出过程改进计划 0.5h
    合计 15h

    三,解题思路描述

    四,代码规范

    五,具体实现

    1.头文件和类的定义

    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);
    }
    

    2.统计文本文件的字符数以及行数

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

    3.统计单词数并存储单词

    void loadword(char w[])
    {
        string wr;
        wr = w;
        map<string, int>::iterator it1 = mapword1.find(wr);//在map红黑树中查找单词 
        if (it1 == mapword1.end())
            mapword1.insert(pair<string, int>(wr, 1));//未找到单词,插入单词并设定频次为1 
        else
            ++it1->second;//找到单词,单词出现频次增加 
    }
    
    testfile testfile::countword(char *t, testfile f1)
    {
        int n = 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;//防止最后一个字符输出两次
            if (c >= 65 && c <= 90)
                c += 32;//大写字母转小写 
            f1.content[n++] = c;//把文本文件内的数据存入类的content字符数组中    
        }
        myfile.close();
        char temp[4];
        int i = 0, j = 0, flag = 0, words = 0, m = 0, k = 0;
        for (i = 0; i < n; i++)
        {
            if (!((f1.content[i] >= 48 && f1.content[i] <= 57) || (f1.content[i] >= 97 && f1.content[i] <= 122)))//跳过非字母和非数字字符 
                continue;
            else
            {
                for (j = 0; j < 4 && i < n; j++)
                {
                    if (!((f1.content[i] >= 48 && f1.content[i] <= 57) || (f1.content[i] >= 97 && f1.content[i] <= 122)))
                        break;
                    temp[j] = f1.content[i++];//temp中存入四个非空格字符
                }
                if (j == 4)
                {
                    for (m = 0; m < 4; m++)
                    {
                        if (temp[m] < 97 || temp[m]>122)
                        {
                            flag = 1;
                            break;//判断这四个字符是否都是字母
                        }
                    }
                    if (flag == 0)//四个字符都是字母的情况,判断为一个单词
                    {
                        char *w = new char[100];//存放单词 
                        for (m = 0; m < 4; m++)
                        {
                            w[k++] = temp[m];//temp中字符存入w
                        }
                        while (((f1.content[i] >= 48 && f1.content[i] <= 57) || (f1.content[i] >= 97 && f1.content[i] <= 122)) && i < n)//继续存入单词剩余字符
                        {
                            w[k++] = f1.content[i++];
                        }
                        w[k] = '';
                        loadword(w);//可以在此处插入一个外部函数返回一个单词存入map红黑树 
                        delete[]w;
                        words++;
                        k = 0;
                    }
                    else
                    {
                        flag = 0;
                        j = 0;
                    }
                }
            }
        }
        f1.words = words;
        return f1;
    }
    

    六,接口

    #ifndef wordcount_h
    #define wordcount_h
    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;
    
    };
    #endif
    

    七,测试

    #include<wordcount.h>
    #include<iostream>
    #include<locale>
    using namespace std;
    
    int main()
    {
        char filename[10];
        testfile f1;
        cin >> filename;
        f1.init();
        f1 = f1.countcha(filename, f1);
        cout << f1.getcharacters()<< endl;
        return 0;
    }
    

    八,心得和收获

    刚开始看到这个题目的时候,一脸迷茫,发现自己的c语言又忘的差不多了,无从下笔,再接着码云还有很多东西,一下子不知道怎么办,发现自己有空还是得多专研一下代码了,我要学的还有太多太多,虽然看了很久还是不知道怎么做,但是突然发现时间这么过的好快,好像打开了新世界的大门,加油吧!

  • 相关阅读:
    GC算法 垃圾收集器
    Distinct
    生产者消费者实现
    单例模式(七种实现方法)
    【JUC】JDK1.8源码分析之AbstractQueuedSynchronizer
    【JUC】JDK1.8源码分析之ConcurrentHashMap
    【集合框架】JDK1.8源码分析之HashMap
    一个整数,它加上100后是一个完全平方数, 再加上168又是一个完全平方数,请问该数是多少?
    猴子吃桃问题:猴子第一天摘下若干个桃子, 当即吃了一半,还不过瘾,又多吃了一个; 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。 * 以后每天早上都吃了前一天剩下的一半零一个。 到第10天早上想再吃时,见只剩下一个桃子了。 求第一天共摘了多少。 * 1.程序分析:采取逆向思维的方法,从后往前推断。
    输出9*9口诀 输出9*9乘法表
  • 原文地址:https://www.cnblogs.com/chenxiaolong-cxl/p/14610745.html
Copyright © 2011-2022 走看看