马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 鱼C丶彪哥 于 2018-1-21 19:52 编辑
1.实现strcat
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *mystrcat(char *s1, char *s2)
{
char *tmp = s1;
while (*(++tmp));
for (int i = 0; i <= strlen(s2); i++)
{
*(tmp + i) = *(s2 + i);
}
return s1;
}
int main(void)
{
char *s1 = (char *)malloc(sizeof(char)* 50);
char *s2 = "1401902399";
strcpy(s1, "BiaoGe");
mystrcat(s1, s2);
printf("s1 = %s\n", s1);
printf("Hello World!\n");
return 0;
}
2.用链表实现简单用户管理系统
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void printMenum(void)
{
printf("\t\t1.录入用户\n");
printf("\t\t2.查询用户\n");
printf("\t\t3.排序输出\n");
printf("\t\t4.删除用户\n");
printf("\t\t5.保存用户\n");
printf("\t\t6.载入用户\n");
printf("\t\t7.退出程序\n");
}
typedef struct TAGUSER
{
char name[50];
char phone[12];
struct TAGUSER *next;
} USER, *pUSER;
pUSER g_uHeader;
void _OutUser(pUSER head)
{
pUSER tmp;
tmp = head->next;
printf("姓 名 手 机 号\n");
while (tmp)
{
printf("%-10s%s\n", tmp->name, tmp->phone);
tmp = tmp->next;
}
}
void InputUser(pUSER head)
{
pUSER tmp = head;
pUSER user = (pUSER)malloc(sizeof(struct TAGUSER));
fflush(stdin);
printf("请输入用户名:");
scanf_s("%s", user->name, 49);
printf("请输入手机号:");
scanf_s("%s", user->phone,11);
while (tmp->next)
{
tmp = tmp->next;
}
user->next = NULL;
tmp->next = user;
printf("录入成功!\n\t姓名:%s\n\t手机号%s\n", tmp->next->name, tmp->next->phone);
}
void QueryUser(pUSER head)
{
pUSER tmp = head->next;
char name[50] = { 0 };
printf("请输入要查询的名字:");
scanf_s("%s", name,49);
while (tmp)
{
if (!strcmp(name, tmp->name))
{
printf("该用户信息如下:\n");
printf("\t姓名:%s\n\t手机号:%s\n",tmp->name,tmp->phone);
return;
}
tmp = tmp->next;
}
printf("抱歉,没有该用户!\n");
}
void SortOutput(pUSER head)
{
pUSER tmp1 = head->next;
pUSER tmp2 = head->next->next;
pUSER temp = (pUSER)malloc(sizeof(struct TAGUSER));
while (tmp1)
{
while (tmp2)
{
if (strcmp(tmp1->name, tmp2->name) > 0)
{
strcpy(temp->name, tmp1->name);
strcpy(temp->phone, tmp1->phone);
strcpy(tmp1->name, tmp2->name);
strcpy(tmp1->phone, tmp2->phone);
strcpy(tmp2->name, temp->name);
strcpy(tmp2->phone, temp->phone);
}
tmp2 = tmp2->next;
}
if (!tmp1->next)
break;
tmp1 = tmp1->next;
tmp2 = tmp1->next;
}
_OutUser(head);
}
void SaveUser(pUSER head)
{
pUSER tmp = head->next;
FILE *wj = fopen("userdata.dat", "w");
if (!wj)
{
printf("文件打开失败!\n");
return;
}
while (tmp)
{
fwrite(tmp, sizeof(struct TAGUSER), 1, wj);
tmp = tmp->next;
}
fclose(wj);
printf("文件写入完毕,同目录下userdata.dat\n");
}
void DeleteUser(pUSER head)
{
pUSER tmp = head->next;
pUSER pre = head;
char name[50] = { 0 };
printf("请输入要删除的用户:");
scanf_s("%s", name,49);
while (tmp)
{
if (!strcmp(name, tmp->name))
{
pre->next = tmp->next;
free(tmp);
printf("删除成功!\n");
_OutUser(head);
return;
}
pre = tmp;
tmp = tmp->next;
}
}
void LoadUser(pUSER head)
{
pUSER tmp = head;
pUSER tmp1 = (pUSER)malloc(sizeof(struct TAGUSER));
FILE *wj = fopen("userdata.dat", "r");
while (fread(tmp1, sizeof(struct TAGUSER), 1, wj))
{
tmp1->next = NULL;
tmp->next = tmp1;
tmp = tmp->next;
tmp1 = (pUSER)malloc(sizeof(struct TAGUSER));
}
free(tmp1);
fclose(wj);
_OutUser(head);
}
void ExitPro()
{
printf("感谢您的使用,再见!\n");
exit(0);
}
int main(void)
{
int n = 0;
g_uHeader = (pUSER)malloc(sizeof(struct TAGUSER));
g_uHeader->next = NULL;
while (1)
{
system("cls");
printMenum();
printf("\n请输入:");
scanf("%d", &n);
switch (n)
{
case 1:
InputUser(g_uHeader);
break;
case 2:
QueryUser(g_uHeader);
break;
case 3:
SortOutput(g_uHeader);
break;
case 4:
DeleteUser(g_uHeader);
break;
case 5:
SaveUser(g_uHeader);
break;
case 6:
system("cls");
LoadUser(g_uHeader);
break;
case 7:
ExitPro(g_uHeader);
break;
default:
printf("\n输入有误,请重新输入!\n");
}
n = 0;
system("pause");
}
printf("Hello World!\n");
return 0;
}
|