当前位置:首页 » 编程语言 » 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码了。 编号是数字,直接比较大小。