当前位置: 代码迷 >> C语言 >> [求助]请教一个命令行的问题!
  详细解决方案

[求助]请教一个命令行的问题!

热度:205   发布时间:2006-11-30 18:10:30.0

#include <stdio.h>
#include <string.h>
#define N 1000

int main(void)
{
char ch, str[N];
int i = 0;

while(1)
{
ch = getchar();
while(ch != EOF)
{
str[i] = ch;
i ++;
if(ch == '\n')
break;
ch = getchar();
}
if(ch == EOF)
break;
else if(str[5] == '-')
printf("I am showing help.\n");
else if(str[5] == 'e'||str[5] == 'o')
printf("I am a c89 preprocessor + compiler + linker. I start from \"hello.c\" and end with \"a.exe\".\n");
else if(str[5] == 'c')
printf("I am a c89 preprocessor + compiler. I start from \"hello.c\" and end with \"hello.o\".\n");
else if(str[5] == 'E')
printf("I am a c89 preprocessor. I start from \"hello.c\" and end with \"expanded.c\".\n");
else
{
if(str[14] == 'e')
printf("I am a c99 preprocessor + compiler + linker. I start from \"hello.c\" and end with \"a.exe\".\n");
else if(str[14] == 'c')
printf("I am a c99 preprocessor + compiler. I start from \"hello.c\" and end with \"hello.o\".\n");
else if(str[14] == 'o')
printf("I am a c99 preprocessor. I start from \"hello.c\" and end with \"foobar.c\".\n");
}
i = 0;
}

return 0;

}
这是我写的一段代码!我知道有很多错误!


----------------解决方案--------------------------------------------------------
gcc -std=c99 -o foobar.c -E hello.c在这里我糊涂了!请说明一下!谢谢了!

----------------解决方案--------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1000

int main(void)
{
char str[N], ch;
int i = 0;
int string(char str[N]);

while(1)
{
ch = getchar();
while(ch != EOF)
{
str[i] = ch;
i ++;
ch = getchar();
if(ch == '\n')
break;
}
if(ch == EOF)
break;
else
string(str);
i = 0;
}

return 0;
}

int string(char str[N])
{
int i, length ,word = 0;

length = strlen(str);
for(i = 0;i < length;i ++)
{
if(str[5] == '-'&&word == 0)
{
printf("I am showing help.\n");
word = 1;
}
else if(str[5] == 's')
{
if((strcmp(&str[i], "-") == 0)&&(strcmp(&str[i+1], "E") == 0)&&word == 0)
{
printf("I am a c99 preprocessor. I start from \"hello.c\" and end with \"foobar.c\".\n");
word = 1;
}


}
}

return 0;
}

我想问一下!为什么红色部分的总也不执行啊?


----------------解决方案--------------------------------------------------------

还是有错误啊!我无语!看来只能得60分了!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1000

int main(void)
{
char str[N], ch;
int i = 0;
int string(char str[N], int k);

while(1)
{
str[N] = 'a';
ch = getchar();
while(ch != EOF)
{
str[i] = ch;
i ++;
ch = getchar();
if(ch == '\n')
break;
}
if(ch == EOF)
break;
else
string(str, i - 1);
i = 0;
}

return 0;

}

int string(char str[N], int k)
{
int i, word = 0, j, n = 0, m = 0;

if(str[5] == 's')
{
for(i = 0;i < k;i ++)
{
if(str[i] == '-'&&str[i + 1] == 'o')
{
for(j = 0;j < k;j ++)
{
if(str[j] == '-'&&str[j + 1] == 'E'&&word == 0)
{
printf("I am a c99 preprocessor. I start from \"hello.c\" and end with \"expanded.c\".\n");
word = 1;
}
else if(str[j] == '-'&&str[j + 1] == 'c'&&word == 0)
{
printf("I am a c99 preprocessor + compiler. I start from \"hello.c\" and end with \"hello.o\".\n");
word = 1;
}
else if(word == 0)
{
m ++;
}
}
}
}
if(word == 0)
{
printf("I am a c99 preprocessor + compiler + linker. I start from \"hello.c\" and end with \"a.exe\".\n");
word = 1;
}
else if(m == k)
{
printf("I am a c99 preprocessor + compiler + linker. I start from \"hello.c\" and end with \"hello.exe\".\n");
}

}
else if(str[5] == '-')
{
printf("I am showing help.\n");
}
else if(str[4] != '-')
{
printf("I am a c89 preprocessor + compiler + linker. I start from \"hello.c\" and end with \"a.exe\".\n");
}
else
{
for(i = 0;i < k;i ++)
{
if(str[i] == '-'&&str[i + 1] == 'o')
{
for(j = 0;j < k;j ++)
{
if(str[j] == '-'&&str[j + 1] == 'E'&&word == 0)
{
printf("I am a c89 preprocessor. I start from \"hello.c\" and end with \"expanded.c\".\n");
word = 1;
}
else if(str[j] == '-'&&str[j + 1] == 'c'&&word == 0)
{
printf("I am a c89 preprocessor + compiler. I start from \"hello.c\" and end with \"hello.o\".\n");
word = 1;
}
else if(word == 0)
{
n ++;
}
}
}
}
if(n == k)
{
printf("I am a c89 preprocessor + compiler + linker. I start from \"hello.c\" and end with \"hello.exe\".\n");
}
}

return 0;

}


----------------解决方案--------------------------------------------------------
  相关解决方案