我完全坚持这个任务.我首先在int main()中写了所有内容,没有任何问题.这一切都很可爱!不幸的是,我们的教练希望它分成多个功能(每个功能少于35行).我已将其分解,如下所示,但不幸的是,我的知识(和谷歌没有太多帮助)的功能和传递/参考通过它们并不是那么高.我的程序现在根本不起作用.所有的"书籍"都会出错,所以我不确定我是否正在不正确地传递结构或数组.请帮忙!
原始的txt文件如下:
number of books
title
author
price
title
author
price
Run Code Online (Sandbox Code Playgroud)
码:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void setStruct() {
struct bookTable {
string title;
string author;
double price;
};
}
void setArray(int &arraySize, struct bookTable *Book[]) {
ifstream infile;
int bookCounter = 0;
infile.open("books2.txt");
if (!infile) {
cout << "Unable to open Books.txt" << endl;
}
infile >> arraySize;
infile.ignore(100, '\n');
bookTable *Book = new bookTable[arraySize];
infile.close();
}
void readFile(struct bookTable *Book[]) …Run Code Online (Sandbox Code Playgroud) 试图将这个 for 循环从 C++ 转换为 Rust,我很难弄清楚,因为我对 Rust 语法很陌生。
double sinError = 0;
for (float x = -10 * M_PI; x < 10 * M_PI; x += M_PI / 300) {
double approxResult = sin_approx(x);
double libmResult = sinf(x);
sinError = MAX(sinError, fabs(approxResult - libmResult));
}
Run Code Online (Sandbox Code Playgroud)