C++读取txt文件并统计数字范围个数
txt文件内容:有一万多个整型数字,每个字一行,如图2-记事本.运行结果如下
文章图片
文章图片
【C++读取txt文件并统计数字范围个数】代码如下:
// temp_project.cpp: 定义控制台应用程序的入口点。
///*
#include "stdafx.h"int main()
{
printf("------------------");
return 0;
}*///#include "stdafx.h"//我的vs2012编译器版本低,所以直接将此句注释掉了,仍可以运行出来
#include
#include
#include
#include
#include
#define hh printf("\n===================================\n");
using std::string;
using namespace std;
int main()
{
int count0_2_10 = 0;
int count11_2_20 = 0;
int count21_2_30 = 0;
int count31_2_40 = 0;
int count41_2_infinite = 0;
string s = "D:\\2.txt";
ifstream infile;
infile.open(s.data());
assert(infile.is_open());
string a;
int temp = 0;
while (getline(infile,a))
{
// cout << a << endl;
for ( int i = 0;
i < a.length();
i++)
{
temp *= 10;
temp = temp+a.at(i) - 0x30;
}
if (temp>=0&&temp<=10)
{
count0_2_10++;
}
if (temp >=11 && temp <= 20)
{
count11_2_20++;
}
if (temp >= 21 && temp <= 30)
{
count21_2_30++;
}
if (temp >= 31 && temp <= 40)
{
count31_2_40++;
}
if (temp >= 41)
{
count41_2_infinite++;
}
temp = 0;
}
printf("0-10: %d\n", count0_2_10);
printf("11-20: %d\n", count11_2_20);
printf("21-30: %d\n", count21_2_30);
printf("31-40: %d\n", count31_2_40);
printf(">41: %d\n", count41_2_infinite);
infile.close();
system("pause");
return 1;
}
推荐阅读
- opencv|opencv C++模板匹配的简单实现
- C语言学习|第十一届蓝桥杯省赛 大学B组 C/C++ 第一场
- c++基础概念笔记
- ExcelPackage读取写入
- springboot中.yml文件的值无法读取的问题及解决
- 牛逼!C++开发的穿越丛林真人游戏,游戏未上线就有百万人气
- C++Primer之|C++Primer之 函数探幽
- c/c++|有感 Visual Studio 2015 RTM 简介 - 八年后回归 Dot Net,终于迎来了 Mvc 时代,盼走了 Web 窗体时代...
- QML基础信息
- Mac下怎么新建|Mac下怎么新建 .txt 文本文件