给这个程序提供一个交互界面
时间: 2024-02-28 20:54:44 浏览: 20
好的,这里提供一种简单的交互界面实现方法:
```
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SMS_LENGTH 200
#define MAX_NAME_LENGTH 8
#define MAX_PHONE_LENGTH 11
typedef struct {
char word[MAX_SMS_LENGTH]; /*短信内容*/
int length; /*短信长度*/
} Message;
typedef struct {
char name[MAX_NAME_LENGTH]; /*姓名*/
char phone[MAX_PHONE_LENGTH]; /*电话号码或手机号码*/
} Note;
void extract_phone_number(Message* message, Note* note) {
int start = 0;
int phone_start = -1;
int phone_end = -1;
while (start < message->length) {
if (message->word[start] >= '0' && message->word[start] <= '9') {
phone_start = start;
phone_end = start;
start++;
while (start < message->length && message->word[start] >= '0' && message->word[start] <= '9') {
phone_end = start;
start++;
}
if (phone_end - phone_start == 6 || phone_end - phone_start == 7) {
strncpy(note->phone, message->word + phone_start, phone_end - phone_start + 1);
note->phone[phone_end - phone_start + 1] = '\0';
} else if (phone_end - phone_start == 10 || phone_end - phone_start == 11) {
strncpy(note->phone, message->word + phone_start, phone_end - phone_start + 1);
note->phone[phone_end - phone_start + 1] = '\0';
}
if (strlen(note->phone) > 0) {
break;
}
} else {
start++;
}
}
}
void save_to_contact(Note* note) {
printf("请输入姓名:");
fgets(note->name, MAX_NAME_LENGTH, stdin);
note->name[strlen(note->name) - 1] = '\0'; /*将最后的换行符去掉*/
printf("已将 %s 的电话号码 %s 存储到通讯录中。\n", note->name, note->phone);
/*将 note 存储到通讯录中*/
}
int main() {
Message message;
Note note;
char input[MAX_SMS_LENGTH + 1]; /*输入缓存*/
// 读取短信
printf("请输入一条短信:\n");
fgets(input, MAX_SMS_LENGTH + 1, stdin);
message.length = strlen(input) - 1;
strncpy(message.word, input, message.length);
message.word[message.length] = '\0';
memset(note.name, 0, MAX_NAME_LENGTH);
memset(note.phone, 0, MAX_PHONE_LENGTH);
extract_phone_number(&message, ¬e);
save_to_contact(¬e);
return 0;
}
```
这个程序会在终端中提示用户输入短信和联系人姓名,并输出存储成功的信息。当然,这只是一个简单的示例,你可以根据自己的需求修改代码来实现更复杂的交互界面。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)