| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 513 人关注过本帖
标题:哪位大神能解释一下这个程序!!!在VC6.0下怎么运行!!!
只看楼主 加入收藏
nayizhan
Rank: 2
等 级:论坛游民
帖 子:10
专家分:33
注 册:2011-12-22
结帖率:0
收藏
已结贴  问题点数:10 回复次数:1 
哪位大神能解释一下这个程序!!!在VC6.0下怎么运行!!!
章编辑:输入一页文字,程序可以统计出文字、数字、空格的个数。
静态存储一页文章,每行最多不超过80个字符,共N行;要求(1)分别统计出其中英文字母数和空格数及整篇文章总字数;(2)统计某一字符串在文章中出现的次数,并输出该次数;(3)删除某一子串,并将后面的字符前移。
存储结构使用线性表,分别用几个子函数实现相应的功能;
输入数据的形式和范围:可以输入大写、小写的英文字母、任何数字及标点符号。
输出形式:(1)分行输出用户输入的各行字符;(2)分4行输出"全部字母数"、"数字个数"、"空格个数"、"文章总字数"(3)输出 删除某一字符串后的文章。
#include"alist.h"
#include<string>
#include<algorithm>
#include<iostream>
#include<sstream>
using namespace std;
int main()
{
        string line,word;
        AList<string> line_of_txt(20);
        int alphanum=0,digitnum=0,space =0,all=0;
        while(getline(cin,line))
        {
                line_of_txt.append(line);
                for(string::iterator iter=line.begin();iter!=line.end();iter++)
                {
                        if(*iter==' ')space++;
                        if(isalpha(*iter))alphanum++;
                        if(isdigit(*iter))digitnum++;
                        all++;
                }
        }
        cin.clear();
        cout<<"the line of txt is:"<<endl;
        line_of_txt.moveToStart();
        for(int i=0;i<line_of_txt.length();i++)
        {
                cout<<line_of_txt.getValue()<<endl;
                line_of_txt.next();
        }
        cout<<"alpha:"<<alphanum<<endl<<"digit:"<<digitnum<<endl<<"all:"<<all<<endl<<"space:"<<space<<endl;
        cout<<"please enter the word need look"<<endl;
        string s;
        cin>>s;
        int number=0;
        line_of_txt.moveToStart();
        for( i=0;i<line_of_txt.length();i++)
        {
                line=line_of_txt.getValue();
                string::size_type iter=0;
                while((iter=line.find(s,iter))!=string::npos)
                {
                        number++;
                        iter++;
                }
                line_of_txt.next();
        }
        cout<<number<<": "<<s<<endl;
        char exclude[80];
        cout<<"please enter the word need exclude:"<<endl;
        cin>>exclude;
        line_of_txt.erase(exclude);
        cout<<"after exclude"<<endl;
        line_of_txt.moveToStart();
        for( i=0;i<line_of_txt.length();i++)
        {
                cout<<line_of_txt.getValue()<<endl;
                line_of_txt.next();
        }
        return 0;
}


// From the software distribution accompanying the textbook
// "A Practical Introduction to Data Structures and Algorithm Analysis,
// Third Edition (C++)" by Clifford A. Shaffer.
// Source code Copyright (C) 2007-2011 by Clifford A. Shaffer.

// This is the file to include in your code if you want access to the
// complete AList template class

// First, get the declaration for the base list class
#ifndef ALIST_H
#define ALIST_H
#include "list.h"
#include<string>
// This is the declaration for AList. It is split into two parts
// because it is too big to fit on one book page
template <typename E> // Array-based list implementation
class AList : public List<E> {
private:
        int maxSize;        // Maximum size of list
        int listSize;       // Number of list items now
        int curr;           // Position of current element
        E* listArray;    // Array holding list elements

public:
        AList(int size=0) { // Constructor
                maxSize = size;
                listSize = curr = 0;
                listArray = new E[maxSize];
        }

        ~AList() { delete [] listArray; } // Destructor

        void clear() {                    // Reinitialize the list
                delete [] listArray;            // Remove the array
                listSize = curr = 0;            // Reset the size
                listArray = new E[maxSize];  // Recreate array
        }

        // Insert "it" at current position
        void insert(const E& it) {
                for(int i=listSize; i>curr; i--)  // Shift elements up
                        listArray[i] = listArray[i-1];  //   to make room
                listArray[curr] = it;
                listSize++;                       // Increment list size
        }

        void append(const E& it) {       // Append "it"
                listArray[listSize++] = it;
        }

        // Remove and return the current element.
        E remove() {
                E it = listArray[curr];           // Copy the element
                for(int i=curr; i<listSize-1; i++)  // Shift them down
                        listArray[i] = listArray[i+1];
                listSize--;                          // Decrement size
                return it;
        }
        void moveToStart() { curr = 0; }        // Reset position
        void moveToEnd() { curr = listSize; }     // Set at end
        void prev() { if (curr != 0) curr--; }       // Back up
        void next() { if (curr < listSize) curr++; } // Next

        // Return list size
        int length() const  { return listSize; }
        // Return current position
        int currPos() const { return curr; }

        // Set current list position to "pos"
        void moveToPos(int pos) {
                curr = pos;
        }

        const E& getValue() const { // Return current element
                return listArray[curr];
        }
        void erase(char* s)
        {
                for(int i=0;i<length();i++)
                {
                        string::size_type pos=0;
                        while((pos=listArray[i].find(s))!=string::npos)
                        {
                                listArray[i].erase(pos,strlen(s));
                        }
                }
        }
};
#endif

// From the software distribution accompanying the textbook
// "A Practical Introduction to Data Structures and Algorithm Analysis,
// Third Edition (C++)" by Clifford A. Shaffer.
// Source code Copyright (C) 2007-2011 by Clifford A. Shaffer.


template <typename E> class List { // List ADT
private:
        void operator =(const List&) {}      // Protect assignment
        List(const List&) {}           // Protect copy constructor
public:
        List() {}          // Default constructor
        virtual ~List() {} // Base destructor

        // Clear contents from the list, to make it empty.
        virtual void clear() = 0;

        // Insert an element at the current location.
        // item: The element to be inserted
        virtual void insert(const E& item) = 0;

        // Append an element at the end of the list.
        // item: The element to be appended.
        virtual void append(const E& item) = 0;

        // Remove and return the current element.
        // Return: the element that was removed.
        virtual E remove() = 0;

        // Set the current position to the start of the list
        virtual void moveToStart() = 0;

        // Set the current position to the end of the list
        virtual void moveToEnd() = 0;

        // Move the current position one step left. No change
        // if already at beginning.
        virtual void prev() = 0;

        // Move the current position one step right. No change
        // if already at end.
        virtual void next() = 0;

        // Return: The number of elements in the list.
        virtual int length() const = 0;

        // Return: The position of the current element.
        virtual int currPos() const = 0;

        // Set current position.
        // pos: The position to make current.
        virtual void moveToPos(int pos) = 0;

        // Return: The current element.
        virtual const E& getValue() const = 0;
};
搜索更多相关主题的帖子: 文章 统计 英文字母 标点符号 线性表 
2011-12-25 14:36
andy_xsh
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2011-12-12
收藏
得分:10 
看看。帮不了你。。
2011-12-25 22:39
快速回复:哪位大神能解释一下这个程序!!!在VC6.0下怎么运行!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045812 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved