LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2011, 03:36 AM   #1
kaylachris
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Rep: Reputation: 0
C++ number formating help


I have the following code that complies and executes just fine. However, its been some time since I've dealt with C++ and need to format the output a little better.

Code:
#include <iostream>  
#include <fstream>  
#include <math.h>  
#include <unistd.h>  
using namespace std;  

void work(void)  
 {  
 double y;  
 double x = 3.0;  
 double e = 2.0;  
 int i,j;  

 for(i=0; i<5; i++)  
 {  
   for(j=0; j<400000; j++)  
   {  
     y=pow(x,e);  
   }  
   printf("Loop %d of work cycle\n", i);  
  //pause for one second between loops so that the work cycle takes a little time.  
   sleep(1); 
 }  
}  
    
int main(void)  
{  
 double beginTotalTime;  
 double beginIdleTime;  
 double endTotalTime;  
 double endIdleTime;  
 double programTotalTime = 0.0;  
 double programIdleTime = 0.0;  
 double programWorkTime = 0.0;  
 double percentage = 0.0;  

 ifstream out("/proc/uptime");  
 out >> beginTotalTime >> beginIdleTime;  
 out.close();
 work();  
 out >> endTotalTime >> endIdleTime;  
 out.close();  

 cout << "beginTotalTime: " << beginTotalTime << " beginIdleTime: " << beginIdleTime <<endl;
 cout << "  endTotalTime: " << endTotalTime << "   endIdleTime: " <<endIdleTime << endl;

 //Perform calculations
 programTotalTime = beginTotalTime - endTotalTime;  
 programIdleTime = beginIdleTime - endIdleTime;  
 programWorkTime = programTotalTime - programIdleTime;  
 percentage = (programWorkTime/programTotalTime) * 100;  

 //Print results
 cout << "\nThe program's Total Time is:   " << programTotalTime << endl;  
 cout << "The program's Idle Time is:    " << programIdleTime << endl;  
 cout << "The program's Work Time is:    " << programWorkTime << endl;  
 cout << "The percentage the CPU was busy: " << percentage << "%" << endl;  

 return 0;  
}
With output of:
Quote:
Loop 0 of work cycle
Loop 1 of work cycle
Loop 2 of work cycle
Loop 3 of work cycle
Loop 4 of work cycle

beginTotalTime: 1.92928e+07 beginIdleTime: 2.08111e+06
endTotalTime: 1.92928E+07 endIdleTime: 2.08111e+06

The program's Total Time is: 5.23
The program's Idle Time is: 5.02
The program's Work Time is: 0.21
The percentage the CPU was busy: 4.0153%
my question is how do I format the beginTotalTime, etc into a more readable form? something along the lines of days, hours, mins, sec...
Any help would be appreciated.
thank you
 
Old 02-11-2011, 05:34 AM   #2
goossen
Member
 
Registered: May 2006
Location: Bayern, Germany
Distribution: Many
Posts: 224

Rep: Reputation: 41
<wrong post>

Last edited by goossen; 02-11-2011 at 05:35 AM. Reason: lack of coffee
 
Old 02-11-2011, 06:08 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You could consider setting the float-point precision on the cout object. For example:
Code:
...
#include <iomanip>

...

std::cout.precision(6);
std::cout << "beginTotalTime ...

...
Btw, after you close the file stream referencing /proc/uptime, don't you think that you should re-open the file to get fresh time readings for your end-times? Examine the code you posted; this step is missing. :-)

As for converting the system times in /proc/utime to something more readable, you may need to develop your own function to convert the seconds. Something like this:
Code:
#include <sstream>
...

std::string date(double time)
{
   double days, hours, minutes, seconds;

   days  = floorl(time / 86400.0);
   time -= (86400.0 * days);

   hours = floorl(time / 3600.0);
   time -= (3600.0 * hours);

   minutes = floorl(time / 60.0);
   time -= (60.0 * minutes);

   seconds = time;

   std::stringstream ss;
   ss << days << " days "
      << std::setw(2) << std::setfill('0')
      << hours << ":" << minutes << ":" << seconds;

   return ss.str();
}

...

std::cout << "beginTotalTime: " << date(beginTotalTime) << ...
 
  


Reply

Tags
c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Identify and explain the major number, minor number, and revision number in Linux... turbomen Linux - Newbie 1 11-16-2010 02:48 AM
why sector number not match block number? bitzsk Linux - Kernel 1 06-09-2009 05:32 AM
How to grab one number from a row in a text but not another number Mike_V Programming 9 04-25-2009 04:57 AM
how do you edit your virtual console number? (or VT number 3) jjorloff1 Linux - General 2 04-03-2004 07:21 PM
why there is a need for minor number and major number for monitor/keyboard in pc? tripathi Solaris / OpenSolaris 1 11-07-2003 09:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration