关于统计人数,求思路
求助:关于高校职工信息的课程设计 有一个功能是“统计指定二级单位的职工人数、工资总额”,我想问下统计职工人数是不是先列出不同二级单位的名称(字符),然后用比较相同字符的方法,再用循环i++累计,有没有更简单的方法??然后求工资总额应该怎么弄?数组还是什么的?谢谢!
#include <stdio.h> #define SIZE 5//定义员工数,此数值可以根据实际情况进行修改 void get_data(int number[], float level[], int hours[]); int count_data(float level[], int hours[], float salary[]); void pay(int number[], float salary[]); void put_data(int number[], float level[], int hours[], float salary[]); int main() { int number[SIZE]; float level[SIZE]; int hours[SIZE]; float salary[SIZE]; get_data(number, level, hours); count_data(level, hours, salary); put_data(number, level, hours, salary); pay(number, salary); return 0; } void get_data(int number[], float level[], int hours[]) { int i; for(i = 0; i < SIZE; i++) { printf("Please enter number,level and hours:\n"); scanf("%d%f%d", &number[i], &level[i], &hours[i]); while(level[i] < 40 || (level[i] < 10000 && level[i] > 800)) { printf("level Error!Enter level again:\n"); scanf("%f", &level[i]); } } } int count_data(float level[], int hours[], float salary[]) { int i; for(i = 0; i <SIZE; i++) { if(level[i] > 10000) salary[i] = level[i] / 24; else salary[i] = level[i] * hours[i]; } } void put_data(int number[], float level[], int hours[], float salary[]) { int i; printf("Number Level Hours Salary Salary of year\n"); for(i = 0; i < SIZE; i++) { printf("%-8d%-12.2f%-8d%12.2f%12.2f\n", number[i], level[i], hours[i], salary[i], salary[i] * 24); } } void pay(int number[], float salary[]) { int i, num; printf("Please enter number you want to find:\n"); scanf("%d", &num); for(i = 0; i < SIZE; i++) if(num == number[i]) { printf("Salary is %-12.2f", salary[i]); break; } if(i == SIZE) printf("not found\n"); }
#include<string.h> #include<stdlib.h> #include <stdio.h> typedef struct employee { char num[10]; //保存职工编号 char name[15]; //保存职工姓名 char sex[4]; //保存职工性别 char danwei[15]; //保存职工所属二级单位 char majoy[10]; //保存职工所学专业 char call[15]; //保存职工职称 int salary; //保存职工月工资 char tell[10]; //保存职工电话号码 }worker; void menu() { printf("****************************欢迎来到高校职工管理界面****************************\n"); printf("================================================================================"); printf(" ① 输入记录 ② 删除记录 ③ 增加记录\n"); printf(" ④ 查询记录 ⑤ 统计记录 ⑥ 排序操作\n"); printf(" ⑦ 保存记录 ⑧ 显示记录 ⑨ 退出界面\n"); printf("================================================================================\n"); printf("请输入(1-9):\n"); } int input(worker a[],int k) //输入记录 { int j,x; char str[10]; j=x=0; while(1) { printf("----------------------------\n"); printf("请输入职工的工号(按0返回):"); scanf("%s",str); strcpy(a[k].num,str); for(j=0;j<k;j++) { if(strcmp(a[j].num,str)==0) { printf("已经有工号为%s的员工信息!请重新输入员工号。\n",str); printf("请输入职工的编号:"); scanf("%s",str); strcpy(a[k].num,str); } } if(strcmp(a[k].num,"0")==0) break; printf("请输入职工的姓名:"); scanf("%s",&a[k].name); printf("请输入职工的性别:"); scanf("%s",&a[k].sex); printf("请输入职工所属二级单位:"); scanf("%s",&a[k].danwei); printf("请输入职工所学专业:"); scanf("%s",&a[k].majoy); printf("请输入职工的职称:"); scanf("%s",&a[k].call); printf("请输入职工的月薪:"); scanf("%d",&a[k].salary); printf("请输入职工的电话号码:"); scanf("%s",&a[k].tell); k++; } return k; } int del(worker a[],int y) //删除记录 { int i=0; int x=0; char str[15]; printf("请输入你要删除的职工号:"); scanf("%s",str); printf("--------删除成功--------\n"); for(i=0;i<y;i++) if(!strcmp(str,a[i].num)) { while(i<y) { strcpy(a[i].num,a[i+1].num); strcpy(a[i].name,a[i+1].name); strcpy(a[i].sex,a[i+1].sex); strcpy(a[i].danwei,a[i+1].danwei); strcpy(a[i].majoy,a[i+1].majoy); strcpy(a[i].call,a[i+1].call); a[i].salary=a[i+1].salary; strcpy(a[i].tell,a[i+1].tell); i++; } x++; } if(x==0) { printf("输入的职工号不存在,请重新输入:\n"); } return y-x; } int add(worker a[],int y) //增加记录 { int i; int x=0; char str[15]; printf("请输入你要插入的职工工号:"); scanf("%s",str); printf("请输入该职工的相关信息:\n"); for(i=0;i<y;i++) if(strcmp(str,a[i].num)==0) { x=i; for(i=y;i>x+1;i--) { strcpy(a[i].num,a[i-1].num); strcpy(a[i].name,a[i-1].name); strcpy(a[i].sex,a[i-1].sex); strcpy(a[i].danwei,a[i-1].danwei); strcpy(a[i].majoy,a[i-1].majoy); strcpy(a[i].call,a[i-1].call); a[i].salary=a[i-1].salary; strcpy(a[i].tell,a[i-1].tell); } } x++; printf("请输入职工的工号:"); scanf("%s",a[i].num); printf("请输入职工的姓名:"); scanf("%s",&a[i].name); printf("请输入职工所属二级单位:"); scanf("%s",&a[i].danwei); printf("请输入职工所学专业:"); scanf("%s",&a[i].majoy); printf("请输入职工的职称:"); scanf("%s",&a[i].call); printf("请输入职工的月薪:"); scanf("%d",&a[i].salary); printf("请输入职工的电话号码:"); scanf("%s",&a[i].tell); return y+1; } int search(worker a[],int y) //查询记录 { int i; int x=0; char str[15]; printf("请输入你要查找的职工工号或电话号码:"); scanf("%s",str); for(i=0;i<y;i++) if(!strcmp(str,a[i].num)||!strcmp(str,a[i].tell)) { printf("职工的工号为%s\n",a[i].num); printf("职工的姓名为%s\n",a[i].name); printf("职工的性别:%s\n",a[i].sex); printf("职工的所属二级单位:%s\n",a[i].danwei); printf("职工的所学专业:%s\n",a[i].majoy); printf("职工的职称:%s\n",a[i].call); printf("职工的月工资:%d\n",a[i].salary); printf("职工的电话号码:%s\n",a[i].tell); x++; } if(x==0) { printf("未查找到该职工的记录\n"); return -1; } } void count(worker a[],int y) //统计指定二级单位人数及工资总额 { int q[9]={0,0,0,0,0,0,0,0,0}; int sum[9]={0,0,0,0,0,0,0,0,0}; int i=0; for(i=0;i<y;i++) { if(a[i].danwei='A') { q[0]++; sum[0]+=a[i].salary; } if(a[i].danwei='B') { q[1]++; sum[1]+=a[i].salary; } if(a[i].danwei='C') { q[2]++; sum[2]+=a[i].salary; } if(a[i].danwei='D') { q[3]++; sum[3]+=a[i].salary; } if(a[i].danwei='E') { q[4]++; sum[4]+=a[i].salary; } if(a[i].danwei='F') { q[5]++; sum[5]+=a[i].salary; } if(a[i].danwei='G') { q[6]++; sum[6]+=a[i].salary; } if(a[i].danwei='H') { q[7]++; sum[7]+=a[i].salary; } if(a[i].danwei='I') { q[8]++; sum[8]+=a[i].salary; } } printf("请输入需要统计的二级单位代号:\n"); switch(); { case 'A': printf("A.财务部的人数为%d,工资总额为:%d\n",q[0],sum[0]); break; case 'B': printf("B.行政部的人数为%d,工资总额为:%d\n",q[1],sum[1]); break; case 'C': printf("C.人事部的人数为%d,工资总额为:%d\n",q[2],sum[2]); break; case 'D': printf("D.教学部的人数为%d,工资总额为:%d\n",q[3],sum[3]); break; case 'E': printf("E.校长办公室的人数为%d,工资总额为:%d\n",q[4],sum[4]); break; case 'F': printf("F.学院办公室的人数为%d,工资总额为:%d\n",q[5],sum[5]); break; case 'G': printf("G.学生处的人数为%d,工资总额为:%d\n",q[6],sum[6]); break; case 'H': printf("H.后勤部的人数为%d,工资总额为:%d\n",q[7],sum[7]); break; case 'I': printf("I.保卫处的人数为%d,工资总额为%d\n",q[8],sum[8]); break; default:printf("查找不到该二级单位代号,请重新输入!\n"); } } void sort(worker a[],int y) //排序,递增序列 { int i,j; float t; char str[15]; for(j=0;j<y-1;j++) for(i=0;i<y-1-j;i++) if(a[i].salary>a[i+1].salary) { t=a[i].salary; a[i].salary=a[i+1].salary; a[i+1].salary=t; strcpy(str,a[i].name); strcpy(a[i].name,a[i+1].name); strcpy(a[i+1].name,str); strcpy(str,a[i].sex); strcpy(a[i].sex,a[i+1].sex); strcpy(a[i+1].sex,str); strcpy(str,a[i].num); strcpy(a[i].num,a[i+1].num); strcpy(a[i+1].num,str); strcpy(str,a[i].danwei); strcpy(a[i].danwei,a[i+1].danwei); strcpy(a[i+1].danwei,str); strcpy(str,a[i].majoy); strcpy(a[i].majoy,a[i+1].majoy); strcpy(a[i+1].majoy,str); strcpy(str,a[i].call); strcpy(a[i].call,a[i+1].call); strcpy(a[i+1].call,str); strcpy(str,a[i].tell); strcpy(a[i].tell,a[i+1].tell); strcpy(a[i+1].tell,str); } } void save(worker a[],int n) //保存记录 { FILE *fp; int i; if((fp=fopen("worker.txt","wb"))==NULL) { printf("cannot open file\n"); return; } for(i=0;i<n;i++) if(fwrite(&a[i],sizeof(struct employee),1,fp)!=1) printf("file write error\n"); fclose(fp); } void display(worker a[],int n) //显示操作 { int i=0; if(n==0) { printf("Files are empty\n"); return; } do { printf("----------------------------\n"); printf("职工的工号为%s\n",a[i].num); printf("职工的姓名为%s\n",a[i].name); printf("职工的性别:%s\n",a[i].sex); printf("职工的所属二级单位:%s\n",a[i].danwei); printf("职工的所学专业:%s\n",a[i].majoy); printf("职工的职称:%s\n",a[i].call); printf("职工的月工资:%d\n",a[i].salary); printf("职工的电话号码:%s\n",a[i].tell); i++; }while(i<n); } void main() //主函数 { int selete,c=0; int i=0; int z=0; char q,p,x,s; worker b[100]; FILE *fp; if((fp=fopen("worker++.txt","rb"))==NULL) printf("Not Found!\n"); else { do { fread(&b[i],sizeof(struct employee),1,fp); i++; }while(!feof(fp)); fclose(fp); c=i-1; } while(1) { menu(); //菜单 scanf("%d",&selete); if(selete==9) { printf("感谢您的使用,再见!\n"); break; } switch(selete) { case 1: c=input(b,c); break; case 2: { display(b,c); do { c=del(b,c); printf("是否继续删除,是按y,否则请按任意值\n"); getchar(); scanf("%c",&p); }while(p=='Y'||p=='y'); break; } case 3: { do { c=add(b,c); printf("是否继续插入,是按y,否则请按任意值\n"); getchar(); scanf("%c",&s); }while(s=='Y'||s=='y'); break; } case 4: { do { search(b,c); printf("是否继续查找,是按y,否则请按任意值\n"); getchar(); scanf("%c",&x); }while(x=='Y'||x=='y'); break; } case 5: count(b,c); break; case 6: sort(b,c); display(b,c); break; case 7: save(b,c); break; case 8: display(b,c); break; default:printf("请重新输入!\n"); } } }你好~我想问下第五个功能(统计count那个)应该怎么改正,出错了TT