输入1英里后,我的程序无法正确输出
#include <stdio.h>
int main(void)
{
int kilometers;
int miles;
printf("\n");
printf("Please enter the distance in miles: ");
scanf("%d", &miles);
printf("\n");
kilometers = 1.52 * miles;
printf("%d miles is equal to ", miles);
printf("%d kilometers", kilometers);
printf("\n\n");
return 0;
}
当我将1英里行驶时,它应该输出1.52公里,但给我1公里
答案 0 :(得分:3)
如评论中所述,您使用的是#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QVBoxLayout>
#include <QPushButton>
#include <QTableWidget>
#include <QHeaderView>
#include <QLineEdit>
class MainWindow : public QMainWindow {
Q_OBJECT
QWidget anotherWidget;
QVBoxLayout vLayout;
QTableWidget table;
QLineEdit lineEdit;
QPushButton bt{"from the QTableWidgetItem to the QLineEdit"};
int row = 0;
protected slots:
void toLineEdit();
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
};
#endif // MAINWINDOW_H
而不是#include "mainwindow.h"
struct Item {
QString portnumber;
QString pwmport;
QString dutycycle;
QString frequency;
};
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
setCentralWidget(&lineEdit);
setWindowTitle("Main UI");
anotherWidget.setLayout(&vLayout);
vLayout.addWidget(&table);
vLayout.addWidget(&bt);
anotherWidget.setWindowTitle("Another UI");
anotherWidget.show();
connect(&bt, &QPushButton::clicked, this, &MainWindow::toLineEdit);
int rows = 5;
QList<Item> container;
for (int row = 0; row < rows; row++) {
Item item;
item.portnumber = QString::number(row);
container.append(item);
}
QStringList labels{"portnumber", "pwmport", "dutycycle", "frequency"};
table.setRowCount(rows);
table.setColumnCount(labels.count());
table.setHorizontalHeaderLabels(labels);
table.verticalHeader()->hide();
for(int i = 0; i < rows; i++) {
table.setItem(i, 0, new QTableWidgetItem(container[i].portnumber));
table.setItem(i, 1, new QTableWidgetItem(container[i].pwmport));
table.setItem(i, 2, new QTableWidgetItem(container[i].dutycycle));
table.setItem(i, 3, new QTableWidgetItem(container[i].frequency));
}
}
MainWindow::~MainWindow() {}
void MainWindow::toLineEdit(){
QString text = table.item(row++, 0)->text();
lineEdit.setText(text);
}
或int
,因此您将无法获得所需的精度。同样,您将英里换算为公里是不正确的;每float
英里就有double
公里。
1.60934