求C语言编程,要求500行,作业……感激不尽

如题所述

第1个回答  2010-02-05
我以前写的,贴给你有600行,学生管理程序
参见hi.baidu.com/mark063/blog/item/21ea0cb11e185cacd8335a60.html

#include<stdio.h>
#include <malloc.h>
#include<time.h>
#include<string.h>

typedef struct stu{
char id[20];
char name[14];
int age;
int year,month,day;
char sex[4];
int gpa;
struct stu*next;
}*student;

void out();
void insertStu();
void printStu();
void searchStu();
void dele();
void update();
void open();
void save();

int main()
{
student head=NULL;
int choose,ifopen=0;
printf("\n\t\tWellcome to student manger!\n\n\t\t\t\t\tmade by 马健(09080208)");
printf("\n\n1:insert a student:\n2:show all student\n3:search a student\n4:delete a student\n5:update\n6:open database from disk\n7:save database to disk\n8:exit\n");
printf("\n\nPlease choose:\n");
while(scanf("%d",&choose)!=EOF){
switch(choose){
case 1:insertStu(&head);printf("Press any key..\n");getch();system("cls");break;
case 2:printStu(&head);printf("Press any key..\n");getch();system("cls");break;
case 3:searchStu(&head);printf("Press any key..\n");getch();system("cls");break;
case 4:dele(&head);printf("Press any key..\n");getch();system("cls");break;
case 5:update(&head);printf("Press any key..\n");getch();system("cls");break;
case 6:open(&head,&ifopen);printf("Press any key..\n");getch();system("cls");break;
case 7:save(&head);printf("Press any key..\n");getch();system("cls");break;
case 8:out(&head);return 0;
}
printf("\n\t\tWellcome to student manger!\n\n\t\t\t\t\tmade by 马健(09080208)");
printf("\n\n1:insert a student:\n2:show all student\n3:search a student\n4:delete a student\n5:update\n6:open database from disk\n7:save database to disk\n8:exit\n");
printf("\n\nPlease choose:\n");
}
}

void insertStu(student*head)
{
student last=*head,tmp;
int flag=0;
char id[20];
if(*head==NULL){
*head=(student)malloc(sizeof(struct stu));
printf("Creating new table:\n\n");
printf("Please input id:");
scanf("%s",(*head)->id);
printf("Please intput name:");
scanf("%s",&(*head)->name);
printf("Please input age:");
scanf("%d",&(*head)->age);
printf("Please input year:");
scanf("%d",&(*head)->year);
printf("Please input month:");
scanf("%d",&(*head)->month);
printf("Please input day:");
scanf("%d",&(*head)->day);
printf("Please input sex:");
scanf("%s",&(*head)->sex);
printf("Please input gpa:");
scanf("%d",&(*head)->gpa);
(*head)->next=NULL;
}
else{
printf("Please input id:");
scanf("%s",id);
if(strcmp(id,(*head)->id)<0)flag=1;
while(flag!=1&&last->next!=NULL&&(strcmp(last-> next->id,id)<0)){//为排序做准备,找出应该插入的位置
last=last->next;
}
tmp=(student)malloc(sizeof(struct stu));
strcpy(tmp->id,id);
printf("Please intput name:");
scanf("%s",&tmp->name);
printf("Please input age:");
scanf("%d",&tmp->age);
printf("Please input year:");
scanf("%d",&tmp->year);
printf("Please input month:");
scanf("%d",&tmp->month);
printf("Please input day:");
scanf("%d",&tmp->day);
printf("Please input sex:");
scanf("%s",&tmp->sex);
printf("Please input gpa:");
scanf("%d",&tmp->gpa);
if(flag==1){//插在第一个
tmp->next=(*head);
(*head)=tmp;
}
else if(last->next==NULL){//判断是否该插在最后一个
last->next=tmp;
tmp->next=NULL;
}
else{//该插在中间
tmp->next=last->next;
last->next=tmp;
}
}
}

void printStu(student*head)
{
student last=*head;
float begin=clock(),end;
while(last!=NULL){
printf("id:%s name:%-10sage:%-5dbrithday:%4d-%2d-%2d sex:%-4sgpa:%-d\n"
,last->id,last->name,last->age,last->year,last->month,last->day,last->sex,last->gpa);
last=last->next;
}
end=clock();
printf("you spend:%.2f\n",(end-begin)/1000);
}

void searchStu(student*head)
{
float begin,end;
int flag=0;
student last=*head;
char id[20];
printf("Please input the student id you want to search:\n");
scanf("%s",id);
begin=clock();
while(last->next!=NULL){
if(strcmp(id,last->id)==0){
printf("id:%s name:%-10sage:%-5dbrithday:%4d-%2d-%2d sex:%-4sgpa:%-d\n"
,last->id,last->name,last->age,last->year,last->month,last->day,last->sex,last->gpa);
flag=1;
break;
}
last=last->next;
}
if(flag==0)printf("Not found!\n");
end=clock();
printf("you spend:%.2f\n",(end-begin)/1000);
}

void dele(student*head)
{
student last=(*head)->next,pre=(*head),tmp;//pre来记录last前一个节点
char id[20];
float begin,end;
while(1){
printf("Please input the student id you want to delete:");
scanf("%s",id);
begin=clock();
if(strcmp((*head)->id,id)==0){//检验是否在第一个
tmp=(*head);
(*head)=(*head)->next;
free(tmp);
end=clock();
printf("you spend:%.2f\n",(end-begin)/1000);
return;
}
else{//如果不在第一个
while(1){
if(strcmp(last->id,id)==0){
pre->next=last->next;
free(last);
end=clock();
printf("you spend:%.2f\n",(end-begin)/1000);
return;
}
if(last==NULL)printf("NO id=%.0f student",id);
pre=last;
last=last->next;
}
}
}
}

void update(student*head)
{
char id[20];
float begin=clock(),end;
student last=(*head);
printf("Please input the student id you want to update:");
scanf("%s",id);
while(last!=NULL)
if(strcmp(id,last->id)==0)break;
else last=last->next;
if(last==NULL){
printf("No such student!\n");
return;
}
end=clock();
printf("you spend:%.2f\n",(end-begin)/1000);
printf("\nid:%-13.0s name:%-10sage:%-5dbrithday:%4d-%2d-%2d sex:%-4sgpa:%-d\n"
,last->id,last->name,last->age,last->year,last->month,last->day,last->sex,last->gpa);
printf("\nPlease input the new info:\n");
strcpy(last->id,id);
printf("Please intput name:");
scanf("%s",&last->name);
printf("Please input age:");
scanf("%d",&last->age);
printf("Please input year:");
scanf("%d",&last->year);
printf("Please input month:");
scanf("%d",&last->month);
printf("Please input day:");
scanf("%d",&last->day);
printf("Please input sex:");
scanf("%s",&last->sex);
printf("Please input gpa:");
scanf("%d",&last->gpa);
}

void open(student*head,int*ifopen)
{
FILE*fp;
student tmp,last;
int flag=0;
char id[20];
double begin,end;
if(*ifopen==1){
out(&*head);
}
fp=fopen("database.dat","r");
if(fp==NULL){
printf("No file!\n");
return;
}
(*head)=NULL;
begin=clock();
printf("Loading please wait..\n");
while(fscanf(fp,"%s",&id)&&id[0]!='e'){
if((*head)==NULL){
(*head)=malloc(sizeof(struct stu));
strcpy((*head)->id,id);
fscanf(fp,"%s%d%d%d%d%s%d",(*head)->name,&(*head)->age,&(*head)->year
,&(*head)->month,&(*head)->day,(*head)->sex,&(*head)->gpa);
(*head)->next=NULL;
}
else{
flag=0;
last=(*head);
tmp=malloc(sizeof(struct stu));
if(strcmp(id,(*head)->id)<0)flag=1;//判断是否该插在在第一个
strcpy(tmp->id,id);
last=(*head);
while(flag!=1&&last->next!=NULL&&strcmp(last-> next->id,id)<0){//为排序做准备,找出应该插入的位置
last=last->next;
}
fscanf(fp,"%s%d%d%d%d%s%d",tmp->name,&tmp->age,&tmp->year
,&tmp->month,&tmp->day,tmp->sex,&tmp->gpa);
if(flag==1){//插在第一个
tmp->next=(*head);
(*head)=tmp;
}
else if(last->next==NULL){//判断是否该插在最后一个
last->next=tmp;
tmp->next=NULL;
}
else{//该插在中间
tmp->next=last->next;
last->next=tmp;
}
}
}
end=clock();
printf("you spend:%.2f(s)\n",(end-begin)/1000);
fclose(fp);
*ifopen=1;
}

void save(student*head)
{
FILE*fp;
student last=(*head);
fp=fopen("database.dat","w");
while(last){
fprintf(fp,"%s %s %d %d %d %d %s %d\n",last->id,last->name,last->age,last->year,last->month,last->day,last->sex,last->gpa);
last=last->next;
}
fprintf(fp,"%c\n",'e');
fclose(fp);
}

void out(student*head)
{
student last=*head,tmp;
char sav;
printf("if you want to save?(y/n)\n");
sav=getch();
if(sav=='y'||sav=='Y')save(&*head);
while(last){
tmp=last;
last=last->next;
free(tmp);
}
}

#include<stdio.h>
#include<time.h>
#include<malloc.h>

int main()
{
FILE*fp;
unsigned int id;
int n,k,i,name,age,year,month,day,sex,gpa;
int*out=NULL;
srand(time(NULL));
fp=fopen("database.dat","w");
printf("This is can help lab7 build a database to debug\n");
printf("Please input a number to limit the n:\n");
while(scanf("%d",&n)&&n>32768)printf("intput should less than int,input again:\n");
k=n;
if(fp==NULL)printf("ON FILE");
out=(int*)malloc(n*sizeof(int)+11);
for(i=0;i<n;i++)
out[i]=0;
if(out==NULL)printf("Not enough memoney!\n");
while(n--){
while(1){
id=rand()%k;
if(out[id]==0){
out[id]=1;
break;
}
}
age=rand()%5;
sex=rand()%2+1;
year=rand()%3;
month=rand()%12+1;
day=rand()%30+1;
gpa=rand()%40+61;
fprintf(fp,"%c%.0f ",'0',id+(float)9080201);
for(i=0;i<4;i++){
name=rand()%26;
fprintf(fp,"%c",name+'a');
}
fprintf(fp," %d",(int)16+age);
fprintf(fp," %d %d %d ",year+(int)1989,month,day);
if(sex%2==0)fprintf(fp,"%c",'f');
else fprintf(fp,"%c",'m');
fprintf(fp," %d\n",gpa);
}
fprintf(fp,"%c",'e');
printf("All build ok\n");
fclose(fp);
free(out);
return 0;
}本回答被提问者采纳
第2个回答  2010-02-05
敲500个printf
500个回车
500个空格
500个分号
搞定
第3个回答  2010-02-05
第4个回答  2010-02-05
怎么算一行... 什么都不干的程序也能写出500行...
相似回答