| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 821 人关注过本帖
标题:昨天做了个题,感觉开心死了
只看楼主 加入收藏
残风败柳
Rank: 1
来 自:北京
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-9-1
收藏
 问题点数:0 回复次数:0 
昨天做了个题,感觉开心死了
/* Note:Your choice is C IDE */
#include "stdio.h"
#define N 20
struct list
{
    int data[N];
    int length;   
};
void main()
{
    struct list s;
      int i,t,key,xia;
      while(1)
      {
          printf("1:输入五个数\n");
          printf("2:插入一个数\n");
          printf("3:删除一个数\n");
          printf("4:输出所有的数\n");
          printf("0:退出\n");
          printf("请输入功能编号\n");
          scanf("%d",&t);
          switch(t)
          {
              case 1:
                  s.length=0;
                  printf("请输入五个数\n");
                  for(i = 0 ; i < 5 ; i ++)
                  {
                      printf("请输入第%d个数:",i+1);
                      scanf("%d",&s.data[i]);
                      s.length++;   
                  }
                  break;
              case 2:
                  printf("请输入插入的下标\n");
                  scanf("%d",&xia);
                  if(xia < 0 || xia >= s.length)
                  {
                      printf("下标有误\n");   
                  }
                  else
                  {
                      if(s.length==N)
                      {
                          printf("顺序表满了\n");   
                      }
                      else
                      {
                          printf("请输入插入的元素值:\n");
                          scanf("%d",&key);
                          for(i  = s.length - 1 ; i >= xia ; i --)
                          {
                              s.data[i+1]=s.data[i];   
                          }
                          s.data[xia]=key;
                          s.length++;
                          printf("插入成功\n");
                      }
                     
                  }
                  break;
              case 3:
                  printf("请输入删除的下标的数:");
                  scanf("%d",&xia);
                  if(xia < 0 ||  xia >= s.length)
                  {
                      printf("下标有误\n");   
                  }
                  else
                  {
                      if(s.length==0)
                      {
                          printf("顺序表为空\n");   
                      }
                      else
                      {
                          for(i = xia ;i <s.length - 1 ; i++)
                          {
                              s.data[i]=s.data[i+1];   
                          }
                          s.length--;
                          printf("删除成功\n");   
                      }
                  }
                  break;
              case 4:
                  printf("输出所有的数\n");
                  for(i = 0 ; i < s.length ; i ++)
                  {
                      printf("  %d",s.data[i]);
                  }
                  printf("\n");
                  break;
              case 0:
                  exit(0);
              default:
                  printf("编号有误\n");   
          }
      }      
}
搜索更多相关主题的帖子: data length printf 输入 scanf 
2017-09-01 16:16
快速回复:昨天做了个题,感觉开心死了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045482 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved