第一题
# include <iostream.h>
void main()
{
int a=0,x=0;
for(;!x&&a<10;a++)
{ a++;
x++;
}
cout<<a<<endl;
}
试卷上有上面这么一道题,但是,我的visual studio说不认识# include <iostream.h>还有最后一句cout<<a<<endl的couh和endl未定义
请问这是怎么回事,我也是这两个地方懂不起,哪位朋友帮我一下嘛
第二题
# include <stdio.h>
void fun(char *str)
{
int i,j=0;
for(i=0;str[i]!='\0';i++)
if(str[i]!=' ') str[j++]=str[i];
str[j]='\0';
}
void main()
{
char str[80];
int n;
printf("Input a string:");
gets(str);
fun(str);
printf("%s\n",str);
}
这个程序输入asd ab 123求运行结果
我认为是执行完for循环后j=2,所以,str[2]的值为\0,对于字符数组,‘\0’代表结束,所以最后的答案是as
但是执行完后结果是asdab123,非常不解,
第三题
# include <stdio.h>
char *cat(char *str1,char *str2)
{
char *pt;
for(pt=str1;*pt!='\0';pt++)
while (*str2!='\0')
*pt++=*str2++;
return (pt);
}
void main()
{
char*p,s1[30]="210",s2[30]="45";
p=cat(s1,s2);
printf("%d\n",p-s1);
p=cat(s1,s2);
printf("%d\n",p-s1);
}
此题根本没有读懂,第一:在for循环中,运用了指针改变了数组中元素的值,我认为第一次调用cat函数*str2=4,它将值赋予*str1,然后完成自加,其中pt自加了两次,所以*pt=str1[2],然后第二次循*pt变成了str1[4]然后跳出for循环,
str1[4]的值是不确定的,最后的语句
p=cat(s1,s2);
printf("%d\n",p-s1);
p=cat(s1,s2);
printf("%d\n",p-s1);
完全不知道p的值和s1的值,因为我觉得被指针改了,
还有printf里面为什么是p-s1,我觉得应该是*p-*s1,非常不解,
希望高手详细点,而且快二级考试了,很急,真的,谢谢各位了
只有5分了,以后再答谢