Gå til innhold

Program review, alternativ til evig lange if.


Anbefalte innlegg

Skrevet

Har laget et lite program for å hente ut informasjon om PCen (Linux).

Programmet fungerer, eneste som plager meg er at jeg måtte bruke hele programmet på "if" blocks. Så jeg lurte på om det var noen som hadde ideer på hvordan jeg kunne endre på dette.

 

Her er koden:

 

 

 

/*
*	  lcs.cpp
*	  
*	  Copyright 2009 marius <marius@marius-laptop>
*	  
*	  This program is free software; you can redistribute it and/or modify
*	  it under the terms of the GNU General Public License as published by
*	  the Free Software Foundation; either version 2 of the License, or
*	  (at your option) any later version.
*	  
*	  This program is distributed in the hope that it will be useful,
*	  but WITHOUT ANY WARRANTY; without even the implied warranty of
*	  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	  GNU General Public License for more details.
*	  
*	  You should have received a copy of the GNU General Public License
*	  along with this program; if not, write to the Free Software
*	  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
*	  MA 02110-1301, USA.
*/


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <cstring>

#define SPACE "_____________________________________________\n\n"

using namespace std;






int main(int argc, char* argv[])
{
	string POST;
	int i = 1;
	int help = 0;
	stringstream CHECK;
	int FIRST_Q = 0;
	int LAST_Q = 0;
	string KEY_STROKE;

	system("clear");


for (i = 1; i < argc; i++) {




	stringstream ss;

	POST = " ";
	ss << argv[i];
	ss >> POST;


		if (POST == "-m")
			{
				cout <<"\nMemory Info (/proc/meminfo)\n";
				cout << SPACE;
					system("cat /proc/meminfo");
					help = 1;
			}

		if (POST == "-p")
			{	
				cout <<"\nNumbers of prossesors (/proc/cpuinfo)\n";
				cout << SPACE;
					system("cat /proc/cpuinfo");
					help++;
			}

		if(POST=="-pt")
			{
				cout <<"\nPartition Table(/proc/partitions)\n";
				cout << SPACE;
					system("cat /proc/partitions");
					help++;
			}

		if(POST=="-n")
			{
				cout <<"\nRouting Table(netstat)\n";
				cout << SPACE;
					system("netstat -rn");
					help++;
			}

		if(POST=="-i")
			{
				cout <<"\nIP confugurations (ifconfig)\n";
				cout << SPACE;
					system("ifconfig");
					help++;
			}

		if(POST=="-k")
			{
				cout <<"\nKernel version (uname -r)\n";
				cout << SPACE;
					system("uname -r");
					help++;
			}	

		if(POST=="-x")
			{
				cout <<"\nXorg Config (X -showconfig)\n";
				cout << SPACE;
					system("X -showconfig");
					help++;
			}	

		if(POST=="-d")
			{
				cout <<"\nDistrubution (/etc/issue.net)\n";
				cout << SPACE;
					system("cat /etc/issue.net");
					help++;
			}	

		if(POST=="-hw")
			{
				cout <<"\nHardware List (lspci)\n";
				cout << SPACE;
					system("lspci");
					help = 1;
			}	




				FIRST_Q = argc;
				LAST_Q = argc-1;


					if(FIRST_Q >= 1 && LAST_Q < argc)
						{
	cout <<"\n\n\n" << SPACE <<"\n\33[7mPress any key(and <ENTER>) to continue\33[0m";
							cin >> KEY_STROKE;
							system("clear");
						}	

			}

		if (help<1)
				{
				cout <<"\nLCS\n";
			cout <<"\nThis program is created to help you find out all about your computer.\n";
				cout <<"Syntax: lcs [-m] [-p] [-k] [-i] etc...\n";
				cout << SPACE;
					cout << "Command\t\tFunction\n";
					cout << "\n-m	 \t\tMemory";
					cout << "\n-p	 \t\tProssesor";
					cout << "\n-pt	\t\tPartition Table";
					cout << "\n-n	\tRouting Table";
					cout << "\n-i	 \t\tIp configuration";
					cout << "\n-k	 \t\tKernel Version";
					cout << "\n-x	 \t\tXorg Config";
					cout << "\n-d	 \t\tDistrobution";
					cout << "\n-hw	\t\tHardware List";
				}

		cout << endl;
	}

 

 

Videoannonse
Annonse
Skrevet

Hmmm i dette tilfellet ville vel det eneste alternativet vært å lage en struktur med en pointer to function og fylle slike inn i en tabell.

 

typedef void (*sys_info_proc) ();
struct func
{
private:
 string m_name;
 sys_info_proc m_func;
public:
 string& Name() { return m_name; }
 Execute() { m_func(); }

 func(char* name, sys_info_proc function)
 {
m_name = name;
m_func = function;
 }
}

int main(int argc, char** argv)
{
 vector<func> functions = vector<func>();
 functions.pushback(func("-m", memory_info));

...

 

Og iterere over hele functions listen, og kalle Execute() funksjonen.

Skrevet (endret)

Du kan jo gjøre den noe penere ved å legge felles koden til slutten av loopen som dette

  if (POST == "-m")
  {
text = string"\nMemory Info (/proc/meminfo)\n";
command = String("cat /proc/meminfo");
help = 1;
  }
<snip>
cout << text;
cout << SPACE;
system(command.c_str());
help++;

Endret av Mr.Garibaldi
Skrevet (endret)

Har nå raffinert koden litt:

 

 

 

 

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <cstring>

#define SPACE "_____________________________________________\n\n"

using namespace std;

	string POST;
	int i = 1;
	int u = 0;
	int help = 0;
	int FIRST_Q = 0;
	int LAST_Q = 0;
	string KEY_STROKE;
	string POST_SYSTEM;







	const char * TEXT[10] = {"\nMemory Info (/proc/meminfo)\n", 
					"\nNumbers of prossesors (/proc/cpuinfo)\n", 
					"\nPartition Table(/proc/partitions)\n",
					"\nRouting Table(netstat)\n",
					"\nIP confugurations (ifconfig)\n",
					"\nKernel version (uname -r)\n",
					"\nXorg Config (X -showconfig)\n",
					"\nDistrubution (/etc/issue.net)\n",
					"\nHardware List (lspci)\n",
					''};

	const char * SYSTEM[10] =  {"cat /proc/meminfo",
					   "cat /proc/cpuinfo",
					   "cat /proc/partitions",
					   "netstat -rn",
					   "ifconfig",
					   "uname -r",
					   "X -showconfig",
					   "cat /etc/issue.net",
					   "lspci",
					   ''};

	const char * COMMAND[10] = {"-m",
					   "-p",
					   "-pt",
					   "-n",
					   "-i",
					   "-k",
					   "-x",
					   "-d",
					   "-hw",
					   ''};



int main(int argc, char* argv[])
{

	system("clear");


	for (i = 1; i < argc; i++) {


		for (u = 0; u == argc; u++){


			if (argv[i] == COMMAND[u])
				{

					cout << TEXT[u] << SPACE;
					system(SYSTEM[u]);
					help++;
				}

			}

		FIRST_Q = argc;
			LAST_Q = argc-1;


				if(FIRST_Q >= 1 && LAST_Q < argc)
					{
											 cout <<"\n\n\n" << SPACE <<"\n\33[7mPress any key(and <ENTER>) to continue\33[0m";
						cin >> KEY_STROKE;
						system("clear");
					}
}








		if (help<1)
				{
				cout <<"\nLCS\n";
cout <<"\nThis program is created to help you find out all about your computer.\n";
				cout <<"Syntax: lcs [-m] [-p] [-k] [-i] etc...\n";
				cout << SPACE;
					cout << "Command\t\tFunction\n";
					cout << "\n-m	 \t\tMemory";
					cout << "\n-p	 \t\tProssesor";
					cout << "\n-pt	\t\tPartition Table";
					cout << "\n-n	\tRouting Table";
					cout << "\n-i	 \t\tIp configuration";
					cout << "\n-k	 \t\tKernel Version";
					cout << "\n-x	 \t\tXorg Config";
					cout << "\n-d	 \t\tDistrobution";
					cout << "\n-hw	\t\tHardware List";
				}

		cout << endl;	
	}

 

 

 

 

Ikke for å virke irriterende, men jeg trenger litt hjelp.

Problemet er bare at verken teksten eller kommandoen kommer opp.

 

:)

Endret av Eldox
Skrevet (endret)

Siden du er på linux, vurdér å bruke getopt()-funksjonen for å håndtere argumenter litt "ryddigere". Det ser også litt penere ut å bruke switch/case enn if-setninger. Innholdet i case'ne (eller if-setningene) bør skilles ut i egne metoder.

 

Edit: Manualen til getopt -> "man 3 getopt"

Endret av Sokkalf^
Skrevet (endret)

Har funnet ut problemet med den siste koden, programmet kommer seg ikke inn i den andre for-loopen, har lagt inn noe debug kode for å forsikre meg om at dette er tilfellet. Noen som klarer å se hva som er galt?

 

Geri:

Tviler meget sterkt på det :p

Endret av Eldox
Skrevet

Har nå endelig klart å løse problemene :D:D:D

 

Kode:

 

 

 

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <cstring>

#define SPACE "_____________________________________________\n\n"

using namespace std;


	int i = 1;
	int u = 0;
	int help = 0;
	int FIRST_Q = 0;
	int LAST_Q = 0;
	string KEY_STROKE;
	string POST;






	const char * TEXT[10] = {"\nMemory Info (/proc/meminfo)\n", 
					"\nNumbers of prossesors (/proc/cpuinfo)\n", 
					"\nPartition Table(/proc/partitions)\n",
					"\nRouting Table(netstat)\n",
					"\nIP confugurations (ifconfig)\n",
					"\nKernel version (uname -r)\n",
					"\nXorg Config (X -showconfig)\n",
					"\nDistrubution (/etc/issue.net)\n",
					"\nHardware List (lspci)\n",
					''};

	const char * SYSTEM[10] =  {"cat /proc/meminfo",
					   "cat /proc/cpuinfo",
					   "cat /proc/partitions",
					   "netstat -rn",
					   "ifconfig",
					   "uname -r",
					   "X -showconfig",
					   "cat /etc/issue.net",
					   "lspci",
					   ''};

	const char * COMMAND[10] = {"-m",
					   "-p",
					   "-pt",
					   "-n",
					   "-i",
					   "-k",
					   "-x",
					   "-d",
					   "-hw",
					   ''};



int main(int argc, char* argv[])
{

	system("clear");


	for (i = 1; i < argc; i++) {

		stringstream ss;

		POST.clear();
		ss << argv[i];
		ss >> POST;


		for (u = 0; u < 9; u++){


			if (POST == COMMAND[u] )
				{

					cout << TEXT[u] << SPACE;
					system(SYSTEM[u]);
					help++;
				}

			}






		FIRST_Q = argc;
			LAST_Q = argc-1;


				if(FIRST_Q >= 1 && LAST_Q < argc)
					{
						cout <<"\n\n\n" << SPACE <<"\n\33[7mPress any key(and <ENTER>) to continue\33[0m";
						cin >> KEY_STROKE;
						system("clear");
					}

	}







		if (help<1)
				{
				cout <<"\nLCS\n";
				cout <<"\nThis program is created to help you find out all about your computer.\n";
				cout <<"Syntax: lcs [-m] [-p] [-k] [-i] etc...\n";
				cout << SPACE;
					cout << "Command\t\tFunction\n";
					cout << "\n-m	 \t\tMemory";
					cout << "\n-p	 \t\tProssesor";
					cout << "\n-pt	\t\tPartition Table";
					cout << "\n-n	\tRouting Table";
					cout << "\n-i	 \t\tIp configuration";
					cout << "\n-k	 \t\tKernel Version";
					cout << "\n-x	 \t\tXorg Config";
					cout << "\n-d	 \t\tDistrobution";
					cout << "\n-hw	\t\tHardware List";
				}

		cout << endl;	
	}

 

 

 

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...