c语言程序案例

c语言程序案例,第1张

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

struct Message

{

char name[10]

int number

char sort[4]

int price

char company[4]

struct Message *next

}

void Interface(void)

{

printf("\n*******************************\n")

printf(" 1:添加记录 2: 显示记录 \n")

printf(" 3:查询记录 4: 代号的排序 \n")

printf(" 5:删除记录 6: 保存记录 \n")

printf(" 7:退出 \n")

printf("*********************************\n")

return

}

struct Message * AddMessage(struct Message *head)

{

struct Message *message

message = (struct Message *) malloc (sizeof (struct Message))

printf("请输入名称: \n")

scanf("%s", message->name)

fflush(stdin)

printf("请输入代号: \n")

scanf("%d", &message->number)

fflush(stdin)

printf("请输入类别: \n")

scanf("%d", &message->sort)

fflush(stdin)

printf("请输入公司: \n")

scanf("%d", &message->company)

fflush(stdin)

printf("请输入价格: \n")

scanf("%d", &message->price)

message->next = NULL

if (head == NULL)

{

return message

}

else

{

message->next = head

return message

}

}

void ShowMessage(struct Message *head)

{

if (head == NULL)

{

printf("没有信息显示!\n")

}

else

{

while (head != NULL)

{

printf("股票名称: %s\n", head->name)

printf("代号: %d\n", head->number)

printf("类别: %s\n", head->sort)

printf("公司: %s\n", head->company)

printf("价格: %d\n", head->price)

head = head ->next

}

}

}

void SearchMessage(struct Message *head)

{

char name[10]

printf("请输入你要查询的名称: \n")

scanf("%s", name)

while (head != NULL)

{

if (strcmp(head->name, name) == 0)

{

printf("股票名称: %s\n", head->name)

printf("代号: %d\n", head->number)

printf("类别: %s\n", head->sort)

printf("公司: %s\n", head->company)

printf("价格: %d\n", head->price)

break

}

else

{

head = head->next

}

}

if (head == NULL)

{

printf("没有你要查询的信息\n")

}

}

void Sort(struct Message *head)

{

/* struct Message *temp

temp = (struct Message *)malloc(sizeof(struct Message))

memset(temp, 0, sizeof(struct Message))

struct Message *nextTo

nextTo = head->next

if (head != NULL &&nextTo != NULL)

{

while (head != NULL)

{

while (nextTo != NULL)

{

if (head->number >nextTo->number)

{

temp = head

head = nextTo

head ->next = temp

temp ->next = nextTo ->next

nextTo = temp

}

nextTo = nextTo->next

}

head = head->next

}

}

else

{

return

}

return*/

printf("暂时不能实现\n")

}

struct Message * RemoveMessage(struct Message *head)

{

struct Message *h = head

char name[10]

struct Message *nextTo, *p

printf("请输入你要删除的名称: \n")

scanf("%s", name)

if (head == NULL)

{

printf("还没有添加任何信息,无法删除!\n")

return head

}

else if (strcmp(head->name, name) == 0)

{

//p = head

head = head->next

return head

//free(p)

}

else

{

nextTo = head->next

while (strcmp(nextTo->name, name) != 0 &&nextTo != NULL)

{

head = nextTo

nextTo = nextTo ->next

}

if (strcmp(nextTo->name, name) == 0)

{

head->next = nextTo ->next

free(nextTo)

return h

}

else

{

printf("没有你要删除的信息!\n")

return h

}

}

return h

}

void SaveMessage(struct Message *head)

{

if (head == NULL)

{

return

}

FILE *fp

fp = fopen("file.txt", "ab+")

if (fp == NULL)

{

printf("打开文件出错!\n")

return

}

while (head != NULL)

{

fwrite(head, sizeof(struct Message), 1, fp)

head = head ->next

}

printf("保存成功!\n")

fclose(fp)

}

int main(void)

{

int choice

struct Message *head = NULL

while (1)

{

Interface()

printf("请选择功能项: \n")

scanf("%d", &choice)

switch (choice)

{

case 1:

head = AddMessage(head)

break

case 2:

ShowMessage(head)

break

case 3:

SearchMessage(head)

break

case 4:

Sort(head)

break

case 5:

head = RemoveMessage(head)

break

case 6:

SaveMessage(head)

break

case 7:

exit(1)

break

default:

printf("选择有误,请重新选项!\n")

}

}

return 0

}

我正好知道一个使用递归算法的经典例子

汉诺塔算法, 一个柱子1上n个盘子套着,大的在下,借用柱子2,全部转移到柱子3上

#include <stdio.h>

int main()

{

void hanoi(int n,char one,char two,char three)// 对hanoi函数的声明

int m

printf("input the number of diskes:")

scanf("%d",&m)

printf("The step to move %d diskes:\n",m)

hanoi(m,'A','B','C')

}

void hanoi(int n,char one,char two,char three) // 定义hanoi函数

// 将n个盘从one座借助two座,移到three座

{

void move(char x,char y) // 对move函数的声明

if(n==1)

move(one,three)

else

{

hanoi(n-1,one,three,two)

move(one,three)

hanoi(n-1,two,one,three)

}

}

void move(char x,char y) // 定义move函数

{

printf("%c-->%c\n",x,y)

}

在hanoi调用hanoi就是递归了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12100215.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存