本文目录一览:
C语言课程设计不少于200行代码
#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);
break;case '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 = stu; flag = 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];brFILE * fileLoad;brstruct SStudent * temp;brwhile(stu-link)br{brtemp = stu-link;brstu-link = stu-link-link;brfree(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;brstu = stu-link;brtemp = (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");
}
}
200行的c语言代码,51单片机运行完要多长时间,没有循环结构
如果没有调用子程序的话,12M晶振时那么执行时间大概是200~600微妙
急求啊,写一个200行的C语言程序,什么都可以,带解释,哪位大神帮帮忙
#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=0;iMAXSIZE-2;i++)
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 (i1||iListLength(L))
return ERROR;
for (j=1;j=i;j++)
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 (iL[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 (iL[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 (i1||iListLength(L)+1)
return ERROR;
j=Malloc(L);
if (j)
{
L[j].data=e;
for(l=1;l=i-1;l++)
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 (i1||iListLength(L))
return ERROR;
k=MAXSIZE-1;
for (j=1;j=i-1;j++)
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=1;j=5;j++)
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=1;j=10;j++)
ListInsert(L,j,j);
//插入新节点后
ListTraverse(L,Visit);
//获取链表中的第5个元素
GetElem(L,5,e);
printf("%d\n",e);
for(j=0;j=1;j++)
{
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=1;j=2;j++) //测试头两个数据
{
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)-1;j=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+1;j=k;j--)
{
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;
}
给你找了个静态链表的代码,能编译运行