Thread: Freezer

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Cool Freezer

    It's a speed tester.
    I wrote this program with some of information I gathered from here. Please place the attached file on your C: partition and compile it. tell me your idea about the code, bugs and if possible your score and machine type.
    Thank you.

    Code:
    #include<math.h>
    #include<iostream.h>
    #include<fstream.h>
    #include<string.h>
    #include<windows.h>
    #include<conio.h>
    
    //prototypes
    void func(int);
    int getrange(int minimum, int Maximum);
    void showdata();
    
    void func(int a)
    {
    	a++;
    	a--;
    	a+=34;
    	a-=32;
    	a/=3;
    }
    
    void showdata()
    {
    	ifstream dat_if;
    	int readn;
    	char readc[20];
    
    	dat_if.open("C:\\data.txt",ios::in | ios::nocreate);
    	if(!dat_if.good()) 
    	{
    		cout <<"Error opening data file!"<<endl;
    		dat_if.close();
    		return;
    	}
    
    	
    
    	dat_if>>readc>>readn;
    	while(!dat_if.eof()&&dat_if.good())
    	{
    		cout<<readc<<" : "<<readn <<endl;
    		dat_if>>readc>>readn;
    		
    	}
    	
    	dat_if.close();
    }
    
    int getrange(int m, int M)
    {
    	int inpv; //input value
    	
    	cout <<"A number from "<< m<<" to " << M<<": ";
    	cin >>inpv;
    	if(inpv >3 || inpv <1) 
    	{
    		cout<<"Not in range!"<<endl;
    		cin.clear(); 
    		cin.ignore(300,'\n'); 
    		inpv=getrange(m,M);  //<=Look at this.
    		
    	}
    	else 
    	{
    		//cin.clear(); It seems not required
    		cout<<endl;
    	}
    	return inpv;
    }
    
    int main()    //Salem forced me to use int instead of void for compatibility
    {
    	cout<<"Define the program's precision:"<<endl;
    	
    	//This function gets input in specified range;
    	int pric =getrange(1,3);
    	
    	//////////////////////// Selfish function:
    	SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS );
    	////////////////////////
    	
    	//Initializings
    	int x=0;
    	char ch[1000]="uv";
    	char ch2[]="j";
    	double doa[100];
    	double doa2[100];
    	double doub=33.3433;
    	DWORD TimeFrame = 1000 * 5 * pric;
    	
    	cout<< "Freezing..."   <<endl;
    	
    	/*This function returns
    	miliseconds passed since Windows started.*/
    	DWORD StartTime = GetTickCount(); 
    	
    	
    	/*In this loop some simple operations execute to make CPU work 
    	harder. Actually I didn't spend enough time to optimize this block. 
    	Tell me your suggestions. */
    	
    	while((GetTickCount() - StartTime) < TimeFrame)
    	{
    		(x<100) ? doa[x]=doa2[x]:doa[32]=doa2[23];
    		if(x<500)  strcat(ch,ch2); else func(x);
    		doub+=3433.52234;
    		
    		doub-=4334.3;
    		doub/=3.34;
    		doub*=4.544;
    		doub=sqrt(doub);
    		doub=x-(x*x*x)+19*x/3*x;
    		doub=doub/2-322343;
    		x++;
    
    	}
    	
    	cout<<"Your score is: "<< int(x/(10000*pric))<<endl;
    	
    	//This function reads some recent score records from a file.
    	showdata(); 
    	
    	//This function sends the string as a command to system.
    	system("pause");//waits till user hits any key
    	
    	
    	return 0;	
    
    }
    All windows compilers should compile it, shouldn't they?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    You do realise that calling a function "func" is an offence whereby you're taken out the back and shot at dawn, don't you?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by siavoshkc
    It's a speed tester.
    All windows compilers should compile it, shouldn't they?
    No, VC++7.1 and later (Visual Studio.Net 2003) won't compile it, because you used the old, outdated headers. They were removed in 7.1.

    On a funny side note, I do believe that enabling all optimization options would cause the compiler to simply remove most of your "make CPU work" stuff.
    Last edited by CornedBee; 01-19-2006 at 10:43 AM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    What if I add x to all of them?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by ahluka
    You do realise that calling a function "func" is an offence whereby you're taken out the back and shot at dawn, don't you?
    What do you mean? I mean I understood your goal. But your sentences seems heavy to me.
    Last edited by siavoshkc; 01-20-2006 at 07:44 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    The name of a function should be descriptive. When a project gets larger using names like func make it difficult too see whats going on.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    OK rename it to worker_sample().
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Take a look to new version
    Code:
    /* Freezer ver 1.8
    * - New CPU work stuff
    * - Security hole patched (no shell command anymore)
    * - Now searches for data file. If not found asks user for path
    * - Better search for data file(NEW)
    * - Tests for priority(NEW)
    * - Very modular design(NEW)
    * - Goes to high priority only in needed time(NEW)
    */
    
    int cpuburner(int);
    void testfunc(int);
    int getrange(int minimum, int Maximum);
    void showdata(char *);
    void getpath(char* );
    
    int main(int argc , char *argv[])
    {
    
    	cout<<"Define the program's precision:"<<endl;
    	int pric =getrange(1,3);
    	
    	cout<< "Freezing..."   ;
    	SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS );
    	//cout<< "Priority: "<< GetPriorityClass(GetCurrentProcess())  <<endl;
    	if(200>GetPriorityClass(GetCurrentProcess()))
    		cout<<" (Could not swicth to highest priority)"<<endl;
    	else
    		cout<<endl;
    	
    	cout<<"Your score is: "<< cpuburner(pric)<<endl;
    
    	SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS );
    		
    	char pat[100];
    	strncpy(pat,argv[0],99);
    	
    	strcpy(strrchr(pat,92),"\\freez.dat");
    	showdata(pat);
    	cout<<  "Press a key to exit...";
    	cout.flush();
    	getch();
    	return 0;
    	
    }
    
    
    void getpath(char *pat)
    {
    	
    	cout<<"Enter path of freez.dat: " ;
    	cin.ignore(1,'\n'); //Tell me why this should be here
    	cin.getline(pat,30,'\n');
    	
    }
    
    void showdata(char *pat)
    {
    	
    	
    	ifstream dat_if;
    	int readn;
    	char readc[10];
    	
    	dat_if.open(pat,ios::in | ios::nocreate);
    	if(!dat_if.good()) 
    	{
    		
    		dat_if.clear();
    		getpath(pat);
    		strcat(pat, "\\freez.dat");
    		
    		dat_if.open(pat,ios::in | ios::nocreate);
    		
    		if(!dat_if.good()) 
    		{
    			cerr <<"Error opening data file from path: "<<pat<<endl;
    		
    			return;
    		}
    	}
    
    	
    
    	dat_if>>readc>>readn;
    	while(!dat_if.eof()&&dat_if.good())
    	{
    		
    		cout<<readc<<" : "<<readn <<endl;
    		dat_if>>readc>>readn;
    		
    	}
    	
    	dat_if.close();
    }
    
    int getrange(int m, int M)
    {
    	int inpv;
    	
    	cout <<"A number from "<< m<<" to " << M<<": ";
    	cin >>inpv;
    	if(inpv>3 || inpv<1) 
    	{
    		cout<<"Not in range!"<<endl;
    		cin.clear(); 
    		cin.ignore(300,'\n'); 
    		inpv=getrange(m,M);
    		
    	}
    	else 
    	{
    		
    		cout<<endl;
    	}
    	return inpv;
    }
    
    
    int cpuburner(int pric)
    {
    	int x=0;
    	int sigma(int);
    	int fac(int);
    	char ch[1000]="sa";
    	char ch2[]="j";
    	double doa[100];
    	int c;
    	int n=GetTickCount()%10000;
    	int seed=120+100*cos(n);
    	
    	double doa2[100];
    	double doub=33.3433;
    	DWORD TimeFrame = 1000 * 5 * pric;
    	DWORD StartTime = GetTickCount();
    	
    
    	while((GetTickCount() - StartTime) < TimeFrame)
    	{
    		x<100 ? doa[x]=doa2[x]:doa[32]=doa2[23];
    		if(x<500)  strcat(ch,ch2); else testfunc(x);
    		doub+=3433.52234;
    		
    		doub-=4334.3+x;
    		doub/=3.34+x;
    		doub*=4.544;
    		testfunc(doub);
    		doub=sqrt(doub);
    		doub=x-(x*x*x)+19*x/3*x;
    		doub=doub/2-322343/(x+1);
    		
    	
    		/*ofstream ofile;
    		*ofile.open("C:\\randnum.txt");
    		*for(int n=0;n<6000;n++){
    		*This is a random number generator*/
    
    		c=(fac(n+3)*seed-(floor((pow(60*n,3)*cos(n)*seed)-sigma(n+seed)))+100*seed/
    			(n-pow(floor(log(n)),3)));
    		c=abs((int)(c+10*cos(n))%10000+(doub));
    
    		doub-=4334.2323+x;
    		doub/=3.342+x;
    		doub*=43.54423223;
    		doub=sqrt(doub)-1*x;
    		doub=x-(x*x*x+12/(x+2))+19*x/3*x*x;
    		doub=doub/2-322343*x;
    
    		//ofile<<c<<endl;
    
    		//cout<<c<<endl; //Enable it for wathing random nums.
    		//}
    		x++;
    		
    	}
    	return (x/(10*pric));
    }
    
    
    void testfunc(int a)
    {
    	a++;
    	a--;
    	a+=35564;
    	a-=320;
    
    	a/=3;
    }
    
    int sigma(int n)
    {
    	double dub; //It is random so no need to be initialized
    	
    	for(int i=1;i<=n;i++)
    	{
    		dub+=(i*i)/(i+i*2);
    	}
    	return floor(dub);
    }
    
    int fac(int n)
    {
    	int f=1;
    	for(int i=1;i<=n;i++)
    	{
    		f*=i;
    	}
    	return f;
    }
    Last edited by siavoshkc; 01-21-2006 at 11:19 PM. Reason: cerr
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Msn Protocol (Freezer) Help
    By gmaster1440 in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2007, 10:03 AM