c语言100行简单一点的代码
登录幼儿园200个小朋友的数据:姓名、性别、年龄、身高、体重、出生日期,分别按年龄排序后输出。
#includestdio.h
#define N 200
struct child
{
char name[10];
char sex[3];
int age;
int height;
float weight;
struct {
int year;
int month;
int day;
}bdate;
}ch[N];
void input()
{
int i;
for(i=0;iN;i++)
{
printf("\n请输入第%d名小朋友信息:\n",i+1);
printf("姓名:");
scanf("%s",ch[i].name);
printf("性别:");
scanf("%s",ch[i].sex);
printf("年龄:");
scanf("%d",ch[i].age);
printf("身高:");
scanf("%d",ch[i].height);
printf("体重:");
scanf("%f",ch[i].weight);
printf("出生日期[YYYY-MM-DD]:");
scanf("%d-%d-%d",ch[i].bdate.year,ch[i].bdate.month,ch[i].bdate.day);
}
}
void sort()
{
struct child ct;
int i,j;
for(i=0;iN-1;i++)
for(j=0;jN-i-1;j++)
if(ch[j].heightch[j+1].height)
{
ct=ch[j];
ch[j]=ch[j+1];
ch[j+1]=ct;
}
}
void output()
{
int i;
printf("\n\t幼儿园小朋友一览(依身高排序)\n");
printf("===================================================\n");
printf(" 姓名 性别 年龄 身高 体重 出生日期 \n");
printf("===================================================\n");
for(i=0;iN;i++)
printf(" %-8s %-2s %2d %d %3.1f %d.%d.%d\n",ch[i].name,ch[i].sex,ch[i].age,ch[i].height,ch[i].weight,ch[i].bdate.year,ch[i].bdate.month,ch[i].bdate.day);
}
void main()
{
input();
sort();
output();
}
C语言程序100行
//学生成绩管理系统C代码
/*头文件*/
#include stdio.h
#includedos.h
#includestdlib.h /*其它说明*/
#includestring.h /*字符串函数*/
#includemem.h /*内存操作函数*/
#includectype.h /*字符操作函数*/
#includealloc.h /*动态地址分配函数*/
#define LEN sizeof(STUDENT)
typedef struct stu /*定义结构体数组用于缓存数据*/
{
char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}STUDENT;
/*函数原型*/
STUDENT *init(); /*初始化函数*/
int menu_select(); /*菜单函数*/
STUDENT *create(); /*创建链表*/
void print(STUDENT *head); /* 显示全部记录*/
void search(STUDENT *head); /*查找记录*/
STUDENT *delete(STUDENT *head); /*删除记录*/
STUDENT *sort(STUDENT *head); /*排序*/
STUDENT *insert(STUDENT *head,STUDENT *newnode); /*插入记录*/
void save(STUDENT *head); /*保存文件*/
STUDENT *load(); /*读文件*/
/*主函数界面*/
main()
{
STUDENT *head,newnode;
head=init(); /*链表初始化,使head的值为NULL*/
for(;;) /*循环无限次*/
{
switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,newnode);break; /*newnode表示返回地址*/
case 7:save(head);break;
case 8:head=load(); break;
case 9:exit(0); /*如菜单返回值为9则程序结束*/
}
}
}
/*初始化函数*/
STUDENT *init()
{
return NULL; /*返回空指针*/
}
/*菜单选择函数*/
menu_select()
{
int n;
struct date d; /*定义时间结构体*/
getdate(d); /*读取系统日期并把它放到结构体d中*/
printf("press any key to enter the menu......"); /*按任一键进入主菜单*/
getch(); /*从键盘读取一个字符,但不显示于屏幕*/
clrscr(); /*清屏*/
printf("********************************************************************************\n");
printf("\t\t Welcome to\n");
printf("\n\t\t The student score manage system\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. Enter the record\n"); /*输入学生成绩记录*/
printf("\t\t\t2. Print the record\n"); /*显示*/
printf("\t\t\t3. Search record on name\n"); /*寻找*/
printf("\t\t\t4. Delete a record\n"); /*删除*/
printf("\t\t\t5. Sort to make new a file\n"); /*排序*/
printf("\t\t\t6. Insert record to list\n"); /*插入*/
printf("\t\t\t7. Save the file\n"); /*保存*/
printf("\t\t\t8. Load the file\n"); /*读取*/
printf("\t\t\t9. Quit\n"); /*退出*/
printf("\n\t\t Made by Hu Haihong.\n");
printf("********************************************************************************\n");
printf("\t\t\t\t%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day); /*显示当前系统日期*/
do{
printf("\n\t\t\tEnter your choice(1~9):");
scanf("%d",n);
}while(n1||n9); /*如果选择项不在1~9之间则重输*/
return(n); /*返回选择项,主函数根据该数调用相应的函数*/
}
/*输入函数*/
STUDENT *create()
{
int i,s;
STUDENT *head=NULL,*p; /* 定义函数.此函数带回一个指向链表头的指针*/
clrscr();
for(;;)
{p=(STUDENT *)malloc(LEN); /*开辟一个新的单元*/
if(!p) /*如果指针p为空*/
{printf("\nOut of memory."); /*输出内存溢出*/
return (head); /*返回头指针,下同*/
}
printf("Enter the num(0:list end):");
scanf("%s",p-num);
if(p-num[0]=='0') break; /*如果学号首字符为0则结束输入*/
printf("Enter the name:");
scanf("%s",p-name);
printf("Please enter the %d scores\n",3); /*提示开始输入成绩*/
s=0; /*计算每个学生的总分,初值为0*/
for(i=0;i3;i++) /*3门课程循环3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",p-score[i]);
if(p-score[i]0 || p-score[i]100) /*确保成绩在0~100之间*/
printf("Data error,please enter again.\n");
}while(p-score[i]0 || p-score[i]100);
s=s+p-score[i]; /*累加各门成绩*/
}
p-sum=s; /*将总分保存*/
p-average=(float)s/3; /*先用强制类型转换将s转换成float型,再求平均值*/
p-order=0; /*未排序前此值为0*/
p-next=head; /*将头结点做为新输入结点的后继结点*/
head=p; /*新输入结点为新的头结点*/
}
return(head);
}
/* 显示全部记录函数*/
void print(STUDENT *head)
{
int i=0; /* 统计记录条数*/
STUDENT *p; /*移动指针*/
clrscr();
p=head; /*初值为头指针*/
printf("\n************************************STUDENT************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Rec | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
i, p-num,p-name,p-score[0],p-score[1],p-score[2],p-sum,p-average,p-order);
p=p-next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}
/*查找记录函数*/
void search(STUDENT *head)
{
STUDENT *p; /* 移动指针*/
char s[5]; /*存放姓名用的字符数组*/
clrscr();
printf("Please enter name for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p-name,s) p != NULL) /*当记录的姓名不是要找的,或指针不为空时*/
p=p-next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p-num,p-name,p-score[0],p-score[1],p-score[2],p-sum,p-average,p-order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no num %s student on the list.\n",s); /*显示没有该学生*/
}
/*删除记录函数*/
STUDENT *delete(STUDENT *head)
{int n;
STUDENT *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/
char c,s[6]; /*s[6]用来存放学号,c用来输入字母*/
clrscr();
printf("Please enter the deleted num: ");
scanf("%s",s);
p1=p2=head; /*给p1和p2赋初值头指针*/
while(strcmp(p1-num,s) p1 != NULL) /*当记录的学号不是要找的,或指针不为空时*/
{p2=p1; /*将p1指针值赋给p2作为p1的前驱指针*/
p1=p1-next; /*将p1指针指向下一条记录*/
}
if(strcmp(p1-num,s)==0) /*学号找到了*/
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p1-num,p1-name,p1-score[0],p1-score[1],p1-score[2],p1-sum,p1-average,p1-order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("Are you sure to delete the student Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/
for(;;)
{scanf("%c",c);
if(c=='n'||c=='N') break; /*如果不删除,则跳出本循环*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,说明被删结点是首结点*/
head=p1-next; /*把第二个结点地址赋予head*/
else
p2-next=p1-next; /*否则将一下结点地址赋给前一结点地址*/
n=n-1;
printf("\nNum %s student have been deleted.\n",s);
printf("Don't forget to save.\n");break; /*删除后就跳出循环*/
}
}
}
else
printf("\nThere is no num %s student on the list.\n",s); /*找不到该结点*/
return(head);
}
/*排序函数*/
STUDENT *sort(STUDENT *head)
{int i=0; /*保存名次*/
STUDENT *p1,*p2,*t,*temp; /*定义临时指针*/
temp=head-next; /*将原表的头指针所指的下一个结点作头指针*/
head-next=NULL; /*第一个结点为新表的头结点*/
while(temp!=NULL) /*当原表不为空时,进行排序*/
{
t=temp; /*取原表的头结点*/
temp=temp-next; /*原表头结点指针后移*/
p1=head; /*设定移动指针p1,从头指针开始*/
p2=head; /*设定移动指针p2做为p1的前驱,初值为头指针*/
while(t-averagep1-averagep1!=NULL) /*作成绩平均分比较*/
{
p2=p1; /*待排序点值小,则新表指针后移*/
p1=p1-next;
}
if(p1==p2) /*p1==p2,说明待排序点值大,应排在首位*/
{
t-next=p1; /*待排序点的后继为p*/
head=t; /*新头结点为待排序点*/
}
else /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
{
t-next=p1; /*t的后继是p1*/
p2-next=t; /*p2的后继是t*/
}
}
p1=head; /*已排好序的头指针赋给p1,准备填写名次*/
while(p1!=NULL) /*当p1不为空时,进行下列操作*/
{
i++; /*结点序号*/
p1-order=i; /*将结点序号赋值给名次*/
p1=p1-next; /*指针后移*/
}
printf("Sorting is sucessful.\n"); /*排序成功*/
return (head);
}
/*插入记录函数*/
STUDENT *insert(STUDENT *head,STUDENT *newnode)
{STUDENT *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一个结点*/
p0=newnode; /*p0指向要插入的结点*/
printf("\nPlease enter a newnode record.\n"); /*提示输入记录信息*/
printf("Enter the num:");
scanf("%s",newnode-num);
printf("Enter the name:");
scanf("%s",newnode-name);
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新记录的总分,初值为0*/
for(i=0;i3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",newnode-score[i]);
if(newnode-score[i]100||newnode-score[i]0)
printf("Data error,please enter again.\n");
}while(newnode-score[i]100||newnode-score[i]0);
sum1=sum1+newnode-score[i]; /*累加各门成绩*/
}
newnode-sum=sum1; /*将总分存入新记录中*/
newnode-average=(float)sum1/3;
newnode-order=0;
if(head==NULL) /*原来的链表是空表*/
{head=p0;p0-next=NULL;} /*使p0指向的结点作为头结点*/
else
{while((p0-averagep1-average)(p1-next!=NULL))
{p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1-next; /*p1后移一个结点*/
}
if(p0-average=p1-average)
{if(head==p1)head=p0; /*插到原来第一个结点之前*/
else p2-next=p0; /*插到p2指向的结点之后*/
p0-next=p1;}
else
{p1-next=p0;p0-next=NULL;} /*插到最后的结点之后*/
}
n=n+1; /*结点数加1*/
head=sort(head); /*调用排序的函数,将学生成绩重新排序*/
printf("\nStudent %s have been inserted.\n",newnode-name);
printf("Don't forget to save the newnode file.\n");
return(head);
}
/*保存数据到文件函数*/
void save(STUDENT *head)
{FILE *fp; /*定义指向文件的指针*/
STUDENT *p; /* 定义移动指针*/
char outfile[10];
printf("Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,为只写方式*/
{
printf("Cannot open the file\n");
return; /*若打不开则返回菜单*/
}
printf("\nSaving the file......\n");
p=head; /*移动指针从头指针开始*/
while(p!=NULL) /*如p不为空*/
{
fwrite(p,LEN,1,fp); /*写入一条记录*/
p=p-next; /*指针后移*/
}
fclose(fp); /*关闭文件*/
printf("Save the file successfully!\n");
}
/* 从文件读数据函数*/
STUDENT *load()
{STUDENT *p1,*p2,*head=NULL; /*定义记录指针变量*/
FILE *fp; /* 定义指向文件的指针*/
char infile[10];
printf("Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为只读方式*/
{
printf("Can not open the file.\n");
return(head);
}
printf("\nLoading the file!\n");
p1=(STUDENT *)malloc(LEN); /*开辟一个新单元*/
if(!p1)
{
printf("Out of memory!\n");
return(head);
}
head=p1; /*申请到空间,将其作为头指针*/
while(!feof(fp)) /*循环读数据直到文件尾结束*/
{
if(fread(p1,LEN,1,fp)!=1) break; /*如果没读到数据,跳出循环*/
p1-next=(STUDENT *)malloc(LEN); /*为下一个结点开辟空间*/
if(!p1-next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1-next; /*指针后移,新读入数据链到当前表尾*/
}
p2-next=NULL; /*最后一个结点的后继指针为空*/
fclose(fp);
printf("You have success to read data from the file!\n");
return (head);
}
满意请采纳。
求3个100行左右的C语言程序 要代码 无限感谢!!!!
第1个,剪刀石头布,111行
#includeiostream
#includestring
#includectime
using namespace std;
int main()
{
string playStr; //玩家输入的选择
int playWin=0; //玩家赢
int cptWin=0; //电脑赢
int noWin=0; //平局
int cpt; //电脑出什么
int sum=0; //玩了几局
float win=0; //胜效率
begin:
sum=playWin+cptWin+noWin;
if(sum==0)
{
sum=1;
}
if(sum-noWin!=0)
{
win=(float)playWin/(float)(sum-noWin)*100;
}
else
{
win=0;
}
cout"游戏状态:"endlendl" 玩家赢:"playWin
" 电脑赢:"cptWin
" 平局:"noWin" 总局数:"sum" 胜率:"win
"%"endlendl;
cout"请出拳(1-剪刀 2-石头 3-布 Q-退出)"endl;
cinplayStr;
srand(time(0));
cpt=rand()%3+1;
if(cpt==1) //电脑出 剪刀
{
cout"电脑出剪刀"endl;
if(playStr[0]=='1')
{
cout"玩家出剪刀,平局."endl;
noWin++;
}
else if(playStr[0]=='2')
{
cout"玩家出石头,玩家赢."endl;
playWin++;
}
else
{
cout"玩家出布,玩家输."endl;
cptWin++;
}
}
else if(cpt==2)
{
cout"电脑出石头"endl;
if(playStr[0]=='1')
{
cout"玩家出剪刀,玩家输."endl;
cptWin++;
}
else if(playStr[0]=='2')
{
cout"玩家出石头,平局."endl;
noWin++;
}
else
{
cout"玩家出剪布,玩家赢."endl;
playWin++;
}
}
else
{
cout"电脑出布"endl;
if(playStr[0]=='1')
{
cout"玩家出剪刀,玩家赢."endl;
playWin++;
}
else if(playStr[0]=='2')
{
cout"玩家出石头,玩家输."endl;
cptWin++;
}
else
{
cout"玩家出布,平局."endl;
noWin++;
}
}
if(playStr[0]=='q'||playStr[0]=='Q')
{
return 0;
}
else
{
getchar();
getchar();
system("cls");
goto begin;
}
}
第2个:还是111行
#includeiostream
using namespace std;
bool Is(long num);
void Print(long i, long num1, long num2);
int main()
{
long m,m1,m2;
long a,sum;
long i1=0;
begin:
cout"请输入要分解的数字:"endl;
cinm;
if(m1)
{
goto begin;
}
m2=m;
coutendl;
cout"----------------------------------------"endl;
m1=m;
cout" "mendl;
if(Is(m))
{
cout" / \\"endl;
cout" ""1"" "mendl;
}
else
{
for(long i=2; im; )
{
if( (i%2!=0) || (i==2 ) Is(i))
{
a=m%i;
if(a==0)
{
m/=i;
sum*=i;
Print(i1,i,m);
i1++;
if(sum==m1 || (Is(m)) )
{
i1=0;
break;
}
}
else
{
i++;
}
}
else
{
i++;
}
}
}
coutendl"----------------------------------------"endlendl;
return ;
}
bool Is(long num)
{
long m=0;
bool is=false;
if(num==2)
{
return true;
}
if(num%2!=0)
{
m=num+1;
m/=2;
}
else
{
return false;
}
long i;
for( i=2; i=m; i++)
{
if(num%i!=0)
{
is=true;
}
else
{
is=false;
break;
}
}
return is;
}
void Print(long i, long num1, long num2)
{
for(long j=0; j=i+2; j++)
{
cout" ";
}
cout" / \\"endl;
for( j=0; j=i+2; j++)
{
cout" ";
}
cout" "num1" "num2endl;
}
第3个,统计字符串的个数,这个200多行
#include iostream
#include string
#include iomanip
using namespace std;
struct WordNode
{
int num;
char word[3];
WordNode* pNext;
};
WordNode *pHead;
void Fun(char* str)
{
WordNode* pNew= new WordNode;
pNew-pNext = NULL;
char ch[3];
begin:
for(int i=0; istrlen(str); i++)
{
memset( ch, 0, 3);
if( str[i] = 0 str[i] =127 )
{
ch[0] = str[i];
ch[1] = ' ';
}
else
{
ch[0] = str[i];
ch[1] = str[++i];
}
WordNode* head = pHead;
bool flag = false;
if ( !pHead)
{
pNew -num = 0;
strcpy( pNew -word, ch);
pHead = pNew ;
goto begin;
}
while( head )
{
if ( !strcmp( ch, head-word))
{
flag = true;
break;
}
head = head -pNext;
}
if ( flag == true )
{
head -num ++;
}
else
{
pNew = new WordNode;
pNew -num = 1;
strcpy( pNew -word, ch);
pNew -pNext = NULL;
for( WordNode* loop = pHead; loop-pNext; loop= loop-pNext)
{
}
loop -pNext = pNew;
}
}
}
void Print(WordNode* pFirst)
{
cout endl "统计结果:" endl endl;
int maxNum = -1;
int wordNum = 0;
WordNode* head = pFirst;
while ( head )
{
if( maxNum head-num )
{
maxNum = head-num;
}
wordNum ++;
head = head-pNext;
}
head = pFirst;
int num = 0;
cout "-----------------------------------------------------------------" endl;
for( int i=0; iwordNum; i++)
{
num = head-num;
cout head-word " : " ;
for(int j=0; jmaxNum; j++)
{
if( head -num 0 )
{
cout "*" ;
}
else
{
cout " " ;
}
head-num --;
}
cout setw( maxNum + 2 ) num ;
head-num = num;
head = head-pNext;
cout endl;
}
cout "-----------------------------------------------------------------" endl;
head = pFirst;
int layer = maxNum;
while (head)
{
cout head-num setw( 2 ) ;
head = head-pNext;
}
cout endl;
head = pFirst;
for(int j=0; jmaxNum; j++)
{
WordNode* node = head;
for( int i=0; iwordNum; i++)
{
if( layer == node-num )
{
cout "* " ;
node-num --;
}
else
{
cout " " ;
}
node = node-pNext;
}
layer --;
cout endl;
}
head = pFirst;
while( head )
{
cout head-word ;
head = head -pNext;
}
cout endl "-----------------------------------------------------------------" endl;
}
void DeleteWordNode(WordNode* pFirst)
{
WordNode* cp, *np;
cp = pFirst;
while ( cp)
{
np = cp-pNext;
delete cp;
cp = np;
}
pFirst = NULL;
}
int main()
{
char *str = new char[10000];
try
{
if ( str == NULL)
{
throw(str);
}
cout "输入要统计的字符串: " endl;
cin.getline( str, 10000 );
Fun(str);
Print(pHead);
}
catch( char* )
{
cout "动态申请空间发生错误,程序自动退出." endl;
exit( 1 );
}
DeleteWordNode(pHead);
delete []str;
getchar();
return 0;
}
这些都自己写的小程序,你可以看下
跪求100行左右的c语言简单代码,大一水平就行,什么类型都可以。
//学生成绩管理系统C代码
/*头文件*/
#include stdio.h
#includedos.h
#includestdlib.h /*其它说明*/
#includestring.h /*字符串函数*/
#includemem.h /*内存操作函数*/
#includectype.h /*字符操作函数*/
#includealloc.h /*动态地址分配函数*/
#define LEN sizeof(STUDENT)
typedef struct stu /*定义结构体数组用于缓存数据*/
{
char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}STUDENT;
/*函数原型*/
STUDENT *init(); /*初始化函数*/
int menu_select(); /*菜单函数*/
STUDENT *create(); /*创建链表*/
void print(STUDENT *head); /* 显示全部记录*/
void search(STUDENT *head); /*查找记录*/
STUDENT *delete(STUDENT *head); /*删除记录*/
STUDENT *sort(STUDENT *head); /*排序*/
STUDENT *insert(STUDENT *head,STUDENT *newnode); /*插入记录*/
void save(STUDENT *head); /*保存文件*/
STUDENT *load(); /*读文件*/
/*主函数界面*/
main()
{
STUDENT *head,newnode;
head=init(); /*链表初始化,使head的值为NULL*/
for(;;) /*循环无限次*/
{
switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,newnode);break; /*newnode表示返回地址*/
case 7:save(head);break;
case 8:head=load(); break;
case 9:exit(0); /*如菜单返回值为9则程序结束*/
}
}
}
/*初始化函数*/
STUDENT *init()
{
return NULL; /*返回空指针*/
}
/*菜单选择函数*/
menu_select()
{
int n;
struct date d; /*定义时间结构体*/
getdate(d); /*读取系统日期并把它放到结构体d中*/
printf("press any key to enter the menu......"); /*按任一键进入主菜单*/
getch(); /*从键盘读取一个字符,但不显示于屏幕*/
clrscr(); /*清屏*/
printf("********************************************************************************\n");
printf("\t\t Welcome to\n");
printf("\n\t\t The student score manage system\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. Enter the record\n"); /*输入学生成绩记录*/
printf("\t\t\t2. Print the record\n"); /*显示*/
printf("\t\t\t3. Search record on name\n"); /*寻找*/
printf("\t\t\t4. Delete a record\n"); /*删除*/
printf("\t\t\t5. Sort to make new a file\n"); /*排序*/
printf("\t\t\t6. Insert record to list\n"); /*插入*/
printf("\t\t\t7. Save the file\n"); /*保存*/
printf("\t\t\t8. Load the file\n"); /*读取*/
printf("\t\t\t9. Quit\n"); /*退出*/
printf("\n\t\t Made by Hu Haihong.\n");
printf("********************************************************************************\n");
printf("\t\t\t\t%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day); /*显示当前系统日期*/
do{
printf("\n\t\t\tEnter your choice(1~9):");
scanf("%d",n);
}while(n1||n9); /*如果选择项不在1~9之间则重输*/
return(n); /*返回选择项,主函数根据该数调用相应的函数*/
}
/*输入函数*/
STUDENT *create()
{
int i,s;
STUDENT *head=NULL,*p; /* 定义函数.此函数带回一个指向链表头的指针*/
clrscr();
for(;;)
{p=(STUDENT *)malloc(LEN); /*开辟一个新的单元*/
if(!p) /*如果指针p为空*/
{printf("\nOut of memory."); /*输出内存溢出*/
return (head); /*返回头指针,下同*/
}
printf("Enter the num(0:list end):");
scanf("%s",p-num);
if(p-num[0]=='0') break; /*如果学号首字符为0则结束输入*/
printf("Enter the name:");
scanf("%s",p-name);
printf("Please enter the %d scores\n",3); /*提示开始输入成绩*/
s=0; /*计算每个学生的总分,初值为0*/
for(i=0;i3;i++) /*3门课程循环3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",p-score[i]);
if(p-score[i]0 || p-score[i]100) /*确保成绩在0~100之间*/
printf("Data error,please enter again.\n");
}while(p-score[i]0 || p-score[i]100);
s=s+p-score[i]; /*累加各门成绩*/
}
p-sum=s; /*将总分保存*/
p-average=(float)s/3; /*先用强制类型转换将s转换成float型,再求平均值*/
p-order=0; /*未排序前此值为0*/
p-next=head; /*将头结点做为新输入结点的后继结点*/
head=p; /*新输入结点为新的头结点*/
}
return(head);
}
/* 显示全部记录函数*/
void print(STUDENT *head)
{
int i=0; /* 统计记录条数*/
STUDENT *p; /*移动指针*/
clrscr();
p=head; /*初值为头指针*/
printf("\n************************************STUDENT************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Rec | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
i, p-num,p-name,p-score[0],p-score[1],p-score[2],p-sum,p-average,p-order);
p=p-next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}
/*查找记录函数*/
void search(STUDENT *head)
{
STUDENT *p; /* 移动指针*/
char s[5]; /*存放姓名用的字符数组*/
clrscr();
printf("Please enter name for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p-name,s) p != NULL) /*当记录的姓名不是要找的,或指针不为空时*/
p=p-next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p-num,p-name,p-score[0],p-score[1],p-score[2],p-sum,p-average,p-order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no num %s student on the list.\n",s); /*显示没有该学生*/
}
/*删除记录函数*/
STUDENT *delete(STUDENT *head)
{int n;
STUDENT *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/
char c,s[6]; /*s[6]用来存放学号,c用来输入字母*/
clrscr();
printf("Please enter the deleted num: ");
scanf("%s",s);
p1=p2=head; /*给p1和p2赋初值头指针*/
while(strcmp(p1-num,s) p1 != NULL) /*当记录的学号不是要找的,或指针不为空时*/
{p2=p1; /*将p1指针值赋给p2作为p1的前驱指针*/
p1=p1-next; /*将p1指针指向下一条记录*/
}
if(strcmp(p1-num,s)==0) /*学号找到了*/
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p1-num,p1-name,p1-score[0],p1-score[1],p1-score[2],p1-sum,p1-average,p1-order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("Are you sure to delete the student Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/
for(;;)
{scanf("%c",c);
if(c=='n'||c=='N') break; /*如果不删除,则跳出本循环*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,说明被删结点是首结点*/
head=p1-next; /*把第二个结点地址赋予head*/
else
p2-next=p1-next; /*否则将一下结点地址赋给前一结点地址*/
n=n-1;
printf("\nNum %s student have been deleted.\n",s);
printf("Don't forget to save.\n");break; /*删除后就跳出循环*/
}
}
}
else
printf("\nThere is no num %s student on the list.\n",s); /*找不到该结点*/
return(head);
}
/*排序函数*/
STUDENT *sort(STUDENT *head)
{int i=0; /*保存名次*/
STUDENT *p1,*p2,*t,*temp; /*定义临时指针*/
temp=head-next; /*将原表的头指针所指的下一个结点作头指针*/
head-next=NULL; /*第一个结点为新表的头结点*/
while(temp!=NULL) /*当原表不为空时,进行排序*/
{
t=temp; /*取原表的头结点*/
temp=temp-next; /*原表头结点指针后移*/
p1=head; /*设定移动指针p1,从头指针开始*/
p2=head; /*设定移动指针p2做为p1的前驱,初值为头指针*/
while(t-averagep1-averagep1!=NULL) /*作成绩平均分比较*/
{
p2=p1; /*待排序点值小,则新表指针后移*/
p1=p1-next;
}
if(p1==p2) /*p1==p2,说明待排序点值大,应排在首位*/
{
t-next=p1; /*待排序点的后继为p*/
head=t; /*新头结点为待排序点*/
}
else /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
{
t-next=p1; /*t的后继是p1*/
p2-next=t; /*p2的后继是t*/
}
}
p1=head; /*已排好序的头指针赋给p1,准备填写名次*/
while(p1!=NULL) /*当p1不为空时,进行下列操作*/
{
i++; /*结点序号*/
p1-order=i; /*将结点序号赋值给名次*/
p1=p1-next; /*指针后移*/
}
printf("Sorting is sucessful.\n"); /*排序成功*/
return (head);
}
/*插入记录函数*/
STUDENT *insert(STUDENT *head,STUDENT *newnode)
{STUDENT *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一个结点*/
p0=newnode; /*p0指向要插入的结点*/
printf("\nPlease enter a newnode record.\n"); /*提示输入记录信息*/
printf("Enter the num:");
scanf("%s",newnode-num);
printf("Enter the name:");
scanf("%s",newnode-name);
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新记录的总分,初值为0*/
for(i=0;i3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",newnode-score[i]);
if(newnode-score[i]100||newnode-score[i]0)
printf("Data error,please enter again.\n");
}while(newnode-score[i]100||newnode-score[i]0);
sum1=sum1+newnode-score[i]; /*累加各门成绩*/
}
newnode-sum=sum1; /*将总分存入新记录中*/
newnode-average=(float)sum1/3;
newnode-order=0;
if(head==NULL) /*原来的链表是空表*/
{head=p0;p0-next=NULL;} /*使p0指向的结点作为头结点*/
else
{while((p0-averagep1-average)(p1-next!=NULL))
{p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1-next; /*p1后移一个结点*/
}
if(p0-average=p1-average)
{if(head==p1)head=p0; /*插到原来第一个结点之前*/
else p2-next=p0; /*插到p2指向的结点之后*/
p0-next=p1;}
else
{p1-next=p0;p0-next=NULL;} /*插到最后的结点之后*/
}
n=n+1; /*结点数加1*/
head=sort(head); /*调用排序的函数,将学生成绩重新排序*/
printf("\nStudent %s have been inserted.\n",newnode-name);
printf("Don't forget to save the newnode file.\n");
return(head);
}
/*保存数据到文件函数*/
void save(STUDENT *head)
{FILE *fp; /*定义指向文件的指针*/
STUDENT *p; /* 定义移动指针*/
char outfile[10];
printf("Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,为只写方式*/
{
printf("Cannot open the file\n");
return; /*若打不开则返回菜单*/
}
printf("\nSaving the file......\n");
p=head; /*移动指针从头指针开始*/
while(p!=NULL) /*如p不为空*/
{
fwrite(p,LEN,1,fp); /*写入一条记录*/
p=p-next; /*指针后移*/
}
fclose(fp); /*关闭文件*/
printf("Save the file successfully!\n");
}
/* 从文件读数据函数*/
STUDENT *load()
{STUDENT *p1,*p2,*head=NULL; /*定义记录指针变量*/
FILE *fp; /* 定义指向文件的指针*/
char infile[10];
printf("Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为只读方式*/
{
printf("Can not open the file.\n");
return(head);
}
printf("\nLoading the file!\n");
p1=(STUDENT *)malloc(LEN); /*开辟一个新单元*/
if(!p1)
{
printf("Out of memory!\n");
return(head);
}
head=p1; /*申请到空间,将其作为头指针*/
while(!feof(fp)) /*循环读数据直到文件尾结束*/
{
if(fread(p1,LEN,1,fp)!=1) break; /*如果没读到数据,跳出循环*/
p1-next=(STUDENT *)malloc(LEN); /*为下一个结点开辟空间*/
if(!p1-next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1-next; /*指针后移,新读入数据链到当前表尾*/
}
p2-next=NULL; /*最后一个结点的后继指针为空*/
fclose(fp);
printf("You have success to read data from the file!\n");
return (head);
}
求100行左右的代码(C语言,c++,数据结构编写的均可)
#include stdio.h
#include string.h
#include stdlib.h
#define N 10
struct library {
int num;
char book_name[30];
char writer[30];
char sort_num[3];
char pub_company[30];
char pub_time[30];
char prise[30];
};
typedef struct library LIB; /*结构体的定义用于存放书籍及借书的信息*/
LIB lib[N];
FILE *fp; int all=0;
int menu(void);
void input(void);
void output(void);
void save(void);
void del(void);
void search(void);
void xiugai(void);
main()
{
for(;;)
{
switch(menu()) {
case 1:input();break;
case 2:output();break;
case 3:save();break;
case 4:search();break;
case 5:xiugai();break;
case 6:del();break;
case 7:sort();break;
case 0:exit(1);break;
} /*SWITCH定义函数输出*/
}
}
int menu(void)
{
char m[3];
int n; printf(" *********************WELCOME**********************\n\n\n\n\n");
printf("\t\t\t-----图书信息管理系统----\n");
printf("\t\t1:输入\n");
printf("\t\t2:输出\n");
printf("\t\t3:保存\n");
printf("\t\t4:查找\n");
printf("\t\t5:修改\n");
printf("\t\t6:删除\n");
printf("\t\t7:统计\n");
printf("\t\t0:退出\n");
printf("\t\tplease choose a operation(0-6):\n");
scanf("%s",m);
n=atoi(m);
return(n);
} /*主要界面*/
void input(void)
{
int i;
char m[3];
for(i=all;iN;i++)
{
all++;
printf("请输入信息:\n");
printf("\t\t号码:\n");
scanf("%d",lib[i].num);
printf("\t\t书名:\n");
scanf("%s",lib[i].book_name);
printf("\t\t作者:\n");
scanf("%s",lib[i].writer);
printf("\t\tsort_num:\n");
scanf("%s",lib[i].sort_num);
printf("\t\t出版商:\n");
scanf("%s",lib[i].pub_company);
printf("\t\t出版时间:\n");
scanf("%s",lib[i].pub_time);
printf("\t\t价格:\n");
scanf("%s",lib[i].prise);
a: printf("\t\tyes/哦了?\n");
printf("\t\t1:yes\n");
printf("\t\t2:no\n");
scanf("%s",m);
if(atoi(m)==1)
continue;
else if(atoi(m)==2)
return;
else
{
printf("\t\t错误!\n");
goto a;
} /* 输入条件查找*/
}
}
void output(void)
{
int i;
for(i=0;iall;i++)
{
printf("\t\t%d\n",lib[i].num);
printf("%s\t\t%s\t\t%s\n",lib[i].book_name,lib[i].writer,lib[i].sort_num);
printf("%s\t\t%s\t\t%s\n",lib[i].pub_company,lib[i].pub_time,lib[i].prise);
}
} /*输出书名作者出版时间价格等*/
void save(void)
{
int i;
if((fp=fopen("file.c","wb"))==NULL)
{
printf("can not open the file");
exit(1);
}
for(i=0;iall;i++)
{
if(fwrite(lib[i],sizeof(LIB),1,fp)!=1)
{
printf("can not write!");
exit(1);
}
} /*条件不符合时拒绝存储*/
fclose(fp);
}
void search(void)
{
int i,flag;
char m[3];
char name[30];
printf("\t\t请选择您的存储方式:\n");
printf("\t\t1:按书名!\n");
printf("\t\t2:按作者!\n");
scanf("%s",m);
i=atoi(m);
switch(i)
{
case 1:{
printf("\t\t请输入书名:\n");
scanf("%s",name);
flag=0;
for(i=0;iN;i++)
{
if(strcmp(name,lib[i].book_name)==0)
{
printf("\t\t%d\n",lib[i].num);
printf("%s\t\t%s\t\t%s\n",lib[i].book_name,lib[i].writer,lib[i].sort_num);
printf("%s\t\t%s\t\t%s\n",lib[i].pub_company,lib[i].pub_time,lib[i].prise);
flag=1;break;
}
}
if(flag==0)
printf("\t\t没有这本书!\n");
}
case 2:{
printf("\t\t请输入作者:\n");
scanf("%s",name);
flag=0;
for(i=0;iN;i++)
{
if(strcmp(name,lib[i].writer)==0)
{
printf("\t\t%d\n",lib[i].num);
printf("%s\t\t%s\t\t%s\n",lib[i].book_name,lib[i].writer,lib[i].sort_num);
printf("%s\t\t%s\t\t%s\n",lib[i].pub_company,lib[i].pub_time,lib[i].prise);
flag=1;break;
}
}
if(flag==0)
printf("\t\t没有这个作者!\n");
}
} /*查找图书按书名或作者并输出*/
}
void xiugai(void)
{
int i,flag;
char name[30],n[3];
printf("\t\t请输入要修改的书名 :\n");
scanf("%s",name); /*修改书名*/
flag=0;
for(i=0;iN;i++)
{
if(strcmp(name,lib[i].book_name)==0)
{
printf("\t\t%d\n",lib[i].num);
printf("%s\t\t%s\t\t%s\n",lib[i].book_name,lib[i].writer,lib[i].sort_num);
printf("%s\t\t%s\t\t%s\n",lib[i].pub_company,lib[i].pub_time,lib[i].prise);
printf("\t\tplease input xiugai's the informations:\n");
printf("\t\tnum:\n");
scanf("%d",lib[i].num);
printf("\t\tbook_name:\n");
scanf("%s",lib[i].book_name);
printf("\t\twriter:\n");
scanf("%s",lib[i].writer);
printf("\t\tsort_num:\n");
scanf("%s",lib[i].sort_num);
printf("\t\tpub_company:\n");
scanf("%s",lib[i].pub_company);
printf("\t\tpub_time:\n");
scanf("%s",lib[i].pub_time);
printf("\t\tprise:\n");
scanf("%s",lib[i].prise);
flag=1;break;
}
} /*增加图书*/
if(flag==0)
printf("\t\t没有找到啊!\n");
}
void del(void)
{
int i,j,flag;
char name[30];
printf("\t\t请输入要删除的书名:\n");
scanf("%s",name);
flag=0;
for(i=0;iN;i++)
{
if(strcmp(name,lib[i].book_name)==0)
{
printf("\t\t%d\n",lib[i].num);
printf("%s\t\t%s\t\t%s\n",lib[i].book_name,lib[i].writer,lib[i].sort_num);
printf("%s\t\t%s\t\t%s\n",lib[i].pub_company,lib[i].pub_time,lib[i].prise);
for(j=N;ji;j--)
{
lib[j-1].num=lib[j].num;
strcpy(lib[j-1].book_name,lib[j].book_name);
strcpy(lib[j-1].writer,lib[j].writer);
strcpy(lib[j-1].sort_num,lib[j].sort_num);
strcpy(lib[j-1].pub_company,lib[j].pub_company);
strcpy(lib[j-1].pub_time,lib[j].pub_time);
strcpy(lib[j-1].prise,lib[j].prise);
flag=1;
printf("\t\t已经删除!\n");
break;
}
}
}
if(flag==0)
printf("\t\t没有这本书!\n");
} /*删除图书*/