修改C语言程序的语法错误,

2024-11-23 13:42:13
推荐回答(1个)
回答1:

#include
#include

#define TMP_FILE_NAME "tmpcpu123.tmp"

#define DEBUG() (printf("%d\n",__LINE__))

int cpu_clock = 421;

void cpu_init(){}
void set_cpu_clock(int k){cpu_clock = k;}
int cpu_get_clock(){return cpu_clock;}

int main()
{
FILE *fd;
int mhz;
char command[300];
printf("CPUOver Clock\n"
"");
fd = fopen("getMhz.sh", "w");
fprintf(fd, "#!/bin/bash\n\nMHz=0\n\n"
"if [ %d -gt 420 ] ; then\n"
"\techo \"Advice:You'd better not set CPU's clock,"
" it may shutdown myself or no answer!\"\n"
"\tread -s\n\tclear\n\t"
"echo -e \"please enter a number to set CPU's clock up\"\n\t"
"read -e MHz\n\techo $MHz > %s\n"
"fi\n", cpu_get_clock(), TMP_FILE_NAME);
fclose(fd);
system("sh getMhz.sh");
fd = fopen(TMP_FILE_NAME, "r");
fscanf(fd, "%d", &mhz);
fclose(fd);
if (mhz != 0)
{
cpu_init();
set_cpu_clock(mhz);
printf("Now, CPU‘s clock is %dMHz\n",cpu_get_clock());
}
sprintf(command, "rm -f getMhz.sh %s", TMP_FILE_NAME);
system(command);
return 0;
}

这里我假定初始的cpu_clock是421,则中间生成的bash脚本如下

#!/bin/bash

MHz=0

if [ 421 -gt 420 ] ; then
echo "Advice:You'd better not set CPU's clock, it may shutdown myself or no answer!"
read -s
clear
echo -e "please enter a number to set CPU's clock up"
read -e MHz
echo $MHz > tmpcpu123.tmp
fi