‘壹’ 用c语言如何随机给100个人命名
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
main()
{
int i;
srand((unsigned int)time(NULL));
i = ( rand() % 100);
fprintf(stderr,"i=%d",i);
exit(0);
}
/* --------------------------------------*
* 说明:
* 产生0到N随机数的方法是 ( rand() % N);
* srand涵数提供伪随机数涵数的种子
* ---------------------------------------*/
‘贰’ c语言怎么把100个人的名字按姓名排序输出
1 将100个人的姓名,存到二维字符数组中
2 通过strcmp比较。 通过strcpy赋值。 对二维字符数组进行排序
3 输出排序的结果。
排序中,除了比较和赋值与普通的整型数组排序不同外,其他的 都一样。
‘叁’ 请问如何在下面的编程中加入按姓名排序(C语言),可以的话,另外追加50分
bool cmp(node x,node y)
{
if(x.h==y.h)return x.num<y.num;
return x.h < y.h;
}
以上是C++的sort函数;
后看到写C语言。。。不好意思 提供个思路把 用strcmp 姓名排序不过是字典序嘛 自己模拟也行
‘肆’ 如何用C语言编写自己的姓名和学号
用字符串保存就可以了
比如
intmain()
{
char*name="张三";
char*no="123456";
printf("姓名:%s 学号:%s ",name,no);
return0;
}
‘伍’ c语言关于姓名排序程序
void paixu_xingming(Student stud[],int n)
{
int p,q,i;
Student temp;
for(p=0;p<n-1;p++)
{
i=p;
for(q=p+1;q<n;q++)
{
if(strcmp(stud[i].name,stud[q].name)>0) /*选择法按姓名排序*/
{
temp=stud[i];
stud[i]=stud[q];
stud[q]=temp;
}
}
}
‘陆’ 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");
}
}