寫一段編程
A. 請C語言編程達人幫忙編寫一段程序
我沒用用文件啊 用的就是結構體typedef struct employee 這個就是結構體
#include<stdio.h>
#include<stdlib.h>
typedef struct employee
{
int id;
char name[20];
char department[20];
int money;
char position[20];
struct employee *next;
}Employee,*ept;
typedef struct
{
ept head;//頭指針
ept tail;//尾指針
ept current;
ept p,q;
int tot;
}Linkemployee;
int isfound(Linkemployee &l,int id)//判斷是否有重復的職工號
{
int count=0,flag;
l.p=l.head;
while(count++<l.tot)
{
flag=0;
if(l.p->id==id)
flag=1;
else
l.p=l.p->next;
}
if(flag==1)
return 1;
else
return 0;
}
void CreatLink(Linkemployee &l)//構造空鏈表
{
l.head=l.tail=(Employee*)malloc(sizeof(Employee));
l.head=l.tail=NULL;
l.tot=0;
}
void AddInformation(Linkemployee &l)
{
l.current=(Employee*)malloc(sizeof(Employee));
printf("請輸入職工姓名:");
scanf("%s",l.current->name);
printf("請輸入職工號:");
scanf("%d",&l.current->id );
printf("請輸入職工部門:");
scanf("%s",l.current->department);
printf("請輸入職工職位:");
scanf("%s",l.current->position );
printf("請輸入職工工資:");
scanf("%d",&l.current->money );
if(l.head==NULL)
{
l.head=l.tail=l.current;
l.head->next=l.tail;
l.tail->next=NULL;
l.tot++;
printf("職工添加成功!!!\n");
}
else
{
if(!isfound(l,l.current->id))
{
l.tail->next=l.current;
l.current->next=NULL;
l.tail=l.current;
l.tot++;
printf("職工添加成功!!!\n");
}
else
printf("職工號已經存在\n");
}
}
void SearchInformation(Linkemployee &l)
{
if(l.tot>0)
{
int findnumber,count=0;
l.p=l.head;
printf("輸入要要查找的職工號:");
scanf("%d",&findnumber);
while(count++<l.tot)
{
if(findnumber==l.p->id)
{
printf("職工信息找到!\n");
printf("姓名 職工號 職工部門 職工工資 職工職位\n");
printf("%s%6d%8s%8d%8s\n",l.p->name,l.p->id,l.p->department,l.p->money,l.p->position);
}
else
printf("無輸入職工號的信息\n");
l.p=l.p->next;
}
}
else
printf("沒有任何信息\n");
}
void DisplayInformation(Linkemployee &l)
{
if(l.tot>0)
{
int count=0;
l.p=l.head;
printf("姓名 職工號 職工部門 職工工資 職工職位\n");
while(count++<l.tot)
{
printf("%s%8d%8s%8d%8s\n",l.p->name,l.p->id,l.p->department,l.p->money,l.p->position);
l.p=l.p->next;
}
}
else
printf("沒有任何信息\n");
}
void DeleteInformation(Linkemployee &l)
{
int findid, count=0;
int flag;
int selection;
l.p=l.head;
l.q=l.p;//記錄刪除節點的前一個節點
if(l.tot>0)
{
printf("輸入要刪除的職工號:");
scanf("%d",&findid);
while(count++<l.tot)
{
flag=0;
if(findid==l.p->id)
{
flag=1;
}
else
{
l.q=l.p;//記錄刪除節點的前一個節點
l.p=l.p->next;
}
}
if(flag==1)
{
printf("職工信息找到!\n");
printf("姓名 職工號 職工部門 職工工資 職工職位\n");
printf("%s%6d%6s%6d%6s\n",l.p->name,l.p->id,l.p->department,l.p->money,l.p->position);
printf("確認刪除嗎?1刪除,2退出\n");
scanf("%d",&selection);
if(selection==1)
{
if(l.p==l.tail )
{
l.q->next=NULL;
l.tail=l.q;
free(l.p);
}
else if(l.p==l.head)
{
l.q=l.p;
l.p=l.p->next;
l.head=l.p;
free(l.q);
}
else
{
l.q->next=l.p->next;
free(l.p);
}
l.tot--;
}
else
printf("自動退出\n");
}
else
printf("無輸入職工號信息\n");
}
else
printf("沒有任何信息\n");
}
void main()
{
int selection;
Linkemployee l;
CreatLink(l);
printf("----------------------------------------------\n歡迎進入公司職工信息管理程序");
printf("\n----------------------------------------------\n");
printf("請選擇您的操作:\n1. 增加職工信息\n2. 查找職工信息\n3. 顯示所有職工信息\n4. 刪除職工信息\n5. 退出\n");
while(scanf("%d",&selection)&&selection!=5)
{
switch(selection)
{
case 1:AddInformation(l);break;
case 2:SearchInformation(l);break;
case 3:DisplayInformation(l);break;
case 4:DeleteInformation(l);break;
}
printf("----------------------------------------------\n歡迎進入公司職工信息管理程序");
printf("\n----------------------------------------------\n");
printf("請選擇您的操作:\n1. 增加職工信息\n2. 查找職工信息\n3. 顯示所有職工信息\n4. 刪除職工信息\n5. 退出\n");
}
}
B. 編寫一段C語言程序,要求輸入一行字元,找出其中的大寫字母、小寫字母、空格、數字以及其他字元的個數。
代碼資料:
#include "stdio.h"
int main(int argc,char *argv[]){
char s[300];
int i,uc,lc,sp,di,ot;
printf("Please enter a string... ");
i=0;
while(s[i]=getchar(),s[i]!=' ' && ++i<300);
uc=lc=sp=di=ot=0;
for(s[i]='