Kaptein... Skrevet 4. mars 2009 Skrevet 4. mars 2009 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; }
GeirGrusom Skrevet 4. mars 2009 Skrevet 4. mars 2009 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.
Mr.Garibaldi Skrevet 4. mars 2009 Skrevet 4. mars 2009 (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 4. mars 2009 av Mr.Garibaldi
Kaptein... Skrevet 5. mars 2009 Forfatter Skrevet 5. mars 2009 Takker for svar og innspill geir og Garibaldi. Får bla opp i en bok etter noen ukjentheter jeg oppdaget i Geir sin kode
Kaptein... Skrevet 5. mars 2009 Forfatter Skrevet 5. mars 2009 (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 5. mars 2009 av Eldox
GeirGrusom Skrevet 5. mars 2009 Skrevet 5. mars 2009 Litt off-topic: trenger du alle de include filene dine? string.h og stdio.h er vel unødvendig når du bruker stl?
Sokkalf™ Skrevet 5. mars 2009 Skrevet 5. mars 2009 (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 5. mars 2009 av Sokkalf^
Kaptein... Skrevet 5. mars 2009 Forfatter Skrevet 5. mars 2009 (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 Endret 5. mars 2009 av Eldox
Kaptein... Skrevet 5. mars 2009 Forfatter Skrevet 5. mars 2009 Har nå endelig klart å løse problemene :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; }
Anbefalte innlegg
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 kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå