![]() |
#2
apull2023-08-01 08:50
|
目前,爆栈还是内存炸了不知道(大哭),而且用Dev C++写的,不会开C++11,具体也不会调了
希望各位大佬看看我的程序,提出一点点改进建议

蒟蒻的码风很难看,请见谅

// Author:PanDaoxi
#include <bits/stdc++.h>
#include <dirent.h>
using namespace std;
#define endl "\n"
#define ll long long
const int INF = 101;
int k;
bool useStdIO, errors;
string iFile, oFile, problemPath, errType, title, conStr, inp, dataFile[INF];
void getData(string path){
DIR* d = opendir(path.c_str());
if(!d){
errors = true;
errType = "Could not reach the folder";
return;
}
dirent* e;
while((e = readdir(d))){
dataFile[++k] = e -> d_name;
if(dataFile[k] == "." || dataFile[k] == "..") k--;
}
closedir(d);
return;
}
string getInput(string path){
FILE* cur = fopen(path.c_str(), "r");
if(!cur){
errors = true;
return errType = "Could not read from the INPUT file";
}
fseek(cur, 0, SEEK_END);
size_t sz = ftell(cur);
char* tmp = new char[sz];
rewind(cur);
fread(tmp, sizeof(char), sz, cur);
fclose(cur);
return string(tmp);
}
void checkErr(){
if(errors){
cerr << errType;
exit(0);
}
return;
}
int main(){
// 请用户填写如下题目信息
useStdIO = true; // 是否使用标准输入输出(true=使用标准输出,false=使用文件输出)
title = "T1"; // 当前题目名称
iFile = title + ".in"; // 题目要求的输入数据文件(仅当 useStdIO 为真时有效)
oFile = title + ".out"; // 题目要求的输出数据文件(仅当 useStdIO 为真时有效)
// 请用户不要操作以下内容
problemPath = "../../data/" + title + "/";
if(useStdIO){
oFile = "con";
while(getline(cin, conStr)) inp += conStr + "\n";
}
else{
freopen(iFile.c_str(), "r", stdin);
inp = getInput(iFile);
}
freopen(oFile.c_str(), "w", stdout);
checkErr();
getData(problemPath);
checkErr();
for(int i=1; i<=k; i++){
if(dataFile[i].find("in") != dataFile[i].npos) continue;
else if(getInput(problemPath + dataFile[i]) == inp){
string ans = getInput(problemPath + dataFile[i].substr(0, dataFile[i].find(".")) + oFile.substr(oFile.find(".")));
checkErr();
cout << ans;
fclose(stdout);
return 0;
}
}
errors = true;
errType = "Could not find the answer";
checkErr();
fclose(stdout);
return 0;
}