如何实现算式( 如:(1+2)*3-4 )整体的输入输出?

来源:百度知道 编辑:UC知道 时间:2024/06/17 00:11:56
如何用scanf(),getchar()/printf(),putchar()等对算式等混合类型的整体输入/输出?

char s[20];
scanf("%s",s);/*采用scanf()整体输入.注意用%s的话不加&取址符,因为s本身就是字符数组首地址*/
printf("%s",s);//采用printf()整体输出

char s[20];
for(int i=0;i<20;i++)
getchar(s[i]);//由于getchar()只能单个接收字符,所以用循环逐个字符输入
for(int i=0;i<20;i++)
putchar(a[i]);//由于putchar()只能单个输出字符,所以用循环逐个字符输出

char s[20];
gets(s);//gets()可以接收字符串
puts(s);//puts()可以输出字符串

对数字作为常量处理,运算符作为符号处理。c++中特别简单:
cout<<"1+2)*3-4"<<endl;

#include <stdio.h>

int main()
{
char p[10];
scanf("%s", &p);
getchar(p);

printf("%s\n", p);

return 0;
}