17070047 / 17070047/CodeStructureCourse

武智鹏

Open
#65 0 comments 0 reactions 0 assignees View on GitHub
nice
Dominant language
C++
Stars
16
Forks
2
PR merge metrics
No merged PRs in 30d

Description

```c
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#define MAXSIZE 100

typedef struct {
char cardnum[30];
char id[25];
char name[30];
char specialty[30];
char classnum[30];
}stu;
typedef struct{
stu r[MAXSIZE];
int length;
}table;

void create(table *tab,char *filename)
{
int i;
FILE *fp;
fp=fopen(filename,"r");
printf("输入学生个数:\n");
if(fp)
{
fscanf(fp,"%d",&tab->length);
for(i=1;i<=tab->length;i++)
{
fscanf(fp,"%s",tab->r[i].cardnum);
fscanf(fp,"%s",tab->r[i].id);
fscanf(fp,"%s",tab->r[i].name);
fscanf(fp,"%s",tab->r[i].specialty);
fscanf(fp,"%s",tab->r[i].classnum);
}
printf("学生信息读入成功!\n");
}
else
printf("学生信息读入失败!\n");
}
void print(table *tab)
{
int i;
if (tab->length!= 0)
{
printf("输出所有的学生信息:\n");
printf("%22s%15s%12s%20s%18s\n", "借书卡号", "专业", "姓名", "班级号", "学号");
for(i = 1;i<=tab->length; i++)
printf("%20s%20s%10s%20s%20s\n", tab->r[i].cardnum, tab->r[i].specialty, tab->r[i].name, tab->r[i].classnum, tab->r[i].id);
}
else
printf("借书证系统还没有借书记录!!\n");
}
void cardnumsort(table *tab)
{
int i,j,left,right,mid;
for(i=2;i<=tab->length;i++)
{
tab->r[0]=tab->r[i];
left=1;
right=i-1;
while(left<=right)
{
mid=(left+right)/2;
if(strcmp(tab->r[i].cardnum,tab->r[mid].cardnum)<0)
right=mid-1;
else
left=mid+1;
}
for(j=i-1;j>=left;j--)
tab->r[j+1]=tab->r[j];
tab->r[left]=tab->r[0];
}
}
void classnumsort(table *tab)
{
int i,j,left,right,mid;
for(i=2;i<=tab->length;i++)
{
tab->r[0]=tab->r[i];
left=1;
right=i-1;
while(left<=right)
{
mid=(left+right)/2;
if(strcmp(tab->r[i].classnum,tab->r[mid].classnum)<0)
right=mid-1;
else
left=mid+1;
}
for(j=i-1;j>=left;j--)
tab->r[j+1]=tab->r[j];
tab->r[left]=tab->r[0];
}
}
void specialtysort(table *tab)
{
int i,j,left,right,mid;
for(i=2;i<=tab->length;i++)
{
tab->r[0]=tab->r[i];
left=1;
right=i-1;
while(left<=right)
{
mid=(left+right)/2;
if(strcmp(tab->r[i].specialty,tab->r[mid].specialty)<0)
right=mid-1;
else
left=mid+1;
}
for(j=i-1;j>=left;j--)
tab->r[j+1]=tab->r[j];
tab->r[left]=tab->r[0];
}
}
void idsort(table *tab)
{
int i,j,left,right,mid;
for(i=2;i<=tab->length;i++)
{
tab->r[0]=tab->r[i];
left=1;
right=i-1;
while(left<=right)
{
mid=(left+right)/2;
if(strcmp(tab->r[i].id,tab->r[mid].id)<0)
right=mid-1;
else
left=mid+1;
}
for(j=i-1;j>=left;j--)
tab->r[j+1]=tab->r[j];
tab->r[left]=tab->r[0];
}
}
int namesearch(table *tab,char *key)
{
int low=1,high=tab->length,mid;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(tab->r[mid].name,key)==0)
return mid;
else if(strcmp(tab->r[mid].name,key)>0)
high=mid-1;
else
low=mid+1;
}
return -1;
}
int cardnumsearch(table *tab,char *key)
{
int low=1,high=tab->length,mid;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(tab->r[mid].cardnum,key)==0)
return mid;
else if(strcmp(tab->r[mid].cardnum,key)>0)
high=mid-1;
else
low=mid+1;
}
return -1;
}
void sort()
{
printf("1.对卡号排序并输出\n");
printf("2.对学号排序并输出\n");
printf("3.对班级排序并输出\n");
printf("4.对学院排序并输出\n");
printf("5.输出指定卡号\n");
printf("6.输出指定姓名\n");
printf("7.输出指定班号\n");
printf("8.输出指定学院\n");
}
void menu(table *tab)
{
int i=1,c;
char key1[30],key2[30],key3[30],key4[30];
int result1, result2;
printf("输入操作选项:\n");
scanf("%d",&c);
switch(c)
{
case 1:
{
cardnumsort(tab);
print(tab);
menu(tab);
}
break;
case 2:
{
idsort(tab);
print(tab);
menu(tab);
}
break;
case 3:
{
classnumsort(tab);
print(tab);
menu(tab);
}
break;
case 4:
{
specialtysort(tab);
print(tab);
menu(tab);
}
break;
case 5:
{
printf("输入要查找的卡号:\n");
scanf("%s",&key1);
result1=cardnumsearch(tab,key1);
if(result1!=-1)
{
printf("%22s%15s%12s%20s%18s\n", "借书卡号", "专业", "姓名", "班级号", "学号");
printf("%20s%20s%10s%20s%20s\n", tab->r[result1].cardnum, tab->r[result1].specialty,tab->r[result1].name, tab->r[result1].classnum, tab->r[result1].id);
}
else
printf("没有找到该借书证信息!\n");
menu(tab);
}
break;
case 6:
{
printf("输入要查找的姓名:\n");
scanf("%s",&key2);
result2=namesearch(tab,key2);
if(result2!=-1)
{
printf("%22s%15s%12s%20s%18s\n", "借书卡号", "专业", "姓名", "班级号", "学号");
printf("%20s%20s%10s%20s%20s\n", tab->r[result2].cardnum, tab->r[result2].specialty,tab->r[result2].name, tab->r[result2].classnum, tab->r[result2].id);
}
else
printf("没有找到该借书证信息!\n");
menu(tab);
}
break;
case 7:
{
printf("输入要查找的班级:\n");
scanf("%s",&key3);
printf("%22s%15s%12s%20s%18s\n", "借书卡号", "专业", "姓名", "班级号", "学号");
while(i<=tab->length&&i>=1)
{
if (strcmp(tab->r[i].classnum,key3) == 0)
{
printf("%20s%20s%10s%20s%20s\n", tab->r[i].cardnum, tab->r[i].specialty,
tab->r[i].name, tab->r[i].classnum, tab->r[i].id);
}
i++;
}
menu(tab);
}
break;
case 8:
{
printf("输入要查找的学院:\n");
scanf("%s",&key4);
printf("%22s%15s%12s%20s%18s\n", "借书卡号", "专业", "姓名", "班级号", "学号");
while(i<=tab->length&&i>=1)
{
if (strcmp(tab->r[i].specialty,key4) == 0)
{
printf("%20s%20s%10s%20s%20s\n", tab->r[i].cardnum, tab->r[i].specialty,
tab->r[i].name, tab->r[i].classnum, tab->r[i].id);
}
i++;
}
menu(tab);
}
break;
default:
printf("已退出!\n");
exit(0);
break;
}
}

int main()
{
table tab;
char filename[]="D:\\jsz.txt";
create(&tab,filename);
print(&tab);
sort();
menu(&tab);
return 0;
}
```
![1.png](https://upload-images.jianshu.io/upload_images/15335028-19bb6ff14e4a965a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![2.png](https://upload-images.jianshu.io/upload_images/15335028-926a4ca8446c5acc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue contains a full C program for a student library card system, but no description of what needs to be changed or fixed. The images are not accessible. To start, examine the code for logical errors, memory issues, or incorrect sorting/search algorithms. Look at the create function's file handling and the sorting functions' binary insertion sort implementation. Determine what the expected behavior is versus what the program currently does.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.