How can I Change this C++ code into MATLAB code?
3 次查看(过去 30 天)
显示 更早的评论
#pragma once
#include <string>
using namespace std;
#define numDecades 13
void ReadData(string fileName, int decadeCounts[], float decadeLanes[], float decadeSpanLength[], float decadeLength[]);
void ComputeData(int decadeCounts[], float decadeLanes[], float decadeSpanLength[], float decadeLength[]);
void PrintData(string fileName, int decadeCounts[], float decadeLanes[], float decadeSpanLength[], float decadeLength[]);
#include <fstream>
#include <iostream>
#include "BridgeFunctions.h"
int ComputeIndex(int decade)
{
int index;
if (decade < 1900) {
index = 0;
}
else {
index = (decade - 1900) / 10 + 1;
}
return(index);
}
void ReadData(string fileName, int decadeCounts[], float decadeLanes[], float decadeSpanLength[], float decadeLength[])
{
ifstream dataFile(fileName);
if (!dataFile.is_open()) {
cout << "File not opened" << endl;
return;
}
// dataFile.ignore();
string s;
getline(dataFile, s);
while (!dataFile.eof()) {
int decade, index = 0, lanes = 0;
float spanLength = 0.0, length = 0.0;
dataFile >> decade >> lanes >> spanLength >> length;
index = ComputeIndex(decade);
decadeCounts[index]++;
decadeLanes[index] += lanes;
decadeSpanLength[index] += spanLength;
decadeLength[index] += length;
}
dataFile.close();
}
void ComputeData(int decadeCounts[], float decadeLanes[], float decadeSpanLength[], float decadeLength[])
{
for (int i = 0; i < numDecades; i++) {
if (decadeCounts[i] > 0) {
decadeLanes[i] /= decadeCounts[i];
decadeSpanLength[i] /= decadeCounts[i];
decadeLength[i] /= decadeCounts[i];
}
}
}
string GetDecade(int i)
{
switch (i)
{
case 0: return("18xx");
case 1: return("190x");
case 2: return("191x");
case 3: return("192x");
case 4: return("193x");
case 5: return("194x");
case 6: return("195x");
case 7: return("196x");
case 8: return("197x");
case 9: return("198x");
case 10: return("190x");
case 11: return("200x");
case 12: return("201x");
default: return("error");
}
}
void PrintData(string fileName, int decadeCounts[], float decadeLanes[], float decadeSpanLength[], float decadeLength[])
{
ofstream dataFile(fileName);
dataFile << "decade, # bridges built, avg # of lanes, avg max span length, avg bridge length" << endl;
for (int i = 0; i < numDecades; i++) {
dataFile << GetDecade(i) << ", " << decadeCounts[i] << ", " << decadeLanes[i] << ", " << decadeSpanLength[i] << ", " << decadeLength[i] << endl;
}
dataFile.close();
#include "BridgeFunctions.h"
int main()
{
int counts[numDecades] = { 0 };
float lanes[numDecades] = { 0.0 };
float spanLength[numDecades] = { 0.0 };
float length[numDecades] = { 0.0 };
ReadData("bridge data.prn", counts, lanes, spanLength, length);
ComputeData(counts, lanes, spanLength, length);
PrintData("bridge statistics.csv", counts, lanes, spanLength, length);
}
0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!