C语言课程设计不少于200行代码

Python08

C语言课程设计不少于200行代码,第1张

#include "stdio.h"

#include"stdlib.h"

#include "conio.h"

#include"string.h"

struct SStudent

{

unsigned number

char name[10]

char tele[12]

struct SStudent * link

}

void main()

{

struct SStudent * CreateTable()

void AppendToTable(struct SStudent * stu)

void InsertToTable(struct SStudent * stu)

void QueryTable(struct SStudent * stu, unsigned number)

void SortTable(struct SStudent * stu)

void PrintTable(struct SStudent * stu)

void Save(struct SStudent * stu)

void Load(struct SStudent * stu)

void Help()

void modi(struct SStudent *h)

void search(struct SStudent *h)struct SStudent * student

unsigned number

char keyValue

student = CreateTable()

//clrscr()

system("cls")

Help()

printf("\n= ")

while((keyValue = getche()) != 'q' &&keyValue != 'Q' &&keyValue != 27)

{

puts("")

switch(keyValue)

{

case 'l': case 'L':

PrintTable(student)break

case 'd': case 'D':

printf("Please input the number you want delete: ")

scanf("%d", &number)

QueryTable(student, number)

break

case 'a': case 'A':

AppendToTable(student)break

case 'i': case 'I':

InsertToTable(student)break

case 's': case 'S':

SortTable(student)

puts("Sort complished! Please use command L to list.")

break

case 'f': case 'F':

search(student)

break

case 'm': case 'M':

modi(student)

breakcase 'v': case 'V':

Save(student)break

case 'o': case 'O':

Load(student)break

case 'h': case 'H':

Help()break

default: puts("Error command!")

}

printf("\n= ")

}

}

struct SStudent * CreateTable()

{

struct SStudent * stu

stu = (struct SStudent *) malloc(sizeof(struct SStudent))

stu->number = 0

stu->name[0] = '\0'

stu->tele[0] = '\0'

stu->link = NULL

return(stu)

}

void AppendToTable(struct SStudent * stu)

{

struct SStudent * next, * last

int number

last = stu

while(last->link) last = last->link

printf("Please input the number (0 to quit): ")

scanf("%d", &number)

while(number)

{

next = (struct SStudent *) malloc(sizeof(struct SStudent))

next->number = number

printf("Please input name: ")

scanf("%s", next->name)

printf("Please input tele: ")

scanf("%s", next->tele)

last->link = next

last = last->link

printf("\nPlease input the number (0 to quit): ")

scanf("%d", &number)

}

last->link = NULL

}

void InsertToTable(struct SStudent * stu)

{

struct SStudent * next, * last

int number

printf("Please input the number (0 to quit): ")

scanf("%d", &number)

while(number)

{

next = (struct SStudent *) malloc(sizeof(struct SStudent))

next->number = number

printf("Please input name: ")

scanf("%s", next->name)

printf("Please input tele: ")

scanf("%s", next->tele)

last = stu

while(last->link)

{

if(last->link->number >next->number)

{

next->link = last->link

last->link = next

break

}

else last = last->link

}

printf("\nPlease input the number (0 to quit): ")

scanf("%d", &number)

}

}

void QueryTable(struct SStudent * stu, unsigned number)

{

struct SStudent * temp, * next

next = stu

while(next->link)

{

if(next->link->number == number)

{

temp = next->link

next->link = next->link->link

free(temp)

}

else next = next->link

}

}

void PrintTable(struct SStudent * stu)

{

stu = stu->link

if(!stu)

{

puts("The table is EMPTY!")

return

}

printf("number\tname\ttele\n")

while(stu)

{

printf("%3d\t", stu->number)

printf("%-s\t", stu->name)

printf("%-s\t", stu->tele)

printf("\n")

stu = stu->link

}

}

void SortTable(struct SStudent * stu)

{

struct SStudent * next, * last, * temp

int flag

last = stu

while(last->link)

{

next = stuflag = 1

while(next->link != last->link)

{

if(next->link->number >last->link->number)

{

temp = last->link

last->link = last->link->link

temp->link = next->link

next->link = temp

flag = 0

break

}

else next = next->link

}

if(flag) last = last->link

}

}

void Save(struct SStudent * stu)

{

char filename[13]

FILE * fileSave

printf("Please input the filename you want save in: ")

scanf("%s", filename)

if((fileSave = fopen(filename, "wb")) == 0)

{

printf("Cannot open file %s !\n", filename)

return

}

puts("Saveing ...")

stu = stu->link

while(stu)

{

fwrite(stu, sizeof(struct SStudent), 1, fileSave)

stu = stu->link

}

puts("Saveing is finished!")

}

void Load(struct SStudent * stu)

{char filename[13]<br>FILE * fileLoad<br>struct SStudent * temp<br>while(stu->link)<br>{<br>temp = stu->link<br>stu->link = stu->link->link<br>free(temp)<br>}

temp = (struct SStudent *) malloc(sizeof(struct SStudent))

printf("Please input the filename you want load from: ")

scanf("%s", filename)

if((fileLoad = fopen(filename, "rb")) == 0)

{

printf("Cannot open file %s !\n", filename)

return

}

puts("Loading ...")

while(fread(temp, sizeof(struct SStudent), 1, fileLoad))

{stu->link = temp<br>stu = stu->link<br>temp = (struct SStudent *) malloc(sizeof(struct SStudent))<br>}

stu->link = NULL

puts("loading is finished!")

}

void Help()

{ puts(" *********************************************")

puts(" * System Command Help *")

puts(" *********************************************")

puts(" * L = List all records *")

puts(" * D = Delete a record seleced by number *")

puts(" * A = Append records *")

puts(" * I = Insert records *")

puts(" * S = Sort records *")

puts(" * F= Search records *")

puts(" * M= Modi records *")puts(" * H = Show this help message *")

puts(" * V = Save records to a file *")

puts(" * O = Load records from a file *")

puts(" * Q = Quit System *")

puts(" *********************************************")

}

void modi(struct SStudent *h)

{

struct SStudent *p /* 移动指针*/

unsigned num /*存放学号的变量*/

// clrscr() /*清屏幕*/

system("cls")

printf("please enter number for modifi\n")

scanf("%d",&num) /*输入学号*/

p=h /*将头指针赋给p*/

while( (p->number!=num)&&p!=NULL) /*当记录的姓名不是要找的,且指针不为空时*/

p=p->link/*移动指针,指向下一结点*/

if(p==NULL) /*如果指针为空*/

printf("\nlist no %d student\n",num) /*显示没有该学生*/

else /*修改找到的记录信息*/

{

printf("Please input new name: ")

scanf("%s", p->name)

printf("Please input new tele: ")

scanf("%s", p->tele)

printf("|number | name | tel | \n")

printf("|----------|---------------|---------------|\n")

printf("|%6d|%-10s|%12s|\n", p->number,p->name,p->tele) }

}

void search(struct SStudent *h)

{

struct SStudent *p /* 移动指针*/

char s[10] /*存放姓名的字符数组*/

// clrscr()/*清屏幕*/

system("cls")

printf("please enter name for search\n")

scanf("%s",s) /*输入姓名*/

p=h /*将头指针赋给p*/

while(strcmp(p->name,s)&&p!=NULL) /*当记录的姓名不是要找的,且指针不为空时*/

p=p->link/*移动指针,指向下一结点*/

if(p==NULL) /*如果指针为空*/

printf("\nlist no %s student\n",s) /*显示没有该学生*/

else /*显示找到的记录信息*/

{

printf("\n\n***********************havefound***********************\n")

printf("|number | name | tel | \n")

printf("|----------|---------------|---------------|\n")

printf("|%10d|%-10s|%12s|\n", p->number,p->name,p->tele)

printf("****************************end***************************\n")

}

}

#include <stdio.h> #define OK 1   

#define ERROR 0

#define TRUE 1   

#define FALSE 0 #define MAXSIZE 100 #define LElemType int

#define Status int

#define BOOL int typedef struct

{

 LElemType data

 int cur

}Component,SLinkList[MAXSIZE] int Malloc(SLinkList space) 

 //若备用链表非空,则返回分配的结点下标(备用链表的第一个结点),否则返回0

 int i=space[0].cur

 if (i)

  space[0].cur=space[i].cur

 return i

} Status Free(SLinkList space, int k) 

 //将下标为空的空闲结点回收到备用链表(成为备用链表的第一个结点)

 space[k].cur=space[0].cur

 space[0].cur=k

 return OK

} Status InitList(SLinkList L)

 //构造一个空的链表L,表头为L的最后一个单元L[MAXSIZE-1],其余单元链成

 //一个备用链表,表头为L的第一个单元L[0],“0”表示空指针

 int i

 L[MAXSIZE-1].cur=0

 for (i=0i<MAXSIZE-2i++)

  L[i].cur=i+1

 L[MAXSIZE-2].cur=0

 return OK

} Status ClearList(SLinkList L)

 //初始条件:线性表L已存在。操作结果:将L重置为空表

 int i,j,k

 i=L[MAXSIZE-1].cur

 L[MAXSIZE-1].cur=0

 k=L[0].cur

 L[0].cur=i

 while (i)

 {

  j=i

  i=L[i].cur

 }

 L[j].cur=k

 return OK

} BOOL ListEmpty(SLinkList L)

 //若L是空表,返回TRUE;否则返回FALSE

 if (!L[MAXSIZE-1].cur)

  return TRUE

 else

  return FALSE

} int ListLength(SLinkList L)

 //返回L中数据元素个数

 int i,len

 i=L[MAXSIZE-1].cur

 len=0

 while (i)

 {

  i=L[i].cur

  len++

 }

 return len

} Status GetElem(SLinkList L,int i,LElemType *e)

 //用e返回L中第i个元素的值

 int j,k=MAXSIZE-1

 if (i<1||i>ListLength(L))

  return ERROR

 for (j=1j<=ij++)

  k=L[k].cur

 *e=L[k].data

 return OK

} int LocateElem(SLinkList L,LElemType e) 

 //在静态单链线性表L中查找第1个值为e的元素。若找到,则返回它在L中的

 //位序,否则返回0。(与其它LocateElem()的定义不同)

 int i=L[MAXSIZE-1].cur

 while (i&&L[i].data!=e)

  i=L[i].cur

 return i

} Status PriorElem(SLinkList L,LElemType cur_e,LElemType *pre_e)

 //初始条件:线性表L已存在

 //操作结果:若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,

 //          否则操作失败,pre_e无定义

 int i,j

 i=L[MAXSIZE-1].cur 

 do{

  j=i

  i=L[i].cur

 }while (i&&L[i].data!=cur_e)

 if (i)

 {

  *pre_e=L[j].data

  return OK

 }

 return ERROR

} Status NextElem(SLinkList L,LElemType cur_e,LElemType *next_e)

 //初始条件:线性表L已存在

 //操作结果:若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,

 //          否则操作失败,next_e无定义

 int i,j

 i=LocateElem(L,cur_e)

 if (i)

 {

  j=L[i].cur

  if (j)

  {

   *next_e=L[j].data

   return OK

  }

 }

 return ERROR

} Status ListInsert(SLinkList L,int i,LElemType e)

 //在L中第i个元素之前插入新的数据元素e

 int j,k,l

 k=MAXSIZE-1

 if (i<1||i>ListLength(L)+1)

  return ERROR

 j=Malloc(L)

 if (j)

 {

  L[j].data=e

  for(l=1l<=i-1l++)

   k=L[k].cur

  L[j].cur=L[k].cur

  L[k].cur=j

  return OK

 }

 return ERROR

} Status ListDelete(SLinkList L,int i,LElemType *e)

{

 //删除在L中第i个数据元素e,并返回其值

 int j,k

 if (i<1||i>ListLength(L))

  return ERROR

 k=MAXSIZE-1

 for (j=1j<=i-1j++)

  k=L[k].cur

 j=L[k].cur

 L[k].cur=L[j].cur

 *e=L[j].data

 Free(L,j)

 return OK

} Status ListTraverse(SLinkList L,void (* visit)(LElemType e))

 int i=L[MAXSIZE-1].cur

 while (i)

 {

  visit(L[i].data)

  i=L[i].cur

 }

 return OK

} void Visit(LElemType e)

{

 printf("%d\n",e)

} int main()

{

 int i,j,k

 LElemType e,e0

 SLinkList L

 InitList(L)

 for(j=1j<=5j++)

  i=ListInsert(L,1,j)

 ListTraverse(L,Visit)

 //判断链表是否为空

 i=ListEmpty(L)

 printf("%d\n",i)

 //打印链表长度

 printf("%d\n",ListLength(L))

 //清空静态链表

 ClearList(L)

 ListTraverse(L,Visit)

 for(j=1j<=10j++)

  ListInsert(L,j,j)

 //插入新节点后

 ListTraverse(L,Visit)

 //获取链表中的第5个元素

 GetElem(L,5,&e)

 printf("%d\n",e)

 for(j=0j<=1j++)

 {

  k=LocateElem(L,j)

  if(k)

   printf("%d %d\n",j,k)//j在链表中的序号k

  else

   printf("Can't find %d\n",j)//链表中不存在j

 }

 for(j=1j<=2j++) //测试头两个数据

 {

  GetElem(L,j,&e0) //把第j个数据赋给e0

  i=PriorElem(L,e0,&e) //求e0的前驱

  if(!i)

   printf("No elem before %d\n",e0)

  else

   printf("Elem before %d is %d\n",e0,e)//数据e0的前驱

 }

 for(j=ListLength(L)-1j<=ListLength(L)j++) //最后两个数据

 {

  GetElem(L,j,&e0) //把第j个数据赋给e0

  i=NextElem(L,e0,&e) //求e0的后继

  if(!i)

   printf("No elem after %d\n",e0)

  else

   printf("The elem after % is %d\n",e0,e)//数据e0的后继

 }

 k=ListLength(L) //k为表长

 for(j=k+1j>=kj--)

 {

  i=ListDelete(L,j,&e) //删除第j个数据

  if(i)

   printf("Delete Succussfully\n")

  else

   printf("Can't find the elem\n",j)

 }

 ListTraverse(L,Visit)

 return 0

}

给你找了个静态链表的代码,能编译运行

给你一个通讯录管理系统程序 四百多行 够你用了吧!!! #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #define N 100 void input()//添加新用户函数 void amend()//修改用户信息函数 void delete_client()//删除用户信息函数 void demand_client()//用户信息查询函数 void collect_telephone()//用户信息汇总函数 void save_client(struct telephone message)//保存函数 void demand_name()//按用户名查询 void demand_telephone()//按电话号码查询 struct telephone { char client_name[20] char client_address[30] char client_telephone[15] } //添加新用户函数 void input() { struct telephone message char reply='y' char save='y' while (reply=='y') { printf("用户姓名:") scanf("%s",message.client_name) printf("电话号码:") scanf("%s",message.client_telephone) save_client(message) printf("要继续吗?(y/n):") scanf(" %c",&reply) } printf("按任意键返回主菜单……\n") getchar() getchar() } //保存函数 void save_client(struct telephone message) { FILE *fp fp=fopen("message.dat","a+") if (fp!=NULL) { fwrite(&message,sizeof(struct telephone),1,fp) } else { printf("\n打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) } //修改用户信息函数 void amend() { struct telephone message FILE *fp char amend_name[20] char reply='y' char found='y' char save='y' int size=sizeof(struct telephone) while (reply=='y') { found='n' fp=fopen("message.dat","r+w") if (fp!=NULL) { system("cls") printf("\n请输入要修改的姓名:") scanf("%s",amend_name) while ((fread(&message,size,1,fp))==1) { if ((strcmp(amend_name,message.client_name))==0) { found='y' break } } if (found=='y') { printf("==========================================\n") printf("\n用户姓名:%s\n",message.client_name) printf("\n电话号码:%s\n",message.client_telephone) printf("==========================================\n") printf("修改用户信息:\n") printf("\n用户姓名:") scanf("%s",message.client_name) printf("\n电话号码:") scanf("%s",message.client_telephone) printf("\n要保存吗?(y/n):") scanf(" %c",&save) if (save=='y') { fseek(fp,-size,1) fwrite(&message,sizeof(struct telephone),1,fp) } } else { printf("无此人信息!\n") } } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) printf("要继续吗?(y/n):") scanf(" %c",&reply) } printf("按任意键返回主菜单……\n") getchar() getchar() } //删除用户信息函数 void delete_client() { struct telephone message[N] struct telephone temp_str struct telephone delete_str int i=0,j=0 char reply='y' char found='y' char confirm='y' char delete_name[20] FILE *fp while (reply=='y') { system("cls") fp=fopen("message.dat","r") if (fp!=NULL) { i=0 found='n' printf("\n请输入姓名:") scanf("%s",delete_name) while ((fread(&temp_str,sizeof(struct telephone),1,fp))==1) { if ((strcmp(delete_name,temp_str.client_name))==0) { found='y' delete_str=temp_str }//查找要删除的记录 else { message[i]=temp_str i++ }//将其它无关记录保存起来 } } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) if (found=='y') { printf("==========================================\n") printf("用户姓名:%s\n",delete_str.client_name) printf("电话号码:%s\n",delete_str.client_telephone) printf("==========================================\n") } else { printf("无此人信息,按任意键返回……\n") getchar() break } printf("确定要删除吗?(y/n):") scanf(" %c",&confirm) if (confirm=='y') { fp=fopen("message.dat","w") if (fp!=NULL) { for(j=0j<ij++) { fwrite(&message[j],sizeof(struct telephone),1,fp) } printf("记录已删除!!!\n") } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) } printf("要继续吗?(y/n):") scanf(" %c",&reply) } printf("按任意键返回主菜单……\n") getchar() } //用户信息查询函数 void demand_client() { int choice=1 while (choice!=3) { system("cls") printf("电话查询菜单\n") printf(" 1 按联系人姓名查询\n") printf(" 2 按联系人电话号码查询\n") printf(" 3 返回主菜单\n") printf("请选择(1-3):") scanf("%d%*c",&choice) if (choice>3) { printf("请输入1-6之间的整数\n") printf("按任意键返回菜单……\n") getchar() continue } if (choice==1) { demand_name() } else if (choice==2) { demand_telephone() } } } //按用户名查询 void demand_name() { struct telephone message FILE *fp char amend_name[20] char reply='y' char found='y' while (reply=='y') { found='n' fp=fopen("message.dat","r+w") if (fp!=NULL) { system("cls") printf("\n请输入姓名:") scanf("%s",amend_name) while ((fread(&message,sizeof(struct telephone),1,fp))==1) { if ((strcmp(amend_name,message.client_name))==0) { found='y' break } } if (found=='y') { printf("==========================================\n") printf("用户姓名:%s\n",message.client_name) printf("电话号码:%s\n",message.client_telephone) printf("==========================================\n") } else { printf("无此人信息!\n") } } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) printf("要继续吗?(y/n):") scanf(" %c",&reply) } printf("按任意键返回主菜单……\n") getchar() getchar() } //按电话号码查询 void demand_telephone() { struct telephone message FILE *fp char telephone[20] char reply='y' char found='y' while (reply=='y') { found='n' fp=fopen("message.dat","r+w") if (fp!=NULL) { system("cls") printf("\n请输入电话号码:") scanf("%s",telephone) while ((fread(&message,sizeof(struct telephone),1,fp))==1) { if ((strcmp(telephone,message.client_telephone))==0) { found='y' break } } if (found=='y') { printf("==========================================\n") printf("用户姓名:%s\n",message.client_name) printf("电话号码:%s\n",message.client_telephone) printf("==========================================\n") } else { printf("无此电话号码的有关信息!\n") } } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) printf("要继续吗?(y/n):") scanf(" %c",&reply) } printf("按任意键返回主菜单……\n") getchar() getchar() } //用户信息汇总函数 void collect_telephone() { struct telephone message FILE *fp fp=fopen("message.dat","r") if (fp!=NULL) { system("cls") printf("\n用户姓名\t\t电话号码\n") while ((fread(&message,sizeof(struct telephone),1,fp))==1) { printf("\n%-24s",message.client_name) printf("%-12s\n",message.client_telephone) } } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) printf("按任意键返回主菜单……\n") getch() } void main() { char choice[10]="" int len=0 while (choice[0]!='7') { printf("\t==========电话本号码查询系统=============\n") printf("\t\t 1、添加新联系人\n") printf("\t\t 2、修改联系人信息\n") printf("\t\t 3、删除联系人信息\n") printf("\t\t 4、联系人信息查询\n") printf("\t\t 5、联系人信息汇总\n") printf("\t\t 7、退出\n") printf("\t=========================================\n") printf("请选择(1-7):") scanf("%s",choice) len=strlen(choice) if (len>1) { printf("请输入1-6之间的整数\n") printf("按任意键返回主菜单……\n") getchar() getchar() continue } switch (choice[0]) { case '1': input() break case '2': amend() break case '3': delete_client() break case '4': demand_client() break case '5': collect_telephone() break default: break } } }