64,282
社区成员
发帖
与我相关
我的任务
分享
T: 8
p: 30.00 22.00 42.00 14.00 15.00 12.00 12.00 40.00
c: 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00
h: 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00
s: 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00
d: 9.00 12.00 9.00 25.00 9.00 20.00 20.00 25.00
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
int main(int argc, char** argv) {
std::vector<std::vector<double>*> result;
{
std::fstream input("./input.txt", std::fstream::in);
std::string line;
while (std::getline(input, line)) {
std::vector<double>* row = new std::vector<double>();
std::istringstream cutter(line);
std::string token;
cutter >> token;
while (cutter >> token) {
row->push_back(strtod(token.c_str(), NULL));
}
result.push_back(row);
}
}
for (size_t r = 0; r < result.size(); ++r) {
std::vector<double>* row = result[r];
for (size_t c = 0; c < row->size(); ++c) {
std::cout << row->at(c) << " ";
}
std::cout << std::endl;
delete row;
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
double data[6][32]; //假设一行最多32个浮点数
int data_len[6];//记录每个数组中的实际数据的个数
memset(data, 0, sizeof(data));
memset(data_len, 0, sizeof(data_len));
FILE* fp = fopen("D:\\data.txt", "r");
for(int i=0; i<6; i++)
{
char buf[1024] = {0};
fgets(buf, 1023, fp);
char* p = strtok(buf, " \t\r\n");
int index = 0;
do
{
if (p[0]<'0' || p[0]>'9')
continue; //跳过前面的T:之类的字符
data[i][data_len[i]++] = atof(p);
}while(p=strtok(NULL, " \t\r\n"));
}
fclose(fp);
return(0);
}
#include <cstdlib>
#include <vector>
using std::vector;
#include <iostream>
using std::cerr; using std::cout; using std::endl;
#include <string>
using std::string;
#include <fstream>
using std::ifstream; using std::ofstream;
int main()
{
//打开文件
ifstream fin;
fin.open("test.txt");
if (fin.fail())
{
cerr << "打开文件失败!";
exit(-1);
}
//文件操作
string str;
vector<double> arrInner;
vector<vector<double>> arr; //动态二维数组
auto iter = arr.begin();
char ch;
while (true)
{
ch = fin.get(); //读取下一个字符
if (ch <= 57 && ch >= 46) //如果这个字符是数字或者小数点
str.push_back(ch); //就把这个字符放到str中
else if (ch == ' ' && str.size() > 0)
{
double temp = atof(str.c_str()); //将字符串转换为double
str.clear(); //清空字符串
arrInner.push_back(temp);
}
else if (ch == '\n' && str.size() > 0)
{
double temp = atof(str.c_str());
str.clear();
arrInner.push_back(temp);
arr.push_back(arrInner);
arrInner.clear(); //遇到换行符了就清空临时数组
}
else if (ch == EOF)
{
if (str.size() > 0) //如果到了文件结尾还有数字没有被push进去
{
double temp = atof(str.c_str());
str.clear();
arrInner.push_back(temp);
arr.push_back(arrInner);
arrInner.clear();
}
break;
}
}
//输出
for (auto iter = arr.begin(); iter != arr.end(); ++iter)
{
for (auto iterInner = iter->begin(); iterInner != iter->end(); ++iterInner)
{
cout << *iterInner << " ";
}
cout << endl;
}
return 0;
}