鱼C论坛

 找回密码
 立即注册
查看: 2360|回复: 3

[已解决]请各位帮忙检查这段C++作业代码。

[复制链接]
发表于 2021-3-23 13:15:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 肚子饿了233 于 2021-3-23 13:20 编辑

#include<iostream>
#include<string>
void input(struct WORD words[], int len);
void order(struct WORD words[], int len);
using namespace std;
struct WORD
{
        string word;
        int count = 1;
};
int main()
{
        struct WORD words[10];
        int len = sizeof(words) / sizeof(words[0]);
        input(words, len);
        order(words, len);
}
void input(struct WORD words[], int len)
{
        int i;
        cout << "输入几个英语单词:";
        for (i = 0; i < len; i++)
        {
                cin >> words[i].word;
                if (words[i].word == "0") break;
        }
}
void order(struct WORD words[], int len)
{
        int i, j;
        string temp;
        for (i = 0; i < len; i++)
        {
                for (j = i+1; j < len; j++)
                {
                        if (words[i].word.compare(words[j].word) > 0)
                        {
                                temp = words[i].word;
                                words[i].word = words[j].word;
                                words[j].word = temp;
                        }
                        else if (words[i].word.compare(words[j].word) == 0)
                                words[i].count++;
                }
        }
        for (i = 0; i < len; i++)
        {
                cout << words[i].word << " : " << words[i].count << endl;
        }
}

编写一程序(应该有多个函数),允许从键盘输入任意多个英语单词(单词可以重复),中间用空格分开,输入0表示输入结束。该程序可以统计同一个英语单词被输入几次,最后对英文单词按字典顺序输出,后面跟上该单词被输入的次数。(提示,尝试用结构体组织数据,把单词和该单出现的次数用一个结构体来描述。


编译结果:输入几个英语单词:can
ban
app
app
0
: 6
: 5
: 4
: 3
: 2
0 : 2
app : 2
app : 1    //这一行也想删掉,怎么修改?
ban : 1
can : 1


   :6,  :5.......这些是什么玩意儿?!怎么删掉?
请大佬们指出我的错误吧!救救菜鸟5555


最佳答案
2021-3-25 16:47:04
一言难尽,我相当于重写了,你参考下吧...
  1. #include<iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>

  5. using namespace std;

  6. struct WORD
  7. {
  8.     string word;
  9.     int count = 1;

  10.     bool operator()(const WORD& a, const WORD& b)
  11.     {
  12.         return a.word < b.word;
  13.     }
  14. };

  15. void input(vector<WORD>& words)
  16. {
  17.     char c;
  18.     string temp;
  19.     WORD tWord;
  20.     cout << "输入几个英语单词:";
  21.     //从这里开始就不对,不要用cin....
  22.     //题目上说单词中间是空格隔开
  23.     //不是回车区分单词
  24.     while (c = getchar())
  25.     {
  26.         if ((c >= 'A' && c <= 'Z') || c >= 'a' && c <= 'z')
  27.             temp.append(1, c);
  28.         else if (c == ' ' || c == '0')
  29.         {
  30.             tWord.word.assign(temp);
  31.             words.push_back(tWord);
  32.             temp = "";
  33.             if (c == '0')
  34.                 break;
  35.         }
  36.     }
  37. }

  38. void order(vector<WORD>& words)
  39. {
  40.     //去重
  41.     for (int i = 0; i < words.size() - 1; ++i)
  42.     {
  43.         for (int j = i + 1; j < words.size(); ++j)
  44.         {
  45.             if (words[i].word == words[j].word)
  46.             {
  47.                 words[i].count += 1;
  48.                 words.erase(words.begin() + j);
  49.                 j -= 1;
  50.             }
  51.         }
  52.     }

  53.     //排序
  54.     sort(words.begin(), words.begin() + words.size(), WORD());
  55. }

  56. void VectorPrint(const vector<WORD>& words)
  57. {
  58.     for (const auto& item : words)
  59.     {
  60.         printf_s("%s : %d\n", item.word.c_str(), item.count);
  61.     }
  62. }

  63. int main()
  64. {
  65.     vector<WORD> words;
  66.     input(words);
  67.     order(words);
  68.     VectorPrint(words);

  69.     return 0;
  70. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-23 21:33:31 | 显示全部楼层
别沉呀,上去
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-25 16:47:04 | 显示全部楼层    本楼为最佳答案   
一言难尽,我相当于重写了,你参考下吧...
  1. #include<iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>

  5. using namespace std;

  6. struct WORD
  7. {
  8.     string word;
  9.     int count = 1;

  10.     bool operator()(const WORD& a, const WORD& b)
  11.     {
  12.         return a.word < b.word;
  13.     }
  14. };

  15. void input(vector<WORD>& words)
  16. {
  17.     char c;
  18.     string temp;
  19.     WORD tWord;
  20.     cout << "输入几个英语单词:";
  21.     //从这里开始就不对,不要用cin....
  22.     //题目上说单词中间是空格隔开
  23.     //不是回车区分单词
  24.     while (c = getchar())
  25.     {
  26.         if ((c >= 'A' && c <= 'Z') || c >= 'a' && c <= 'z')
  27.             temp.append(1, c);
  28.         else if (c == ' ' || c == '0')
  29.         {
  30.             tWord.word.assign(temp);
  31.             words.push_back(tWord);
  32.             temp = "";
  33.             if (c == '0')
  34.                 break;
  35.         }
  36.     }
  37. }

  38. void order(vector<WORD>& words)
  39. {
  40.     //去重
  41.     for (int i = 0; i < words.size() - 1; ++i)
  42.     {
  43.         for (int j = i + 1; j < words.size(); ++j)
  44.         {
  45.             if (words[i].word == words[j].word)
  46.             {
  47.                 words[i].count += 1;
  48.                 words.erase(words.begin() + j);
  49.                 j -= 1;
  50.             }
  51.         }
  52.     }

  53.     //排序
  54.     sort(words.begin(), words.begin() + words.size(), WORD());
  55. }

  56. void VectorPrint(const vector<WORD>& words)
  57. {
  58.     for (const auto& item : words)
  59.     {
  60.         printf_s("%s : %d\n", item.word.c_str(), item.count);
  61.     }
  62. }

  63. int main()
  64. {
  65.     vector<WORD> words;
  66.     input(words);
  67.     order(words);
  68.     VectorPrint(words);

  69.     return 0;
  70. }
复制代码

调试输出

调试输出
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-17 22:33:17 | 显示全部楼层
yuxijian2020 发表于 2021-3-25 16:47
一言难尽,我相当于重写了,你参考下吧...

谢谢你,但我还是C++小白,还没学vector容器,没太看懂,但还是谢谢你啦~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-15 05:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表