所以,我对QT相当陌生,我主要用Java和Python进行编码,而这是用C++进行的。我想知道如何在这个对话框中传递一个约200个结构数组,而不需要setFunctions,并使用所述QDialog的实例从我的MainWindow中调用它们。这是因为我的结构中有很多数据(大约50个字符串(,复制它听起来效率很低,也很麻烦。我也不知道我是否应该把它做成一个指向structs的指针数组,如果可以的话。这是我的代码:
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tenseselectiondialog.h"
#include <QMessageBox>
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
setWindowTitle("Japanese Conjugator 101");
tenseSelection = new TenseSelectionDialog();
}
MainWindow::~MainWindow(){
delete ui;
delete tenseSelection;
delete conjugateDialog;
}
void MainWindow::on_actionQuit_triggered(){
QApplication::quit();
}
void MainWindow::on_actionAbout_Qt_triggered(){
QString about = "Hey! I am the creator of this program. I want to make the process of learning japanese faster for everybody so here is a program that uses spaced repition of japanese verb conjugation to improve your conjugating skills!";
QMessageBox::information(this, "About Me", about);
}
void MainWindow::on_actionAbout_triggered(){
QApplication::aboutQt();
}
void MainWindow::on_startButton_clicked(){
MainWindow::hide();
this->tenseSelection->setModal(true);
this->tenseSelection->exec();
MainWindow::show();
}
void MainWindow::on_actionContact_Me_triggered()
{
QString contact = "If you notice any errors/bugs with the program itself or conjugations, please notify me at: tomasnew1@hotmail.com, and I will send out a patch ASAP!";
QMessageBox::information(this, "Contact Me", contact);
}
MainWindow* MainWindow::get() {
return this;
}
时态选择对话框.cpp
#include "tenseselectiondialog.h"
#include "ui_tenseselectiondialog.h"
#include "conjugatedialog.h"
TenseSelectionDialog::TenseSelectionDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::TenseSelectionDialog)
{
ui->setupUi(this);
setWindowTitle("Japanese Conjugator 101");
conjugateDialog = new ConjugateDialog();
}
TenseSelectionDialog::~TenseSelectionDialog()
{
delete ui;
}
void TenseSelectionDialog::on_continueButton_clicked()
{
this->conjugateDialog->setModal(true);
this->conjugateDialog->exec();
}
structs.h
#ifndef STRUCTS_H
#define STRUCTS_H
#include <string>
#include <string.h>
#include <algorithm>
#include <cctype>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include <array>
using namespace std;
const string sampleFile = "Japanese verbs";
const string sampleExtension = ".txt";
const string samplePath = "";
const int VERBS = 500;
const int totalTenses = 14;
struct tenseType{
string currentTense;
string plainAffirmative;
string plainNegative;
string formalAffirmative;
string formalNegative;
};
struct verbType{
string verbStem;
string verbInfinitive;
string dictionaryFormRomaji;
string dictionaryFormHiragana;
string dictionaryFormKanji;
string verbDefinition;
int verbGroup;
string verbEnding;
tenseType presentTense;
tenseType volitionalTense;
tenseType imperativeTense;
tenseType TaiTense;
tenseType TaiPastTense;
tenseType pastTense;
tenseType pastPresumptiveTense;
tenseType presentProgressiveTense;
tenseType pastProgressiveTense;
tenseType conditionalTaraTense;
tenseType conditionalEbaTense;
tenseType potentialTense;
tenseType causativeTense;
tenseType passiveTense;
};
void init(verbType verbArray[VERBS]);
void verbInit(verbType verbArray[VERBS], int &count, int flag, string fileName, string fileExtension = sampleExtension, string filePath = samplePath);
string removeSpaces(string str);
void printVerbs(verbType verbArray[VERBS], int count);
#endif // STRUCTS_H
函数.cpp
包括";structs.h">
void verbInit(verbType verbArray[VERBS], int &count, int flag, string fileName, string fileExtension, string filePath){
string fileLine;
string secondLine;
string thirdLine;
string fourthLine;
string fifthLine;
string sixthLine;
string seventhLine;
ifstream myFile (filePath + fileName + fileExtension);
int currentIndex = 0;
int passingInt = 0;
if (myFile.is_open()){
while (getline(myFile,fileLine)){
verbType verb;
verbArray[currentIndex] = verb;
verbArray[currentIndex].verbStem = removeSpaces(fileLine);
getline(myFile,secondLine);
verbArray[currentIndex].verbInfinitive = removeSpaces(secondLine);
getline(myFile,thirdLine);
verbArray[currentIndex].dictionaryFormRomaji = removeSpaces(thirdLine);
getline(myFile,fourthLine);
verbArray[currentIndex].dictionaryFormHiragana = fourthLine;
if (verbArray[currentIndex].dictionaryFormRomaji == "motsu" || verbArray[currentIndex].dictionaryFormRomaji == "matsu"){
verbArray[currentIndex].verbEnding = "tsu";
}else if (verbArray[currentIndex].dictionaryFormRomaji == "arau" || verbArray[currentIndex].dictionaryFormRomaji == "tsukau" ||
verbArray[currentIndex].dictionaryFormRomaji == "omou" || verbArray[currentIndex].dictionaryFormRomaji == "utau" ||
verbArray[currentIndex].dictionaryFormRomaji == "iu" || verbArray[currentIndex].dictionaryFormRomaji == "morau" ||
verbArray[currentIndex].dictionaryFormRomaji == "harau" || verbArray[currentIndex].dictionaryFormRomaji == "kau" ||
verbArray[currentIndex].dictionaryFormRomaji == "au" || verbArray[currentIndex].dictionaryFormRomaji == "narau"||
verbArray[currentIndex].dictionaryFormRomaji == "warau" || verbArray[currentIndex].dictionaryFormRomaji == "suu" ||
verbArray[currentIndex].dictionaryFormRomaji == "chigau"){
verbArray[currentIndex].verbEnding = "u";
}else {
verbArray[currentIndex].verbEnding = verbArray[currentIndex].dictionaryFormRomaji.substr(verbArray[currentIndex].dictionaryFormRomaji.length()-2);
}
if (flag == 1) {
getline(myFile,fifthLine);
verbArray[currentIndex].dictionaryFormKanji = fifthLine;
}
getline(myFile,sixthLine);
verbArray[currentIndex].verbDefinition = "to " + sixthLine;
getline(myFile,seventhLine);
passingInt = stoi(seventhLine);
verbArray[currentIndex].verbGroup = passingInt;
currentIndex++;
count++;
}
}
for (int i = 0; i < count; i++){
if (verbArray[i].verbGroup == 1){
verbArray[i].presentTense.formalAffirmative = verbArray[i].verbInfinitive + "masu";
verbArray[i].presentTense.formalNegative = verbArray[i].verbInfinitive + "masen";
verbArray[i].presentTense.plainAffirmative = verbArray[i].dictionaryFormRomaji;
verbArray[i].presentTense.plainNegative = verbArray[i].verbStem + "anai";
verbArray[i].pastTense.formalAffirmative = verbArray[i].verbInfinitive + "mashita";
verbArray[i].pastTense.formalNegative = verbArray[i].verbInfinitive + "masendeshita";
if (verbArray[i].verbEnding == "u" || verbArray[i].verbEnding == "tsu" || verbArray[i].verbEnding == "ru"){
verbArray[i].pastTense.plainAffirmative = verbArray[i].verbStem.substr(0, verbArray[i].verbStem.length()-1) + "tta";
}else if (verbArray[i].verbEnding == "bu" || verbArray[i].verbEnding == "mu" || verbArray[i].verbEnding == "nu"){
verbArray[i].pastTense.plainAffirmative = verbArray[i].verbStem.substr(0, verbArray[i].verbStem.length()-1) + "nda";
}else if (verbArray[i].verbEnding == "ku"){
verbArray[i].pastTense.plainAffirmative = verbArray[i].verbStem.substr(0, verbArray[i].verbStem.length()-1) + "ita";
}else if (verbArray[i].verbEnding == "gu"){
verbArray[i].pastTense.plainAffirmative = verbArray[i].verbStem.substr(0, verbArray[i].verbStem.length()-1) + "ida";
}else if (verbArray[i].verbEnding == "su"){
verbArray[i].pastTense.plainAffirmative = verbArray[i].verbStem.substr(0, verbArray[i].verbStem.length()-1) + "shita";
}
verbArray[i].pastTense.plainNegative = verbArray[i].verbStem + "anakatta";
verbArray[i].volitionalTense.formalAffirmative = verbArray[i].verbInfinitive + "mashou";
verbArray[i].volitionalTense.formalNegative = verbArray[i].presentTense.plainNegative + "deshou";
verbArray[i].volitionalTense.plainAffirmative = verbArray[i].verbStem + "ou";
verbArray[i].volitionalTense.plainNegative = verbArray[i].presentTense.plainNegative + "darou";
verbArray[i].TaiTense.formalAffirmative = verbArray[i].verbInfinitive + "tai";
verbArray[i].TaiTense.formalNegative = verbArray[i].verbInfinitive + "takunai";
verbArray[i].TaiTense.plainAffirmative = verbArray[i].verbInfinitive + "tai";
verbArray[i].TaiTense.plainNegative = verbArray[i].verbInfinitive + "takunai";
verbArray[i].TaiPastTense.formalAffirmative = verbArray[i].verbInfinitive + "takatta";
verbArray[i].TaiPastTense.formalNegative = verbArray[i].verbInfinitive + "takunakatta";
verbArray[i].TaiPastTense.plainAffirmative = verbArray[i].verbInfinitive + "takatta";
verbArray[i].TaiPastTense.plainNegative = verbArray[i].verbInfinitive + "takunakatta";
if (verbArray[i].verbEnding == "u" || verbArray[i].verbEnding == "tsu" || verbArray[i].verbEnding == "ru"){
verbArray[i].imperativeTense.formalAffirmative = verbArray[i].verbStem.substr(0, verbArray[i].verbStem.length()-1) + "tte";
}else if (verbArray[i].verbEnding == "bu" || verbArray[i].verbEnding == "mu" || verbArray[i].verbEnding == "nu"){
verbArray[i].imperativeTense.formalAffirmative = verbArray[i].verbStem.substr(0, verbArray[i].verbStem.length()-1) + "nde";
}else if (verbArray[i].verbEnding == "ku"){
verbArray[i].imperativeTense.formalAffirmative = verbArray[i].verbStem.substr(0, verbArray[i].verbStem.length()-1) + "ite";
}else if (verbArray[i].verbEnding == "gu"){
verbArray[i].imperativeTense.formalAffirmative = verbArray[i].verbStem.substr(0, verbArray[i].verbStem.length()-1) + "ide";
}else if (verbArray[i].verbEnding == "su"){
verbArray[i].imperativeTense.formalAffirmative = verbArray[i].verbStem.substr(0, verbArray[i].verbStem.length()-1) + "shite";
}
verbArray[i].imperativeTense.formalNegative = verbArray[i].verbStem + "anai de kudasai";
verbArray[i].imperativeTense.plainAffirmative = verbArray[i].verbStem + "e";
verbArray[i].imperativeTense.plainAffirmative = verbArray[i].dictionaryFormRomaji + "na";
verbArray[i].potentialTense.formalAffirmative = verbArray[i].verbStem + "emasu";
verbArray[i].potentialTense.formalNegative = verbArray[i].verbStem + "emasen";
verbArray[i].potentialTense.plainAffirmative = verbArray[i].verbStem + "eru";
verbArray[i].potentialTense.plainNegative= verbArray[i].verbStem + "enai";
verbArray[i].presentProgressiveTense.formalAffirmative = verbArray[i].imperativeTense.formalAffirmative + "imasu";
verbArray[i].presentProgressiveTense.plainAffirmative = verbArray[i].imperativeTense.formalAffirmative + "iru";
verbArray[i].presentProgressiveTense.formalNegative = verbArray[i].imperativeTense.formalAffirmative + "imasen";
verbArray[i].presentProgressiveTense.plainNegative = "N/A";
verbArray[i].pastProgressiveTense.formalAffirmative = verbArray[i].imperativeTense.formalAffirmative + "imashita";
verbArray[i].pastProgressiveTense.plainAffirmative = verbArray[i].imperativeTense.formalAffirmative + "ita";
verbArray[i].pastProgressiveTense.formalNegative = verbArray[i].imperativeTense.formalAffirmative + "imasendeshita";
verbArray[i].pastProgressiveTense.plainNegative = "N/A";
verbArray[i].pastPresumptiveTense.formalAffirmative = verbArray[i].pastTense.plainAffirmative + "deshou";
verbArray[i].pastPresumptiveTense.plainAffirmative = verbArray[i].pastTense.plainAffirmative + "rou";
verbArray[i].pastPresumptiveTense.formalNegative = verbArray[i].pastTense.plainNegative + "deshou";
verbArray[i].pastPresumptiveTense.plainNegative = verbArray[i].pastTense.plainNegative + "darou";
verbArray[i].conditionalEbaTense.plainAffirmative = verbArray[i].verbStem + "eba";
verbArray[i].conditionalEbaTense.plainNegative = verbArray[i].verbStem + "anakereba";
verbArray[i].conditionalEbaTense.formalAffirmative = "N/A";
verbArray[i].conditionalEbaTense.formalNegative = "N/A";
verbArray[i].conditionalTaraTense.formalAffirmative = verbArray[i].verbStem + "imashitara";
verbArray[i].conditionalTaraTense.plainAffirmative = verbArray[i].pastTense.plainAffirmative + "ra";
verbArray[i].conditionalTaraTense.formalNegative = verbArray[i].verbStem + "imasendeshitara";
verbArray[i].conditionalTaraTense.formalNegative = verbArray[i].verbStem + "anakattara";
verbArray[i].causativeTense.formalAffirmative = verbArray[i].verbStem + "asemasu";
verbArray[i].causativeTense.formalNegative = verbArray[i].verbStem + "asemasen";
verbArray[i].causativeTense.plainAffirmative = verbArray[i].verbStem + "aseru";
verbArray[i].causativeTense.plainAffirmative = verbArray[i].verbStem + "asenai";
verbArray[i].passiveTense.formalAffirmative = verbArray[i].verbStem + "aremasu";
verbArray[i].passiveTense.formalNegative = verbArray[i].verbStem + "aremasen";
verbArray[i].passiveTense.plainAffirmative = verbArray[i].verbStem + "areru";
verbArray[i].passiveTense.plainNegative = verbArray[i].verbStem + "arenai";
}else if (verbArray[i].verbGroup == 2){
verbArray[i].presentTense.formalAffirmative = verbArray[i].verbInfinitive + "masu";
verbArray[i].presentTense.formalNegative = verbArray[i].verbInfinitive + "masen";
verbArray[i].presentTense.plainAffirmative = verbArray[i].dictionaryFormRomaji;
verbArray[i].presentTense.plainNegative = verbArray[i].verbStem + "nai";
verbArray[i].pastTense.formalAffirmative = verbArray[i].verbInfinitive + "mashita";
verbArray[i].pastTense.formalNegative = verbArray[i].verbInfinitive + "masendeshita";
verbArray[i].pastTense.plainAffirmative = verbArray[i].verbStem + "ta";
verbArray[i].pastTense.plainNegative = verbArray[i].verbStem + "nakatta";
verbArray[i].volitionalTense.formalAffirmative = verbArray[i].verbInfinitive + "mashou";
verbArray[i].volitionalTense.formalNegative = verbArray[i].presentTense.plainNegative + "deshou";
verbArray[i].volitionalTense.plainAffirmative = verbArray[i].dictionaryFormRomaji + "you";
verbArray[i].volitionalTense.plainNegative = verbArray[i].presentTense.plainNegative + "darou";
verbArray[i].TaiTense.formalAffirmative = verbArray[i].verbStem + "tai";
verbArray[i].TaiTense.formalNegative = verbArray[i].verbStem + "takunai";
verbArray[i].TaiTense.plainAffirmative = verbArray[i].verbStem + "tai";
verbArray[i].TaiTense.plainNegative = verbArray[i].verbStem + "takunai";
verbArray[i].TaiPastTense.formalAffirmative = verbArray[i].verbStem + "takatta";
verbArray[i].TaiPastTense.formalNegative = verbArray[i].verbStem + "takunakatta";
verbArray[i].TaiPastTense.plainAffirmative = verbArray[i].verbStem + "takatta";
verbArray[i].TaiPastTense.plainNegative = verbArray[i].verbStem + "takunakatta";
verbArray[i].imperativeTense.formalAffirmative = verbArray[i].verbStem + "te kudasai";
verbArray[i].imperativeTense.formalNegative = verbArray[i].verbStem + "nai de kudasai";
verbArray[i].imperativeTense.plainAffirmative = verbArray[i].verbStem + "ro";
verbArray[i].imperativeTense.plainAffirmative = verbArray[i].dictionaryFormRomaji + "na";
verbArray[i].potentialTense.formalAffirmative = verbArray[i].verbStem + "raremasu";
verbArray[i].potentialTense.formalNegative = verbArray[i].verbStem + "raremasen";
verbArray[i].potentialTense.plainAffirmative = verbArray[i].verbStem + "rareru";
verbArray[i].potentialTense.plainNegative= verbArray[i].verbStem + "rarenai";
verbArray[i].presentProgressiveTense.formalAffirmative = verbArray[i].imperativeTense.formalAffirmative + "imasu";
verbArray[i].presentProgressiveTense.plainAffirmative = verbArray[i].imperativeTense.formalAffirmative + "iru";
verbArray[i].presentProgressiveTense.formalNegative = verbArray[i].imperativeTense.formalAffirmative + "imasen";
verbArray[i].presentProgressiveTense.plainNegative = "N/A";
verbArray[i].pastProgressiveTense.formalAffirmative = verbArray[i].imperativeTense.formalAffirmative + "imashita";
verbArray[i].pastProgressiveTense.plainAffirmative = verbArray[i].imperativeTense.formalAffirmative + "ita";
verbArray[i].pastProgressiveTense.formalNegative = verbArray[i].imperativeTense.formalAffirmative + "imasendeshita";
verbArray[i].pastProgressiveTense.plainNegative = "N/A";
verbArray[i].pastPresumptiveTense.formalAffirmative = verbArray[i].pastTense.plainAffirmative + "deshou";
verbArray[i].pastPresumptiveTense.plainAffirmative = verbArray[i].pastTense.plainAffirmative + "rou";
verbArray[i].pastPresumptiveTense.formalNegative = verbArray[i].pastTense.plainNegative + "deshou";
verbArray[i].pastPresumptiveTense.plainNegative = verbArray[i].pastTense.plainNegative + "darou";
verbArray[i].conditionalEbaTense.plainAffirmative = verbArray[i].verbStem + "reba";
verbArray[i].conditionalEbaTense.plainNegative = verbArray[i].verbStem + "nakereba";
verbArray[i].conditionalEbaTense.formalAffirmative = "N/A";
verbArray[i].conditionalEbaTense.formalNegative = "N/A";
verbArray[i].conditionalTaraTense.formalAffirmative = verbArray[i].verbStem + "mashitara";
verbArray[i].conditionalTaraTense.plainAffirmative = verbArray[i].verbStem + "tara";
verbArray[i].conditionalTaraTense.formalNegative = verbArray[i].verbStem + "masendeshitara";
verbArray[i].conditionalTaraTense.formalNegative = verbArray[i].verbStem + "nakattara";
verbArray[i].causativeTense.formalAffirmative = verbArray[i].verbStem + "sasemasu";
verbArray[i].causativeTense.formalNegative = verbArray[i].verbStem + "sasemasen";
verbArray[i].causativeTense.plainAffirmative = verbArray[i].verbStem + "saseru";
verbArray[i].causativeTense.plainAffirmative = verbArray[i].verbStem + "sasenai";
verbArray[i].passiveTense.formalAffirmative = verbArray[i].potentialTense.formalAffirmative;
verbArray[i].passiveTense.formalNegative = verbArray[i].potentialTense.formalNegative;
verbArray[i].passiveTense.plainAffirmative = verbArray[i].potentialTense.plainAffirmative;
verbArray[i].passiveTense.plainNegative = verbArray[i].potentialTense.plainNegative;
}else {
verbArray[i].presentTense.formalAffirmative = verbArray[i].verbInfinitive + "masu";
verbArray[i].presentTense.formalNegative = verbArray[i].verbInfinitive + "masen";
verbArray[i].presentTense.plainAffirmative = verbArray[i].dictionaryFormRomaji;
verbArray[i].presentTense.plainNegative = verbArray[i].verbStem + "wanai";
verbArray[i].pastTense.formalAffirmative = verbArray[i].verbInfinitive + "mashita";
verbArray[i].pastTense.formalNegative = verbArray[i].verbInfinitive + "masendeshita";
verbArray[i].pastTense.plainAffirmative = verbArray[i].verbStem + "tta";
verbArray[i].pastTense.plainNegative = verbArray[i].verbStem + "wanakatta";
verbArray[i].volitionalTense.formalAffirmative = verbArray[i].verbInfinitive + "mashou";
verbArray[i].volitionalTense.formalNegative = verbArray[i].presentTense.plainNegative + "deshou";
verbArray[i].volitionalTense.plainAffirmative = verbArray[i].verbStem + "ou";
verbArray[i].volitionalTense.plainNegative = verbArray[i].presentTense.plainNegative + "darou";
verbArray[i].TaiTense.formalAffirmative = verbArray[i].verbInfinitive + "tai";
verbArray[i].TaiTense.formalNegative = verbArray[i].verbInfinitive + "takunai";
verbArray[i].TaiTense.plainAffirmative = verbArray[i].verbInfinitive + "tai";
verbArray[i].TaiTense.plainNegative = verbArray[i].verbInfinitive + "takunai";
verbArray[i].TaiPastTense.formalAffirmative = verbArray[i].verbInfinitive + "takatta";
verbArray[i].TaiPastTense.formalNegative = verbArray[i].verbInfinitive + "takunakatta";
verbArray[i].TaiPastTense.plainAffirmative = verbArray[i].verbInfinitive + "takatta";
verbArray[i].TaiPastTense.plainNegative = verbArray[i].verbInfinitive + "takunakatta";
verbArray[i].imperativeTense.formalAffirmative = verbArray[i].verbStem.substr(0, verbArray[i].verbStem.length()-1) + "tte";
verbArray[i].imperativeTense.formalNegative = verbArray[i].verbStem + "wanai de kudasai";
verbArray[i].imperativeTense.plainAffirmative = verbArray[i].verbStem + "e";
verbArray[i].imperativeTense.plainAffirmative = verbArray[i].dictionaryFormRomaji + "na";
verbArray[i].potentialTense.formalAffirmative = verbArray[i].verbStem + "emasu";
verbArray[i].potentialTense.formalNegative = verbArray[i].verbStem + "emasen";
verbArray[i].potentialTense.plainAffirmative = verbArray[i].verbStem + "eru";
verbArray[i].potentialTense.plainNegative= verbArray[i].verbStem + "enai";
verbArray[i].presentProgressiveTense.formalAffirmative = verbArray[i].imperativeTense.formalAffirmative + "imasu";
verbArray[i].presentProgressiveTense.plainAffirmative = verbArray[i].imperativeTense.formalAffirmative + "iru";
verbArray[i].presentProgressiveTense.formalNegative = verbArray[i].imperativeTense.formalAffirmative + "imasen";
verbArray[i].presentProgressiveTense.plainNegative = "N/A";
verbArray[i].pastProgressiveTense.formalAffirmative = verbArray[i].imperativeTense.formalAffirmative + "imashita";
verbArray[i].pastProgressiveTense.plainAffirmative = verbArray[i].imperativeTense.formalAffirmative + "ita";
verbArray[i].pastProgressiveTense.formalNegative = verbArray[i].imperativeTense.formalAffirmative + "imasendeshita";
verbArray[i].pastProgressiveTense.plainNegative = "N/A";
verbArray[i].pastPresumptiveTense.formalAffirmative = verbArray[i].pastTense.plainAffirmative + "deshou";
verbArray[i].pastPresumptiveTense.plainAffirmative = verbArray[i].pastTense.plainAffirmative + "rou";
verbArray[i].pastPresumptiveTense.formalNegative = verbArray[i].pastTense.plainNegative + "deshou";
verbArray[i].pastPresumptiveTense.plainNegative = verbArray[i].pastTense.plainNegative + "darou";
verbArray[i].conditionalEbaTense.plainAffirmative = verbArray[i].verbStem + "eba";
verbArray[i].conditionalEbaTense.plainNegative = verbArray[i].verbStem + "wanakereba";
verbArray[i].conditionalEbaTense.formalAffirmative = "N/A";
verbArray[i].conditionalEbaTense.formalNegative = "N/A";
verbArray[i].conditionalTaraTense.formalAffirmative = verbArray[i].verbStem + "imashitara";
verbArray[i].conditionalTaraTense.plainAffirmative = verbArray[i].pastTense.plainAffirmative + "ra";
verbArray[i].conditionalTaraTense.formalNegative = verbArray[i].verbStem + "imasendeshitara";
verbArray[i].conditionalTaraTense.formalNegative = verbArray[i].verbStem + "wanakattara";
verbArray[i].causativeTense.formalAffirmative = verbArray[i].verbStem + "wasemasu";
verbArray[i].causativeTense.formalNegative = verbArray[i].verbStem + "wasemasen";
verbArray[i].causativeTense.plainAffirmative = verbArray[i].verbStem + "waseru";
verbArray[i].causativeTense.plainAffirmative = verbArray[i].verbStem + "wasenai";
verbArray[i].passiveTense.formalAffirmative = verbArray[i].verbStem + "waremasu";
verbArray[i].passiveTense.formalNegative = verbArray[i].verbStem + "waremasen";
verbArray[i].passiveTense.plainAffirmative = verbArray[i].verbStem + "wareru";
verbArray[i].passiveTense.plainNegative = verbArray[i].verbStem + "warenai";
}
}
}
void printVerbs(verbType verbArray[VERBS], int count){
for (int i = 0; i < count; i++){
cout << "Verb Ending: " << setw(20) << verbArray[i].verbEnding << endl;
cout << "Verb Form Pres " << setw(20) << verbArray[i].presentTense.formalAffirmative << setw(30) << verbArray[i].presentTense.formalNegative << endl;
cout << "Verb Plain Pres " << setw(20) << verbArray[i].presentTense.plainAffirmative << setw(30) << verbArray[i].presentTense.plainNegative << endl;
cout << "Verb Form Past " << setw(20) << verbArray[i].pastTense.formalAffirmative << setw(30) << verbArray[i].pastTense.formalNegative << endl;
cout << "Verb Plain Past " << setw(20) << verbArray[i].pastTense.plainAffirmative << setw(30) << verbArray[i].pastTense.plainNegative << endl;
}
}
string removeSpaces(string str) {
string placer = "";
for (int i = 0; i < str.length(); i++){
if (!isspace(str[i])){
placer += str[i];
}
}
return placer;
}
我没有包括我的.h文件,因为我没有足够的行,只知道我的数组是私人成员。谢谢你的帮助!
1a。高效传递数组
我的结构中有很多数据(大约50个字符串(,复制它听起来效率很低,也很麻烦。
void printVerbs(verbType verbArray[VERBS], int count);
首先,开始使用像std::vector
或QVector
这样的C++容器,而不是原始的C数组。C++容器类更易于管理和调试。
然后,您可以通过const-reference廉价地传递数组:
void printVerbs(const QVector<verbType> &verbArray);
注意:您不需要通过count
!向量知道它包含多少元素
这实现了两件事:
- 引用部分确保在函数调用过程中不会复制数据,因为函数引用了已经存在的数据
- const部分确保函数不会意外修改现有数据
1b。复制阵列而不复制
QVector
是隐式共享的(也称为"写时复制"(:https://doc.qt.io/qt-5/implicit-sharing.html这意味着您可以将QVector<verbType>
的副本从MainWindow
传递到TenseSelectionDialog
,甚至可以将副本作为成员变量存储在TenseSelectionDialog
中。只要两个副本都未修改,两个向量将在内部共享相同的数据。
或者,如果您保证MainWindow
的寿命始终比TenseSelectionDialog
长,那么您可以让TenseSelectionDialog
存储指向MainWindow
的成员变量的指针或引用。
2.使用-(指向结构的指针(的数组
如果可以的话,我也不知道是否应该让它成为指向结构的指针数组。
如果:,则使用指针数组最有用
-
您的数组将经常被修改或复制
- 插入或删除元素会导致数组内容在内存中移动
- 移动/复制指针比大型结构更便宜
-
你的阵列将是巨大的
- 数组中的数据存储在连续内存块中,由
N * sizeof<ArrayElement>
给出,其中N
是元素数 - 如果你的内存太分散,你的电脑可能没有足够大的连续RAM块来存储数据
- 对于大型结构,存储指针可以减少所需的连续内存量
- 数组中的数据存储在连续内存块中,由
如果这两个标准不适用,那么使用指针数组的好处就更少了(提示:约500个元素很小(
如果要使用指针数组,请使用std::shared_ptr
而不是原始指针,这样就不需要显式管理内存。
3.(其他(字符串编程
如果您愿意在核心逻辑中使用QString
,那么您的字符串操作代码可以大大简化。
示例:
string removeSpaces(string str) { string placer = ""; for (int i = 0; i < str.length(); i++){ if (!isspace(str[i])){ placer += str[i]; } } return placer; }
。。。可以被取代
QString removeSpaces(QString str) {
str.remove( QRegularExpression("s") );
return str;
}
或者,如果只想删除字符串末尾的空白,则可以直接调用QString::trimmed()
。
QString
也支持unicode(与std::string
不同(,因此您可以使用它以独立于平台的方式存储平假名、片假名和汉字。