C语言问题 如果输入复数为!+2iagain1下面的判断复数格式是否正确的while会一直循环,怎么解决
#include<stdio.h>structnum{doublereal;doubleimag;};intmain(){voidAdd(double,double,do...
#include<stdio.h>
struct num
{
double real;
double imag;
};
int main()
{
void Add(double, double, double, double);
void Red(double, double, double, double);
void Mul(double, double, double, double);
void Div(double, double, double, double);
void wrongchoice();
void wrongformat();
int choice;
double a, b;
char c, d;
again:
printf("请选择以下功能:\n");
printf("1.两复数相加\n");
printf("2.两复数相减\n");
printf("3.两复数相乘\n");
printf("4.两复数相除\n");
printf("5.退出程序\n");
scanf("%d", &choice);
while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5)
{
wrongchoice();
goto again;
}
again1:
printf("请输入第一个复数(例如:1+2i):\n");
scanf("%lf%c%lf%c", &a,&c,&b,&d);
while (!scanf("%lf%c%lf%c", &a, &c, &b, &d) || c != '+' || d != 'i')
{
wrongformat();
goto again1;
}
struct num num1 = { a,b };
printf("第一个复数的实部为:%lf\n", num1.real);
printf("第一个复数的虚部为:%lf\n", num1.imag);
again2:
printf("请输入第二个复数(例如:1+2i):\n");
scanf("%lf%c%lf%c", &a, &c, &b, &d);
while (!scanf("%lf%c%lf%c", &a, &c, &b, &d) || c != '+' || d != 'i')
{
wrongformat();
goto again2;
}
struct num num2 = { a,b };
printf("第二个复数的实部为:%lf\n", num2.real);
printf("第二个复数的虚部为:%lf\n", num2.imag);
if (choice == 1)
Add(num1.real, num1.imag, num2.real, num2.imag);
else
if (choice == 2)
Red(num1.real, num1.imag, num2.real, num2.imag);
else
if (choice == 3)
Mul(num1.real, num1.imag, num2.real, num2.imag);
else
if (choice == 4)
Div(num1.real, num1.imag, num2.real, num2.imag);
else
goto end;
goto again;
end:
return 0;
}
void wrongchoice()
{
printf("您输入了一个不存在的选项,请重新选择\n");
}
void wrongformat()
{
printf("您输入的复数格式有误,请按照格式再次输入(例如:1+2i)\n");
}
void Add(double x, double e, double y, double f)
{
double r, i;
r = x + y;
i = e + f;
printf("运算结果为:%lf + %lfi\n", r, i);
printf("结果的实部为:%lf\n", r);
printf("结果的虚部为:%lf\n", i);
}
void Red(double x, double e, double y, double f)
{
double r, i;
r = x - y;
i = e - f;
printf("运算结果为:%lf + %lfi\n", r, i);
printf("结果的实部为:%lf\n", r);
printf("结果的虚部为:%lf\n", i);
}
void Mul(double x, double e, double y, double f)
{
double r, i;
r = x*y - e*f;
i = e*y + x*f;
printf("运算结果为:%lf + %lfi\n", r, i);
printf("结果的实部为:%lf\n", r);
printf("结果的虚部为:%lf\n", i);
}
void Div(double x, double e, double y, double f)
{
double r, i;
r = (x*y - e*f) / (y*y + f*f);
i = (e*y + x*f) / (y*y + f*f);
printf("运算结果为:%lf + %lfi\n", r, i);
printf("结果的实部为:%lf\n", r);
printf("结果的虚部为:%lf\n", i);
}
就是输入一个错误复数时(!+2i),again1下面的判断复数格式的循环就会一直循环这个”您输入的复数格式有误,请按照格式再次输入(例如:1+2i)”,怎么解决,或者说怎么判断用户输入的复数格式是否正确 展开
struct num
{
double real;
double imag;
};
int main()
{
void Add(double, double, double, double);
void Red(double, double, double, double);
void Mul(double, double, double, double);
void Div(double, double, double, double);
void wrongchoice();
void wrongformat();
int choice;
double a, b;
char c, d;
again:
printf("请选择以下功能:\n");
printf("1.两复数相加\n");
printf("2.两复数相减\n");
printf("3.两复数相乘\n");
printf("4.两复数相除\n");
printf("5.退出程序\n");
scanf("%d", &choice);
while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5)
{
wrongchoice();
goto again;
}
again1:
printf("请输入第一个复数(例如:1+2i):\n");
scanf("%lf%c%lf%c", &a,&c,&b,&d);
while (!scanf("%lf%c%lf%c", &a, &c, &b, &d) || c != '+' || d != 'i')
{
wrongformat();
goto again1;
}
struct num num1 = { a,b };
printf("第一个复数的实部为:%lf\n", num1.real);
printf("第一个复数的虚部为:%lf\n", num1.imag);
again2:
printf("请输入第二个复数(例如:1+2i):\n");
scanf("%lf%c%lf%c", &a, &c, &b, &d);
while (!scanf("%lf%c%lf%c", &a, &c, &b, &d) || c != '+' || d != 'i')
{
wrongformat();
goto again2;
}
struct num num2 = { a,b };
printf("第二个复数的实部为:%lf\n", num2.real);
printf("第二个复数的虚部为:%lf\n", num2.imag);
if (choice == 1)
Add(num1.real, num1.imag, num2.real, num2.imag);
else
if (choice == 2)
Red(num1.real, num1.imag, num2.real, num2.imag);
else
if (choice == 3)
Mul(num1.real, num1.imag, num2.real, num2.imag);
else
if (choice == 4)
Div(num1.real, num1.imag, num2.real, num2.imag);
else
goto end;
goto again;
end:
return 0;
}
void wrongchoice()
{
printf("您输入了一个不存在的选项,请重新选择\n");
}
void wrongformat()
{
printf("您输入的复数格式有误,请按照格式再次输入(例如:1+2i)\n");
}
void Add(double x, double e, double y, double f)
{
double r, i;
r = x + y;
i = e + f;
printf("运算结果为:%lf + %lfi\n", r, i);
printf("结果的实部为:%lf\n", r);
printf("结果的虚部为:%lf\n", i);
}
void Red(double x, double e, double y, double f)
{
double r, i;
r = x - y;
i = e - f;
printf("运算结果为:%lf + %lfi\n", r, i);
printf("结果的实部为:%lf\n", r);
printf("结果的虚部为:%lf\n", i);
}
void Mul(double x, double e, double y, double f)
{
double r, i;
r = x*y - e*f;
i = e*y + x*f;
printf("运算结果为:%lf + %lfi\n", r, i);
printf("结果的实部为:%lf\n", r);
printf("结果的虚部为:%lf\n", i);
}
void Div(double x, double e, double y, double f)
{
double r, i;
r = (x*y - e*f) / (y*y + f*f);
i = (e*y + x*f) / (y*y + f*f);
printf("运算结果为:%lf + %lfi\n", r, i);
printf("结果的实部为:%lf\n", r);
printf("结果的虚部为:%lf\n", i);
}
就是输入一个错误复数时(!+2i),again1下面的判断复数格式的循环就会一直循环这个”您输入的复数格式有误,请按照格式再次输入(例如:1+2i)”,怎么解决,或者说怎么判断用户输入的复数格式是否正确 展开
1个回答
展开全部
c语言经典100题:【程序1】题目:有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数都是多少1.程序分析:可填在百位,十位,个位的数字都是1,2,3,4.组成所有的排列后再去掉不满足条件的排列.2.程序源代码:main(){inti,j,k;printf("\n");for(i1;i2)/*如果是闰年且月份大于2,总天数应该加一天*/sum++;printf("Itisthe%dthday.",sum);}【程序5】题目:输入三个整数x,y,z,请把这三个数由小到大输出.1.程序分析:我们想法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小.2.程序源代码:main(){intx,y,z,t;scanf("%d%d%d",&x,&y,&z);if(x>y){tx;xy;yt;}/*交换x,y的值*/if(x>z){tz;zx;xt;}/*交换x,z的值*/if(y>z){ty;yz;zt;}/*交换z,y的值*/printf("smalltobig:%d%d%d\n",x,y,z);}【程序6】题目:用*号输出字母c的图案.1.程序分析:可先用'*'号在纸上写出字母c,再分行输出.2.程序源代码:#include"stdio.h"main(){printf("helloc-world!\n");printf("****\n");printf("*\n");printf("*\n");printf("****\n");}【程序7】题目:输出特殊图案,请在c环境中运行,看一看,verybeautiful!1.程序分析:字符共有256个.不同字符,图形不一样.2.程序源代码:#include"stdio.h"main(){chara176,b219;printf("%c%c%c%c%c\n",b,a,a,a,b);printf("%c%c%c%c%c\n",a,b,a,b,a);printf("%c%c%c%c%c\n",a,a,b,a,a);printf("%c%c%c%c%c\n",a,b,a,b,a);printf("%c%c%c%c%c\n",b,a,a,a,b);}【程序8】题目:输出9*9口诀.1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列.2.程序源代码:#include"stdio.h"main(){inti,j,result;printf("\n");for(i1;ib)a:b这是条件运算符的基本例子.2.程序源代码:main(){intscore;chargrade;printf("pleaseinputascore\n");scanf("%d",&score);gradescore>90'a':(score>60'b':'c');printf("%dbelongsto%c",score,grade);}【程序15】题目:利用条件运算符的嵌套来完成此题:学习成绩>90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。1.程序分析:(a>b)?a:b这是条件运算符的基本例子。2.程序源代码:main(){intscore;chargrade;printf("pleaseinputascore\n");scanf("%d",&score);gradescore>90?'A':(score>60?'B':'C');printf("%dbelongsto%c",score,grade);}【程序16】题目:输入两个正整数m和n,求其最大公约数和最小公倍数.1.程序分析:利用辗除法.2.程序源代码:main(){inta,b,num1,num2,temp;printf("pleaseinputtwonumbers:\n");scanf("%d,%d",&num1,&num2);if(num1{tempnum1;num1num2;num2temp;}anum1;bnum2;while(b!0)/*利用辗除法,直到b为0为止*/{tempa%b;ab;btemp;}printf("gongyueshu:%d\n",a);printf("gongbeishu:%d\n",num1*num2/a);}【程序17】题目:输入一行字符,分别统计出其中英文字母,空格,数字和其它字符的个数.1.程序分析:利用while语句,条件为输入的字符不为'\n'.2.程序源代码:#include"stdio.h"main(){charc;intletters0,space0,digit0,others0;printf("pleaseinputsomecharacters\n");while((cgetchar())!'\n'){if(c>'a'&&c'a'&&c'0'&&c0){x1(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/x2x1;day--;}printf("thetotalis%d\n",x1);}【程序22】题目:两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人.已抽签决定比赛名单.有人向队员打听比赛的名单.a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单.1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数.2.程序源代码:main(){chari,j,k;/*i是a的对手,j是b的对手,k是c的对手*/for(i'x';ia[j])minj;tema[i];a[i]a[min];a[min]tem;}/*outputdata*/printf("Aftersorted\n");for(i0;iprintf("%5d",a[i]);}【程序38】题目:求一个3*3矩阵对角线元素之和1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出.2.程序源代码:main(){floata[3][3],sum0;inti,j;printf("pleaseinputrectangleelement:\n");for(i0;i50)againtrue;elseagainfalse;}}【程序46】题目:宏#define命令练习(1)程序源代码:#include"stdio.h"#defineTRUE1#defineFALSE0#defineSQ(x)(x)*(x)voidmain(){intnum;intagain1;printf("\40:Programwillstopifinputvaluelessthan50.\n");while(again){printf("\40:Pleaseinputnumber>");scanf("%d",&num);printf("\40:Thesquareforthisnumberis%d\n",SQ(num));if(num>50)againTRUE;elseagainFALSE;}}【程序47】题目:宏#define命令练习(2)程序源代码:#include"stdio.h"#defineexchange(a,b){\/*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"*/intt;\ta;\ab;\bt;\}voidmain(void){intx10;inty20;printf("x%d;y%d\n",x,y);exchange(x,y);printf("x%d;y%d\n",x,y);}【程序48】题目:宏#define命令练习(3)程序源代码:#definelag>#definesmay)x:y#defineminimum(x,y)(x>y)y:xvoidmain(){inta10,b20;#ifdefmaxprintf("\40:thelargeroneis%d\n",maximum(a,b));#elseprintf("\40:theloweroneis%d\n",minimum(a,b)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询