本帖最后由 yuxijian2020 于 2021-4-25 17:17 编辑
不知道你用的是c还是c++
c语言:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_LEN 500
typedef struct _Pair
{
double key;
double value;
} Pair;
const Pair errorPair = { -0.1, -0.1 };
//分割字符串
Pair StringSplit(char* str, char sep)
{
int pos = -1;
for (int i = 0; i < strlen(str); i++)
{
if (sep == str[i])
{
pos = i;
break;
}
}
if (pos == -1)
return errorPair;
int headLen = pos + 1;
int tailLen = strlen(str) - pos - 1;
char* front = (char*)malloc(headLen + 1);
char* back = (char*)malloc(tailLen + 1);
if (!front || !back)
return errorPair;
memset(front, 0, headLen + 1);
memset(back, 0, tailLen + 1);
memcpy(front, str, headLen);
front[headLen] = '\0';
memcpy(back, str + headLen, tailLen);
back[tailLen] = '\0';
Pair result = { 0.0, 0.0 };
result.key = atof(front);
result.value = atof(back);
free(front);
free(back);
return result;
}
//读取文件内容,函数返回值需要free
char* ReadFile(const char* path)
{
FILE* file;
fopen_s(&file, path, "r");
if (!file)
return NULL;
fseek(file, 0, SEEK_END);
int length = ftell(file);
fseek(file, 0, SEEK_SET);
char* content = (char*)malloc(length);
if (!content)
return NULL;
fread(content, sizeof(char), length, file);
return content;
}
//返回一个申请空间的Pair指针,此函数调用后,需要free返回值
Pair* StringToPair(const char* str)
{
Pair* result = (Pair*)malloc(MAX_LEN * sizeof(Pair));
if (!result)
return NULL;
memset(result, 0, MAX_LEN * sizeof(Pair));
int start = 0;
int cur = 0;
int size = strlen(str);
for (int i = 0; i < size; i++)
{
if (str[i] == '\n')
{
char* temp = (char*)malloc(i - start + 1);
if (!temp)
{
free(result);
return NULL;
}
memset(temp, 0, i - start + 1);
memcpy(temp, str + start, i - start);
temp[i - start] = '\0';
Pair p = StringSplit(temp, '\t');
result[cur].key = p.key;
result[cur].value = p.value;
cur++;
start = i + 1;
free(temp);
}
}
return result;
}
double FindLike(Pair* pairs, double tar)
{
double min = 1000000.0;
double cur = 0.0;
Pair like = { 0.0, 0.0 };
for (int i = 0; i < MAX_LEN; i++)
{
cur = fabs(pairs[i].key - tar);
if (cur < min)
{
like.key = pairs[i].key;
like.value = pairs[i].value;
min = cur;
}
}
return like.value;
}
int main()
{
char path[] = "data.txt";
char* content = ReadFile(path);
Pair* pairs = StringToPair(content);
free(content);
double in = 0.0;
printf_s("请输入一个小数:");
scanf_s("%lf", &in);
double result = FindLike(pairs, in);
free(pairs);
printf_s("%.1lf\n", result);
return 0;
}
c++:#include<iostream>
#include <fstream>
#include<string>
#include <vector>
using namespace std;
//按sep分割字符串,并转化为2个double类型组成的pair
pair<double, double> StringSplit(const string& str, const string& sep)
{
size_t pos = str.find(sep);
if (pos == string::npos)
return pair<double, double>();
string front;
string back;
front.assign(str, 0, pos);
back.assign(str, pos + sep.size(), str.size() - pos - sep.size());
double first = atof(front.c_str());
double second = atof(back.c_str());
return make_pair(first, second);
}
//读取文件并将文件内容转为 2个double类型组成的pair 的数组
//数组元素为double 和 double 组成的pair
vector<pair<double, double>> ReadFileToVector(const string& path)
{
fstream file(path, ios::in);
vector<pair<double, double>> dataPairs;
string line;
while (getline(file, line))
dataPairs.emplace_back(StringSplit(line, "\t"));
return dataPairs;
}
//获取用户输入,并输出
void GetInputAndPrint()
{
//这里ReadFileToVector方法的参数是文件路径
vector<pair<double, double>> data(ReadFileToVector("data.txt"));
double in;
printf_s("请输入一个小数:");
cin >> in;
pair<double, double> like;
double min = 1000000.0;
double cur = 0.0;
//遍历数组找到最接近的值
for (const auto& p : data)
{
cur = fabs(p.first - in);
if (cur < min)
{
like = p;
min = cur;
}
}
printf_s("%.1lf\n", like.second);
}
int main()
{
GetInputAndPrint();
return 0;
}
|