❶ c語言 按姓名查找學生,這里我輸入hm後就報錯了
1 size_t 是無符號整數,你程序里的邏輯是在搜索時坐標會變負數,所以把size_t改成int型
2. 你搜索的是按字母排序的表,所以最好將「hm」排在「wgp」前
下面的程序可以運行:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
typedef struct StuInfo
{
int id;
char name[100];
}Info;
int binarySearchName(Info b[], char searchKey[], int low, int high);
int main()
{
Info stu[3] = { { 0,"hm"},{1,"wgp"} ,{2,"wj"} };
char a[100];
gets_s(a);
int i;
i=binarySearchName(stu, a, 0, 1);
if (i!= -1)
printf("find");
system("pause");
return 0;
}
int binarySearchName(Info b[], char searchKey[], int low, int high)
{
while (low <= high) {
int middle = (low + high) / 2;
if (strcmp(searchKey, b[middle].name) == 0) {
return middle;
}
else if (strcmp(searchKey, b[middle].name) < 0) {
high = middle - 1;
}
else {
low = middle + 1;
}
}
return -1;
}
❷ c語言中根據姓名查詢成績
#include "conio.h"
struct student
{
char name [15];
int score;
};
int find (struct student s[]);void main()
{
int i=0;
struct student stu[5];
for(i=0;i<5;i++)
{
printf("輸入第%d個學生的姓名:",i+1);
scanf("%s",stu[i].name);
printf("輸入第%d個學生的成績:",i+1);
scanf("%d",&stu[i].score);
}
int nIndex=find(stu);
if(nIndex!=-1)
printf("找到該同學信息,該同學位於第%d個位置(數組下標)",nIndex);
getch();
}int find(struct student s[])
{
char name[15];
int i=0;
int nIndex=-1;
printf("輸入要查詢學生的姓名:");
scanf("%s",name);
for(i=0;i<5;i++)
{
if(strcmp(s[i].name,name)==0)
{
nIndex=i;
break;
}
}
return nIndex;
}
❸ c語言,輸入工號,和姓名,然後線性查找,輸入查找工號,輸出姓名
#include"stdio.h"
#defineN10
#defineThree5
intReadName(charname[][Three],longnum[]);/*Readname()函數原型*/
intLinSearch(longnum[],longx,intn);/*LinSeach()函數原型*/
intmain()
{
charname[N][Three];/*定義二維字元數組*/
intn,pos;
longnum[N],x;
n=ReadName(name,num);/*輸入教師姓名和工號*/
printf("Totalteachersare%d ",n);
printf("InputthesearchingID");
scanf("%d",&x);/*以整型格式從鍵盤輸入待查找的工號*/
pos=LinSearch(num,x,n);/*查找工號為num的教師*/
if(pos!=-1)
printf("name=%s ",name[pos]);/*若找到列印姓名*/
else
printf("Notfound! ");/*若未找到,則列印未找到提示信息*/
return0;
}
/*函數功能:輸入教師的工號和姓名,當輸入負值時,結束輸入,返回教師人數*/
intReadName(charname[][Three],longnum[])/*函數定義*/
{
inti=-1;
do{
i++;
printf("Inputteacher'sIDandname:");
scanf("%d%s",num+i,name[i]);//參數錯誤.
}while(num[i]>0);/*輸入負值時結束輸入*/
returni;
}
/*按線性查找法查找值為x的數組元素,若找到則返回x在數組中的下標位置,否則返回-1*/
intLinSearch(longnum[],longx,intn)/*函數定義*/
{
inti;
for(i=0;i<n;i++)
{
if(num[i]==x)
returni;/*若找到則返回x在數組中的下標*/
}
return-1;/*若循環結束任然未找到,則返回-1*/
}
❹ 求大神解題。c語言,按姓名查詢的一個系統,for循環
#include<stdio.h>
struct member{
char name[20];
char phone[15];
};
void main()
{
int i,j,k;
int flag = 0;
//初始化存入三個記錄
struct member mem[3] = {{"WangFeng","135123"},{"WangJie","136123"},{"LiuJie","189123"}};
char sub_str[20];
printf("輸入查詢條件:\n");
scanf("%s",sub_str);
for(i=0;i<3;i++)
{
for(k=0,j=0;mem[i].name[k]!='\0'&&sub_str[j]!='\0';)//本循環體是匹配子串
{
if(mem[i].name[k]==sub_str[j]){k++;j++;}
else {k++;j=0;}
}
if(sub_str[j]=='\0'){printf("find a person match: %s %s\n",mem[i].name,mem[i].phone);flag++;}
}
if(!flag)printf("not found!\n");
}
❺ c語言求解,按學號查找刪除改為按學號或姓名查找刪除應該怎麼改
第三行改成字元串輸入,查找按名字,該改兩個地方就好
❻ c語言求解,下面一段程序是按姓名查詢學生信息,我想改為輸入學號或姓名查詢學生信息
printf("輸入要查詢的學生姓名或學號:");
if(!strcmp(stu[i].name,str))
改成
if(!strcmp(stu[i].name,str)||!strcmp(stu[i].no,str))//no學號
❼ 用C語言設計一個學生信息查詢系統程序
1、首先創建一個c語言項目。然後右鍵頭文件,創建一個Stu的頭文件。
❽ 求助C語言學生系統中按照姓名進行查找學生
#include
#include
#include
struct Link/*定義結構體鏈表*/
{
int number;
char name[20];
char sex[4];
int chinese;
int math;
int lizong;
int english;
int sum;
float average;
struct Link*next;
};
void Picture(void);/*進行函數調用*/
char Menu1(void);
char Menu2(void);
struct Link *Append(struct Link *head);
void Print(struct Link*head);
struct Link *Delete(struct Link *head);
struct Link *Change(struct Link *head);
void Find(struct Link*head);
void Sort1(struct Link*head);
void Sort2(struct Link*head);
void Sort3(struct Link*head);
void Sort4(struct Link*head);
void Deletememory(struct Link*head);
void Beifen(struct Link*head);
struct Link*Huanyuan(struct Link*head);
int Total(struct Link*head);
const char *file="wenjian";
char *Mima(void);
int main()
{
char choice,ch,*shuru,mima[7]="123456";
struct Link *head=NULL;
system("color 4f");
Picture();
printf("登陸身份認證:\n你是一位老師還是一名學生?(Teacher/Student)\n");
scanf("%c",&ch);
if(ch=='T'||ch=='t')
{
do
{ printf("請輸入6位密碼:\n");
scanf("%s",shuru);
if(strcmp(shuru,mima)==0)
break;
else {printf("輸入密碼不正確\n");}
}while(strcmp(shuru,mima)!=0);
while(1)
{
system("cls");
choice=Menu1();
switch(choice)
{
case '1':
head=Append(head);
system("cls");
break;
case '2':
Print(head);
break;
case '3':
head=Delete(head);
break;
case '4':system("cls");
head=Change(head);
printf("想要查看嗎?(Y/N)");
scanf(" %c",&ch);
if(ch=='Y'||ch=='y')
Print(head);
break;
case '5':Find(head);
break;
case '6':Sort1(head);
printf("已經排好順序了\n");
break;
case '7':Sort2(head);
printf("已經排好順序了\n");
break;
case '8':Sort3(head);
printf("已經排好順序了\n");
break;
case '9':Sort4(head);
printf("已經排好順序了\n");
break;
case 'A':Beifen(head);
exit(0);
case 'B':head=Huanyuan(head);
char i=getchar();
printf("您已經成功還原,按ENTER鍵返回主菜單\n");
i=getchar();
break;
case 'C':Deletememory(head);
exit(0);
default:printf("input error!");
break;
}
}
}
else
{
while(1)
{
system("cls");
choice=Menu2();
switch(choice)
{
case '1':
Print(head);
break;
case '2':Find(head);
break;
case '3':Sort1(head);
char i=getchar();
printf("已經排好順序了,按ENTER鍵返回主菜單\n");
i=getchar();
break;
case '4':Sort2(head);
i=getchar();
printf("已經排好順序了,按ENTER鍵返回主菜單\n");
i=getchar();
break;
case '5':Sort3(head);
i=getchar();
printf("已經排好順序了,按ENTER鍵返回主菜單\n");
i=getchar();
break;
case '6':Sort4(head);
i=getchar();
printf("已經排好順序了,按ENTER鍵返回主菜單\n");
i=getchar();
Print(head);
break;
case '7':head=Huanyuan(head);
i=getchar();
printf("您已經成功還原,按ENTER鍵返回主菜單\n");
i=getchar();
break;
case '8':Deletememory(head);
exit(0);
default:printf("input error!");
break;
}
}
}
}
char Menu1(void)/*教師菜單*/
{
char ch;
printf("%5\t教師菜單\n");
printf("創名校 做名師 育名人\n");
printf("%5\t1、增添\n");
printf("%5\t2、列表顯示\n");
printf("%5\t3、刪除\n");
printf("%5\t4、修改\n");
printf("%5\t5、查詢\n");
printf("%5\t6、以總分降序排列\n");
printf("%5\t7、以總分升序排列\n");
printf("%5\t8、以學號降序排列\n");
printf("%5\t9、以學號升序排列\n");
printf("%5\tA、備份文件\n");
printf("%5\tB、還原文件\n");
printf("%5\tC、退出\n");
printf("please enter your choice:\n");
scanf(" %c",&ch);
return ch;
}
char Menu2(void)/*學生菜單*/
{
char ch;
printf("%5\t學生菜單\n");
printf("規格嚴格 功夫到家\n");
printf("%5\t1、列表顯示\n");
printf("%5\t2、查詢\n");
printf("%5\t3、以總分降序排列\n");
printf("%5\t4、以總分升序排列\n");
printf("%5\t5、以學號降序排列\n");
printf("%5\t6、以學號升序排列\n");
printf("%5\t7、還原文件\n");
printf("%5\t8、退出\n");
printf("please enter your choice:\n");
scanf(" %c",&ch);
return ch;
}
struct Link *Append(struct Link *head)/*增添的函數*/
{
char c;
do{
system("cls");
system("color 1f");
struct Link *p=NULL;
struct Link *pr=head;
p=(struct Link*)malloc(sizeof(struct Link));
if(p==NULL)
{
printf("沒有足夠內存,返回主菜單\n");
break;
}
if(head==NULL)
{
head=p;
}
else
{
while(pr->next!=NULL)
{
pr=pr->next;
}
pr->next=p;
}
pr=p;
printf("請輸入學號\n");
scanf("%d",&p->number);
printf("請輸入姓名\n");
scanf("%s",p->name);
printf("請輸入性別\n");
scanf("%s",p->sex);
printf("請輸入語文成績(0——150)\n");
do{
scanf("%d",&p->chinese);
if((p->chinese)chinese)>150)
printf("輸入分數有誤,請重新輸入\n");
}while((p->chinese)chinese)>150);
printf("請輸入數學(0——150)\n");
scanf("%d",&p->math);
printf("請輸入理科綜合成績(0——300)\n");
scanf("%d",&p->lizong);
printf("請輸入英語成績(0——150)\n");
scanf("%d",&p->english);
p->sum=(p->chinese)+(p->math)+(p->lizong)+(p->english);
p->average=(float)(p->sum)/4;
pr->next=NULL;
printf("\nnew nodes have been append!\n");
printf("\n是否想新增加一個新成員?(Y/N)\n");
scanf(" %c",&c);
}
while(c=='Y'||c=='y');
return head;
}
void Print(struct Link*head)/*列表顯示的函數*/
{
if(head==NULL)
{
char i=getchar();
printf("無內容,無法顯示,按ENTER鍵返回菜單\n");
i=getchar();
system("cls");
}
else{
printf("學號 姓名 性別 語文 數學 理科綜合 英語 總分 平均分\n");
struct Link*p=head;
while(p!=NULL)
{
printf("\n%d %s %s %d",p->number,p->name,p->sex,p->chinese);
printf(" %d %d %d %d %.2f",p->math,p->lizong,p->english,p->sum,p->average);
p=p->next;
}
char i=getchar();
printf("\n按ENTER鍵返回主菜單\n");
i=getchar();
system("cls");
}
}
struct Link *Delete(struct Link *head)/*刪除的函數*/
{
if(head==NULL)
{
char i=getchar();
printf("鏈表為空,沒有要刪除對象,按ENTER鍵返回主菜單\n");
i=getchar();
system("cls");
}
else{
int c;
char ch;
do{
if(head==NULL)
{
char i=getchar();
printf("鏈表為空,沒有要刪除對象,按ENTER鍵返回主菜單\n");
i=getchar();
system("cls");
break;
}
printf("請輸入要刪除的學號\n");
scanf("%d",&c);
struct Link *p=head,*pr=head;
while(c!=p->number&&p->next!=NULL)
{
pr=p;
p=p->next;
}
if(c==p->number)
{
if(p==head)
{
head=p->next;
}
else
{
pr->next=p->next;
}
free(p);
printf("您已經成功刪除");
}
else printf("沒有找到!");
printf("\n想繼續嗎?(Y/N)");
scanf(" %c",&ch);
}while(ch=='Y'||ch=='y');
system("cls");
}
return head;
}
struct Link *Change(struct Link *head)/*修改的函數*/
{
int c;
char ch;
do{
if(head==NULL)
{
printf("鏈表為空,沒有要刪除對象\n");
break;
}
printf("請輸入要修改的學號\n");
scanf("%d",&c);
struct Link *p=head;
while(c!=p->number&&p!=NULL)
{
p=p->next;
}
if(c==p->number)
{
printf("請輸入改後學號\n");
scanf("%d",&p->number);
printf("請輸入改後姓名\n");
scanf("%s",p->name);
printf("請輸入改後性別\n");
scanf("%s",p->sex);
printf("請輸入改後語文成績\n");
scanf("%d",&p->chinese);
printf("請輸入改後數學\n");
scanf("%d",&p->math);
printf("請輸入改後理科綜合成績\n");
scanf("%d",&p->lizong);
printf("請輸入改後英語成績\n");
scanf("%d",&p->english);
p->sum=(p->chinese)+(p->math)+(p->lizong)+(p->english);
p->average=(float)(p->sum)/4;
printf("您已經成功修改\n");
}
else printf("沒找到!\n");
printf("想繼續修改嗎?(Y/N)");
scanf(" %c",&ch);
}while(ch=='Y'||ch=='y');
return head;
}
void Find(struct Link*head)/*查找的函數*/
{
int c;
char ch;
struct Link*p=head;
do{
printf("請輸入想要查詢的學號:\n");
scanf("%d",&c);
if(head==NULL)
{
printf("鏈表為空,沒有查詢對象\n");
break;
}
while(c!=p->number&&p!=NULL)
{
p=p->next;
}
if(c==p->number)
{
printf("學號 姓名 性別 語文 數學 理科綜合 英語 總分 平均分\n");
printf("\n%d %s %s %d",p->number,p->name,p->sex,p->chinese);
printf(" %d %d %d %d %.2f",p->math,p->lizong,p->english,p->sum,p->average);
}
else printf("您的輸入有誤,暫時沒有此學號的學生,請查證後再查詢\n");
printf("想繼續查詢嗎?(Y/N)\n");
scanf(" %c",&ch);
}while(ch=='Y'||ch=='y');
}
void Picture(void)/*超豪華界面,後來發現可以比這更簡單,但已經做完了,就沒有修改*/
{
printf ("hello world");
}
void Sort1(struct Link*head)/*排序函數1*/
{
struct Link*p;
int flag=0;
char temp1[20];
int temp2;
do
{
if(head==NULL)
{
char i=getchar();
printf("鏈表為空,按ENTER鍵返回菜單\n");
i=getchar();
break;
}
flag=0;
p=head;
while(p->next!=NULL)
{
if((p->sum)next->sum))/*只交換節點內容*/
{
temp2=(p->next->sum);//sum
(p->next->sum)=(p->sum);
(p->sum)=temp2;
strcpy(temp1,(p->next->name));//name
strcpy((p->next->name),(p->name));
strcpy((p->name),temp1);
strcpy(temp1,(p->next->sex));//sex
strcpy((p->next->sex),(p->sex));
strcpy((p->sex),temp1);
temp2=(p->next->chinese);
(p->next->chinese)=(p->chinese);
(p->chinese)=temp2;
temp2=(p->next->math);
(p->next->math)=(p->math);
(p->math)=temp2;
temp2=(p->next->lizong);
(p->next->lizong)=(p->lizong);
(p->lizong)=temp2;
temp2=(p->next->english);
(p->next->english)=(p->english);
(p->english)=temp2;
temp2=(p->next->average);
(p->next->average)=(p->average);
(p->average)=temp2;
flag=1;
}
p=p->next;
}
}while(flag);
}
void Sort2(struct Link*head)/*排序函數2*/
{
struct Link*p;
int flag=0;
char temp1[20];
int temp2;
do
{
if(head==NULL)
{
char i=getchar();
printf("鏈表為空,按ENTER鍵返回菜單\n");
i=getchar();
break;
}
flag=0;
p=head;
while(p->next!=NULL)
{
if((p->sum)>(p->next->sum))/*只交換節點內容*/
{
temp2=(p->next->sum);//sum
(p->next->sum)=(p->sum);
(p->sum)=temp2;
strcpy(temp1,(p->next->name));//name
strcpy((p->next->name),(p->name));
strcpy((p->name),temp1);
strcpy(temp1,(p->next->sex));//sex
strcpy((p->next->sex),(p->sex));
strcpy((p->sex),temp1);
temp2=(p->next->chinese);
(p->next->chinese)=(p->chinese);
(p->chinese)=temp2;
temp2=(p->next->math);
(p->next->math)=(p->math);
(p->math)=temp2;
temp2=(p->next->lizong);
(p->next->lizong)=(p->lizong);
(p->lizong)=temp2;
temp2=(p->next->english);
(p->next->english)=(p->english);
(p->english)=temp2;
temp2=(p->next->average);
(p->next->average)=(p->average);
(p->average)=temp2;
flag=1;
}
p=p->next;
}
}while(flag);
}
void Sort3(struct Link*head)/*排序函數3*/
{
struct Link*p;
int flag=0;
char temp1[20];
int temp2;
do
{
if(head==NULL)
{
char i=getchar();
printf("鏈表為空,按ENTER鍵返回菜單\n");
i=getchar();
break;
}
flag=0;
p=head;
while(p->next!=NULL)
{
if((p->number)next->number))/*只交換節點內容*/
{
temp2=(p->next->sum);//sum
(p->next->sum)=(p->sum);
(p->sum)=temp2;
strcpy(temp1,(p->next->name));//name
strcpy((p->next->name),(p->name));
strcpy((p->name),temp1);
strcpy(temp1,(p->next->sex));//sex
strcpy((p->next->sex),(p->sex));
strcpy((p->sex),temp1);
temp2=(p->next->chinese);
(p->next->chinese)=(p->chinese);
(p->chinese)=temp2;
temp2=(p->next->math);
(p->next->math)=(p->math);
(p->math)=temp2;
temp2=(p->next->lizong);
(p->next->lizong)=(p->lizong);
(p->lizong)=temp2;
temp2=(p->next->english);
(p->next->english)=(p->english);
(p->english)=temp2;
temp2=(p->next->average);
(p->next->average)=(p->average);
(p->average)=temp2;
flag=1;
}
p=p->next;
}
}while(flag);
}
void Sort4(struct Link*head)/*排序函數4*/
{
struct Link*p;
int flag=0;
char temp1[20];
int temp2;
do
{
if(head==NULL)
{
char i=getchar();
printf("鏈表為空,按ENTER鍵返回菜單\n");
i=getchar();
break;
}
flag=0;
p=head;
while(p->next!=NULL)
{
if((p->number)>(p->next->number))/*只交換節點內容*/
{
temp2=(p->next->sum);//sum
(p->next->sum)=(p->sum);
(p->sum)=temp2;
strcpy(temp1,(p->next->name));//name
strcpy((p->next->name),(p->name));
strcpy((p->name),temp1);
strcpy(temp1,(p->next->sex));//sex
strcpy((p->next->sex),(p->sex));
strcpy((p->sex),temp1);
temp2=(p->next->chinese);
(p->next->chinese)=(p->chinese);
(p->chinese)=temp2;
temp2=(p->next->math);
(p->next->math)=(p->math);
(p->math)=temp2;
temp2=(p->next->lizong);
(p->next->lizong)=(p->lizong);
(p->lizong)=temp2;
temp2=(p->next->english);
(p->next->english)=(p->english);
(p->english)=temp2;
temp2=(p->next->average);
(p->next->average)=(p->average);
(p->average)=temp2;
flag=1;
}
p=p->next;
}
}while(flag);
}
void Deletememory(struct Link *head)/*清楚內存*/
{
struct Link*p=head,*pr=NULL;
while(p!=NULL)
{
pr=p;
p=p->next;
free(pr);
}
}
void Beifen(struct Link*head)/*仿照資料庫的思路,用備份和還原的方法解決文件問題*/
{
FILE*fp;
struct Link*p=head;
int x=0;
fp=fopen(file,"w+");
if(fp==NULL)
{
printf("Can't open %s file\n",file);
exit(1);
}
x=Total(head);
fputc(x,fp);
while(p!=NULL)
{
fwrite(p,sizeof(struct Link),1,fp);
p=p->next;
}
printf("您已經成功備份,按ENTER鍵退出系統\n");
fclose(fp);
}
struct Link*Huanyuan(struct Link*head)/*還原文件*/
{
int m=1,n;
FILE*fp;
fp=fopen(file,"r+");
if(fp==NULL)
{
printf("Can't open %s file\n",file);
//exit(1);
}
struct Link*p=NULL;
struct Link*pr=NULL;
n=fgetc(fp);
pr=(struct Link*)malloc(sizeof(struct Link));
fread(pr,sizeof(struct Link),1,fp);
head=pr;
while(m<=n)
{
p=(struct Link*)malloc(sizeof(struct Link));
fread(p,sizeof(struct Link),1,fp);
pr->next=p;
pr=p;
pr->next=NULL;
m=m+1;
}
return head;
}
int Total(struct Link*head)/*計算輸入的學生總數,在還原函數中要用到*/
{
struct Link*p=head;
int n=-1;
while(p!=NULL)
{
n++;
p=p->next;
}
return n;
}
❾ C語言編寫通訊錄怎麼按姓名查找聯系人
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineN50
structfriend_list{
charname[16];
chartel[16];
charemail[20];
}fri[N];
/*voidinitstu(structfriend_list*p);*/
voidadd_friend(structfriend_list*p);
voidsearch_friend(structfriend_list*p);
voiddel_friend(structfriend_list*p,char*name);
voidrenew_friend(structfriend_list*p,char*name);
intn=0;//全局變數,用來記錄現有人數
voidmain(void){
intfriendnumber=0;
intchose=0;
while(1){
printf("1:新增聯系人 ");
printf("2:按姓名查找聯系人 ");
printf("3:按姓名刪除聯系人 ");
printf("4:按姓名修改聯系人 ");
printf("0:退出 ");
printf(" 請選擇輸入(0-4):");
scanf("%d",&chose);
switch(chose){
case1:add_friend(fri);break;
case2:
case3:
case4:
case0:
default:return;
}
}
}
voidadd_friend(structfriend_list*p){
if(n>=N){
printf("已經滿員,不能添加了。 ");
return;
}
printf("請輸入新增聯系人的姓名,聯系電話,電子郵箱: ");
scanf("%s%s%s",p[n].name,p[n].tel,p[n].email);
++n;
}
voidsearch_friend(structfriend_list*p){
inti,flag=1;
charname[16];
printf("請輸入要查找的聯系人的名字: ");
scanf("%s",name);
for(i=0;i<n&&flag;i++){
if(strcmp(p[i].name,name)==0){
printf("%s%s%s ",name,p[i].tel,p[i].email);
flag=0;
}
}
if(flag)printf("沒有找到名字叫:%s的人! ",name);
}
❿ C語言如何查找名字
字元串函數,頭文件包括#include
用其中的一個比較函數if(strcmp(name1,name2)==
0),所有的名字都存在一個二維字元數組里,把這句話加在循環體內每個名字遍歷一次即可找到。希望能幫到你。