[英]C++ User interaction in classes
在第一學期的 CS 中,當我進行程序編程時,講師總是告訴不要將程序功能和與用戶的交互混合在相同的功能中。 現在我有面向 object 的編程,我很困惑如何在課堂上實現與用戶的交互。
在這里,我開始編寫一個讓用戶解決數學方程的項目,我寫了一個像這樣的 class 'Task'。 在面向 object 的編程中將輸入/輸出與代碼的 rest 混合是一種不好的做法嗎?
#include <iostream>
#include <ctime>
#include <tgmath.h>
using namespace std;
enum operation{
add, sub, mult, divis, random
};
class Task{
private:
int min;
int max;
public:
Task(int mi, int ma){
min = mi;
max = ma;
}
void setIntervall(int mi, int ma){
min = mi;
max = ma;
}
int getRand(){
return (rand() % ((max-min) + 1)) + min;
int min = max / 10;
return (rand() % (max - min)) + min;
}
void printTask(operation op){
if(op == random)
op = (operation)(rand() % 4);
if(op == add)
cout << getRand() << " + " << getRand() << endl;
if(op == sub)
cout << getRand() << " - " << getRand() << endl;
if(op == mult)
cout << getRand() << " * " << getRand() << endl;
if(op == divis)
cout << getRand() << " / " << getRand() << endl;
}
bool checkifRight(double result);
};
int main(){
srand(time(NULL));
Task Task(15,100);
return 0;
}
cout << something
不完全是“與用戶的交互”,它只是通知用戶某事,所以我認為這沒關系。 由於您的代碼中沒有任何輸入,因此到目前為止一切正常。
但是連續使用多個 if 語句來檢查正在使用哪個操作是沒有意義的,而是使用else if
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.