Thread: Long program, looking for shorter

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    Long program, looking for shorter

    I wrote this program for my homework, I added a ton of Windows things for the User Interface, BASIC stuph, thanks to Prelude and Shadow!

    Code:
    //Taxes
    //Lucas Campbell, Period 1, Computer Science AP
    /*--Includes--*/
    #include <iostream.h> // cout & cin
    #include <iomanip.h> // setiosflags() & setprecision()
    #include <time.h> // time_t & ctime()
    #include <conio.h> // getch() 
    #include <windows.h> // manipulations of console and handlers
    /*------------*/
    // NOTE: to run on Unix platform use system() to modify for conio and windows
    // Function prototypes
    int full_screen();
    int kill_cursor();
    
    void clrscrn();
    // End prototypes
    //Main!
    int main(int argc, char* argv[]) // Accepts arguments
    {
    	// Declarations
    	time_t timel = time(NULL); // Get time for ctime()
    	const double fedTax=0.15, FICA = 0.08, stateTax = 0.032; // Constants for rates
    	double hoursWorked, hourlyRate, gross, net, tmp1, tmp2, tmp3; // Variables
    	full_screen(); // We like BIG screens!
    	kill_cursor(); // Can't have a mouse in DOS (technically)
    	// Done with declarations
    
    	if(argc > 1) // Make sure we don't have anymore program arguments
    	{
    		cout<<"This program doesn't accept arguments!"<<endl;
    		return 0;
    	}
    
    	cout<<setiosflags(0x0100 | 0x1000); // showpoint & fixed
    	
    
    	cout<<"\t\t\tWelcome to Taxes\t\t"<<ctime(&timel); // Title and date
    	//Output / Input block
    	cout<<endl<<"Hours worked: ";
    	cin>>hoursWorked; // Storing in hoursWorked
    	cout<<endl<<"Hourly rate: ";
    	cin>>hourlyRate; // Storing in hourlyRate
    	cout<<endl<<"Press any key to see your taxes!"<<endl;
    	getch(); // pause for key-press
    	clrscrn(); // clear screen
    
    	//		NEW SCREEN HERE
    
    	cout<<"\t\t\t\t\t\t\t"<<ctime(&timel)<<endl; //date in corner
    	cout<<"Hours worked\t\t"<<setprecision(0x0000) 
    		<<hoursWorked<<endl; // displays no decimal for output
    	cout<<"Hourly rate\t\t"<<setprecision(0x0002)
    		<<hourlyRate<<endl; // now displays to two decimals from now on
    	// Math / Output section
    	gross = (hoursWorked * hourlyRate)+0.00001;
    	
    	cout<<endl<<"Gross pay\t\t"
    		<<gross<<endl;
    	
    	tmp1 = (fedTax * gross) + 0.0001; // 0.001 for truncation cases
    	cout<<endl<<"Federal tax(15%)\t"
    		<<tmp1<<endl;
    	tmp2 = (FICA * gross) + 0.0001;
    	cout<<"FICA (8%)\t\t"
    		<<tmp2<<endl;
    	tmp3 = (stateTax * gross) + 0.0001;
    	cout<<"State tax (3.2%)\t"
    		<<tmp3<<endl;
    	net = (gross - (tmp1+tmp2+tmp3));
    	cout<<endl<<"Net pay\t\t\t"
    		<<net<<endl<<endl;
    	// end section
    	cout<<"Press return to continue"<<endl;
    	getch(); // pause for user
    
    
    	return 0;
    }
    int full_screen() // Maximize screen
    {
    	keybd_event(VK_MENU,0x38,0,0); 
    	keybd_event(VK_RETURN, 0x1c, 0,0); 
    	keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0); 
    	keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0); 
    	return 0;
    }
    int kill_cursor() // no mouse in fullscreen
    {
    	CONSOLE_CURSOR_INFO cci; 
    	cci.dwSize = 1; 
    	cci.bVisible = FALSE; 
    	SetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE), &cci); 
    	return 0;
    }
    
    
    void clrscrn() // Clear screen
    
    {
       //declaring objects
       COORD coordScreen = { 0, 0 }; 
       DWORD cCharsWritten;
       CONSOLE_SCREEN_BUFFER_INFO csbi; 
       DWORD dwConSize;
       HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //GET READY FOR HANDLERS!
       //end declarations
       GetConsoleScreenBufferInfo(hConsole, &csbi); // Set handlers
       dwConSize = csbi.dwSize.X * csbi.dwSize.Y; // Change coords
       FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
       GetConsoleScreenBufferInfo(hConsole, &csbi); // rematch buffers
       FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
       SetConsoleCursorPosition(hConsole, coordScreen); // rematch buffers to coords
    }
    I am looking for 3 things
    An explanation to what REALLY happens in those clrscrn() full_screen() and kill_cursor

    a shorter way of doing this whole program

    and a way so that the net pay is not rounded

    also (don't want to use ios::whatever)
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    also
    I am not a big Windows fan
    UNIX FAN!

    however, I have done some basic Windows programming
    so I understand some of the lingo like handlers and how they
    work but just not the whole thing of what those EXCELLENT programmers did
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    An explanation to what REALLY happens in those clrscrn()
    Those are the Windows functions used to interface with the "dos console".
    So the DOS console window you see is actually a Windows program in disguise! You can prove this by booting into dos and try running the program. You can learn more about these functions by visiting the MSDN website.


    [edit]
    You call THAT a large program? Wait till you're writing 10, 000+ lines of code and then come back whining Seriously, though, the program is about as small as it can be...no worries...
    Last edited by Sebastiani; 10-07-2002 at 10:51 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I meant large in the terms of Homework
    my programs get MUCH LARGER THAN THAT
    like Kernel Code
    I had to get a book to keep up with all the libraries so I can write the code.

    Kernel writing is sooo much fun espeically when the tester comes up and says, "Hey you idiot, learn how to program" j/k
    they say something like address overflow, or syntax or something sinple, however when they give you the list of errors its similar to being called an idiot.

    People should lern to program in miniOS so they learn what really happens, none of that Iostream crap.

    the point is, I don't like windows, its ugly and for some reason I am better with LINUX KERNEL CODE Than windows application code.

    comeon people
    like
    what the heck is UINT
    however everyone knows what
    init() is! (They should if they have EVER turned on a Unix system)
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Originally posted by Lynux-Penguin
    I meant large in the terms of Homework
    my programs get MUCH LARGER THAN THAT
    like Kernel Code
    I had to get a book to keep up with all the libraries so I can write the code.

    Kernel writing is sooo much fun espeically when the tester comes up and says, "Hey you idiot, learn how to program" j/k
    they say something like address overflow, or syntax or something sinple, however when they give you the list of errors its similar to being called an idiot.

    People should lern to program in miniOS so they learn what really happens, none of that Iostream crap.

    the point is, I don't like windows, its ugly and for some reason I am better with LINUX KERNEL CODE Than windows application code.

    comeon people
    like
    what the heck is UINT
    however everyone knows what
    init() is! (They should if they have EVER turned on a Unix system)
    Don't like windows because it's ugly. None of this iostream crap. So you're the type of person who likes to completely reinvent the wheel 10^x times over instead of building ontop of what someone else has already done for you. Windows programming is quite easy once you get the HANDLE of it. Also your "stream" crap is also found in your beloved UNIX...they are VERY handy especially when you wish to handle raw data in whatever way you want to.

    UINT is short for "unsigned integer"...do you even know the difference between a signed integer and an unsigned integer? Variable types such as WORD, DWORD and the like are created for a reason. A WORD will always be 16 bits regardless of what architecture you are working on. A DWORD will always be 32 bits regardless of what architecture you are working on. Your primatives such as integers will vary depending on what architecture you are working on. It's called consistency, it's dramatically important when porting code especially if you don't want to rewrite quite a bit of it.

    Woohoo, you can modify a Linux kernel. Now that you've learned that trick why not move on and learn something else. Sorry to break it to you buddy, but there's a whole hell of a lot more to "programming" than "Linux kernels".
    Last edited by Divx; 10-08-2002 at 02:54 AM.

  6. #6
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    sounds like something from Java

    *shivers*

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    -_-

    I understood what a UINT is but some don't
    the point i was trying to make across is that I find it easier learning COMPLEX Unix code than learning simple windows code.

    init() is a function in the kernel that initializes the order of memory and devices AND decompresses the kernel image.
    hah init made up...
    turn on a computer what happens?
    do you think the cpu has the OS on it?
    something has to start the OS that is what INIT is, the initialization

    why is that in windows all void memory allocations are CC
    or int 3?
    something up with that, shouldn't they be FF?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Originally posted by vVv
    > everyone knows what
    > init() is!

    I don't. There's no such thing like an ``init()'' function on any system I use. Is that something you wrote yourself?
    FreeBSD has init()
    its not a default C function its a Kernel function, in linux its in <linux/init.h>
    however i believe (though may be wrong) that init for the kernel is somewhere else, don't know right know, I need to get my book again.

    When you turn on your computer you should see text fly by like Kernel decompressing.
    eth0 up
    and the whole initialization process
    that is called because its is on the boot sector


    BOOT PROCESS:
    1. Computer powers on, CPU starts recycling and loads BIOS
    2. BIOS loads I/O
    3. CPU executes Boot Sector's addresses
    4. Boot Sector contains LILO, which contains init()
    5. init() decompresses kernel image and runs it
    6. kernel re-runs init() to load drivers
    7. kernel runs
    8. computer is up
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    "init()" - ya, it's pretty much universally known as an abreviation for ...*drum roll*... initialize.

    a void pointer really isn't anything and could be anything. Void pointers are really awesome to be honest. Though a suitable replacement could be an integer since to the best of my knowledge is the same size as a pointer from architecture to architecture...though perhaps for 'consistency sake' a void pointer should be used at all times.

    My view point of a void pointer is as such, it's a marker, the beginning in memory for -anything-. It could be a primative, it could be any object you created. It simply marks the beginning in memory where the object exists.

    A good example of WELL THOUGHT OUT usage would be in multithreading in windows.

    Code:
    HANDLE CreateThread(
      LPSECURITY_ATTRIBUTES lpThreadAttributes,  // pointer to security attributes
      DWORD dwStackSize,                         // initial thread stack size
      LPTHREAD_START_ROUTINE lpStartAddress,     // pointer to thread function
      LPVOID lpParameter,                        // argument for new thread
      DWORD dwCreationFlags,                     // creation flags
      LPDWORD lpThreadId                         // pointer to receive thread ID
    Here's our method to create a thread...though the only thing we're worried about is "LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function". For the record variable type "LPTHREAD_START_ROUTINE" is a function pointer of type "DWORD WINAPI ThreadProc(LPVOID lpParameter)". LPVOID is your 32-bit pointer. Think of "ThreadProc" as you would "main" to your application. It IS what your thread will be doing. It's parameter is a void pointer, well crap, how are you going to tell the thread all that it's going to need to know without having to post to it off the bat? Create a class WITH all the information you want the thread to have and cast type it as a void pointer (or LPVOID to be 'consistent'). In the thread's main function cast type it back to the class type and batabing bataboom your thread is now informed.

    Just a side note, simple coding is simple for a reason. Why grow the wheat, mill the wheat, milk the cow, buy the chicken to lay the eggs and go through the process of making bread when you can go down to your local supermarket to buy your bread. The only USEFUL thing in going through the entire process, is to become more knowledgable of the process...though you'll quickly learn to skim over what the person did and learn that it's already done, and if you aren't having any problems with it, screw it just use it and move on.
    Last edited by Divx; 10-08-2002 at 03:56 AM.

  10. #10
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Lynux-Penguin

    the point i was trying to make across is that I find it easier learning COMPLEX Unix code than learning simple windows code.
    you are BSing, thats why.

    Originally posted by Lynux-Penguin
    FreeBSD has init()
    its not a default C function its a Kernel function, in linux its in <linux/init.h>
    your statement that any function is contained "in" a header file reveals that you have no clue what you're talking about.

    Originally posted by Lynux-Penguin

    Kernel writing is sooo much fun espeically when the tester comes up and says, "Hey you idiot, learn how to program" j/k
    they say something like address overflow, or syntax or something sinple, however when they give you the list of errors its similar to being called an idiot.
    you submit code to the linux kernel that contains syntax errors? i feel sorry for the maintainers.

    Originally posted by Lynux-Penguin
    I wrote this program for my homework, I added a ton of Windows things for the User Interface, BASIC stuph, thanks to Prelude and Shadow!

    Code:
    //Taxes
    //Lucas Campbell, Period 1, Computer Science AP
    /*--Includes--*/
    #include <iostream.h> // cout & cin
    #include <iomanip.h> // setiosflags() & setprecision()
    #include <time.h> // time_t & ctime()
    #include <conio.h> // getch() 
    #include <windows.h> // manipulations of console and handlers
    /*------------*/
    // NOTE: to run on Unix platform use system() to modify for conio and windows
    // Function prototypes
    int full_screen();
    int kill_cursor();
    
    void clrscrn();
    // End prototypes
    //Main!
    int main(int argc, char* argv[]) // Accepts arguments
    {
    	// Declarations
    	time_t timel = time(NULL); // Get time for ctime()
    	const double fedTax=0.15, FICA = 0.08, stateTax = 0.032; // Constants for rates
    	double hoursWorked, hourlyRate, gross, net, tmp1, tmp2, tmp3; // Variables
    	full_screen(); // We like BIG screens!
    	kill_cursor(); // Can't have a mouse in DOS (technically)
    	// Done with declarations
    
    	if(argc > 1) // Make sure we don't have anymore program arguments
    	{
    		cout<<"This program doesn't accept arguments!"<<endl;
    		return 0;
    	}
    
    	cout<<setiosflags(0x0100 | 0x1000); // showpoint & fixed
    	
    
    	cout<<"\t\t\tWelcome to Taxes\t\t"<<ctime(&timel); // Title and date
    	//Output / Input block
    	cout<<endl<<"Hours worked: ";
    	cin>>hoursWorked; // Storing in hoursWorked
    	cout<<endl<<"Hourly rate: ";
    	cin>>hourlyRate; // Storing in hourlyRate
    	cout<<endl<<"Press any key to see your taxes!"<<endl;
    	getch(); // pause for key-press
    	clrscrn(); // clear screen
    
    	//		NEW SCREEN HERE
    
    	cout<<"\t\t\t\t\t\t\t"<<ctime(&timel)<<endl; //date in corner
    	cout<<"Hours worked\t\t"<<setprecision(0x0000) 
    		<<hoursWorked<<endl; // displays no decimal for output
    	cout<<"Hourly rate\t\t"<<setprecision(0x0002)
    		<<hourlyRate<<endl; // now displays to two decimals from now on
    	// Math / Output section
    	gross = (hoursWorked * hourlyRate)+0.00001;
    	
    	cout<<endl<<"Gross pay\t\t"
    		<<gross<<endl;
    	
    	tmp1 = (fedTax * gross) + 0.0001; // 0.001 for truncation cases
    	cout<<endl<<"Federal tax(15%)\t"
    		<<tmp1<<endl;
    	tmp2 = (FICA * gross) + 0.0001;
    	cout<<"FICA (8%)\t\t"
    		<<tmp2<<endl;
    	tmp3 = (stateTax * gross) + 0.0001;
    	cout<<"State tax (3.2%)\t"
    		<<tmp3<<endl;
    	net = (gross - (tmp1+tmp2+tmp3));
    	cout<<endl<<"Net pay\t\t\t"
    		<<net<<endl<<endl;
    	// end section
    	cout<<"Press return to continue"<<endl;
    	getch(); // pause for user
    
    
    	return 0;
    }
    int full_screen() // Maximize screen
    {
    	keybd_event(VK_MENU,0x38,0,0); 
    	keybd_event(VK_RETURN, 0x1c, 0,0); 
    	keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0); 
    	keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0); 
    	return 0;
    }
    int kill_cursor() // no mouse in fullscreen
    {
    	CONSOLE_CURSOR_INFO cci; 
    	cci.dwSize = 1; 
    	cci.bVisible = FALSE; 
    	SetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE), &cci); 
    	return 0;
    }
    
    
    void clrscrn() // Clear screen
    
    {
       //declaring objects
       COORD coordScreen = { 0, 0 }; 
       DWORD cCharsWritten;
       CONSOLE_SCREEN_BUFFER_INFO csbi; 
       DWORD dwConSize;
       HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //GET READY FOR HANDLERS!
       //end declarations
       GetConsoleScreenBufferInfo(hConsole, &csbi); // Set handlers
       dwConSize = csbi.dwSize.X * csbi.dwSize.Y; // Change coords
       FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
       GetConsoleScreenBufferInfo(hConsole, &csbi); // rematch buffers
       FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
       SetConsoleCursorPosition(hConsole, coordScreen); // rematch buffers to coords
    }
    I am looking for 3 things
    An explanation to what REALLY happens in those clrscrn() full_screen() and kill_cursor

    a shorter way of doing this whole program

    and a way so that the net pay is not rounded

    also (don't want to use ios::whatever)
    no kernel programmer would ever ever ever ever ever write
    Code:
    if(argc > 1) // Make sure we don't have anymore program arguments
    as the usage and meaning of argc is very well known and so is the conditional (argc > 1).
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 08:08 PM
  3. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM
  4. Peculiar Problem with Printf
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-02-2002, 12:34 AM
  5. Peculiar Problem with Printf
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-02-2002, 12:03 AM