『壹』 用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");
}
}