15.这个1+1写了 半天 也没调试出来 5555
#include "stdio.h"
int numstack[100];
char symbolstack[100];
int ntop=0,stop=0;
int exe(int a,char c,int b) /*最小单位的计算*/
{
switch(c)
{
case '+':return (a+b);
case '-':return (a-b);
case '*':return (a*b);
case '/':return (a/b);
}
}
char compare(char a,char b) /*前后2个符号判断,是出栈还是入栈*/
{switch (a) {
case '*':
case '/': if(b=='(') return('<');
else return('>');
case '-':
case '+': if(b=='+'||b=='-'|| b==')'|| b=='#') return('>');
else return('<');
case '(': if(b==')') return('=');
else return('<');
case ')': return('>');
case '#': if(b=='#') return('=');
else return('<');
} /*switch*/
}
main()
{
char c,result;
symbolstack[stop++]='#';
printf("input your expression!/n");
c=getchar();
while(c!='#')
{
if(c>='0')
{int temp=0;printf("/n1111%c/n",c);
while(c>='0') /*数字入栈*/
{printf("/n33333%c/n",c);temp=temp*10+(c-'0');
c=getchar();
}
numstack[ntop++]=temp;
}
else /*符号入栈*/
{printf("/n2222%c/n",c);
result=compare(symbolstack[stop],c); /*前后两个符号做比较从而判断具体操作*/
switch(result)
{
int a,b,r;
char c;
case '>':
{
a=numstack[ntop--];
b=numstack[ntop--];
c=symbolstack[stop--];
r=exe(a,c,b);
numstack[ntop++] =r;
c=getchar();
break;}
case '<':
{
symbolstack[stop++]=c;
c=getchar();
break;
}
case '=':
{
a=numstack[ntop--];
b=numstack[ntop--];
r=exe(a,c,b);
numstack[ntop++] =r;
c=getchar();
break;}
}
}
}
printf("/nthe final result is %d/n",numstack[ntop]);
getch();
}
16.
main()主函数的返回值是什么
我看有的书这样写:
int main()
{
}
而有的书这样写:
void main()
{
}
到底哪个正确? 还是两个都正确?
int main(void);这个符合标准。
return 0;表示程序正常退出。其他值表示有错误退出。
这个返回值是给操作系统看的。
18.malloc 获得的内存空间,
即使是作用域范围超出了,
也不会被回收。
(局部指针会被销毁,但是指针指向的 malloc 空间不会, 造成内存泄漏)
虽然在程序结束的时候,
系统会回收所有这个程序用到的内存,
但是如果要等到这个时候,
可能严重的问题已经发生了 ,所以,malloc后free 是必须的。