『壹』 用c語言做員工工資排序
#include<stdio.h>
#include<string.h>
typedef struct _student{
char name[20];
char sex[20];
double salary;
}Student;
int main(){
int i,j;
Student student[5];
for(i=0;i<5;i++){
scanf("%s %s %lf",student[i].name,student[i].sex,&student[i].salary);
}
for(i=0;i<5;i++){
for(j=i;j<5;j++){
if(student[i].salary > student[j].salary){
Student temp = student[i];
student[i]=student[j];
student[j]=temp;
}
}
}
for(i=0;i<5;i++){
printf("%s %s %.2f\n",student[i].name,student[i].sex,student[i].salary);
}
return 0;
}
『貳』 c語言員工管理系統求教 急!!
樓主,我編出來了
第一次編這個,算算用了12個小時左右,汗~
程序如下
----------------------------------------------------
/*每個員工的信息包括:編號、姓名、性別、出生年月、學歷、職務、電話、住址等。系統能夠完成員工信息的查詢、更新、插入、刪除、排序等功能。
(1) 排序:按不同關鍵字,對所有員工的信息進行排序。
(2) 查詢:按特定條件查找員工。
(3) 更新:按編號對某個員工的某項信息進行修改。
(4) 插入:加入新員工的信息。
(5) 刪除:按編號刪除已離職的員工的信息。
(6) 程序要求操作方便,靈活。*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> //清屏函數頭文件
#include <string.h>
struct Stuff
{
char number[10]; //員工編號
char name[10]; //員工姓名
char sex[8]; //員工性別
char borth[10]; //員工生日
char degree[20]; //員工學歷
char business[20]; //員工職務
char phone[15]; //員工電話
char place[50]; //員工住址
char con[50]; //判斷關鍵字專用
struct Stuff *next;
};
char Menu(void); //菜單顯示
struct Stuff *App(struct Stuff *head); //添加
void Sort(struct Stuff *head); //排序
struct Stuff *Ser(struct Stuff *head); //查找
void Chn(struct Stuff *head,char n[10]); //更改
void Scpy(char *p,char *q); //排序中用於交換員工信息
struct Stuff *Del(struct Stuff *head,char n[10]); //刪除
int Sel(char ch,struct Stuff *p,struct Stuff *q); //判斷排序及關鍵字專用函數
void Prf(struct Stuff *head); //輸出
void Fre(struct Stuff *head); //釋放
int i=1; //定義全局變數,實現實時員工人數統計
int main(void)
{
char n[10];
struct Stuff *head=NULL; //鏈表頭指針定義
while(1)
{
switch(Menu())
{
case '1':
printf("請輸入員工信息,直接輸入'#'結束\n");
head=App(head);
break;
case '2':
Sort(head);
break;
case '3':
head=Ser(head);
break;
case '4':
printf("員工信息如下:\n");
Prf(head);
break;
case '5':
printf("請輸入員工編號:");
scanf("%s",n);
Chn(head,n);
break;
case '6':
printf("請輸入員工編號:");
scanf("%s",n);
head=Del(head,n);
break;
case '0':
printf("歡迎下次光臨,88!\n");
exit(0);
default:
printf("輸入錯誤,請重新輸入!\n");
}
fflush(stdin); //清楚緩沖區
printf("按任意鍵繼續~");
getchar();
system("cls"); //清屏效果
}
Fre(head);
return 0;
}
//菜單函數
char Menu(void)
{
char ch;
printf("------------請選擇-----------\n");
printf("1.添加員工信息\n2.員工信息排序\n3.查找員工信息\n4.輸出員工信息\n5.更改員工信息\n6.刪除員工信息\n0.退出\n-----------------------------\n");
scanf(" %c",&ch);
return ch;
}
//添加成員函數
//輸入參數:鏈表頭指針
//返回參數:鏈表頭指針
struct Stuff *App(struct Stuff *head)
{
struct Stuff *p=NULL,*q=head;
while(i)
{
p=(struct Stuff *)malloc(sizeof(struct Stuff)); //申請結構體空間
if(p==NULL)
{
printf("內存不夠!\n");
exit(0);
}
p->next =NULL; //指針域為空
printf("請輸入第%d名員工:\n",i);
printf(" 編號 | 姓名 | 性別 | 出生年月 | 學歷 | 職務 | 電話 | 住址 :\n");
fflush(stdin);
scanf("%s",p->number );
if(!strcmp(p->number ,"#"))
{
free(p); //釋放不需要的結構體內存
break;
}
else
{
++i;
scanf("%s%s%s%s%s%s%s",p->name ,p->sex ,p->borth ,p->degree ,p->business ,p->phone ,p->place );
p->con[0]='\0'; //防止後面判斷出現隨機值
if(head==NULL)
head=p;
else
{
while(q->next !=NULL) //防止結束後再次輸入時出現問題
q=q->next ;
q->next =p;
}
q=p; //每次都加在鏈表尾
}
}
return head;
}
//排序函數
//輸入參數:頭指針
void Sort(struct Stuff *head)
{
char ch;
struct Stuff *p,*q,*r;
while(1)
{
printf("請選擇排序條件:1.編號2.姓名3.性別4.出生年月5.學歷6.職務7.電話8.地址0.退出\n");
scanf(" %c",&ch);
if(ch=='0')
break;
if(ch<'1'||ch>'8')
{
printf("輸入錯誤,請重新輸入!\n");
continue;
}
p=head;
while(p->next!=NULL) //選擇排序
{
q=p->next;
r=p;
while(q!=NULL)
{
if(Sel(ch,r,q)) //調用判斷函數
r=q;
q=q->next;
}
if(r!=p) //交換內容
{
Scpy(r->number,p->number);
Scpy(r->name,p->name);
Scpy(r->sex,p->sex);
Scpy(r->borth,p->borth);
Scpy(r->degree,p->degree);
Scpy(r->business,p->business);
Scpy(r->phone,p->phone);
Scpy(r->place,p->place);
}
p=p->next;
}
Prf(head); //輸出
}
}
//交換函數
void Scpy(char *p,char *q)
{
char c[50];
strcpy(c,p);
strcpy(p,q);
strcpy(q,c);
}
//判斷函數
//輸出參數:1為真,0為假
int Sel(char ch,struct Stuff *p,struct Stuff *q)
{
switch(ch) //實現各個關鍵字查找
{
case '1':
return strcmp(q->number ,p->number )<0||strcmp(q->con ,p->number )==0 ; //排序條件及查找條件
case '2':
return strcmp(q->name ,p->name )<0||strcmp(q->con ,p->name )==0 ;
case '3':
return strcmp(q->sex ,p->sex )<0||strcmp(q->con ,p->sex )==0 ;
case '4':
return strcmp(q->borth ,p->borth)<0 ||strcmp(q->con ,p->borth )==0 ;
case '5':
return strcmp(q->degree ,p->degree )<0||strcmp(q->con ,p->degree )==0 ;
case '6':
return strcmp(q->business ,p->business )<0||strcmp(q->con ,p->business)==0 ;
case '7':
return strcmp(q->phone ,p->phone )<0 ||strcmp(q->con ,p->phone)==0;
case '8':
return strcmp(q->place ,p->place )<0||strcmp(q->con ,p->place )==0;
default :
exit(0);
}
}
//查找函數
struct Stuff *Ser(struct Stuff *head)
{
struct Stuff *p=NULL,*q,a={"\0","\0","\0","\0","\0","\0","\0","\0"}; //防止判斷時錯誤
int flag; //查找判斷
char ch,sh;
q=&a;
while(1)
{
printf("請輸入要查找的條件:1.編號2.姓名3.性別4.出生年月5.學歷6.職務7.電話8.住址0.退出\n");
scanf(" %c",&ch);
if(ch=='0')
break;
if(ch<'1'||ch>'8')
{
printf("輸入錯誤,請重新輸入!\n");
continue;
}
fflush(stdin);
printf("請輸入:");
gets(q->con );
p=head; //指向表頭
flag=0;
while(p!=NULL)
{
if(Sel(ch,p,q))
{
printf("員工信息如下:\n");
printf(" 編號 | 姓名 | 性別 | 出生年月 | 學歷 | 職務 | 電話 | 住址 \n%s %s %s %s %s %s %s %s\n"
,p->number ,p->name ,p->sex ,p->borth ,p->degree ,p->business ,p->phone ,p->place );
printf("是否需要:1.更改 2.刪除 3.繼續\n");
scanf(" %c",&sh);
if(sh=='1')
Chn(head,p->number); //調用更改函數
else if(sh=='2')
head=Del(head,p->number); //調用刪除函數,得到的head必須return
flag=1;
break;
}
p=p->next ;
}
if(flag==0)
printf("沒有找到該員工信息!\n");
}
return head;
}
//更改函數
//輸入參數:n[10] 為員工編號
void Chn(struct Stuff *head,char n[10])
{
struct Stuff *p=head;
int flag=0;
if(head==NULL)
printf("未找到員工信息!\n");
else
{
while(p!=NULL)
{
if(!strcmp(p->number,n))
{
printf("請輸入新的信息:\n編號|姓名|性別|出生年月|學歷|職務|電話|住址\n");
scanf("%s%s%s%s%s%s%s%s",p->number ,p->name ,p->sex ,p->borth ,p->degree ,p->business ,p->phone ,p->place );
printf("員工信息如下:\n");
flag++;
break;
}
p=p->next;
}
if(flag==0)
printf("未找到該員工信息!\n");
}
Prf(head);
}
//刪除函數
//輸入參數:n為員工編號
//輸出參數:頭指針
struct Stuff *Del(struct Stuff *head,char n[10])
{
struct Stuff *p,*pr;
int flag;
flag=0;
p=head,pr=head;
if(head==NULL)
printf("未找到員工信息!\n");
else
{
while(strcmp(p->number ,n)&&p->next !=NULL)
{
pr=p;
p=p->next ;
}
if(!strcmp(p->number ,n))
{
if(p==head)
head=p->next ;
else
pr->next=p->next ;
free(p);
printf("刪除成功!\n");
i--;
}
else
printf("未找到員工信息!\n");
}
Prf(head);
return head;
}
//輸出函數
void Prf(struct Stuff *head)
{
struct Stuff *p=head;
int i=1;
while(p!=NULL)
{
printf("%d. %s %s %s %s %s %s %s %s\n"
,i++,p->number ,p->name ,p->sex ,p->borth ,p->degree ,p->business ,p->phone ,p->place);
p=p->next ;
}
}
//釋放函數
void Fre(struct Stuff *head)
{
struct Stuff *p;
while(head!=NULL)
{
p=head;
head=head->next ;
free(p);
}
}
『叄』 c語言編程員工管理檔案怎樣按照學歷來排序
學歷使用ID編號,對ID索引排序,加ID到學歷名稱映射
『肆』 c語言圖書管理系統排序功能
創建另外一個文件用於排序,假如有一個文件名稱為a,創建一個文件b將文件a中的內容讀取出來並排好序輸出到文件b中去,然後刪除文件a並將文件b的名字改為文件a
『伍』 C語言:做職工管理系統,需要按職工號排序,我用冒泡怎麼都弄不出來,麻煩大家幫我看看,謝謝了
改過了,試試看
void printstud (struct employee *stud)
{
int i,j;
struct employee b; //用結構體來完成數據交換,因為交換的是全體數據
for (j=0;j<n-1;j++)
{
for (i=0;i<n-1-j;i++)
{
if ( strcmp(stud[i].num,stud[i+1].num) > 0 ) //串比較大小用strcmp
{
b=stud[i];
stud[i]=stud[i+1];
stud[i+1]=b;
}
}
}
printf ("以下是所有職工的信息\n");
for (i=0;i<n;i++)
{
printf ("%6s%6s%6s%6s%6s\n",stud[i].num,stud[i].name,stud[i].sex,stud[i].old,stud[i].nation); //多了一個%6s
}
printf ("請按任意數字鍵返回主菜單");
getchar();
}
『陸』 c語言學生管理系統 排序問題求教
我沒有過不使用結構體處理這種處理的經歷,但想了種方法倒是可以讓你試試。
你定義一個char型的數組,用來保存其他數組下標的。在排序時不改變成績在數組中的順序,也就是說排序後char型數組中會按順序保留成績對應的下標。
如果學號testno[]、姓名name[]、成績score[],那麼定義一個數組sort[]。
如果成績最高為score[8],則將8存入到sort[0]中,則testno[sort[0]]就是最高成績的學號。
『柒』 C語言中學生信息管理系統怎麼將成績進行排序呢
用讀「r or r+」或者追加「a or a+」的方式打開並讀取文件然後「」關閉文件「」,這時文件中是空的,把全部內容讀取到內存數組中,在內存中進行排序,存文件是用「w」打開,存入進去然後關閉文件。不用可以覆蓋原有序列,文件中是不能進行除讀寫外的操作的
『捌』 c語言編程題目求教---輸入員工姓名工號進行排序和查找
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineEMPCNT10
structemployee
{
intid;
charname[16];
};
voidsort(structemployee*e)
{
inti,j;
structemployeetemp;
for(i=0;i<EMPCNT;i++)
{
for(j=i+1;j<EMPCNT;j++)
{
if(e[j].id<e[i].id)
{
/*swap*/
temp=e[i];
e[i]=e[j];
e[j]=temp;
}
}
}
}
intsearch(structemployee*e,intid)
{
intlow,high,mid;
low=0;
high=EMPCNT-1;
while(low<=high)
{
mid=(low+high)/2;
if(id<e[mid].id)
high=mid-1;
elseif(id>e[mid].id)
low=mid+1;
else
returnmid;
}
return-1;
}
voidmain()
{
structemployeee[EMPCNT];
inti,id=0,m=-1;
for(i=0;i<EMPCNT;i++)
{
printf("輸入第%d員工的員工號,姓名:",i+1);
scanf("%d%s",&e[i].id,e[i].name);
fflush(stdin);
}
sort(e);
for(i=0;i<EMPCNT;i++)
printf("%d%s ",e[i].id,e[i].name);
printf("要查找的員工號:");
scanf("%d",&id);
fflush(stdin);
m=search(e,id);
if(m<0)
printf("%d未找到 ",id);
else
printf("%d%s ",e[m].id,e[m].name);
getchar();
}
『玖』 c語言工資管理系統的排序功能用不了了
/*我們開始搞課程設計,我選的這個課題,下面我編的,看看*/
#include "stdio.h" /*標准輸入輸出函數庫*/
#include "stdlib.h" /*標准函數庫*/
#include "string.h" /*字元串函數庫*/
#include "conio.h" /*屏幕操作函數庫*/
#define HEADER1 " -------------------------------ZGGZ---------------------------------------- \n"
#define HEADER2 "| number| name | jbgz | jj | kk | yfgz | sk | sfgz | \n"
#define HEADER3 "|--------|-----------|--------|--------|--------|--------|--------|--------| \n"
#define FORMAT "|%-8s|%-10s |%8.2f|%8.2f|%8.2f|%8.2f|%8.2f|%8.2f| \n"
#define DATA p->num,p->name,p->jbgz,p->jj,p->kk,p->yfgz,p->sk,p->sfgz
#define END "---------------------------------------------------------------------------- \n"
#define N 60
int saveflag=0; /*是否需要存檔的標志變數*/
/*定義與職工有關的數據結構*/
typedef struct employee /*標記為employee*/
{
char num[10]; /*職工編號*/
char name[15]; /*職工姓名*/
float jbgz; /*基本工資*/
float jj; /*獎金*/
float kk; /*扣款*/
float yfgz; /*應發工資*/
float sk; /*稅款*/
float sfgz; /*實發工資*/
}ZGGZ;
void menu() /*主菜單*/
{
system("cls"); /*調用DOS命令,清屏.與clrscr()功能相同*/
textcolor(10); /*在文本模式中選擇新的字元顏色*/
gotoxy(10,5); /*在文本窗口中設置游標*/
cprintf(" The Employee' Salary Management System \n");
gotoxy(10,8);
cprintf(" *************************Menu********************************\n");
gotoxy(10,9);
cprintf(" * 1 input record 2 delete record *\n");
gotoxy(10,10);
cprintf(" * 3 search record 4 modify record *\n");
gotoxy(10,11);
cprintf(" * 5 insert record 6 count record *\n");
gotoxy(10,12);
cprintf(" * 7 sort reord 8 save record *\n");
gotoxy(10,13);
cprintf(" * 9 display record 0 quit system *\n");
gotoxy(10,14);
cprintf(" *************************************************************\n");
/*cprintf()送格式化輸出至文本窗口屏幕中*/
}
void printheader() /*格式化輸出表頭*/
{
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
}
void printdata(ZGGZ pp) /*格式化輸出表中數據*/
{
ZGGZ* p;
p=&pp;
printf(FORMAT,DATA);
}
void Disp(ZGGZ tp[],int n) /*顯示數組tp[]中存儲的記錄,內容為employee結構中定義的內容*/
{
int i;
if(n==0) /*表示沒有職工工資記錄*/
{
printf("\n=====>Not employee record!\n");
getchar();
return;
}
printf("\n\n");
printheader(); /*輸出表格頭部*/
i=0;
while(i<n) /*逐條輸出數組中存儲的職工信息*/
{
printdata(tp[i]);
i++;
printf(HEADER3);
}
getchar();
}
void Wrong() /*輸出按鍵錯誤信息*/
{
printf("\n\n\n\n\n***********Error:input has wrong! press any key to continue**********\n");
getchar();
}
void Nofind() /*輸出未查找此職工的信息*/
{
printf("\n=====>Not find this employee record!\n");
}
/*************************************************************
作用:用於定位數組中符合要求的記錄,並返回保存該記錄的數組元素下標值
參數:findmess[]保存要查找的具體內容; nameornum[]保存按什麼在數組中查找;
**************************************************************/
int Locate(ZGGZ tp[],int n,char findmess[],char nameornum[])
{
int i=0;
if(strcmp(nameornum,"num")==0) /*按職工編號查詢*/
{
while(i<n)
{
if(strcmp(tp[i].num,findmess)==0) /*若找到findmess值的職工編號*/
return i;
i++;
}
}
else if(strcmp(nameornum,"name")==0) /*按職工姓名查詢*/
{
while(i<n)
{
if(strcmp(tp[i].name,findmess)==0) /*若找到findmess值的姓名*/
return i;
i++;
}
}
return -1; /*若未找到,返回一個整數-1*/
}
/*輸入字元串,並進行長度驗證(長度<lens)*/
void stringinput(char *t,int lens,char *notice)
{
char n[255];
do{
printf(notice); /*顯示提示信息*/
scanf("%s",n); /*輸入字元串*/
if(strlen(n)>lens) printf("\n exceed the required length! \n"); /*進行長度校驗,超過lens值重新輸入*/
}while(strlen(n)>lens);
strcpy(t,n); /*將輸入的字元串拷貝到字元串t中*/
}
/*輸入數值,0<=數值)*/
float numberinput(char *notice)
{
float t=0.00;
do{
printf(notice); /*顯示提示信息*/
scanf("%f",&t); /*輸入如工資等數值型的值*/
if(t<0) printf("\n score must >=0! \n"); /*進行數值校驗*/
}while(t<0);
return t;
}
/*增加職工工資記錄*/
int Add(ZGGZ tp[],int n)
{
char ch,num[10];
int i,flag=0;
system("cls");
Disp(tp,n); /*先列印出已有的職工工資信息*/
while(1) /*一次可輸入多條記錄,直至輸入職工編號為0的記錄才結束添加操作*/
{
while(1) /*輸入職工編號,保證該編號沒有被使用,若輸入編號為0,則退出添加記錄操作*/
{
stringinput(num,10,"input number(press '0'return menu):"); /*格式化輸入編號並檢驗*/
flag=0;
if(strcmp(num,"0")==0) /*輸入為0,則退出添加操作,返回主界面*/
{return n;}
i=0;
while(i<n) /*查詢該編號是否已經存在,若存在則要求重新輸入一個未被佔用的編號*/
{
if(strcmp(tp[i].num,num)==0)
{
flag=1;
break;
}
i++;
}
if(flag==1) /*提示用戶是否重新輸入*/
{ getchar();
printf("==>The number %s is existing,try again?(y/n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
continue;
else
return n;
}
else
{break;}
}
strcpy(tp[n].num,num); /*將字元串num拷貝到tp[n].num中*/
stringinput(tp[n].name,15,"Name:");
tp[n].jbgz=numberinput("jbgz:"); /*輸入並檢驗基本工資*/
tp[n].jj=numberinput("jiangjin:"); /*輸入並檢驗獎金*/
tp[n].kk=numberinput("koukuan:"); /*輸入並檢驗扣款*/
tp[n].yfgz=tp[n].jbgz+tp[n].jj-tp[n].kk; /*計算應發工資*/
tp[n].sk=tp[n].yfgz*0.12; /*計算稅金,這里取應發工資的百分之一十二*/
tp[n].sfgz=tp[n].yfgz-tp[n].sk; /*計算實發工資*/
saveflag=1;
n++;
}
return n;
}
/*按職工編號或姓名,查詢記錄*/
void Qur(ZGGZ tp[],int n)
{
int select; /*1:按編號查,2:按姓名查,其他:返回主界面(菜單)*/
char searchinput[20]; /*保存用戶輸入的查詢內容*/
int p=0;
if(n<=0) /*若數組為空*/
{
system("cls");
printf("\n=====>No employee record!\n");
getchar();
return;
}
system("cls");
printf("\n =====>1 Search by number =====>2 Search by name\n");
printf(" please choice[1,2]:");
scanf("%d",&select);
if(select==1) /*按編號查詢*/
{
stringinput(searchinput,10,"input the existing employee number:");
p=Locate(tp,n,searchinput,"num");/*在數組tp中查找編號為searchinput值的元素,並返回該數組元素的下標值*/
if(p!=-1) /*若找到該記錄*/
{
printheader();
printdata(tp[p]);
printf(END);
printf("press any key to return");
getchar();
}
else
Nofind();
getchar();
}
else if(select==2) /*按姓名查詢*/
{
stringinput(searchinput,15,"input the existing employee name:");
p=Locate(tp,n,searchinput,"name");
if(p!=-1)
{
printheader();
printdata(tp[p]);
printf(END);
printf("press any key to return");
getchar();
}
else
Nofind();
getchar();
}
else
Wrong();
getchar();
}
/*刪除記錄:先找到保存該記錄的數組元素的下標值,然後在數組中刪除該數組元素*/
int Del(ZGGZ tp[],int n)
{
int sel;
char findmess[20];
int p=0,i=0;
if(n<=0)
{ system("cls");
printf("\n=====>No employee record!\n");
getchar();
return n;
}
system("cls");
Disp(tp,n);
printf("\n =====>1 Delete by number =====>2 Delete by name\n");
printf(" please choice[1,2]:");
scanf("%d",&sel);
if(sel==1)
{
stringinput(findmess,10,"input the existing employee number:");
p=Locate(tp,n,findmess,"num");
getchar();
if(p!=-1)
{
for(i=p+1;i<n;i++) /*刪除此記錄,後面記錄向前移*/
{
strcpy(tp[i-1].num,tp[i].num);
strcpy(tp[i-1].name,tp[i].name);
tp[i-1].jbgz=tp[i].jbgz;
tp[i-1].jj=tp[i].jj;
tp[i-1].kk=tp[i].kk;
tp[i-1].yfgz=tp[i].yfgz;
tp[i-1].jbgz=tp[i].sk;
tp[i-1].sfgz=tp[i].sfgz;
}
printf("\n==>delete success!\n");
n--;
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
else if(sel==2) /*先按姓名查詢到該記錄所在的數組元素的下標值*/
{
stringinput(findmess,15,"input the existing employee name:");
p=Locate(tp,n,findmess,"name");
getchar();
if(p!=-1)
{
for(i=p+1;i<n;i++) /*刪除此記錄,後面記錄向前移*/
{
strcpy(tp[i-1].num,tp[i].num);
strcpy(tp[i-1].name,tp[i].name);
tp[i-1].jbgz=tp[i].jbgz;
tp[i-1].jj=tp[i].jj;
tp[i-1].kk=tp[i].kk;
tp[i-1].yfgz=tp[i].yfgz;
tp[i-1].jbgz=tp[i].sk;
tp[i-1].sfgz=tp[i].sfgz;
}
printf("\n=====>delete success!\n");
n--;
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
return n;
}
/*修改記錄。先按輸入的職工編號查詢到該記錄,然後提示用戶修改編號之外的值,編號不能修改*/
void Modify(ZGGZ tp[],int n)
{
char findmess[20];
int p=0;
if(n<=0)
{ system("cls");
printf("\n=====>No employee record!\n");
getchar();
return ;
}
system("cls");
printf("modify employee recorder");
Disp(tp,n);
stringinput(findmess,10,"input the existing employee number:"); /*輸入並檢驗該編號*/
p=Locate(tp,n,findmess,"num"); /*查詢到該數組元素,並返回下標值*/
if(p!=-1) /*若p!=-1,表明已經找到該數組元素*/
{
printf("Number:%s,\n",tp[p].num);
printf("Name:%s,",tp[p].name);
stringinput(tp[p].name,15,"input new name:");
printf("jbgz:%8.2f,",tp[p].jbgz);
tp[p].jbgz=numberinput("jbgz:");
printf("jiangjin:%8.2f,",tp[p].jj);
tp[p].jj=numberinput("jiangjin:");
printf("koukuan:%8.2f,",tp[p].kk);
tp[p].kk=numberinput("koukuan:");
tp[n].yfgz=tp[n].jbgz+tp[n].jj-tp[n].kk;
tp[n].sk=tp[n].yfgz*0.12;
tp[n].sfgz=tp[n].yfgz-tp[n].sk;
printf("\n=====>modify success!\n");
getchar();
Disp(tp,n);
getchar();
saveflag=1;
}
else
{Nofind();
getchar();
}
return ;
}
/*插入記錄:按職工編號查詢到要插入的數組元素的位置,然後在該編號之後插入一個新數組元素。*/
int Insert(ZGGZ tp[],int n)
{
char ch,num[10],s[10]; /*s[]保存插入點位置之前的編號,num[]保存輸入的新記錄的編號*/
ZGGZ newinfo;
int flag=0,i=0,kkk=0;
system("cls");
Disp(tp,n);
while(1)
{ stringinput(s,10,"please input insert location after the Number:");
flag=0;i=0;
while(i<n) /*查詢該編號是否存在,flag=1表示該編號存在*/
{
if(strcmp(tp[i].num,s)==0) {kkk=i;flag=1;break;}
i++;
}
if(flag==1)
break; /*若編號存在,則進行插入之前的新記錄輸入操作*/
else
{ getchar();
printf("\n=====>The number %s is not existing,try again?(y/n):",s);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{continue;}
else
{return n;}
}
}
/*以下新記錄的輸入操作與Add()相同*/
while(1)
{ stringinput(num,10,"input new employee Number:");
i=0;flag=0;
while(i<n) /*查詢該編號是否存在,flag=1表示該編號存在*/
{
if(strcmp(tp[i].num,num)==0) {flag=1;break;}
i++;
}
if(flag==1)
{
getchar();
printf("\n=====>Sorry,The number %s is existing,try again?(y/n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{continue;}
else
{return n;}
}
else
break;
}
strcpy(newinfo.num,num); /*將字元串num拷貝到newinfo.num中*/
stringinput(newinfo.name,15,"Name:");
newinfo.jbgz=numberinput("jbgz:"); /*輸入並檢驗jbgz*/
newinfo.jj=numberinput("jiangjin:"); /*輸入並檢驗jiangjin*/
newinfo.kk=numberinput("koukuan:"); /*輸入並檢驗koukuan*/
newinfo.yfgz=newinfo.jbgz+newinfo.jj-newinfo.kk; /*計算yfgz*/
newinfo.sk=newinfo.yfgz*0.12; /*計算sk*/
newinfo.sfgz=newinfo.yfgz-newinfo.sk;
saveflag=1; /*在main()有對該全局變數的判斷,若為1,則進行存檔操作*/
for(i=n-1;i>kkk;i--) /*從最後一個組織元素開始往向移一個元素位置*/
{ strcpy(tp[i+1].num,tp[i].num);
strcpy(tp[i+1].name,tp[i].name);
tp[i+1].jbgz=tp[i].jbgz;
tp[i+1].jj=tp[i].jj;
tp[i+1].kk=tp[i].kk;
tp[i+1].yfgz=tp[i].yfgz;
tp[i+1].sk=tp[i].sk;
tp[i+1].sfgz=tp[i].sfgz;
}
strcpy(tp[kkk+1].num,newinfo.num); /*在kkk的元素位置後插入新記錄*/
strcpy(tp[kkk+1].name,newinfo.name);
tp[kkk+1].jbgz=newinfo.jbgz;
tp[kkk+1].jj=newinfo.jj;
tp[kkk+1].kk=newinfo.kk;
tp[kkk+1].yfgz=newinfo.yfgz;
tp[kkk+1].sk=newinfo.sk;
tp[kkk+1].sfgz=newinfo.sfgz;
n++;
Disp(tp,n);
printf("\n\n");
getchar();
return n;
}
/*統計公司的員工的工資在各等級的人數*/
void Tongji(ZGGZ tp[],int n)
{
int count10000=0,count5000=0,count2000=0,count0=0;
int i=0;
if(n<=0)
{ system("cls");
printf("\n=====>Not employee record!\n");
getchar();
return ;
}
system("cls");
Disp(tp,n);
i=0;
while(i<n)
{
if(tp[i].sfgz>=10000) {count10000++;i=i+1;continue;} /*實發工資>10000*/
if(tp[i].sfgz>=5000) {count5000++;i=i+1;continue;} /*5000<=實發工資<10000*/
if(tp[i].sfgz>=2000) {count2000++;i=i+1;continue;} /*2000<=實發工資<5000*/
if(tp[i].sfgz<2000) {count0++;i=i+1;continue;} /*實發工資<2000*/
}
printf("\n------------------------------the TongJi result--------------------------------\n");
printf("sfgz>= 10000:%d (ren)\n",count10000);
printf("5000<=sfgz<10000:%d (ren)\n",count5000);
printf("2000<=sfgz< 5000:%d (ren)\n",count2000);
printf("sfgz< 2000:%d (ren)\n",count0);
printf("-------------------------------------------------------------------------------\n");
printf("\n\npress any key to return");
getchar();
}
/*利用冒泡排序法實現數組的按實發工資欄位的降序排序,從高到低*/
void Sort(ZGGZ tp[],int n)
{
int i=0,j=0,flag=0;
ZGGZ newinfo;
if(n<=0)
{ system("cls");
printf("\n=====>Not employee record!\n");
getchar();
return ;
}
system("cls");
Disp(tp,n); /*顯示排序前的所有記錄*/
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<n-1;j++)
if((tp[j].sfgz<tp[j+1].sfgz))
{ flag=1;
strcpy(newinfo.num,tp[j].num); /*利用結構變數newinfo實現數組元素的交換*/
strcpy(newinfo.name,tp[j].name);
newinfo.jbgz=tp[j].jbgz;
newinfo.jj=tp[j].jj;
newinfo.kk=tp[j].kk;
newinfo.yfgz=tp[j].yfgz;
newinfo.sk=tp[j].sk;
newinfo.sfgz=tp[j].sfgz;
strcpy(tp[j].num,tp[j+1].num);
strcpy(tp[j].name,tp[j+1].name);
tp[j].jbgz=tp[j+1].jbgz;
tp[j].jj=tp[j+1].jj;
tp[j].kk=tp[j+1].kk;
tp[j].yfgz=tp[j+1].yfgz;
tp[j].sk=tp[j+1].sk;
tp[j].sfgz=tp[j+1].sfgz;
strcpy(tp[j+1].num,newinfo.num);
strcpy(tp[j+1].name,newinfo.name);
tp[j+1].jbgz=newinfo.jbgz;
tp[j+1].jj=newinfo.jj;
tp[j+1].kk=newinfo.kk;
tp[j+1].yfgz=newinfo.yfgz;
tp[j+1].sk=newinfo.sk;
tp[j+1].sfgz=newinfo.sfgz;
}
if(flag==0) break;/*若標記flag=0,意味著沒有交換了,排序已經完成*/
}
Disp(tp,n); /*顯示排序後的所有記錄*/
saveflag=1;
printf("\n =====>sort complete!\n");
}
/*數據存檔,若用戶沒有專門進行此操作且對數據有修改,在退出系統時, 會提示用戶存檔*/
void Save(ZGGZ tp[],int n)
{
FILE* fp;
int i=0;
fp=fopen("c:\\zggz","wb");/*以只寫方式打開二進制文件*/
if(fp==NULL) /*打開文件失敗*/
{
printf("\n=====>open file error!\n");
getchar();
return ;
}
for(i=0;i<n;i++)
{
if(fwrite(&tp[i],sizeof(ZGGZ),1,fp)==1)/*每次寫一條記錄或一個結構數組元素至文件*/
{
continue;
}
else
{
break;
}
}
if(i>0)
{
getchar();
printf("\n\n=====>save file complete,total saved's record number is:%d\n",i);
getchar();
saveflag=0;
}
else
{system("cls");
printf("the current link is empty,no employee record is saved!\n");
getchar();
}
fclose(fp); /*關閉此文件*/
}
void main()
{
ZGGZ gz[N]; /*定義ZGGZ結構體*/
FILE *fp; /*文件指針*/
int select; /*保存選擇結果變數*/
char ch; /*保存(y,Y,n,N)*/
int count=0; /*保存文件中的記錄條數(或元素個數)*/
fp=fopen("C:\\zggz","ab+");
/*以追加方式打開二進制文件c:\zggz,可讀可寫,若此文件不存在,會創建此文件*/
if(fp==NULL)
{
printf("\n=====>can not open file!\n");
exit(0);
}
while(!feof(fp))
{
if(fread(&gz[count],sizeof(ZGGZ),1,fp)==1) /*一次從文件中讀取一條職工工資記錄*/
count++;
}
fclose(fp); /*關閉文件*/
printf("\n==>open file sucess,the total records number is : %d.\n",count);
getchar();
menu();
while(1)
{
system("cls");
menu();
printf("\n Please Enter your choice(0~9):"); /*顯示提示信息*/
scanf("%d",&select);
if(select==0)
{
if(saveflag==1) /*若對數組的數據有修改且未進行存檔操作,則此標志為1*/
{ getchar();
printf("\n==>Whether save the modified record to file?(y/n):");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
Save(gz,count);
}
printf("\n===>thank you for useness!");
getchar();
break;
}
switch(select)
{
case 1:count=Add(gz,count);break; /*增加職工工資記錄*/
case 2:count=Del(gz,count);break; /*刪除職工工資記錄*/
case 3:Qur(gz,count);break; /*查詢職工工資記錄*/
case 4:Modify(gz,count);break; /*修改職工工資記錄*/
case 5:count=Insert(gz,count);break; /*插入職工工資記錄*/
case 6:Tongji(gz,count);break; /*統計職工工資記錄*/
case 7:Sort(gz,count);break; /*排序職工工資記錄*/
case 8:Save(gz,count);break; /*保存職工工資記錄*/
case 9:system("cls");Disp(gz,count);break; /*顯示職工工資記錄*/
default: Wrong();getchar();break; /*按鍵有誤,必須為數值0-9*/
}
}
}
『拾』 c語言學生成績管理系統按照學號排序怎麼排
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedefstructstudent
{
charname[12];
charstudent_id[12];
unsignedshortChinese;
unsignedshortEnglish;
unsignedshortMath;
unsignedshortaverage;
}student_t;
unsignedshortnum;
unsignedshortpass_rate;
voidenter_message(student_t*p);
voidsort(student_t*p);
voidprint_message(student_t*p);
voidswit_student(student_t*p,student_t*q);
intmain(intargc,charconst*argv[])
{
printf("Enterthenumberofstudents:");
scanf("%hu",&num);
student_t*p=NULL;
p=(student_t*)malloc(num*sizeof(student_t));
printf("Enterstudentsmessage,example:namestudent_idChineseEnglishMath ");
enter_message(p);
sort(p);
print_message(p);
free(p);
return0;
}
unsignedshortnum;
unsignedshortpass_rate;
voidenter_message(student_t*p);
voidsort(student_t*p);
voidprint_message(student_t*p);
voidswit_student(student_t*p,student_t*q);
intmain(intargc,charconst*argv[])
{
printf("Enterthenumberofstudents:");
scanf("%hu",&num);
student_t*p=NULL;
p=(student_t*)malloc(num*sizeof(student_t));
printf("Enterstudentsmessage,example:namestudent_idChineseEnglishMath ");
enter_message(p);
sort(p);
print_message(p);
free(p);
return0;
}
voidswit_student(student_t*p,student_t*q)
{
student_ttemp;
strcpy(temp.name,p->name);
strcpy(p->name,q->name);
strcpy(q->name,temp.name);
strcpy(temp.student_id,p->student_id);
strcpy(p->student_id,q->student_id);
strcpy(q->student_id,temp.student_id);
temp.Chinese=p->Chinese;
p->Chinese=q->Chinese;
q->Chinese=temp.Chinese;
temp.English=p->English;
p->English=q->English;
q->English=temp.English;
temp.Math=p->Math;
p->Math=q->Math;
q->Math=temp.Math;
temp.average=p->average;
p->average=q->average;
q->average=temp.average;
}