#include <stdio.h>
#define NUM 26
#define DOCTOR_INIT -100
#define OBJECT_INIT -100
#define OFFSET_INIT -100
typedef struct Statement /*用来存储每一个语句object1存储第一个字母,2存储第二个,offset存储最后的数字*/
{
int object1;
int object2;
int offset;
};
void manage(struct Statement* statement,int* doctor,int n,int equal)
/*递归函数,处理刚才的输入*/
/*前三个参数是传递当前情况,第四个参数表示刚刚确定的那个doctor[],整次递归只处理刚刚确定的那一个*/
{
int i;
for(i=0;i<n;i++)
{
if(statement[i].object1==equal && doctor[statement[i].object2]==DOCTOR_INIT && statement[i].offset!=OFFSET_INIT)
{
doctor[statement[i].object2] = doctor[equal]+statement[i].offset;
manage(statement,doctor,n,statement[i].object2);
}
if(statement[i].object2==equal && doctor[statement[i].object1]==DOCTOR_INIT && statement[i].offset!=OFFSET_INIT)
{
doctor[statement[i].object1] = doctor[equal]-statement[i].offset;
manage(statement,doctor,n,statement[i].object1);
}
}
}
void main()
{
/*变量声明*/
int n;
struct Statement statement[NUM];
int doctor[NUM];
int i,k,l;
char c[5];
char oper;
/*变量初始化*/
for(i=0;i<NUM;i++)
{
statement[i].object1=OBJECT_INIT;
statement[i].object2=OBJECT_INIT;
statement[i].offset=OFFSET_INIT;
doctor[i]=DOCTOR_INIT;
}
/*读入语句*/
printf("请输入n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%d:",i+1);
scanf("%s",c);
statement[i].object1=c[0]-'A';
oper=c[1];
if(oper != '=')
{
statement[i].object2=c[2]-'A';
statement[i].offset=c[3]-'0';
}
else
statement[i].offset=c[2]-'0';
if(oper == '<')
statement[i].offset*=-1;
}
/*递归处理语句*/
for(i=0;i<n;i++)
{
if(statement[i].object2==OBJECT_INIT && doctor[statement[i].object1]==DOCTOR_INIT)
{
doctor[statement[i].object1]=statement[i].offset;
manage(statement,doctor,n,statement[i].object1);
}
}
/*输出*/
for(k=0;k<n;k++)
{
l=OBJECT_INIT;
for(i=0;i<n;i++)
if(doctor[i]!=DOCTOR_INIT)
if(l==OBJECT_INIT || doctor[l]>doctor[i])
l=i;
if(l==OBJECT_INIT)
break;
printf("%c ",l+'A');
doctor[l]=DOCTOR_INIT;
}
}
编译运行效果:
E:\>gcc 11.c
11.c:12: warning: useless storage class specifier in empty declaration
E:\>a.exe
请输入n:7
1:A<C1
2:D<E1
3:E>B2
4:B>G4
5:F<B1
6:F>C1
7:F=4
E D B F C A G
E:\>
写了我将近一个小时,程序没有问题,需要修改或者有什么问题可以留言。
温馨提示:内容为网友见解,仅供参考