鱼C论坛

 找回密码
 立即注册
查看: 412|回复: 1

学校的作业!求助!要求在附件里。能做的加微信哦Michael-Xiongda

[复制链接]
最佳答案
0 
发表于 2018-5-8 00:26:56 | 显示全部楼层 |阅读模式
100鱼币
  1. //
  2. //  main.c
  3. //  qxiong1_project3
  4. //
  5. //  Created by Qicheng Xiong on 2018/4/27.
  6. //  Copyright © 2018 Qicheng Xiong. All rights reserved.
  7. //

  8. /*
  9. * Stack Project
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <assert.h>
  14. #include <time.h>

  15. #define EXTRA 0
  16. /*########################### IMPLEMENT YOUR CODE HERE #########################*/
  17. //Implement all your code here









  18. /*##############################################################################*/

  19. /*
  20. DO NOT ALTER ANY CODE BELOW HERE
  21. You may comment out certain tests for debugging purposes, but your final
  22. submission should be unchanged from what is below.

  23. */
  24. unsigned int memory_used = 0;

  25. void main (){
  26.     printf("Heap Memory in use: %lu\n", memory_used);
  27.     /******************** Test #1 **************************/
  28.     printf("\n =========== Test # 1: Stack creation =========== \n");
  29.     struct stack * s = newStack(10);
  30.     assert(s->stk != 0);
  31.     assert(s->top == -1);
  32.     assert(s->max_size == 10);
  33.     printf("Heap Memory in use: %lu\n", memory_used);
  34.     printf("\t...Test # 1 passed\n");
  35.     /******************** Test #2 **************************/
  36.     printf("\n =========== Test # 2: push 1 value onto the stack =========== \n");
  37.     assert(push(s, 5) != -1);
  38.     display(s);
  39.     assert(s->stk[0] == 5);
  40.     assert(s->top == 0);
  41.     assert(s->max_size == 10);
  42.     printf("Heap Memory in use: %lu\n", memory_used);
  43.     printf("\t...Test # 2 passed\n");
  44.    
  45.     /******************** Test #3 **************************/
  46.     printf("\n =========== Test # 3: push 2nd value onto the stack =========== \n");
  47.     assert(push(s, 7) != -1);
  48.     display(s);
  49.     assert(s->stk[0] == 5);
  50.     assert(s->stk[1] == 7);
  51.     assert(s->top == 1);
  52.     assert(s->max_size == 10);
  53.     printf("Heap Memory in use: %lu\n", memory_used);
  54.     printf("\t...Test # 3 passed\n");
  55.    
  56.     /******************** Test #4 **************************/
  57.     printf("\n =========== Test # 4: fill the stack =========== \n");
  58.     for(int i = 2; i < 10; i++){
  59.         assert(push(s, i+10) != -1);
  60.         display(s);
  61.         assert(s->stk[i] == i+10);
  62.         assert(s->top == i);
  63.         assert(s->max_size == 10);
  64.     }
  65.     printf("Heap Memory in use: %lu\n", memory_used);
  66.     printf("\t...Test # 4 passed\n");
  67.    
  68.     /******************** Test #5 **************************/
  69.     printf("\n =========== Test # 5: insert into full stack =========== \n");
  70.     assert(push(s, 10) == -1);
  71.     assert(s->top == 9);
  72.     assert(s->max_size == 10);
  73.     printf("Heap Memory in use: %lu\n", memory_used);
  74.     printf("\t...Test # 5 passed\n");
  75.    
  76.     /******************** Test #6 **************************/
  77.     printf("\n =========== Test # 6: pop 1 value off the stack =========== \n");
  78.     assert(pop(s) == 19);
  79.     display(s);
  80.     assert(s->top == 8);
  81.     assert(s->max_size == 10);
  82.     printf("Heap Memory in use: %lu\n", memory_used);
  83.     printf("\t...Test # 6 passed\n");
  84.    
  85.     /******************** Test #7 **************************/
  86.     printf("\n =========== Test # 7: pop 2nd value off the stack =========== \n");
  87.     assert(pop(s) == 18);
  88.     display(s);
  89.     assert(s->stk[s->top] == 17);
  90.     assert(s->top == 7);
  91.     assert(s->max_size == 10);
  92.     printf("Heap Memory in use: %lu\n", memory_used);
  93.     printf("\t...Test # 7 passed\n");
  94.    
  95.     /******************** Test #7 **************************/
  96.     printf("\n =========== Test # 8: push value onto the stack =========== \n");
  97.     assert(push(s, 20) != -1);
  98.     assert(push(s, 21) != -1);
  99.     display(s);
  100.     assert(s->stk[s->top] == 21);
  101.     assert(s->top == 9);
  102.     assert(s->max_size == 10);
  103.     printf("Heap Memory in use: %lu\n", memory_used);
  104.     printf("\t...Test # 8 passed\n");
  105.    
  106.     /******************** Test #9 **************************/
  107.     printf("\n =========== Test # 9: remove remaining elements from the stack =========== \n");
  108.     int array[] = {5, 7, 12, 13, 14, 15, 16, 17, 20, 21};
  109.     for(int i = sizeof(array)/sizeof(int) - 1; i >= 0; i--){
  110.         assert(pop(s) == array[i]);
  111.         display(s);
  112.         assert(s->top == i-1);
  113.         assert(s->max_size == 10);
  114.     }
  115.     printf("Heap Memory in use: %lu\n", memory_used);
  116.     printf("\t...Test #9 passed\n");
  117.    
  118.     /******************** Test #10 **************************/
  119.     printf("\n =========== Test # 10: pop from an empty stack =========== \n");
  120.     assert(pop(s) == -1);
  121.     assert(s->top == -1);
  122.     assert(s->max_size == 10);
  123.     printf("Heap Memory in use: %lu\n", memory_used);
  124.     printf("\t...Test # 10 passed\n");
  125.    
  126.     /******************** Test #11 **************************/
  127.     printf("\n =========== Test # 11: random elements into a random sized stack =========== \n");
  128.     srand(time(NULL));
  129.     int stack_size = (rand() % 500) + 5;
  130.     struct stack * rand_stack = newStack(stack_size);
  131.     int stest = -1;
  132.     int direction = 2;
  133.     for(int i = 0; i < stack_size * 10; i++){
  134.         if(rand() % 10 <= direction){
  135.             (pop(rand_stack) == -1) ? direction = 2 : stest--;
  136.             assert(rand_stack->top == stest);
  137.         }else{
  138.             (push(rand_stack, rand() % 100) == -1) ? direction = 8 : stest++;
  139.             assert(rand_stack->top == stest);
  140.         }
  141.         assert(rand_stack->max_size == stack_size);
  142.     }
  143.     printf("Heap Memory in use: %lu\n", memory_used);
  144.     printf("\t...Test #11 passed\n");
  145.    
  146.     /******************** EXTRA CREDIT **************************/
  147. #if EXTRA
  148.     printf("\n=========== EXTRA CREDIT: Reverse a string ===========\n");
  149.    
  150.     char str1[] = "Hello";
  151.     struct stack * str_stack1 = newStack(sizeof(str1));
  152.     printf("Heap Memory in use: %lu\n", memory_used);
  153.     printf("%s => ", str1);
  154.     stringReverse(str1, str_stack1);
  155.     printf("%s\n", str1);
  156.    
  157.     char str2[] = "";
  158.     struct stack * str_stack2 = newStack(sizeof(str2));
  159.     printf("Heap Memory in use: %lu\n", memory_used);
  160.     printf("%s => ", str2);
  161.     stringReverse(str2, str_stack2);
  162.     printf("%s\n", str2);
  163.    
  164.     char str3[] = "Computer Science";
  165.     struct stack * str_stack3 = newStack(sizeof(str3));
  166.     printf("Heap Memory in use: %lu\n", memory_used);
  167.     printf("%s => ", str3);
  168.     stringReverse(str3, str_stack3);
  169.     printf("%s\n", str3);
  170.    
  171.     deleteStack(str_stack1);
  172.     deleteStack(str_stack2);
  173.     deleteStack(str_stack3);
  174.     printf("Heap Memory in use: %lu\n", memory_used);
  175.     printf("\t...Extra Credit passed\n");
  176. #endif
  177.     /******************** Test #12 **************************/
  178.     printf("\n=========== Test # 12: Clean up memory ===========\n");
  179.     deleteStack(s);
  180.     s = NULL;
  181.     deleteStack(rand_stack);
  182.     rand_stack = NULL;
  183.     printf("Heap Memory in use: %lu\n", memory_used);
  184.     printf("\t...Test #12 passed\n");
  185.    
  186.     printf("All tests passed! Don't forget to submit to MyCourses.\n");
  187. #if EXTRA
  188.     printf("========== evaluate for EXTRA CREDIT ===========\n\n");
  189. #endif
  190. }
复制代码

最佳答案
2 
发表于 2018-5-16 16:59:46 | 显示全部楼层
提问时把问题讲清楚,复制了这么多代码也不知道到底要干嘛,谁有那个功夫猜你代码实现什么功能。你把要求说了我们才能回答,或者指出你程序里的错误啊。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

小甲鱼强烈推荐上一条 /1 下一条

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号

GMT+8, 2018-8-20 13:17

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表