我正在编写一个程序,用于使用结构和指针查找两个有理数的加法、乘法和除法。我在用指针输入数字时遇到问题。我的代码应该如何更正?谢谢!
#include <stdio.h>
struct rational
{
int nu;
int de;
}*p1,*p2,*p3;
struct rational add()
{
p1->nu = p1->nu*p2->de + p1->de*p2->nu;
p3->de = p1->de * p2->de;
printf("%d\n--\n%d\n",p3->nu,p3->de);
}
struct rational multiply()
{
p3->nu = p1->nu * p2->nu;
p3->de = p1->de * p2->de;
printf("%d\n--\n%d\n",p3->nu,p3->de);
}
struct rational divide()
{
p3->nu = p1->nu * p2->de;
p3->de = p1->de * p2->nu;
printf("%d\n--\n%d\n",p3->nu,p3->de);
}
int main()
{
int a,b,choice;
printf("Enter the first rational number.\n");
scanf("%d%d",&p1->nu,&p1->de);
printf("Enter the second rational number.\n");
scanf("%d%d",&p2->nu,&p2->de);
scanf("%d",&choice);
switch (choice)
{
case 1: add();
break;
case 2: multiply();
break;
case 3: divide();
break;
}
return 0;
}