| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 510 人关注过本帖
标题:公司要求周一前交c++程序,我的程序不符合要求但是改进实在不会请求帮助
只看楼主 加入收藏
lordys
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-7-2
收藏
 问题点数:0 回复次数:0 
公司要求周一前交c++程序,我的程序不符合要求但是改进实在不会请求帮助
程序的要求是:用一个char *readword(FILE *MyFile)函数读取一个文件,该文件中有许
多不同的英文单词,需要将这些英文单词根据字长进行分类,每一类又按照单词首个字母
的顺序按字母表排列显示;

下面是要求现实的结果示意:word  of 1 lettre :

à

l

 

word  of 2 lettre(s) :

Au

de

le

 

word  of 5 lettre(s) :

autre

Esker



这是我现有写的程序:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int SIZE = 100000;

struct ITEM
{
        char* st;
        int len;
};

bool operator < (const ITEM& x, const ITEM& y)
{
        if (x.len == y.len) return strcmp(x.st, y.st) < 0;
        return x.len < y.len;
}

vector<ITEM> data;
char buffer[SIZE];

int isLetter(char ch)
{
        return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z';
}

char* LireMot()
{
        char* ret;
        char ch;
        int i = 0;
        ch = getchar();
        while (ch != EOF && !isLetter(ch)) ch = getchar();
        while (isLetter(ch))
        {
                buffer[i++] = ch;
                ch = getchar();
        }
        if (i == 0) return NULL;
        buffer[i] = 0;
        ret = (char*)malloc(sizeof(char) * (i + 1));
        strcpy(ret, buffer);
        return ret;
}

int main()
{
        ITEM tmp;
        char filename[100];
        char* word;
        int i;

        printf("Please input the filename: ");
        scanf("%s", filename);
        freopen(filename, "r", stdin);
        while (word = LireMot())
        {
                tmp.st = word;
                tmp.len = strlen(tmp.st);
                data.push_back(tmp);
        }
        sort(data.begin(), data.end());
        for (i=0; i<data.size(); i++)
        {
                if (i == 0 || data[i].len != data[i-1].len)
                {
                        if (data[i].len > 1) printf("word of %d lettre(s) :\n"
, data[i].len);
                        else printf("word of 1 lettre :\n");
                }
                printf("%s\n", data[i].st);
        }
        for (i=0; i<data.size(); i++) free(data[i].st);
        data.clear();
        return 0;
}
但是公司验证后给的意见是,第一,不要给变量buffer设定上限值;第二,不要使用STL方
法,即sort函数之类的;第三,整个程序不完全正确。
下周一就要啊。。。。c++两年没碰了,再短时间捡起来真是困难啊,谁知道公司就要用这个考核,现在很急啊。希望各
位高手帮助一下。谢谢。
搜索更多相关主题的帖子: word 字母表 英文单词 现实 
2011-07-02 18:58
快速回复:公司要求周一前交c++程序,我的程序不符合要求但是改进实在不会请求帮 ...
数据加载中...
 
   



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

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