यहां हम देखेंगे कि C++ में सिस्टम कॉल का उपयोग करके टाइमर कैसे डिजाइन करें। हम किसी ग्राफिक्स या एनिमेशन का उपयोग नहीं करेंगे। यहां टाइमर का मतलब स्टॉपवॉच है, जो समय को अप-काउंट करता है। उपयोग किए गए सिस्टम कॉल हैं -
sleep(n) - यह प्रोग्राम को n सेकंड के लिए स्लीप करने में मदद करेगा
system() - इसका उपयोग सिस्टम कमांड को निष्पादित करने के लिए किया जाता है जिसमें कमांड को इस फंक्शन के तर्क के रूप में पास किया जाता है।
उदाहरण
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int hrs = 0;
int mins = 0;
int sec = 0;
void showClk() {
system("cls");
cout << setfill(' ') << setw(55) << " TIMER \n";
cout << setfill(' ') << setw(66) << " --------------------------------------\n";
cout << setfill(' ') << setw(29);
cout << "| " << setfill('0') << setw(2) << hrs << " Hours | ";
cout << setfill('0') << setw(2) << mins << " Minutes | ";
cout << setfill('0') << setw(2) << sec << " Seconds |" << endl;
cout << setfill(' ') << setw(66) << " --------------------------------------\n";
}
void systemCallTimer() {
while (true) {
showClk();
sleep(1);
sec++;
if (sec == 60) {
mins++;
if (mins == 60) {
hrs++;
mins = 0;
}
sec = 0;
}
}
}
int main() {
systemCallTimer();
}
आउटपुट
