C++文件读取和字符串转数字

大多数文章只强调了如何读取文件,但未说明如何将读取的字符串转换成需要的数据(int等类型)。这里提供相应的转换代码,适用于需要从文件中读取大量数据的情形。比如蓝桥杯的测试用例(需要自己写IO)。同时也提供了测试代码,和测试用例。

 9719 7515 5916 6467 7157 9614 8560 9075 2099 2838 1403 7652 6238 1699 8907 1804 5384 7942 7546 1978
  8785 1944 8108 6040 2010 6646 2750 5410 4516 8757 5624 9257 9030 9290 6833 4646 9749 5304 5633 1573
  8525 8244 8514 7474 7896 9731 8402 9036 1869 2688 2085 1667 7753 8466 4911 3812 8585 8319 4020 7350
  1949 9120 4424 4057 8277 4511 6333 1533 7624 8932 1053 8682 9284 4134 1466 3607 8753 5310 3728 4163
  9420 9185 7055 2342 4143 4499 2036 5374 7026 8638 8866 8364 1706 8767 1601 8309 5695 8179 4142 8489
  5876 5660 4658 8307 2582 7544 8793 8207 3979 1692 1400 1893 4500 6389 7198 4836 4761 6603 2859 1312
  6367 4174 9956 6668 6771 4795 6492 3937 7096 8041 8644 9379 8071 8667 5810 5794 8147 3823 7877 4822
  4809 3297 8518 4972 9754 6854 3271 7891 8882 1052 3197 6035 5628 7674 7931 8085 8970 7733 4745 8785
  7536 1511 6964 4763 5409 7032 8963 8576 3411 5853 3316 1267 7851 2735 6953 2970 1810 6830 5576 6903
  2241 1575 2379 4679 9519 9290 4802 1562 3509 8365 6777 5143 5610 1061 7880 1935 5793 7023 5629 9571
  2480 5937 4612 8890 1964 8532 3309 9737 8507 1849 8544 1500 9282 6288 2137 4730 4239 3473 4643 6377
  7341 2881 3430 5815 1972 6629 3817 4547 7561 4779 6578 6114 4972 5505 7515 1800 4784 2272 4502 7541
  7665 8607 2022 8192 2605 1346 4155 8725 8167 7022 6136 3615 6057 6329 8671 2033 3151 2249 5981 6412
  9046 3353 8650 6965 4179 1248 5659 5219 8083 5615 3821 4436 9217 7356 3914 5717 3734 3765 4435 7210
  8951 5013 2951 7401 2329 5686 6530 9581 6539 6881 8634 2663 2916 3019 8529 5645 8201 9270 1939 7275
  6429 1531 6322 9586 2793 7968 4001 9665 7624 4369 6245 5146 9567 6801 6064 6199 3210 6753 2586 7795
  5771 8507 7973 1470 1475 6896 6781 6572 8412 8557 8255 5268 8960 7251 9214 2489 6920 9917 3810 4605
  9116 7950 3715 1697 4703 2868 8673 3106 2579 1074 3992 3547 4279 3149 3396 6081 6221 1125 9358 2471
  8360 1526 4116 9278 6325 5175 5533 4107 7522 7599 7711 9211 1752 2431 8321 3844 3579 1047 3987 8487
  7600 2401 8748 8945 2078 1519 4614 4576 5706 4040 9358 1928 1327 6699 5258 2846 3418 8310 1249 3866
  7796 8668 4087 4258 8992 8996 4617 5997 2527 8204 8927 1456 9340 2088 1605 2299 9878 8347 7789 2122
  8372 1102 4243 4208 1651 7861 4947 7802 4704 6204 4455 6012 8494 9060 3747 2786 2136 1830 7424 8309
  6919 4420 2031 5399 2652 7219 4048 7013 5094 5276 4225 5976 4157 6722 8765 4679 1604 4986 5033 2623
  4015 2297 3067 6261 6623 4577 4589 4747 6659 7667 7853 4040 6393 9606 7219 9334 1316 3430 9963 5187
  4998 3735 9884 2990 1374 8436 6674 3018 5714 9352 8708 8789 7879 2965 1444 4671 4743 9817 6066 8057
  6996 9609 2884 4601 7287 3432 4145 8858 6857 8624 4531 6579 1615 2894 4521 3274 5237 1093 3317 9289
  7117 1850 3210 8010 2512 1394 4718 9332 5593 4118 4995 3994 5063 9426 1709 5128 4997 9287 1907 9068
  4258 7328 6490 2603 5333 5093 8070 2116 8489 1994 7098 7409 1463 4268 9509 2358 1192 2460 5031 6292
  4911 1192 1012 2494 5276 8981 3540 3306 8869 6678 7879 7526 8847 6270 7653 3109 6955 9760 8520 8673
  6328 7277 7818 3285 9398 4929 4639 1617 4023 1051 9320 4955 6580 6481 3824 9611 2863 6492 6281 6203
#include <iostream>
#include <vector>
#include <string>
#include <cmath> 
#include <fstream>
#include <sstream>
using namespace::std;

//函数声明 
int sum_num(vector<vector<int>> & num, int x, int y); //矩阵对5x5矩阵求和
void read(vector<vector<int>> & num);					//控制台读取文件 
int transform(string& str); 							//改数字字符串为数字 

 

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int N = 30;
	int M = 20;
	int res = 0,x ;
	int maxval = 0;
	vector<vector<int>> record(N, vector<int>(M,0)); 
	//变量构建 
	read(record); //数据读取 
	
	
	for(int i = 0; i < N; i++){
		for(int j = 0; j < M; j++){
			maxval = std::max(sum_num(record,i,j), maxval);
		}
	}
	cout << maxval;
	
	return 0;
}

//函数定义 
int sum_num(vector<vector<int>> & num, int x, int y){
	int N = 30;
	int M = 20;
	int res = 0;
	if(x + 5 < N && y + 5 < M){
		for(int i = 0; i < 5; i++){
			for(int j = 0; j < 5; j++){
				res += num[x+i][y+j];
			}
		}
	}
	return res;
	
}

//文件读写函数 
void read(vector<vector<int>> & num){
		int	N = num.size();
		int M = num[0].size();
		int x;
		string s;
	 fstream file;
	 file.open("nums.txt",ios::in); //打开文件
	 if(!file.is_open()){
	 	cout << "文件打开失败!" <<endl; 
	 } 
	 //按行读取数据 
	 for(int i = 0; i < N; i++){
	 	getline(file, s);
	 	stringstream ss(s); //用s初始化string流,进行字符串分割
	 	string temp; 
	 	int j = 0;
		while(ss >> temp){
			num[i][j] = transform(temp); //处理结果 
			j++; 						 
		}  
	 /*	for(int j = 0; j < M; j++){
	 		file >> s;
	 		cout << s << endl; //打印数据 
	 		num[i][j] = x;
	 	}*/
	 }
	 
	 file.close(); //关闭文件 
} 

int transform(string& str){
	int n = str.size(); //字符的长度
	int res = 0;
	for(int i = 0; i < n; i++){
		res += (str[i] - '0') * pow(10,n - i - 1);
	} 
	 
	return res; 
}

注意文件名改为“nums.txt”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值