数据结构作业(第二题)

作业代码是从网上进行参考的,对代码进行了修改之后的到的。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
void init_dlink(void);
void read_file(void);
void write_file(void);
void insert_item(void);
void search_item(void);
void sort_item(void);
void delete_item(void);
void print_item(void);
void modify_item(void);
void anykey(void);
typedef struct node *link;
struct node {
 char stu_id[10];
 char name[20];
 char sex[10];
 char _zip[50];
 char age[50];
 char address[100];
char phone[100];
char qq[50];
char wechat[100];
char birth[100];
 link prev, next;
};
link ptr, head, tail, current;
bool fFlag;
int main(void)
{
 char option1, option2;
 system("cls");
 init_dlink();
 read_file();
 while(1)
 {
  system("cls");
  printf("**********************************\n");
  printf(" 1.新建联系人\n");
  printf(" 2.删除联系人\n");
  printf(" 3.查找联系人\n");
  printf(" 4.显示联系人\n");
  printf(" 5.修改联系人\n");
  printf(" 6.退出\n");
  printf("**********************************\n");
  printf(" Please enter your choice (1-5)...");
  option1 = getche();
  switch(option1)
  {
   case '1':
    fFlag = 1;
    insert_item();
    break;
   case '2':
    delete_item();
    break;
   case '3':
    search_item();
    break;
   case '4':
    print_item();
    break;
   case '5':
    modify_item();
    break;
   case '6':
    if (fFlag)
    {
     printf("\n");
     printf("Save changes? (Y or N)");
     option2 = getche();
     if(option2 == 'Y')
     {
      write_file();
      exit(0);
     }
     if(option2 = 'N')
      exit(0);
    }
    else
    {
     write_file();
     exit(0);
    }
  }
 }
}
void init_dlink(void)
{
 ptr = (link)malloc(sizeof *ptr);
 strcpy(ptr->stu_id, "0");
 strcpy(ptr->name, "0");
 strcpy(ptr->sex, "0");
 strcpy(ptr->_zip, "0");
 strcpy(ptr->age, "0");
 strcpy(ptr->address, "0");
strcpy(ptr->phone,  "0");
strcpy(ptr->qq, "0");
strcpy(ptr->wechat, "0");
strcpy(ptr->birth, "0");
 ptr-> prev = ptr;
 ptr->next = ptr;
 head = ptr;
 tail = ptr;
}
void read_file(void)
{
 FILE *fptr;
 if((fptr = fopen("dlist.dat", "r")) == NULL)
 {
  printf(" Data file not exist!\n");
  printf(" Press any key to edit first record...\n");
  getch();
  insert_item();
 }
 else
 {
  ptr = (link)malloc(sizeof *ptr);
  while(fscanf(fptr, "%s %s %s %s %s %s %s %s %s %s %s ", ptr->stu_id, ptr->name, ptr->sex, ptr->_zip, ptr->age, ptr->address ,ptr->phone, ptr->qq, ptr->wechat, ptr->birth) != EOF)
  {
   if(strcmp(ptr->stu_id, " ") != 0)
   {
    sort_item();
    ptr = (link)malloc(sizeof *ptr);
   }
   else
    free(ptr);
  }
  fclose(fptr);
 }
}
void write_file(void)
{
 FILE *fptr;
 fptr = fopen("dlist.dat", "w");
 current = head->next;
 while(current != head)
 {
  fprintf(fptr, "%s %s %s %s %s %s %s %s %s %s\n", current->stu_id, current->name, current->sex, current->_zip, current->age, current->address, current->phone, current->qq, current->wechat, current->birth);
  current = current->next;
 }
 fclose(fptr);
}
void insert_item(void)
{
 system("cls");
 ptr = (link)malloc(sizeof *ptr);
 printf(" Student ID:");
 gets(ptr->stu_id);
 printf(" 姓名:");
 gets(ptr->name);
 printf(" 性别:");
 gets(ptr->sex);
 printf(" 邮编:");
 gets(ptr->_zip);
 printf(" 年龄:");
 gets(ptr->age);
 printf(" 地址:");
 gets(ptr->address);
 printf(" 电话号码:");
 gets(ptr->phone);
 printf(" 微信账号:");
 gets(ptr->wechat);
 printf(" QQ:");
 gets(ptr->qq);
 printf(" 生日:");
 gets(ptr->birth);

 sort_item();
}
void sort_item(void)
{
 current = head->next;
 while(current != head)
 {
  if(strcmp(ptr->stu_id, current->stu_id) < 0)
  {
   ptr->next = current;
   ptr->prev = current->prev;
   current->prev->next = ptr;
   current->prev = ptr;
   break;
  }
  current = current->next;
 }
 if(head->next == head || current == head)
 {
  ptr->next = head;
  ptr->prev = head->prev;
  head->prev->next = ptr;
  head->prev = ptr;
  tail = ptr;
 }
}
void delete_item(void)
{
 char del_id[10];
 int count = 0;
 link clear;
 system("cls");
 if(head->next == head)
  printf(" No student record!\n");
 else
 {
  printf(" Delete student ID:");
  gets(del_id);
  current = head->next;
  while(current->next != head)
  {
   if(strcmp(del_id, current->stu_id)==0)
   {
    count++;
    clear = current;
    current->prev->next = current->next;
    current->next->prev = current->prev;
    current = current->next;
    free(clear);
   }
   current = current->next;
  }
  if(strcmp(del_id, current->stu_id) == 0)
  {
   count++;
   clear = current;
   current->prev->next = head;
   head->prev = current->prev;
   tail = current->prev;
   free(clear);
  }
  if(count > 0)
   printf(" %s student record(s) deleted\n", del_id);
  else
   printf(" Student %s not found\n", del_id);
 }
 anykey();
}
void search_item(void)
{
 char search_id[10];
 system("cls");
 if(head->next == head)
  printf(" No student record\n");
 else
 {
  printf(" Search student ID:");
  gets(search_id);
  current = head->next;
  while((current != head) && (strcmp(search_id, current->stu_id)!=0))
   current = current->next;
  if (current != head)
  {
   printf("--------------------------------------\n");
   printf(" Student ID: %s\n", current->stu_id);
   printf(" Student name: %s\n", current->name);
   printf(" Student sex: %s\n", current->sex);
   printf(" Student _zip: %s\n", current->_zip);
   printf(" Student age: %s\n", current->age);
   printf(" Student address: %s\n", current->address);
   printf(" Student phone: %s\n", current->phone);
   printf(" Student qq: %s\n", current->qq);
   printf(" Student wechat: %s\n", current->wechat);
   printf(" Student birth: %s\n", current->birth);
   printf("--------------------------------------\n");
  }
  else
   printf(" Student %s not found\n", search_id);
 }
 anykey();
}
void modify_item(void)
{
 int count = 0;
 char modify_id[10];
 system("cls");
 if(head->next == head)
  printf(" No student record\n");
 else
 {
  printf(" Modify student ID:");
  gets(modify_id);
  current = head->next;
  while(current != head)
  {
   if(strcmp(modify_id, current->stu_id) == 0)
   {
    printf("******************************\n");
    printf(" Student ID: %s\n", current->stu_id);
    printf(" Student name: %s\n", current->name);
    printf(" Student sex: %s\n", current->sex);
    printf(" Student _zip: %s\n", current->_zip);
    printf(" Student age: %s\n", current->age);
    printf(" Student address: %s\n", current->address);
printf(" Student phone: %s\n", current->phone);
    printf(" Student qq: %s\n", current->qq);
    printf(" Student wechat: %s\n", current->wechat);
    printf(" Student birth: %s\n", current->birth);
    printf(" Please enter new _zip:");
    gets(current->_zip);
    printf(" Please enter new age:");
    gets(current->age);
    printf(" Please enter new address:");
    gets(current->address);
printf(" Please enter new phone:");
    gets(current->phone);
printf(" Please enter new qq:");
    gets(current->qq);
printf(" Please enter new wechat:");
    gets(current->wechat);
printf(" Please enter new birth:");
    gets(current->birth);
    count++;
   }
   current = current->next;
  }
  if(count > 0)
   printf(" %d student record(s) modified\n", count);
  else
   printf(" Student %s not found\n", modify_id);
 }
 anykey();
}

void print_item(void)
{
 int count = 0;
 system("cls");
 if(head->next == head)
  printf(" No student record\n");
 else
 {
  printf(" 序号      姓名        性别     邮编        年龄         地址    电话    QQ     微信账号    生日\n");
  printf("-------------------------------------------------------------------------------\n");
  current = head->next;
  while(current != head)
  {
   printf(" %-7s  %-12s  %-6s  %-12s  %-12s  %-s  %-6s  %-12s  %-12s  %-s \n", current->stu_id, current->name, current->sex, current->_zip, current->age, current->address, current->phone, current->qq, current->wechat, current->birth);
   count++;
   current = current->next;
   if(count % 20 == 0)
    getch();
  }
  printf("-------------------------------------------------------------------------------\n");
  printf(" Total %d record(s) found\n", count);
 }
 anykey();
}
void anykey(void)
{

 printf(" Press any key to continue...");
 getch();
}

经过验证,程序能够正常运行。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值