题目01:在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,空格用来分隔不同的单词。
直接编译,程序执行结果如下图所示:
题目02:编写一个int string_len(char *s),返回字符串s的字符长度(不包括\0)。
直接编译,程序执行结果如下图所示:
扩展资料:C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。
尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。
#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
}