数据结构(C语言版)严蔚敏 吴伟民版----抽象数据类型之三元组的实现

暂时摆脱了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块钱,倒是也拿回报名费了,也算不亏了,继续加油鸭!

全部评论

相关推荐

bg双非本科,方向是嵌入式。这次秋招一共拿到了&nbsp;8&nbsp;个&nbsp;offer,最高年包&nbsp;40w,中间也有一段在海康的实习经历,还有几次国家级竞赛。写这篇不是想证明什么,只是想把自己走过的这条路,尽量讲清楚一点,给同样背景的人一个参考。一、我一开始也很迷茫刚决定走嵌入式的时候,其实并没有一个特别清晰的规划。网上的信息很零散,有人说一定要懂底层,有人说项目更重要,也有人建议直接转方向。很多时候都是在怀疑:1.自己这种背景到底有没有机会2.现在学的东西到底有没有用3.是不是已经开始晚了这些问题,我当时一个都没答案。二、现在回头看,我主要做对了这几件事第一,方向尽早确定,但不把自己锁死。我比较早就确定了嵌入式这个大方向,但具体做哪一块,是在项目、竞赛和实习中慢慢调整的,而不是一开始就给自己下结论。第二,用项目和竞赛去“证明能力”,而不是堆技术名词。我不会刻意追求学得多全面,而是确保自己参与的每个项目,都能讲清楚:我负责了什么、遇到了什么问题、最后是怎么解决的。第三,尽早接触真实的工程环境。在海康实习的那段时间,对我触动挺大的。我开始意识到,企业更看重的是代码结构、逻辑清晰度,以及你能不能把事情说清楚,而不只是会不会某个知识点。第四,把秋招当成一个需要长期迭代的过程。简历不是一次写完的,面试表现也不是一次就到位的。我会在每次面试后复盘哪些问题没答好,再针对性补。三、我踩过的一些坑现在看也挺典型的:1.一开始在底层细节上纠结太久,投入产出比不高2.做过项目,但前期不会总结,导致面试表达吃亏3.早期有点害怕面试,准备不充分就去投这些弯路走过之后,才慢慢找到节奏。四、给和我背景相似的人一点建议如果你也是双非,准备走嵌入式,我觉得有几件事挺重要的:1.不用等“准备得差不多了”再投2.项目一定要能讲清楚,而不是做完就算3.不要只盯着技术,多关注表达和逻辑很多时候,差的不是能力,而是呈现方式。五、写在最后这篇总结不是标准答案,只是我个人的一次复盘。后面我会陆续把自己在嵌入式学习、竞赛、实习和秋招中的一些真实经验拆开来讲,希望能对后来的人有点帮助。如果你正好也在这条路上,希望你能少走一点弯路。
x_y_z1:蹲个后续
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务