
C++计算文件中单词的个数
//wenjian.cpp:定义控制台应用程序的入口点。#include"stdafx.h"#include<string>#include<iostream>#incl...
// wenjian.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i,word=0;
int num=0;
char c;
vector<char> v;
ifstream in("wenjian.cpp");
for (i = 0; (c = v[i]) != '\0'; i++)
{
if (c == ' ')
word = 0;
else
if (word == 0)
{
word = 1;
num++;
}
}
cout<<"\nThere are %d words int the line.\n"<< num<<endl;
return 0;
}
调试可以通过,一运行及出错 展开
#include "stdafx.h"
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i,word=0;
int num=0;
char c;
vector<char> v;
ifstream in("wenjian.cpp");
for (i = 0; (c = v[i]) != '\0'; i++)
{
if (c == ' ')
word = 0;
else
if (word == 0)
{
word = 1;
num++;
}
}
cout<<"\nThere are %d words int the line.\n"<< num<<endl;
return 0;
}
调试可以通过,一运行及出错 展开
3个回答
展开全部
char c;
vector<char> v;
ifstream in("wenjian.cpp");
for (i = 0; (c = v[i]) != '\0'; i++)//你这里v[i]的值都没初始化,怎么可能不错啊?你得先把文件的内容读入这个vector
__________________________________________________
按照你的思路给你改了
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
#define _TCHAR char
using namespace std;
int main(int argc, _TCHAR* argv[])
{
int i,word=0;
int num=0;
char c;
vector<char> v;
ifstream in("wenjian.cpp");
while((c=in.get())!=EOF) //这里将文件内容读入容器
v.push_back(c);
v.push_back('\0'); //最后得加上'\0'标识
for (i = 0; (c = v[i]) != '\0'; i++)
{
if (c == ' '||c=='\n') //这里增加回车
word = 0;
else
if (word == 0)
{
word = 1;
num++;
}
}
cout<<"\nThere are "<<num<<" words int the line.\n"<<endl;
return 0;
}
vector<char> v;
ifstream in("wenjian.cpp");
for (i = 0; (c = v[i]) != '\0'; i++)//你这里v[i]的值都没初始化,怎么可能不错啊?你得先把文件的内容读入这个vector
__________________________________________________
按照你的思路给你改了
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
#define _TCHAR char
using namespace std;
int main(int argc, _TCHAR* argv[])
{
int i,word=0;
int num=0;
char c;
vector<char> v;
ifstream in("wenjian.cpp");
while((c=in.get())!=EOF) //这里将文件内容读入容器
v.push_back(c);
v.push_back('\0'); //最后得加上'\0'标识
for (i = 0; (c = v[i]) != '\0'; i++)
{
if (c == ' '||c=='\n') //这里增加回车
word = 0;
else
if (word == 0)
{
word = 1;
num++;
}
}
cout<<"\nThere are "<<num<<" words int the line.\n"<<endl;
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?

2024-10-22 广告
百事牛是共享提供商,我们提供可靠有效的服务,适当合理的授权费有利于的继续更新优化。同样的事情,同样的方法,百事牛团队十年磨一剑,始终至聚焦在密码恢复领域,深耕于此,我们已研制出有别于其他公司的算法和运算模式, 百事牛的暴力模式加入了分布式点...
点击进入详情页
本回答由百事牛提供
展开全部
你对向量v都没初始化啊
#include<stdafx.h>
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
int i,word=0;
int num=0;
char c;
vector<char> v;
ifstream in("wenjian.cpp");
char ch[100]={0};
in.read(ch,100);
for(int t=0;t<strlen(ch)-1;t++)
{
v.push_back(ch[t]);
}
for (i = 0; (c = v[i]) != '\0'; i++)
{
if (c == ' '||c=='\n')
word = 0;
else
if (word == 0)
{
word = 1;
num++;
}
}
cout<<"There are "<< num<<" words the line"<<endl;
return 0;
}
#include<stdafx.h>
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
int i,word=0;
int num=0;
char c;
vector<char> v;
ifstream in("wenjian.cpp");
char ch[100]={0};
in.read(ch,100);
for(int t=0;t<strlen(ch)-1;t++)
{
v.push_back(ch[t]);
}
for (i = 0; (c = v[i]) != '\0'; i++)
{
if (c == ' '||c=='\n')
word = 0;
else
if (word == 0)
{
word = 1;
num++;
}
}
cout<<"There are "<< num<<" words the line"<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<ctype.h>
#define STOP '|'
int main(void)
{
char c;
char prev;
long n_chars = 0L;
int n_lines = 0;
int n_words = 0;
int p_lines = 0;
int inword = 0;
printf("Enter text to be analyzed( | to be quit):\n ");
prev = '\n';
while((c = getchar()) != STOP)
{
n_chars++;
if (c == '\n')
n_lines++;
if (!isspace(c) && !inword)
{
inword = 1;
n_words++;
}
if(isspace (c) && inword)
inword = 0;
prev = c;
}
if(prev != '\n')
p_lines = 1;
printf("characters = %ld,word = %d,line = %d,",
n_chars,n_words,n_lines);
printf("partial lines = %d\n",p_lines);
return 0;
}
prev 是一个字符变量,用于检查是否到了一行的末尾。 这个例子不是很难,用#define是为了增加程序的可修改性,你可以只看关键的方法部分也就是如何计算单词数和行数。这种符合标准的程序应该多看看,有好处,像这些C语言算法都不算什么,为的就是学习编程的标准和语法,根本涉及不到算法呢,呵呵。
isspace (c)检查参数c是否为空格字符,也就是判断是否为空格('')、定位字符
('\t')、CR('\r')、换行('\n')、垂直定位字符('\v')或翻页('\f')的情况。
返回值
#include<ctype.h>
#define STOP '|'
int main(void)
{
char c;
char prev;
long n_chars = 0L;
int n_lines = 0;
int n_words = 0;
int p_lines = 0;
int inword = 0;
printf("Enter text to be analyzed( | to be quit):\n ");
prev = '\n';
while((c = getchar()) != STOP)
{
n_chars++;
if (c == '\n')
n_lines++;
if (!isspace(c) && !inword)
{
inword = 1;
n_words++;
}
if(isspace (c) && inword)
inword = 0;
prev = c;
}
if(prev != '\n')
p_lines = 1;
printf("characters = %ld,word = %d,line = %d,",
n_chars,n_words,n_lines);
printf("partial lines = %d\n",p_lines);
return 0;
}
prev 是一个字符变量,用于检查是否到了一行的末尾。 这个例子不是很难,用#define是为了增加程序的可修改性,你可以只看关键的方法部分也就是如何计算单词数和行数。这种符合标准的程序应该多看看,有好处,像这些C语言算法都不算什么,为的就是学习编程的标准和语法,根本涉及不到算法呢,呵呵。
isspace (c)检查参数c是否为空格字符,也就是判断是否为空格('')、定位字符
('\t')、CR('\r')、换行('\n')、垂直定位字符('\v')或翻页('\f')的情况。
返回值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询