當前位置:首頁 » 編程語言 » c語言根據名字找編號
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言根據名字找編號

發布時間: 2023-04-07 21:11:23

A. 用c語言怎樣能從小到大隨機給出編號啊

問題的敘述沒看明白,編號的形式是什麼啊,是數字組成的字串,還是整型變數核宴,要是整型有沒逗氏漏有范圍限制啊。看代碼好象是要對bian它隨機山爛賦值吧,代碼也不完整。

B. 如何編寫C語言程序使得輸入編號就可以顯示其他的個人信息

1、寫一個結構體數組用來記錄信息

這里我寫了一個可以存儲一個人的姓名、電話、郵箱的結構體。

structnote{
charname[100];
charphone[100];
charmail[100];
}people[1000];

2、用文件儲存更加方便

p=fopen("list.txt","r");
if(p==NULL)
{
fclose(p);
p=fopen("list.txt","w");
fclose(p);
}

3、寫一個簡單的界面(可以用死循環)

while(1)
{
n=0;
p=fopen("list.txt","r");
while(fscanf(p,"%s%s%s",people[n].name,people[n].phone,people[n].mail)!=EOF)
n++;
fclose(p);
///--------一次循環更新一次數據

4、寫一個簡單的查找程序

intk;
cout<<"輸入1讀取,輸入2輸入"<<endl;
cin>>k;
if(k==1)
{
cout<<"輸入信息"<<endl;
chars[100];
cin>>s;
boolok=0;
for(i=0;i<strlen(s);i++)
if(s[i]>='0'&&s[i]<='9')
ok=1;
//自動識別輸入的是姓名還是電話號碼
if(ok==0)
{
//cout<<"通過姓名找到聯系人"<<endl;
//system("pause");
boolyou=0;
for(i=0;i<n;i++)
if(strcmp(s,people[i].name)==0)
{
you=1;
cout<<"姓名"<<people[i].name<<endl;
cout<<"電話號碼"<<people[i].phone<<endl;
cout<<"郵箱"<<people[i].mail<<endl;
}
if(you==0)
cout<<"沒有通過姓名找到聯系人"<<endl;
}
if(ok==1)
{
//cout<<"通過電話找聯系人"<<endl;
//system("pause");
boolyou=0;
for(i=0;i<n;i++)
if(strcmp(s,people[i].phone)==0)
{
you=1;
cout<<"姓名"<<people[i].name<<endl;
cout<<"電話號碼"<<people[i].phone<<endl;
cout<<"郵箱"<<people[i].mail<<endl;
}
if(you==0)
cout<<"沒有通過電話找到聯系人"<<endl;
}

5、添加信息的代碼

if(k==2)
{
p1=fopen("list.txt","a+");
charss[1000];
cout<<"請輸入姓名"<<endl;
cin>>ss;
fprintf(p1,"%s ",ss);
cout<<"請輸入電話"<<endl;
cin>>ss;
fprintf(p1,"%s ",ss);
cout<<"請輸入郵箱"<<endl;
cin>>ss;
fprintf(p1,"%s ",ss);
fclose(p1);
}
}

最終的程序

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
usingnamespacestd;
FILE*p,*p1;
structnote{
charname[100];
charphone[100];
charmail[100];
}people[1000];
intmain()
{
intn=0,i,j;
p=fopen("list.txt","r");
if(p==NULL)
{
fclose(p);
p=fopen("list.txt","w");
fclose(p);
}
while(1)
{
n=0;
p=fopen("list.txt","r");
while(fscanf(p,"%s%s%s",people[n].name,people[n].phone,people[n].mail)!=EOF)
n++;
fclose(p);
///---------------
intk;
cout<<"輸入1讀取,輸入2輸入"<<endl;
cin>>k;
if(k==1)
{
cout<<"輸入信息"<<endl;
chars[100];
cin>>s;
boolok=0;
for(i=0;i<strlen(s);i++)
if(s[i]>='0'&&s[i]<='9')
ok=1;
if(ok==0)
{
//cout<<"通過姓名找到聯系人"<<endl;
//system("pause");
boolyou=0;
for(i=0;i<n;i++)
if(strcmp(s,people[i].name)==0)
{
you=1;
cout<<"姓名"<<people[i].name<<endl;
cout<<"電話號碼"<<people[i].phone<<endl;
cout<<"郵箱"<<people[i].mail<<endl;
}
if(you==0)
cout<<"沒有通過姓名找到聯系人"<<endl;
}
if(ok==1)
{
//cout<<"通過電話找聯系人"<<endl;
//system("pause");
boolyou=0;
for(i=0;i<n;i++)
if(strcmp(s,people[i].phone)==0)
{
you=1;
cout<<"姓名"<<people[i].name<<endl;
cout<<"電話號碼"<<people[i].phone<<endl;
cout<<"郵箱"<<people[i].mail<<endl;
}
if(you==0)
cout<<"沒有通過電話找到聯系人"<<endl;
}

}
if(k==2)
{
p1=fopen("list.txt","a+");
charss[1000];
cout<<"請輸入姓名"<<endl;
cin>>ss;
fprintf(p1,"%s ",ss);
cout<<"請輸入電話"<<endl;
cin>>ss;
fprintf(p1,"%s ",ss);
cout<<"請輸入郵箱"<<endl;
cin>>ss;
fprintf(p1,"%s ",ss);
fclose(p1);
}
}
return0;
}

C. 一個c語言程序,想要實現名字隨著序號重新排列

主函數沒問題。主要是sort()中排序出錯了。
看你兩兩比較數據,似乎是想用冒泡排序法。
將sort()稍作修改即可:
void sort(char (*p)[10],int *q)
{
int i,j,m;
char t[10];
for(i=0;i<9;i++)
{
for(j=0;j<9-i;j++)
if(*(q+j)>*(q+j+1))
{
m=*(q+j);*(q+j)=*(q+j+1);*(q+j+1)=m;
strcpy(t,*(p+j));
strcpy(*(p+j),*(p+j+1));
strcpy(*(p+j+1),t);
}
}
for(i=0;i<10;i++)
printf("%s %d\n",*(p+i),*(q+i));
}
我把形參的類型改了,不鉛姿然會有警告的。
當然,你也槐並絕蔽拿可以用順序排序或插入排序等等方法。C語言書上都有例子的。

D. 如何在c語言中用數組把商品的編號與名稱聯系起來

簡單地用幾個數組:
int i,k=-999,N=5;
int n=888;
char x[20]="c28";
int bianhao[100] ={ 212,351,286,754,888}
char mc[100][20]={"a2","b35","c28","d7","e88888"};
float price[100]={10.2,15.3,16.2,8.2,5.67};
對應的耐團0號商品仔畝孝存 bianhao[0],mc[0],price[0];
對應的i號商品存 bianhao[i],mc[i],price[i];
---------------------------------------
已念稿知 商品編號 n=888 查找:
for (i=0;i<N;i++) if (bianhao[i]==n){k=i;break;};
if (k >= 0) printf("%d %s %f\n",bianhao[k],mc[k],price[k]);
------------------------------------
已知 商品名 x[]="c28" 查找:
k=-999;
for (i=0;i<N;i++) if (strcmp(mc[i],x)==0) {k=i;break;};
if (k >= 0) printf("%d %s %f\n",bianhao[k],mc[k],price[k]);

E. c語言程序設計——編一個通訊錄管理程序:序號 姓名 電話 地址

(晚了)這是我以前的一個作業,你可以借鑒一下

#include <string.h>
#include<iostream>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;
#define LEN sizeof(struct friends)
#define CODE_SIZE 10
struct friends
{

char num[20];//編號
char name[100];//用戶姓名
char phone[20];//電話號碼
char home[100];//通訊地址
char email[100]; //email
char occupation[100];//工作
char birthday[20];//出生日期
struct friends *next;
};
int password()
{
FILE *fp;
char s1[CODE_SIZE], s2[CODE_SIZE], s3[CODE_SIZE], fun;
while (1)
{
fp = fopen("password.txt", "r");
if (fp == NULL)
{
printf("第一次運行,請輸入初始密碼(最多8位):\n");
scanf("%s", s1);
printf("請再次輸入初始密碼:\n");
scanf("%s", s2);
if (strcmp(s1, s2) == 0)
{
fp = fopen("password.txt", "w+");
if (fp == NULL)
{
printf("創建文件失敗退出\n");
getch();
exit(1);
}
else
{
//對s1加密
for (int i = 0; i<CODE_SIZE&&s1[i] != ' '; i++)
{
s1[i] = s1[i] + i;
}

fputs(s1, fp);
printf("初始密碼創建完成.\n");
}
}
else
{
printf("兩次輸入的密碼不一致!\n");
}
fclose(fp);
}
else
{
fgets(s1, CODE_SIZE, fp);
fclose(fp);

printf("輸入密碼:\n");
scanf("%s", s2);
//對s1解密
for (int i = 0; i<CODE_SIZE&&s1[i] != ' '; i++)
{
s1[i] = s1[i] - i;
}
loop:
if (strcmp(s1, s2) == 0)
{

printf("-----密碼正確-----\n");
printf("-----請選擇功能-----\n");
printf("-----1:修改密碼-----\n");
printf("-----2:進入通訊錄-----\n");
scanf("%d", &fun);
switch (fun)
{
case 1: printf("請輸入新密碼\n");
scanf("%s", s1);
printf("請再次輸入新密碼\n");
scanf("%s", s2);
if (strcmp(s1, s2) == 0)
{
fp = fopen("password.txt", "w+");
if (fp == NULL)
{
printf("文件錯誤!\n");
}
else
{ //對s1加密
for (int i = 0; i<CODE_SIZE&&s1[i] != ' '; i++)
{
s1[i] = s1[i] + i;
}
fputs(s1, fp);
fclose(fp);
printf("密碼修改成功\n");
}
}
else
{
printf("兩次輸入的密碼不一致,修改失敗\n");
}

break;
case 2: return 1;
default: printf("無效指令\n");
}

}
else
{
printf("密碼錯誤\n請重新輸入\n");
scanf("%s", s2);
goto loop;

}
}
printf("------------------\n\n\n\n");
}
}
void face(void)//功能選擇面板
{
cout << "********************************************************************" << endl;
cout << "\t\t\t☆★歡迎使用***通訊錄☆★" << endl;
cout << "\n\n\t☆★選擇你需要操作的功能:★☆" << endl;
cout << "\t\t\t1.增加通訊錄信息" << endl;
cout << "\t\t\t2.顯示通訊錄中所有記錄" << endl;
cout << "\t\t\t3.刪除需要刪除的信息" << endl;
cout << "\t\t\t4.以名字查詢所需的信息" << endl;
cout << "\t\t\t5.保存並退出" << endl;
cout << "\t\t\t6.退出不保存!!" << endl;

cout << "********************************************************************" << endl;
}
void print(struct friends *head)
{
struct friends *p;
p = head;
system("CLS");
cout << "*************************************************************" << endl;
cout << "==================== → 用戶信息記錄表 ← ===================" << endl;
cout << "*************************************************************" << endl;
if (head != NULL)
do
{
cout << "聯系人編號:" << p->num << endl;
cout << "聯系人姓名:" << p->name << endl;
cout << "聯系人電話號碼:" << p->phone << endl;
cout << "聯系人地址:" << p->home << endl;
cout << "聯系人電子郵箱:" << p->email << endl;
cout << "聯系人職業" << p->occupation << endl;
cout << "聯系人出生日期:" << p->birthday << endl;
cout << "********************************************************" << endl;
p = p->next;
} while (p != NULL);
else
{
cout << "對不起!!沒有任何聯系人記錄!!" << endl;
cout << "=============================================================\n" << endl;
}
}
//增添電子通訊錄中的內容,即創建連表過程
struct friends *append(struct friends *head)
{
struct friends *p0 = NULL, *p1, *p2;
p1 = head;
p2 = head;
system("CLS");
cout << "********************************************************************" << endl;
cout << "\t\t你能在此目錄下創建並添加聯系人信息" << endl;
cout << "********************************************************************" << endl;
p0 = (struct friends *)malloc(LEN);
cout << "請輸入聯系人編號:" << endl;
gets(p0->num);
cout << "請輸入聯系人姓名:" << endl;
gets(p0->name);
cout << "請輸入聯系人電話號碼:" << endl;
gets(p0->phone);

cout << "請輸入聯系人地址:" << endl;
gets(p0->home);
cout << "請輸入聯系人Email:" << endl;
gets(p0->email);
cout << "請輸入聯系人職位:" << endl;
gets(p0->occupation);
cout << "請輸入聯系人出生日期:" << endl;
gets(p0->birthday);
//對插入的節點排序,按姓名的拼音順序
if (head == NULL)
{
head = p0; p0->next = NULL;
}
else
{
while ((strcmp(p0->name, p1->name)>0) && (p1->next != NULL))
{
p2 = p1; p1 = p1->next;
}
if ((strcmp(p0->name, p1->name)) <= 0)
{
if (head == p1)
head = p0;
else
p2->next = p0;
p0->next = p1;
}
else
{
p1->next = p0; p0->next = NULL;
}

cout << "恭喜你!!成功添加了聯系人信息!!" << endl;
cout << "********************************************************************" << endl;
}
return(head);

}

//電子通訊錄的維護(刪除),通過輸入聯系人ID號碼刪除聯系人數據
struct friends *del(struct friends *head)
{
struct friends *p1, *p2;
char num[12];
system("CLS");
cout << "********************************************************************" << endl;
cout << "================= → 用戶信息記錄刪除功能 ← ===============" << endl;
cout << "********************************************************************" << endl;
printf("輸入要刪除的聯系人編號:");
gets(num);
p1 = head;
if (head == NULL)
{
cout << "很抱歉!!沒有任何聯系人紀錄!!" << endl;
cout << "*******************************************************" << endl;
return(head);
}
while (p1 != NULL)
{
if (strcmp(p1->num, num) == 0)
{
if (p1 == head)
head = p1->next;
else p2->next = p1->next;
free(p1);
cout << "刪除記錄成功!!" << endl;
return(head);
}
p2 = p1;
p1 = p1->next;
}
cout << "對不起!!沒有要刪除的聯系人紀錄!!\n" << endl;
return(head);
}

//電子通訊錄的查找,關鍵字為用戶姓名;
void search(struct friends *head)
{
struct friends *p1, *p2;
char name[20];
p1 = head;
p2 = p1;
system("CLS");
cout << "***************************************************************" << endl;
cout << "================ → 用戶信息記錄查詢功能 ← ==================\n" << endl;
cout << "***************************************************************" << endl;
cout << "輸入要查找聯系人的姓名:" << endl;
gets(name);
while (p1 != NULL)
{
if (strcmp(p1->name, name) == 0)
{
cout << "聯系人編號:" << endl;
puts(p1->num);
cout << "聯系人姓名:" << endl;
puts(p1->name);
cout << "聯系人電話號碼:" << endl;
puts(p1->phone);
cout << "聯系人地址:" << endl;
puts(p1->home);
cout << "聯系人電子郵箱:" << p1->email << endl;
puts(p1->email);
cout << "聯系人職業" << p1->occupation << endl;
puts(p1 ->occupation);
cout << "聯系人出生日期:" << endl;
puts(p1->birthday);
cout << "=============================================================" << endl;
break;
}
p2 = p1;
p1 = p1->next;
}
if (p1 == NULL)
cout << "對不起!!沒有該聯系人的紀錄!!" << endl;

}
void save(struct friends *head)
{
FILE *fp, *fp1;
struct friends *p;
p = head;
fp = fopen("record.txt", "w");
fp1 = fopen("record.txt", "w");
fprintf(fp1, "=============== → 用戶信息記錄表 ← =================\n");

while (p != NULL)
{

fprintf(fp1, "====================================================\n");
fprintf(fp1, "聯系人編號:%s\n", p->num);
fprintf(fp1, "聯系人姓名:%s\n", p->name);
fprintf(fp1, "聯系人電話:%s\n", p->phone);
fprintf(fp1, "聯系人家庭地址:%s\n", p->home);
fprintf(fp1, "聯系人電子郵箱:%s\n", p->email);
fprintf(fp1, "聯系人職業:%s\n", p->occupation);
fprintf(fp1, "聯系人出生日期:%s\n", p->birthday);
p = p->next;
}
fprintf(fp1, "*************************************************************\n");
fclose(fp1);
fclose(fp);
cout << "成功儲存,你能在record.txt找到相應紀錄" << endl;
cout << "**************************************************************" << endl;
cout << "PRESS ANY KEY TO EXIT." << endl;
getchar();
exit(1);
}
//電子通訊錄的記錄讀盤操作,使用文件指針;
struct friends *load(void)
{
FILE *fp;
struct friends *head = NULL, *p1 = NULL, *p2 = NULL;
char c;
int i;
fp = fopen("record.txt", "r");
for (i = 1; (c = fgetc(fp)) != -1; i++)
{
p1 = (struct friends *)malloc(LEN);
//fscanf(fp,"%s %s %s %s %s %s",p1->num,p1->name,p1->phone,p1->email,p1->home,p1->birthday);
if (i == 1)
{
head = p1; p2 = p1;
}
else
{
p2->next = p1; p2 = p1;
}
}
if (p1 == NULL)
{
fclose(fp); return(head);
}
p2->next = NULL;
fclose(fp);
return(head);
}

int main()
{
FILE *fp1, *fp2;
int a;//功能選擇需要的號碼
system("cls");
system("cls");
struct friends *head = NULL;
if ((fp1 = fopen("record.txt", "r")) == NULL)
{
fp2 = fopen("record.txt", "w");//如果不存在record.txt就創建一個
fclose(fp2);
}
head = load();

password();
system("cls");
while (1)
{
face();
printf("選擇你需要操作的功能號碼:");
cin >> a;
getchar();
switch (a)
{
case 1:head = append(head); break;
case 2:print(head); break;
case 3:head = del(head); break;
case 4:search(head); break;
case 5:save(head); break;
case 6:exit(0); break;
default:cout << "Enter error!!" << endl;
}

cout << "◇◆請按ENTER返回功能操作菜單◇◆" << endl;

getchar();
system("cls");

}

}

F. 用c語言對輸入的數據編號並在輸出時輸出相應編號

先輸入編號,然後判斷一下編號是否重復,然後再輸入姓名和薪金,放到定義的結構體類型里

G. C語言中怎麼對名字及其編號排序,舉例看看

名字當然是比較ASCII碼了。 編號是數字,直接比較大小。