⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 斯坦福Energy211/CME211课《c++编程——地球科学科学家和工程师》的课件
💻 CPP
字号:
// This program demonstrates command-line arguments#include <iostream>#include <sstream>#include <fstream>#include "linsys.h"using namespace std;const char szUsage[] = {	"Usage: custom_solve [-tol eps] [-method ge|jacobi|gs|sor] [-maxiter n] [-w val] datafile\n"} ;void UsageError( const string& sMesg ) {	string total_mesg = "Error: " + sMesg + "\n" + szUsage;	throw runtime_error( total_mesg.c_str() );}int main( int argc, char *argv[] ){		try {				// default values		double tol = 1e-8;		bool direct = true;		int maxiter = 100;		double w = 1.0;		LinearSystem::IterMethod m = LinearSystem::MethodJacobi;			string filename;				// this points to the current argument		char **cp;		// iterate through arguments, skipping over		// first one before we begin		for ( cp = ++argv, --argc; argc > 0; 				++cp, --argc ) {			// all arguments begin with a -, except			// for the name of the input file			if ( **cp == '-' ) {				string s = ++*cp;				if ( s == "tol" ) {					++cp;					--argc;					istringstream is( *cp );					is >> tol;				}				else if ( s == "w" ) {					++cp;					--argc;					istringstream is( *cp );					is >> w;				}				else if ( s == "method" ) {					++cp;					--argc;					string smethod = *cp;					if ( smethod == "ge" )						direct = true;					else if ( smethod == "jacobi" ) {						direct = false;						m = LinearSystem::MethodJacobi;					}					else if ( smethod == "gs" ) {						direct = false;						m = LinearSystem::MethodGaussSeidel;					}					else if ( smethod == "sor" ) {						direct = false;						m = LinearSystem::MethodSOR;					}					else						UsageError( "invalid iterative method" );				}				else if ( s == "maxiter" ) {					++cp;					--argc;					istringstream is( *cp );					is >> maxiter;				}				else {					string sMesg = string( "Invalid option: " ) + *cp;					UsageError( sMesg );				}			}			else				filename = *cp;		}				// this argument is required		if ( filename.size() == 0 )			UsageError( "no filename specified" );			// open file, making sure it exists		ifstream datafile( filename.c_str() );		if ( datafile.fail() ) {			string mesg = string( "file " ) + *cp + 				" does not exist";			UsageError( mesg );		}			Matrix A;		ColVector b;		// read system		datafile >> A;		cout << "A = \n" << A;		datafile >> b;		cout << "b = \n" << A;			// and solve it, in manner prescribed		// by command-line arguments		LinearSystem S( A );		ColVector x;			if ( direct )			x = S.DirectSolve( b );		else {			S.SetMaxIter( maxiter );			S.SetTol( tol );			x = S.IterativeSolve( b, m, w );		}				cout << "x = \n" << x;			}	catch( runtime_error e ) {		// any exception, due to incorrect usage or		// a problem solving the system, is caught here		cout << e.what();	}			return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -