#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
int main(int argc, char* argv[])
{
int i, j, n;
char c;
char chs[255] = { 0 };
char ch[255] = { 0 };
char input[255] = { 0 };
char* yes[2] = { "y","Y" }, * no[2] = { "n","N" };
bool b = false, b2 = false;
char output[255] = { 0 };
for (i = 0; i < argc; i++)//寻找要编译的下标索引和是否需要-lm编译
{
for (j = 0; j < strlen(argv[i]); j++)
{
//查询命令行中输入的是否是.c文件名
if (*(argv[i] + j) == '.')
{
if (*(argv[i] + j + 1) == 'c')//找到就给input
{
b2 = true;
strcpy(input, argv[i]);
strcpy(output, input);
}
}
//查询命令行中是否是数字字符串
else if (*(argv[i] + j) >= 48 && *(argv[i] + j) <= 57)
{
b2 = true;
strcpy(input, argv[i]);
}
}
//查找是否有n,N,y,Y
if (!strcmp(argv[i], yes[0]) || !strcmp(argv[i], yes[1]))
{
b = true;
}
else if (!strcmp(argv[i], no[0]) || !strcmp(argv[i], no[1]))
{
b = false;
}
else//都没找到,就默认不加-lm
{
b = false;
}
}
if (!b2)//没找到有.c或数字的下标索引
{
printf("请手动输入要编译的名称:");
scanf("%s", input);
getchar();//吃掉这里多打的回车
strcpy(output, input);
}
if (input[0] >= 48 && input[0] <= 57)
{
n = atoi(input);
switch (n)
{
case 0:
strcat(chs, "t0.c");
strcpy(output, "t0.c");
break;
case 1:
strcat(chs, "t1.c");
strcpy(output, "t1.c");
break;
case 2:
strcat(chs, "t2.c");
strcpy(output, "t2.c");
break;
case 3:
strcat(chs, "t3.c");
strcpy(output, "t3.c");
break;
case 4:
strcat(chs, "t4.c");
strcpy(output, "t4.c");
break;
case 5:
strcat(chs, "t5.c");
strcpy(output, "t5.c");
break;
case 6:
strcat(chs, "t6.c");
strcpy(output, "t6.c");
break;
case 7:
strcat(chs, "t7.c");
strcpy(output, "t7.c");
break;
case 8:
strcat(chs, "t8.c");
strcpy(output, "t8.c");
break;
case 9:
strcat(chs, "t9.c");
strcpy(output, "t9.c");
break;
case 10:
strcat(chs, "test.c");
strcpy(output, "t10.c");
break;
case 11:
strcat(chs, "t11.c");
strcpy(output, "t11.c");
break;
case 12:
strcat(chs, "t12.c");
strcpy(output, "t12.c");
break;
case 13:
strcat(chs, "t13.c");
strcpy(output, "t13.c");
break;
case 14:
strcat(chs, "t14.c");
strcpy(output, "t14.c");
break;
case 15:
strcat(chs, "t15.c");
strcpy(output, "t15.c");
break;
case 16:
strcat(chs, "t16.c");
strcpy(output, "t16.c");
break;
case 17:
strcat(chs, "t17.c");
strcpy(output, "t17.c");
break;
case 18:
strcat(chs, "t18.c");
strcpy(output, "t18.c");
break;
case 19:
strcat(chs, "t19.c");
strcpy(output, "t19.c");
break;
case 20:
strcat(chs, "t20.c");
strcpy(output, "t20.c");
break;
default:
strcat(chs, "bianyi.c");
strcpy(output, "bianyi.c");
break;
}
}
else
{
strcat(chs, input);
}
//打印细节
printf("要编译的名字为:%s\n", output);
if (b)
{
printf("已加-lm进行编译!\n");
}
else
{
printf("未加-lm进行编译!\n");
}
printf("\n\n\n");
int len = strlen(chs);
strcat(ch, "gcc ");
strcat(ch, chs);
if (b)
{
strcat(ch, " -lm ");
}
strcat(ch, " -o ");
strncat(ch, chs, len - 2);
strcat(ch, " && ./");
strncat(ch, chs, len - 2);
system(ch);
return 0;
}