用C语言写下面的程序 具体请参考图片 麻烦大神们帮下忙

从记事本读取数据后 创建5个函数 具有以下功能
add函数 输入必要数据后 写入记事本最后一行(格式需要和上面统一)
del函数 输入指定学号后 将内容一整行删除
view函数 单纯查看记事本内容
up函 以学号从小到大查看
down函 以学号从大到小查看

记事本名字:list.txt

内容:

学号 姓名 年龄
000001 张达明 18
000007 王稼祥 20
000003 李永忠 21
000002 赵曾望 19
000008 马大成 19

# include<stdio.h>
# include<string.h>
# include<stdlib.h>
# define NUM 100
struct student{
char id[20];
char name[20];
int age;
}stud[NUM];
void add()
{
struct student s;
FILE *fp;
char ch;
if((fp=fopen("list.txt","a+"))==NULL)
{
fprintf(stderr,"Can't open the file\n");
exit(1);
}
printf("请输入要增加的学生学号:");
scanf("%s",s.id);
getchar();
printf("请输入要增加的学生姓名:");
scanf("%s",s.name);
getchar();
printf("请输入要增加的学生年龄:");
while(scanf("%d",&(s.age))!=1)
{
printf("请输入要增加的学生年龄:");
while((ch=getchar())!='\n')
{
continue;
}
}
getchar();
fprintf(fp,"%s\t%s\t%d\n",s.id,s.name,s.age);
printf("%s\t%s\t%d添加成功\n",s.id,s.name,s.age);
if(fclose(fp)==EOF)
{
fprintf(stderr,"Closing error\n");
exit(1);
}
}
void del()
{
unsigned i=0,j=0,k=0,flag=0;
struct student s;
char temp_id[20];
FILE *fp;
if((fp=fopen("list.txt","r+"))==NULL)
{
fprintf(stderr,"Can't open the file\n");
exit(1);
}
while(!feof(fp))
{
fscanf(fp,"%s\t%s\t%d\n",stud[i].id,stud[i].name,&(stud[i++].age));
}
if(fclose(fp)==EOF)
{
fprintf(stderr,"Closing error\n");
exit(1);
}
printf("请输入要删除的学生的学号:");
scanf("%s",temp_id);
while(j++<i)
{
if(strcmp(stud[j].id,temp_id)==0)
{
for(k=j;k<i-1;k++)
{
s=stud[k];
stud[k]=stud[k+1];
stud[k+1]=s;
}
flag=1;
if((fp=fopen("list.txt","w+"))==NULL)
{
fprintf(stderr,"Can't open the file\n");
exit(1);
}
for(k=0;k<i-1;k++)
{
fprintf(fp,"%s\t%s\t%d\n",stud[k].id,stud[k].name,stud[k].age);
}
if(fclose(fp)==EOF)
{
fprintf(stderr,"Closing error\n");
exit(1);
}
printf("%s\t%s\t%d已删除\n",s.id,s.name,s.age);
break;
}
}
if(!flag)
{
printf("您输入的学生不在文件中\n");
}
}
void view()
{
unsigned i=0,j;
FILE *fp;
if((fp=fopen("list.txt","r"))==NULL)
{
fprintf(stderr,"Can't open the file\n");
exit(1);
}
while(!feof(fp))
{
fscanf(fp,"%s\t%s\t%d\n",(stud[i].id),(stud[i].name),&(stud[i++].age));
}
printf("文件中的所有学生信息如下:\n");
for(j=0;j<i;j++)
{
printf("%s\t%s\t%d\n",stud[j].id,stud[j].name,stud[j].age);
}
if(fclose(fp)==EOF)
{
fprintf(stderr,"Closing error\n");
exit(1);
}
}
void up()
{
unsigned i=0,j,k;
struct student s;
FILE *fp;
if((fp=fopen("list.txt","r"))==NULL)
{
fprintf(stderr,"Can't open the file\n");
exit(1);
}
while(!(feof(fp)))
{
fscanf(fp,"%s\t%s\t%d\n",stud[i].id,stud[i].name,&(stud[i++].age));
}
for(j=0;j<i;j++)
{
for(k=0;k<i-1;k++)
{
if(strcmp(stud[k].id,stud[k+1].id)>0)
{
s=stud[k];
stud[k]=stud[k+1];
stud[k+1]=s;
}
}
}
printf("升序排列为:\n");
for(j=0;j<i;j++)
{
printf("%s\t%s\t%d\n",stud[j].id,stud[j].name,stud[j].age);
}
if(fclose(fp)==EOF)
{
fprintf(stderr,"Closing error\n");
exit(1);
}
}
void down()
{
unsigned i=0,j,k;
struct student s;
FILE *fp;
if((fp=fopen("list.txt","r"))==NULL)
{
fprintf(stderr,"Can't open the file\n");
exit(1);
}
while(!(feof(fp)))
{
fscanf(fp,"%s\t%s\t%d\n",stud[i].id,stud[i].name,&(stud[i++].age));
}
for(j=0;j<i;j++)
{
for(k=0;k<i-1;k++)
{
if(strcmp(stud[k].id,stud[k+1].id)<0)
{
s=stud[k];
stud[k]=stud[k+1];
stud[k+1]=s;
}
}
}
printf("降序排列为:\n");
for(j=0;j<i;j++)
{
printf("%s\t%s\t%d\n",stud[j].id,stud[j].name,stud[j].age);
}
if(fclose(fp)==EOF)
{
fprintf(stderr,"Closing error\n");
exit(1);
}
}
int main()
{
view();
add();
del();
puts("");
up();
puts("");
down();
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-23

写了个简单的,仅供参考。

/*
* Author: 吾好梦中杀猪
* 2013-14 All rights reserved.
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
#define ID_LENGTH 6
#define NAME_LENGTH 10
#define FILE_LINE_SIZE 26
#define TRUE 1
#define FALSE 0
#define IS_VALID_NUM(s) (((s) >= '0' && (s) <= '9') ? TRUE : FALSE)
typedef struct
{
char m_Id[ID_LENGTH+1];
char m_Name[NAME_LENGTH+1];
int m_age;
}TStudent;
enum
{
upSort,
downSort,
};
typedef int (*pStrCmpFunc)(const char *src, const char *dst, int len);
const char *fileName = "list.txt";
int isStringEqual(const char *src, const char *dst, int len)
{
int i;
for (i=0; i<len; i++)
if (*src++ != *dst++)
return FALSE;
return TRUE;
}
int isLeftGreater(const char *src, const char *dst, int len)
{
int i;
for (i=0; i<len; i++)
{
if (*src < *dst)
return FALSE;
else if (*src > *dst)
return TRUE;
src++;
dst++;
}
return FALSE;
}
int isLeftSmaller(const char *src, const char *dst, int len)
{
int i;
for (i=0; i<len; i++)
{
if (*src > *dst)
return FALSE;
else if (*src < *dst)
return TRUE;
src++;
dst++;
}
return FALSE;
}
void add()
{
FILE *pFile = NULL;
TStudent student;
int nameLen = 0;
if (NULL == (pFile = fopen(fileName, "ab")))
{
printf("Open file %s error!\n", fileName);
return;
}
printf("Please input student ID: ");
scanf("%s", student.m_Id);
printf("Please input student name: ");
scanf("%s", student.m_Name);
nameLen = strlen(student.m_Name);
memset(student.m_Name + nameLen, ' ', sizeof(student.m_Name) - nameLen);
student.m_Name[NAME_LENGTH] = '\0';
printf("Please input student age: ");
scanf("%d", &student.m_age);
fprintf(pFile, "\r\n%s %s %d", student.m_Id, student.m_Name, student.m_age);
fclose(pFile);
printf("%s is added!\n", student.m_Id);
}
void del()
{
FILE *pFile = NULL;
char *pBuf = NULL;
char id[ID_LENGTH + 1];
int fileLen;
int i;
int isFound = FALSE;
if (NULL == (pFile = fopen(fileName, "rb")))
{
printf("Open file %s error!\n", fileName);
return;
}
fseek(pFile, 0, SEEK_END);
fileLen = ftell(pFile);
rewind(pFile);
printf("Please input student ID: ");
scanf("%s", id);
if (NULL == (pBuf = (char *)malloc(sizeof(char) * fileLen)))
{
printf("Malloc memory error!\n");
return;
}
fread(pBuf, sizeof(char), fileLen, pFile);
fclose(pFile);
for (i=0; i<fileLen; i++)
{
if (IS_VALID_NUM(pBuf[i]) && isStringEqual(&pBuf[i], id, ID_LENGTH))
{
isFound = TRUE;
if (i + FILE_LINE_SIZE < fileLen)
{
memcpy(&pBuf[i], &pBuf[i+FILE_LINE_SIZE], fileLen - i - FILE_LINE_SIZE);
fileLen -= FILE_LINE_SIZE;
}
else
fileLen = i;
break;
}
}
if (isFound)
{
if (NULL == (pFile = fopen(fileName, "wb")))
{
printf("Open file %s error!\n", fileName);
return;
}
fwrite(pBuf, sizeof(char), fileLen, pFile);
fclose(pFile);
printf("%s is deleted!\n", id);
}
else
{
printf("%s is not found!\n", id);
}
free(pBuf);
}
void view()
{
FILE *pFile = NULL;
char *pBuf = NULL;
int fileLen;
if (NULL == (pFile = fopen(fileName, "rb")))
{
printf("Open file %s error!\n", fileName);
return;
}
fseek(pFile, 0, SEEK_END);
fileLen = ftell(pFile);
rewind(pFile);
if (NULL == (pBuf = (char *)malloc(sizeof(char) * (fileLen + 1))))
{
printf("Malloc memory error!\n");
return;
}
fread(pBuf, sizeof(char), fileLen, pFile);
pBuf[fileLen] = '\0';
printf("%s\n", pBuf);
free(pBuf);
fclose(pFile);
}
void sort(int direction)
{
FILE *pFile = NULL;
char *pBuf = NULL;
char *pLeft, *pRight;
char tmpBuf[FILE_LINE_SIZE - 2];
int fileLen;
int i, j;
int isFound = FALSE;
int pos = 0;
int count = 0;
int result = 0;
pStrCmpFunc compare;
if (direction == upSort)
compare = isLeftGreater;
else
compare = isLeftSmaller;
if (NULL == (pFile = fopen(fileName, "rb")))
{
printf("Open file %s error!\n", fileName);
return;
}
fseek(pFile, 0, SEEK_END);
fileLen = ftell(pFile);
rewind(pFile);
if (NULL == (pBuf = (char *)malloc(sizeof(char) * (fileLen + 1))))
{
printf("Malloc memory error!\n");
return;
}
fread(pBuf, sizeof(char), fileLen, pFile);
pBuf[fileLen] = '\0';
fclose(pFile);
for (i=0; i<fileLen; i++)
if (IS_VALID_NUM(pBuf[i]))
break;
pos = i;
while (i < fileLen)
{
if (IS_VALID_NUM(pBuf[i]))
{
count++;
i += FILE_LINE_SIZE;
}
}
for (i=0; i<count; i++)
{
result = i;
for (j=i+1; j<count; j++)
{
pLeft = pBuf + pos + result * FILE_LINE_SIZE;
pRight = pBuf + pos + j * FILE_LINE_SIZE;
if (compare(pLeft, pRight, ID_LENGTH))
{
result = j;
}
}
if (result != i)
{
pLeft = pBuf + pos + i * FILE_LINE_SIZE;
pRight = pBuf + pos + result * FILE_LINE_SIZE;
memcpy(tmpBuf, pLeft, sizeof(tmpBuf));
memcpy(pLeft, pRight, sizeof(tmpBuf));
memcpy(pRight, tmpBuf, sizeof(tmpBuf));
}
}
printf("%s\n", pBuf);
free(pBuf);
}
void up()
{
sort(upSort);
}
void down()
{
sort(downSort);
}
void showMenu()
{
printf("\n");
printf("======================================================\n");
printf("= 1. Show all students info =\n");
printf("= 2. Add a student info =\n");
printf("= 3. Delete a student info =\n");
printf("= 4. Sort up student info =\n");
printf("= 5. Sort down student info =\n");
printf("= 0. Exit =\n");
printf("======================================================\n");
printf("Please enter your choice: ");
}
int main()
{
char choice;
int quit = FALSE;
while (!quit)
{
showMenu();
scanf("%c", &choice);
switch(choice)
{
case '0':
quit = TRUE;
break;
case '1':
view();
break;
case '2':
add();
break;
case '3':
del();
break;
case '4':
up();
break;
case '5':
down();
break;
default:
printf("You enter a wrong choice!\n");
printf("Press any key to continue...\n");
fflush(stdin);
getch();
break;
}
fflush(stdin);
}
return 0;
}

第2个回答  2013-05-23

这个题目比较难写。我用到了文件操作,链表,二叉排序树。如果记录的条数有最大值的话,

大概用数组写的代码会更容易些。以下代码供参考:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct STU{
char no[7];
char name[10];
int age;
struct STU *next;
}RECORD; //对应list中数据的记录类型(用作链表节点)
RECORD* read(char*filename);
int add(FILE *fp, RECORD *pr);
typedef struct np{ //二叉排序树的节点类型
RECORD dat;
struct np *left,*right;
} node;
node *create(void)
{
return ((node*)malloc(sizeof(node)));
}
//生成降序排列用的二叉排序树
node *treeDOWN(node *list,RECORD d)
{
if (list==NULL) {
list=create();
list->left =list->right =NULL;
list->dat=d;
}
else if (strcmp(d.no,list->dat.no)<=0) {
list->right =treeDOWN(list->right,d);//大的挂在右边子树上
}
else if (strcmp(d.no,list->dat.no)>0) {
list->left =treeDOWN(list->left ,d);//小的挂在左边子树上
}
return list;
}
//升序排列用的二叉排序树
node *treeUP(node *list,RECORD d)
{
if (list==NULL) {
list=create();
list->left =list->right =NULL;
list->dat=d;
}
else if (strcmp(d.no,list->dat.no)>=0) {
list->right =treeUP(list->right,d);//小的挂在左边子树上
}
else if (strcmp(d.no,list->dat.no)<0) {
list->left =treeUP(list->left ,d);//大的挂在左边子树上
}
return list;
}
//通过中序遍历来排序
void inorder(node *r)
{
if (r!=NULL) {
inorder(r->left );
add(stdout,&(r->dat));//输出数据
inorder(r->right );
}
}
void delSortTree(node *t)
{
if(t==NULL) return;
else if(t->left==NULL && t->right==NULL)
free(t);
else
{
delSortTree(t->left);//删除左边子树
t->left=NULL;
delSortTree(t->right);//删除右边子树
t->right=NULL;
}
}
//从fp中读一行记录。当fp为stdin时,从键盘读
RECORD* input(FILE *fp)
{
if(fp!=stdin && (feof(fp) || ferror(fp)))
return NULL;
RECORD *p;
p=(RECORD *)calloc(1,sizeof(RECORD));
if(p==NULL)
{
printf("内存分配失败!\n");
return NULL;
}
if(fp==stdin)printf("请输入:");
if(fp==stdin)printf("学号 姓名 年龄 :\n");
if(3!=fscanf(fp,"%s%s%d",p->no, p->name, &p->age)) //读3个值
{
free(p);
return NULL;
}
p->next=NULL;
return p;
}
//增加一条记录到文件尾。返回是否成功
//文件必须已经以写方式打开
int add(FILE *fp, RECORD *pr)
{
if(fp!=stdout)fseek(fp,0,SEEK_END);
fprintf(fp,"%s\t%s\t%d\n",pr->no, pr->name,pr->age);
if(ferror(fp))return 0;
else return 1;
}
//删除学号对应记录,返回剩余记录条数
int del(char* filename, char *stu_no, RECORD* list)
{
FILE *fp=NULL;
if(NULL == (fp=fopen(filename,"w+"))){
printf("文件打开失败!\n");
exit(-1);
}
RECORD*p=list,*q=p;
int flag=0, count=0;
while(p)
{
if(strcmp(p->no,stu_no)==0)
{
flag=1;
break;
}
q=p;
p=p->next;
}
//找到了,删除之,并重写文件。
if(flag){
q->next=p->next;
printf("删除记录:\n");
add(stdout,p);
free(p);
}
fseek(fp,0,SEEK_SET);
fprintf(fp,"学号 姓名 年龄 \n");
p=list;
while(p)
{
add(fp,p);
count++;
p=p->next;
}
fclose(fp);
return count;
}
//返回记录条数
int view(char *filename)
{
RECORD *list=NULL;
list = read(filename);
int count=0;
RECORD *p=list, *q;
printf("-----------现有记录-----------\n");
while(p)
{
count ++;
printf("(%2d):",count);
add(stdout,p); //显示在标准输出
q=p;
p=p->next;
free(q);
}
return count;
}
//返回记录条数
//链表升序排列
node *up(RECORD** plist)
{
node *bst=NULL;
RECORD* p=*plist;
//char stuno[20]={'\0'};
while (p){
bst=treeUP(bst,*p); /*生成二叉排序数*/
p=p->next;
}
inorder(bst); /*输出二叉排序树*/
return bst;
}
//返回记录条数
//链表降序序排列:按学号排
node * down(RECORD** plist)
{
node *bst=NULL;
RECORD* p=*plist;
//char stuno[20]={'\0'};
while (p){
bst=treeDOWN(bst,*p); /*生成二叉排序数*/
p=p->next;
}
inorder(bst); /*输出二叉排序树*/
return bst;
}
//从文件中读取所有列表,创建链表,并把数据存入链表
RECORD* read(char* filename)
{
FILE *fp=NULL;
if(NULL == (fp=fopen(filename,"r+")))
{
printf("文件打开失败!\n");
exit(-1);
}
RECORD * p=NULL, *list=NULL, *q=NULL;
char s[101]={'\0'};
fseek(fp,0,SEEK_SET);
fgets((char*)s, 100, fp);//标题行
while(!feof(fp)&& !ferror(fp))
{
p = input(fp);
if(!p) break;
if(list==NULL){list=p;q=p;}
else{
q->next = p;
q=p;
}
}
fclose(fp);
return list;
}
int main()
{
char filename[32]=".\\list.txt";
RECORD *list=NULL, *p=NULL;
char stuno[]="000000";
list = read(filename);
printf("文件%s中的记录为:\n",filename);
view(filename);
printf("删除学生,学号为:%s,结果:\n",stuno);
del(filename,stuno,list);
view(filename);
//测试输入记录
/*
printf("------------输入记录------------\n");
p=input(stdin);
FILE *fp=NULL;
if(NULL == (fp=fopen(filename,"list+")))
{
printf("文件打开失败!\n");
exit(-1);
}
add(fp,p);
fclose(fp);
view(filename);
*/
printf("------------升序排列------------\n");
delSortTree(up(&list));
printf("------------降序排列------------\n");
delSortTree(down(&list));
free(p);
return 0;
}

第3个回答  2013-05-23
#include "stdio.h"//
#include "string.h"//
#include "stdlib.h"//
void add(void){
char tmp[21],px[10];
FILE *pfw;
printf("Enter StuId...\nSI=");
gets(tmp);
printf("Enter StuNm...\nSN=");
strcat(strcat(tmp," "),gets(px));
printf("Enter StuAg...\nSA=");
strcat(strcat(strcat(tmp," "),gets(px)),"\n");
printf("%s\n",tmp);
if(!(pfw=fopen("list.txt","a"))){
printf("Open w_file failure...");
exit(0);
}
fputs(tmp,pfw);
fclose(pfw);
}

void del(char txt[][29],char *p){
int i=0,j;
FILE *pfw;
while(txt[i][0]){
if(strstr(txt[i++],p)){
for(j=i-1;txt[j][0];strcpy(txt[j],txt[1+j++]));
txt[--j][0]='\0';
if(!(pfw=fopen("list.txt","w"))){
printf("Creat file failure...");
return;//
}
for(j=0;txt[j][0];fputs(txt[j++],pfw));
fclose(pfw);
return;
}
}
printf("No the line...\n");
}
void view(void){
char txt[100][29]={'\0',},i=0;
FILE *pfr;
if(!(pfr=fopen("list.txt","r"))){
printf("Open file failure...");
exit(0);
}
while(fgets(txt[i++],29,pfr));
fclose(pfr);
for(i=0;txt[i][0];printf("%s",txt[i++]));
}
void up(void){
char *p,*pt[100]={NULL,},txt[100][29]={'\0',},i=0,j;
FILE *pfr;
if(!(pfr=fopen("list.txt","r"))){
printf("Open R_file failure...");
exit(0);
}
while(fgets(txt[i++],29,pfr));
fclose(pfr);
pt[0]=txt[0];
for(i=1;txt[i][0];pt[i]=txt[i++]);
for(i=1;pt[i];i++)
for(j=i+1;pt[j];j++)
if(strcmp(pt[i],pt[j])>0){
p=pt[i];
pt[i]=pt[j];
pt[j]=p;
}
for(i=0;pt[i];printf("%s",pt[i++]));
}

void down(void){
char *p,*pt[100]={NULL,},txt[100][29]={'\0',},i=0,j;
FILE *pfr;
if(!(pfr=fopen("list.txt","r"))){
printf("Open R_file failure...");
exit(0);
}
while(fgets(txt[i++],29,pfr));
fclose(pfr);
pt[0]=txt[0];
for(i=1;txt[i][0];pt[i]=txt[i++]);
for(i=1;pt[i];i++)
for(j=i+1;pt[j];j++)
if(strcmp(pt[i],pt[j])<0){
p=pt[i];
pt[i]=pt[j];
pt[j]=p;
}
for(i=0;pt[i];printf("%s",pt[i++]));
}

void main(void){
char txt[100][29]={'\0',},i=0,ch;
FILE *pf;
if(!(pf=fopen("list.txt","r"))){
printf("Open file failure...");
exit(0);
}
while(fgets(txt[i++],29,pf));
fclose(pf);
printf("The file has been successfully read. view(y/n)?");
if((ch=getchar()&0xDF)=='Y'){
while(1){
fflush(stdin);
printf("v--view u--up d--down other--quit.\n");
if((ch=getchar()&0xDF)=='V') view();
else if(ch=='U') up();
else if(ch=='D') down();
else break;
}
}
}本回答被网友采纳
相似回答