Objective-C 中带有嵌套开关的 While 循环
我刚刚开始学习 Objective-C。我正在做一项标准计算器练习。它应该创建一个加法机(例如,输入运算符和数字,每次显示结果)。但我搞砸了一些事情,我认为这与我使用“char”数据类型有关。
这是代码,只是程序部分(界面和实现很简单,并且可以在另一个版本中使用;不过,如果有人想查看它们,请询问):
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
double value1 = 0.0;
char operator = 'a';
Calculator *deskCalc = [[Calculator alloc] init];
NSLog(@"Initial value?");
scanf("%lf",&value1);
[deskCalc setAccumulator: value1];
while (operator != 'e') {
{
NSLog(@"Operation and value?");
scanf("%c %lf", &operator, &value1);
}
switch (operator){
case '+':
[deskCalc add: value1];
NSLog(@"%f", [deskCalc accumulator]);
break;
case '-':
[deskCalc subtract: value1];
NSLog(@"%f", [deskCalc accumulator]);
break;
case '*':
case 'x':
[deskCalc multiply: value1];
NSLog(@"%f", [deskCalc accumulator]);
break;
case '/':
if (value1 != 0) {
[deskCalc divide: value1];
NSLog(@"%f", [deskCalc accumulator]);
}
else {
NSLog(@"Division by zero not allowed.");
NSLog(@"%f", [deskCalc accumulator]);
}
break;
case 's':
[deskCalc setAccumulator: value1];
NSLog(@"%f", [deskCalc accumulator]);
break;
case 'e':
NSLog(@"Done, sucker, final answer: %f.", value1);
default:
NSLog(@"Unknown operator.");
NSLog(@"%f", [deskCalc accumulator]);
break;
}
}
[deskCalc release];
[pool drain];
return 0;
}
并且,如果它有帮助,这是我运行时从终端得到的内容这:
从终端:
初始值?
12 // 我的输入
操作和价值?
x 4 // 我的输入
未知操作员。
运营和价值? // 它不会停止请求输入。
48.000000
操作和价值?
帮助和感谢。
PS 我知道这个程序相当笨重。怜悯我,我学的最后一门语言是我的 Coleco Adam 上的 Basic,它有一个磁带驱动器——是的,就像盒式磁带一样,这太棒了。
I've just started learning Objective-C. I'm doing one of the standard calculator exercises. It's supposed to create an adding machine (e.g., input the operator and number, display the result each time). But I messed up something, and I think it has to do with my use of the "char" data type.
Here's the code, just the program section (interface and implementation are straightforward and worked in another version; still, if anyone wants to see them, just ask):
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
double value1 = 0.0;
char operator = 'a';
Calculator *deskCalc = [[Calculator alloc] init];
NSLog(@"Initial value?");
scanf("%lf",&value1);
[deskCalc setAccumulator: value1];
while (operator != 'e') {
{
NSLog(@"Operation and value?");
scanf("%c %lf", &operator, &value1);
}
switch (operator){
case '+':
[deskCalc add: value1];
NSLog(@"%f", [deskCalc accumulator]);
break;
case '-':
[deskCalc subtract: value1];
NSLog(@"%f", [deskCalc accumulator]);
break;
case '*':
case 'x':
[deskCalc multiply: value1];
NSLog(@"%f", [deskCalc accumulator]);
break;
case '/':
if (value1 != 0) {
[deskCalc divide: value1];
NSLog(@"%f", [deskCalc accumulator]);
}
else {
NSLog(@"Division by zero not allowed.");
NSLog(@"%f", [deskCalc accumulator]);
}
break;
case 's':
[deskCalc setAccumulator: value1];
NSLog(@"%f", [deskCalc accumulator]);
break;
case 'e':
NSLog(@"Done, sucker, final answer: %f.", value1);
default:
NSLog(@"Unknown operator.");
NSLog(@"%f", [deskCalc accumulator]);
break;
}
}
[deskCalc release];
[pool drain];
return 0;
}
And, in case it helps, here's what I get from the terminal when I run this:
From the terminal:
Initial value?
12 // My input
Operation and value?
x 4 // My input
Unknown operator.
Operation and value? // It doesn't stop to ask for input.
48.000000
Operation and value?
Help and thanks.
P.S. I know the program is pretty clunky. Have mercy, the last language I learned was Basic on my Coleco Adam, which had a tape drive--yeah, like a cassette tape, which was awesome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在左引号和
%c
之间放置一个空格,以捕获 stdin 上剩余的任何空格。Put a space between the open quotes and the
%c
, to trap any whitespace that is remaining on stdin.operator
大部分是\n
。您可能需要执行flushall()
。operator
is mostly\n
. You will probably need to do aflushall()
.