Skip to content

Commit 74990c0

Browse files
authored
最適化とC++への置換
1 parent 2c8c3c0 commit 74990c0

1 file changed

Lines changed: 48 additions & 54 deletions

File tree

kryptinfo.c

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,54 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <string.h>
1+
#include <iostream>
2+
#include <string>
3+
#include <unordered_map>
4+
#include <cstdlib>
45

5-
int main(void)
6-
{
7-
int intanswer;
8-
int options; // システムの情報を表示するプログラム
9-
char answer[1024]; // コマンドを選ぶ選択肢 char配列なのは数字以外の文字が入力されてもバグを起こさないようにするため
10-
char yesno[1024]; // こちらは最後にもう一度選ぶかどうかをyかnで答える
11-
do {
12-
do {
13-
options = 0;
14-
// 選択肢を表示
15-
fputs("\n\n\n",stdout);
16-
printf("1:dmesg as superuser\n2:lsb_release -a\n3:lspci\n4:uname -a\n5:fastfetch");
17-
fputs("\n",stdout);
18-
//プロンプトを表示
19-
printf("選んでください> ");
20-
fflush(stdout); // 場合によって表示されないため fflush()を使用
6+
void executeCommand(const std::string& command) {
7+
int result = std::system(command.c_str());
8+
if (result != 0) {
9+
std::cerr << "Failed to execute the command: " << command << std::endl;
10+
}
11+
}
12+
13+
void displayMenu() {
14+
std::cout << "\n\n\n\t==== System info ====" << std::endl;
15+
std::cout << "\t1: dmesg as superuser" << std::endl;
16+
std::cout << "\t2: lsb_release -a" << std::endl;
17+
std::cout << "\t3: lspci" << std::endl;
18+
std::cout << "\t4: uname -a" << std::endl;
19+
std::cout << "\t5: fastfetch" << std::endl;
20+
std::cout << "\t0: exit" << std::endl;
21+
std::cout << "\t=====================" << std::endl;
22+
}
23+
24+
int main() {
25+
std::unordered_map<std::string, std::string> commands = {
26+
{"1", "sudo dmesg"},
27+
{"2", "lsb_release -a"},
28+
{"3", "lspci"},
29+
{"4", "uname -a"},
30+
{"5", "fastfetch"}
31+
};
2132

22-
scanf("%1023s%*[^\n]%*c",answer); // 安全第一
33+
std::string choice;
2334

24-
if (strcmp(answer,"1") == 0 || strcmp(answer,"2") == 0 || strcmp(answer,"3") == 0 || strcmp(answer,"4") == 0 || strcmp(answer,"5") ==0) {
25-
; // Do nothing
26-
} else {
27-
printf("1~5以外を指定しています\n");
28-
options = 1;
29-
}
30-
} while(options != 0);
31-
intanswer = atoi(answer);
32-
33-
switch(intanswer) {
34-
case 1:
35-
system("sudo dmesg");
36-
break;
37-
case 2:
38-
system("lsb_release -a");
39-
break;
40-
case 3:
41-
system("lspci");
42-
break;
43-
case 4:
44-
system("uname -a");
45-
break;
46-
case 5:
47-
system("fastfetch");
48-
break;
49-
default:
50-
fputs("Unknown error!! \n",stderr);
51-
break;
35+
do {
36+
displayMenu();
37+
std::cout << "> ";
38+
std::cin >> choice;
39+
40+
if (choice == "0") {
41+
std::cout << "\nexit the program." << std::endl;
42+
break;
5243
}
53-
54-
printf("\n\nもう一度選びますか?(Y or n)> ");
55-
scanf("%1023s%*[^\n]%*c",yesno);
56-
fputs("\n",stdout);
57-
} while(strcmp(yesno,"n"));
58-
44+
45+
if (commands.find(choice) != commands.end()) {
46+
executeCommand(commands[choice]);
47+
} else {
48+
std::cout << "\nIt is an invalid option. Please specify 1~5 or 0.\n";
49+
}
50+
51+
} while (true);
52+
5953
return 0;
6054
}

0 commit comments

Comments
 (0)