原文内容来自免费考研论坛,请点击查看全文
http://bbs.freekaoyan.com/viewthread.php?tid=244859
———过滤广告线
———过滤广告线
———过滤广告线
———过滤广告线
———过滤广告线
———过滤广告线
若你有什么具体不明白的问题,可以发在这个帖子里面
呵呵 请勿新开帖[我错了 - - 有需求的可开新帖,如果问题有代表性还有奖励 - -]
[ 本帖最后由 沦陷在2009 于 2008-3-25 20:47 编辑 ]
---------------------------------
/*设计一个计算 、-、*、/的程序
程序循环从标准输入读入表达式:
输入: 4 2 回车
则输出:4 2=6
输入: 4*2 回车
则输出:4*2=8
………
当用户输入:000回车时程序退出
实现要求:
1、有输入提示和输出提示,如要输入表达式时,可以提示: "pealse intput arithmetic expression: " 输出时,提示:
" 4 2=6"。 计算在用户输入的表达式为 0 0 0 时程序结束。
2、为你的程序加上注释,使得其清晰可读。
3、尝试利用调试程序来修改你程序的逻辑错误。 */
#include <iostream>
using namespace std;
void main()
{
long double opd1,opd2,result;
long int op;
cout << "pealse intput arithmetic expression: " ;
cin >> opd1 >> op >> opd2 ;
if( opd1!=0 && op!=0 && opd2!=0 )
{
if (op == ' ' || op == '-' || op == '*' || op == '/' )
{
switch(op)
{
case ' ':
result = opd1 opd2;
cout<<opd1<< op << opd2<<"="<<result<<endl;
break;
case '-':
result = opd1 - opd2;
cout<<opd1<< op << opd2<<"="<<result<<endl;
break;
case '*':
result = opd1 * opd2;
cout<<opd1<< op << opd2<<"="<<result<<endl;
break;
case '/':
result = opd1 / opd2;
cout<<opd1<< op << opd2<<"="<<result<<endl;
break;
default : cout <<endl;
}
}
else cout << "This is not a arithmetic expression ! " ;
}
else cout <<"quit the program!"<<endl;
}
我不知道到底哪儿出问题了 程序编译没有错误 可是没能达到题目要求 呵呵
后续:问题已解决,只需将long int op;改成char op;便可
但是我不是怎么明白为什么改了char型后就可以了 哪位高人指教一下
[ 本帖最后由 沦陷在2009 于 2008-3-19 22:13 编辑 ]
---------------------------------
回复二楼:
1)对连续输入三个数(double,int),需要用空格或回车分开,要不不知道在哪断开(除非遇到不同类型的).
2) ,-,*,/ 属于字符,不可以用数字int,(可以用字符char或字符串string),否则输入时会匹配跳过或出错.当你输入4 2时,匹配到 时,会把4当成第一个double,然后 与int不匹配,跳过,后一个为2,会把第二个值赋为2(在VC6这样,估计是用了纠错功能,不过好像只往前看一步),继续往后,没有输入了,会等待你输入第三个数.
---------------------------------
要求是输入一个3×4的数组,然后再逆序输出,然后就是控制只能输入12个数,输完12个数后自动逆序输出12个数;
以下是没有加入控制输入的语句,烦高人指点:
【小贴士:复制整段代码,然后粘帖到VC 中去,选定全部代码,再 ALT F8 系统会帮你自动排版好】
#include <iostream>
using namespace std;
int main()
{
int i,j;
int a[3][4] ;
for (i=0;i<3;i )
{
for (j=0;j<4;j )
{
cin >> a[j];
}
}
for (i=2;i>=0;i--)
{
for (j=3;j>=0;j--)
{
cout << a[j] << " " ;
}
}
return 0;
}
---------------------------------
#include <iostream.h>
void main()
{
double opd1,opd2,result;
char op;
cout << "please input the first number : " ;
cin >> opd1 ;
cout<<"please input arithmetic operator( 、_、*、/):";
cin>> op;
cout << "please input the second number : " ;
cin >> opd2 ;
if( opd1!=0 && opd2!=0 )
{
if (op == ' ' || op == '-' || op == '*' || op == '/' )
{
switch(op)
{
case ' ':
result = opd1 opd2;
cout<<opd1<< op << opd2<<"="<<result<<endl;
break;
case '-':
result = opd1 - opd2;
cout<<opd1<< op << opd2<<"="<<result<<endl;
break;
case '*':
result = opd1 * opd2;
cout<<opd1<< op << opd2<<"="<<result<<endl;
break;
case '/':
result = opd1 / opd2;
cout<<opd1<< op << opd2<<"="<<result<<endl;
break;
default : cout <<endl;
}
}
else cout << "This is not a arithmetic expression ! "<<endl;
}
}
---------------------------------
很好的贴子,很好的问题。
---------------------------------
我说一点,关于做表达式的那个题目:
输入的表达式我们可以把它做为一个字符串来处理,我们可以通过math.h文件里边的函数来识别数字与符号;
---------------------------------
C 忘光了.逆序输出的那个基本就那个程序.我稍微的补了点.不知道对不对.
#include <iostream>
using namespace std;
int main()
{
int i,j;
int a[3][4] ;
for (i=0;i<3;i )
{
Cout<<”input number of the lines ”<<i<<endl;
for (j=0;j<4;j )
{
cin >> a[j];
}
}
cout<<”output the arry”<<endl;
for (i=2;i>=0;i--)
{
for (j=3;j>=0;j--)
{
cout << a[j] << " " ;
}
}
return 0;
}
---------------------------------
是a[j]
具体题目讨论帖
沦陷在2009 免费考研论坛/2008-03-19
相关话题/
领限时大额优惠券,享本站正版考研考试资料!
优惠券领取后72小时内有效,10万种最新考研考试考证类电子打印资料任你选。涵盖全国500余所院校考研专业课、200多种职业资格考试、1100多种经典教材,产品类型包含电子书、题库、全套资料以及视频,无论您是考研复习、考证刷题,还是考前冲刺等,不同类型的产品可满足您学习上的不同需求。 ...考试优惠券 本站小编 Free壹佰分学习网 2022-09-19
Free考研考试FreeKaoYan.Com
欢迎来到Free考研考试,"为实现人生的Free而奋斗"
© 2020 FreeKaoYan! . All rights reserved.
