我写了这段代码,使用结构用虚数做一些代数,但是当我运行它时,我得到了
我做错了什么,我尝试了所有不同的方法来获取char,我很困惑!
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
struct complex { float Rez, Imz; };
int main( )
{
char operand;
struct complex z1, z2;
printf("give the Real part of the first imaginary number z1:\n");
scanf( "%f", &z1.Rez );
printf("zreal=%f",z1.Rez);
printf("give the Imaginary part of the first imaginary number z1: \n");
scanf( "%f", &z1.Imz );
printf("give the Real part of the second imaginary number z2: \n");
scanf( "%f", &z2.Rez );
printf("give the Imaginary part of the second imaginary number z2: \n");
scanf( "%f", &z2.Imz );
do
{
printf("Give + for ADD\nGive * for MULTIPLICATION\nGive - for SUBTRACTION\nGive / for DIVISION\n");
operand=getchar();
if (operand!='+' || operand!='-' || operand!='*' || operand!='/')
printf("Wrong operation.Try again.\n");
}while(operand!='+' || operand!='-' || operand!='*' || operand!='/');
if(operand=='+')
{
printf("\nYou have chosen to add the numbers.\nThe result of the ADD is the inaginary number\n imz=(%f+%f) +(%f+%f)",z1.Rez,z2.Rez,z1.Imz,z2.Imz);
}
if(operand=='-')
{
}
if(operand=='*')
{
}
if(operand=='/')
{
}
system("pause");
return 0;
}
请提出任何建议?我被卡住了!
最佳答案
if (operand!='+' || operand!='-' || operand!='*' || operand!='/')
该语句始终是正确的,因为四个条件之一最多可能是错误的。
你应该写这个:if (operand!='+' && operand!='-' && operand!='*' && operand!='/')
while
循环的相同错误:始终为真,无限循环。
另外,您认为您的getchar
是错误的,但是您的printf没有任何参数可显示。
operand=getchar();
printf("operand=%c");
本来应该 :
operand=getchar();
printf("operand=%c", operand);