大家帮忙看看怎么回事?我的程序主要是想看一个文本里各个字母出现的次数。我自己感觉程序没有什么问题,但是运行一半就不能运行了。这是怎么回事。

qq_35811698 2016-10-24 02:49:44

这个是代码
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ifstream fin("primary data.txt");
ofstream fout("frequency of protain.txt");

string str;
unsigned int num1 = 0, num2 = 0,num3 = 0, num4 = 0, num5 = 0, num6 = 0, num7 = 0, num8 = 0, num9 = 0, num10 = 0, num11 = 0, num12 = 0, num13 = 0, num14 = 0, num15 = 0, num16 = 0, num17 = 0, num18 = 0, num19 = 0, num20 = 0;
while (getline(fin, str))
{
if (str[0] != '>')
{
for(int i = 0;i < 200;i++)
{
if (str[i] == 'A')
num1++;
else if (str[i] == 'R')
num2++;
else if (str[i] == 'N')
num3++;
else if (str[i] == 'D')
num4++;
else if (str[i] == 'C')
num5++;
else if (str[i] == 'Q')
num6++;
else if (str[i] == 'E')
num7++;
else if (str[i] == 'G')
num8++;
else if (str[i] == 'H')
num9++;
else if (str[i] == 'I')
num10++;
else if (str[i] == 'L')
num11++;
else if (str[i] == 'K')
num12++;
else if (str[i] == 'M')
num13++;
else if (str[i] == 'F')
num14++;
else if (str[i] == 'P')
num15++;
else if (str[i] == 'S')
num16++;
else if (str[i] == 'T')
num17++;
else if (str[i] == 'W')
num18++;
else if (str[i] == 'Y')
num19++;
else if (str[i] == 'U')
num20++;
}
}
}
cout << num2 << endl;
fout << "R : " << num2 << endl;
fout << "N : " << num3 << endl;
fout << "D : " << num4 << endl;
fout << "C : " << num5 << endl;
fout << "Q : " << num6 << endl;
fout << "E : " << num7 << endl;
fout << "G : " << num8 << endl;
fout << "H : " << num9 << endl;
fout << "I : " << num10 << endl;
fout << "L : " << num11 << endl;
fout << "K : " << num12 << endl;
fout << "M : " << num13 << endl;
fout << "F : " << num14 << endl;
fout << "P : " << num15 << endl;
fout << "S : " << num16 << endl;
fout << "T : " << num17 << endl;
fout << "W : " << num18 << endl;
fout << "Y : " << num19 << endl;
fout << "V : " << num20 << endl;
return 0;
}
大家帮忙看看。谢谢啦!
...全文
221 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongwenjun 2016-10-25
  • 打赏
  • 举报
回复
引用 6 楼 gz_qmc 的回复:
看到这么高深的C++,实在看不懂 哥只会老老实实的C ...
你是C 的写法, 最好能写成 #include <stdio.h> int main() 给写成如下,就是个小工具了
#include <stdio.h>

int main(int argc, char* argv[])
{
    unsigned char ch;
    int  num[256] = {0};

    const char* fileName = argv[1];
    FILE* fp = fopen(fileName, "rb");
    if (NULL == fp) {
        printf("\n文件不存在,转入屏幕输入测试,请按Ctrl+Z结束输入\n");
        fp = stdin;
    }
    while (1) {
        ch = fgetc(fp);
        if (feof(fp))
            break;
        num[ch]++;
    }
    fclose(fp);

    for (ch = 33; ch < 127; ch++) {
        if (num[ch] > 0)
            printf("字符 %c : % 4d个\n", ch, num[ch]);
    }

    return 0;
}

gz_qmc 2016-10-24
  • 打赏
  • 举报
回复
看到这么高深的C++,实在看不懂 哥只会老老实实的C
#include "stdio.h"

void main()
{
	unsigned char ch;
	int  num[256]={0};

	FILE *fp=fopen("C:\\xxx.txt","rb");
	if(NULL==fp)
	{
		printf("\n文件不存在\n");
		return;
	}
	while(1)
	{
	   ch=fgetc(fp);
	   if(feof(fp)) break;
	   num[ch]++;
	}
	fclose(fp);

	for(ch=33;ch<127;ch++)
	{
		if(num[ch]>0) printf("字符 %c : % 4d个\n",ch,num[ch]);
	}
}
hongwenjun 2016-10-24
  • 打赏
  • 举报
回复
    std::map<char, int> mymap;
    string  str = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGl\n"
                  "zIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2\n"
                  "YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29ud\n"
                  "GludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRz\n"
                  "IHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\n";

    for (auto it = str.begin() ; it != str.end(); it++)
        ++mymap[*it];

    for (auto i = 'A'; i != 'Z' + 1; ++i)
        cout << i << "-->" << mymap[i] << endl;
hongwenjun 2016-10-24
  • 打赏
  • 举报
回复
#include <iostream>    // 数据流输入/输出
#include <map>

using namespace std;

int main(int argc, char* argv[])
{
    std::map<char, int> mymap;

    string  str = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGl\n"
                  "zIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2\n"
                  "YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29ud\n"
                  "GludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRz\n"
                  "IHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\n";

    for (auto it = str.begin() ; it != str.end(); it++)
        ++mymap[*it];

    for (auto it = mymap.begin(); it != mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';


    return 0;
}
issac_gang 2016-10-24
  • 打赏
  • 举报
回复
这个#include<string.h>
小灸舞 2016-10-24
  • 打赏
  • 举报
回复
你这个很明显是string的越界访问,下标越界了。
你的for循环固定循环200次,你确定你的string读到的一行数据是大于等于200个字符的?
ztenv 版主 2016-10-24
  • 打赏
  • 举报
回复
越界访问string了。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧