数据结构(C语言版)严蔚敏 吴伟民版----抽象数据类型之三元组的实现
程序员文章站
2022-05-22 15:04:10
...
暂时摆脱了ACM严格的输出要求束缚,现在终于也可以瞎搞了。现在随着课堂实验写一写抽象数据类型了,话不多说上代码。
ps:因为输入输出可以自己瞎鸡儿搞,所以用了很多输出语句显得代码很冗长,但是实际上上全部都是c的基础,请读者耐心观看。
pps:写博客时MarkDown有些卡顿,不知道什么原因导致代码之间有许多空格,实际代码长度为211行。(本人代码风格为松散型)
#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
typedef int Status;
typedef int ElemType;
typedef ElemType* Triplet;
int errorcnt = 0;//错误计数器
Status existT(Triplet T);
Status Get(Triplet T, int i, ElemType & e);
Status InitTriplet(Triplet & T, ElemType v1, ElemType v2, ElemType v3);
Status DestroyTriplet(Triplet & T);
Status Put(Triplet & T, int i, ElemType e);
Status Min(Triplet T, ElemType & e);
Status Max(Triplet T, ElemType & e);
Status IsAscending(Triplet T);
Status IsDescending(Triplet T);
Status AddTriplet(Triplet T1, Triplet T2);
Status AddTriplet(Triplet &T1) {
Triplet T2;
ElemType e1, e2,e3;
printf("Please enter the secend Triplet Element with space:");
scanf("%d%d%d", &e1, &e2,&e3);
InitTriplet(T2, e1, e2, e3);
T1[0] +=T2[0];
T1[1] +=T2[1];
T1[2] +=T2[2];
DestroyTriplet(T2);
printf("Now Your original Triplet vlaues are %-4d%-4d%-4d\n", T1[0], T1[1], T1[2]);
return OK;
}
//判断三元组是否存在
Status existT(Triplet T) {
if (T == NULL)
{
printf("The Triplet even doesn't exist!What are you doing man?");
errorcnt++;
if(errorcnt >= 3)//彩蛋函数,连续错误三次时触发
printf("\nPress 8 to create a Triplet dumb ass!( ̄_, ̄ )\n");
return 1;
}
else
{
return 0;
}
}
Status InitTriplet(Triplet & T, ElemType v1, ElemType v2, ElemType v3) {
T = (ElemType*)malloc(3* sizeof(ElemType));//申请内存
if (!T)return ERROR;
T[0] = v1;
T[1] = v2;
T[2] = v3;
return OK;
}
Status DestroyTriplet(Triplet & T) {
free(T);
T = NULL;
return OK;
}
Status Get(Triplet T, int i, ElemType & e) {
if (i < 1|| i>3)return ERROR;
e = T[i - 1];
return OK;
}
Status Put(Triplet & T, int i, ElemType e) {
if (i < 1|| i>3)return ERROR;
T[i - 1] = e;
return OK;
}
Status IsAscending(Triplet T) {
if (T[0] <T[1])
{
return T[1] <T[2] ? 1: 0;
}
else
return 0;
}
Status IsDescending(Triplet T) {
if (T[0] >T[1])
{
return T[1] >T[2] ? 1: 0;
}
else
return 0;
}
Status Max(Triplet T, ElemType & e) {
e = T[0] >T[1] ? T[0] : T[1];
e = e > T[2] ? e : T[2];
return OK;
}
Status Min(Triplet T, ElemType & e) {
e = T[0] <T[1] ? T[0] : T[1];
e = e < T[2] ? e : T[2];
return OK;
}
int main(void)
{
Triplet T;
ElemType e1, e2,e3;
int com =0,i=0,e=0;
printf("Please enter the Triplet Element with space:");
scanf("%d%d%d",&e1, &e2, &e3);
InitTriplet(T, e1, e2, e3);
while (com !=-1)//循环重复接受命令,-1为退出命令
{
//显示菜单操作
printf("Please choose the action that you want:\n");
printf("1.Destorythe Triplet 2.Read i th value of the Triplet\n");
printf("3.Change i th value of the Triplet 4.Check the value of the Triplet is ascending or not\n");
printf("5.Findthe max value of Triplet 6.Check the value of the Triplet is descending or not\n");
printf("7.Findthe minimum value of Triplet 8.Create a Triplet\n");
printf("9.Add one Triplet with the original Triplet -1.Quit\n");
scanf("%d",&com);
swiztch (com)//依据用户指令选择对应操作
{
case 1:
DestroyTriplet(T);
break;
case 2:
if(existT(T))
{
break;
}
printf("Please enter the order number that you want read:");
scanf("%d",&i);//提示输入想要读取的元素的序数词
if (Get(T, i, e))
printf("The %d%s value is %d\n", i, i == 1 ? "st" : i == 2 ? "nd" : "rd", e);//运用三元运算符输出对应序数词的后缀
else//如果输入的i大于三,则输出错误提示
printf("Tips:Just like its name.It just have 3 elements.");
errorcnt = 0;
break;
case 3:
if(existT(T))
{
break;
}
printf("Please enter the order number that you want change:");
scanf("%d",&i);//提示输入想要改变的元素的序数词
printf("Please enter the value that you want change:");
scanf("%d",&e);//提示输入想要改为的值
if (Put(T, i, e)){
Get(T, i, e);
printf("The %d%s value is %d now\n", i, i == 1 ? "st" : i == 2 ? "nd" : "rd", e);
errorcnt = 0;
}
else
printf("Tips:Just like its name.It just have 3 elements.");
break;
case 4:
if(existT(T))
{
break;
}
if(IsAscending(T))
printf("Obviously,it'sascending");
else
printf("Sad~~~it doesn't");
errorcnt = 0;
break;
case 5:
if(existT(T))
{
break;
}
Max(T, e);
printf("The max value is %d.\n", e);
errorcnt = 0;
break;
case 6:
if(existT(T))
{
break;
}
if(IsDescending(T))
printf("Obviously,it's descending");
else
printf("Sad~~~it doesn't");
errorcnt = 0;
break;
case 7:
if(existT(T))
{
break;
}
Min(T, e);
printf("The minimum value is %d.\n", e);
errorcnt = 0;
break;
case 8:
if (T != NULL)//如果T已经存在,则发出警告,会重写该三元组
printf("Warning:Youraction will rewrite the Triplet.\n");
printf("Please enter the Triplet Element with space:");
scanf("%d%d%d", &e1,&e2, &e3);
InitTriplet(T, e1, e2, e3);
break;
case 9:
if(existT(T))
{
break;
}
AddTriplet(T);
break;
} //end of switch
printf("\n\n\n");
}
return 0;
}
题外:蓝桥杯成绩出来了,拿了省二,自闭了一晚上(写了九道题,对了三道填空,大题还没对答案,倒数第二道题在最后五分钟检查时候查到了BUG,也算是累计了经验,以后参加蓝桥杯要多跑几组测试数据,毕竟不像ACM直接出结果233),不过只是自己在二十天里课余时间自己跟着大紫书漫无目的的在瞎学,学校也给发500块钱,倒是也拿回报名费了,也算不亏了,继续加油鸭!