// =====================< EXUTY.CPP >============================== // * UTILITY.CPP Examples * // * Description: Chapter 8 * // * Scientific C++ Building Numerical Libraries * // * the Object-Oriented Way * // * by G. Buzzi-Ferraris * // * Addison-Wesley (1993) * // **************************************************************** // * To be assembled with UTILITY.OBJ * // **************************************************************** // * To execute: exuty * // * If equals a zero it executes all examples * // * otherwise it executes only that example * // * It writes the results on < file name> * // * If not used it writes on screen * // * If one gives Y/N equal to Y it prints messages as well * // * the default is Y * // **************************************************************** // * Use: exuty 0 pro.doc * // * Description: Executes all examples * // * Prints the results on pro.doc * // * Use: exuty 2 pro.doc n * // * Description: Executes example 2 * // * Prints the results on pro.doc * // * Does not print from Message * // * Use: exuty * // * Description: Executes all examples * // * Prints the results on stdout * // * Use: exuty 3 * // * Description: Executes example 3 * // ================================================================ // ================================================================ // * PROTOTYPES * // ================================================================ void Ex_MachEps(void); void Ex_MaxMin(void); void Ex_Message(void); void Ex_Sort(void); void Ex_SqrtSumSqr(void); void Ex_SumDot(void); void Ex_Swap(void); void Ex_TempFile(void); void Ex_Error(void); // ================================================================ // * include * // ================================================================ #include #include #include #include #include #include "utility.hpp" // ================================================================ // * main * // ================================================================ int main(int argc,char **argv) { const int NEXUTY = 9; const int NMAX = NEXUTY + 1; int i; void (*ExUty[NMAX])(void); char myfilename[81]; ExUty[1]=Ex_MachEps; ExUty[2]=Ex_MaxMin; ExUty[3]=Ex_Message; ExUty[4]=Ex_Sort; ExUty[5]=Ex_SqrtSumSqr; ExUty[6]=Ex_SumDot; ExUty[7]=Ex_Swap; ExUty[8]=Ex_TempFile; ExUty[NEXUTY]=Ex_Error; if(argc>=3) { strcpy(myfilename,argv[2]); if((bzzFileOut=fopen(myfilename,"w"))==NULL) Error("fopen Failed in EXUTY"); if(argc==4)bzzYesNo=toupper(argv[3][0]); } else { bzzFileOut=stdout; } if(argc==1||atoi(argv[1])<=0||atoi(argv[1])>NEXUTY) { /* all examples */ for(i=1;i<=NEXUTY;i++) (*ExUty[i])(); } else { (*ExUty[atoi(argv[1])])(); } return 0; } // ================================================================ // ==================== EXAMPLES ============================== // ================================================================ // ================================================================ // =================== MACH_EPS =============================== // ================================================================ void Ex_MachEps(void) { Message ("\n\n\nMACH_EPS calculated and memorised %e",MACH_EPS); } // ================================================================ // ==================== MAX_MIN =============================== // ================================================================ void Ex_MaxMin(void) { { Message("\n\n\n*** Examples of Max and Min ***\n"); float a=-3.,b=-7.; double da=-3.,db=-7.; int ia=-3,ib=-7; Message("\n%f",Max(a,b)); Message("\n%f",Min(a,b)); Message("\n%f",Max(da,db)); Message("\n%f",Min(da,db)); Message("\n%d",Max(ia,ib)); Message("\n%d",Min(ia,ib)); Message("\n%f",MaxAbs(a,b)); Message("\n%f",MinAbs(a,b)); Message("\n%f",MaxAbs(da,db)); Message("\n%f",MinAbs(da,db)); Message("\n%d",MaxAbs(ia,ib)); Message("\n%d",MinAbs(ia,ib)); } { Message("\n\n\nMax Min of Array\n"); float w[6]={-140.,5.,-1.,2.,3.,110.}; float x; int im; x = Max(6,w); Message("\n%f",x); x = Max(6,w,&im); Message("\n%f %d",x,im); x = MaxAbs(6,w); Message("\n%f",x); x = MaxAbs(6,w,&im); Message("\n%f %d",x,im); x = Min(6,w); Message("\n%f",x); x = Min(6,w,&im); Message("\n%f %d",x,im); x = MinAbs(6,w); Message("\n%f",x); x = MinAbs(6,w,&im); Message("\n%f %d",x,im); } } // end max min // ================================================================ // =================== MESSAGE ================================ // ================================================================ void Ex_Message(void) { Message("\n\n\n*** Example of Message ***\n"); float f=3.14; int i=111; char a='A'; Message("\nThis is a message\n"); Message("float %f\tint %d\tchar %c\n",f,i,a); } // ================================================================ // ===================== SORT ================================= // ================================================================ void Ex_Sort(void) { Message("\n\n\nFloat Sort \n"); int n=10; float x[]={10.,3.,2.,1.,4.,5.,6.,8.,9.,7.}; Sort(n,x); for(int i=0;i itmax)Error("\nThe variable iter is worth %d",iter); } // end Error