c现代方法 p210页的一个程序-关于字符串的一个例子
发布日期:2021-05-07 00:55:53 浏览次数:22 分类:原创文章

本文共 1760 字,大约阅读时间需要 5 分钟。

#include <stdio.h>#include <string.h>//define the max length of every message #define MSG_LEN 80//define max number of message#define MAX_MSG 60int  read(char message[],int n);void print(char message[][MSG_LEN + 3],int n);int main(){//define  two dimension of array to store date and message char reminders[MAX_MSG][MSG_LEN + 3] = {""};char empty[MSG_LEN + 3]= ""; //define to storage to store date int day_num;char day_str[3];//define message to store the messagechar message[MSG_LEN + 1];int count = 0;/*char array[MSG_LEN + 1];read(array,MSG_LEN);int i = 0;for(i = 0; i!= MSG_LEN + 1 && array[i] != '\0';i++) {  printf("%c\n",array[i]); }*/for(;;){   if(count == MAX_MSG)    {        printf("--remainders are full.");      break;     }	//"%2d" in funtion scanf can only receive two characters scanf("%2d",&day_num);if(day_num == 0)break;sprintf(day_str,"%2d",day_num);//read(message,MSG_LEN);int i = 0,label = count -1;for(i = 0 ; i != count && count > 0; ++i){if(strcmp(day_str,reminders[i]) < 0)  {  label = i;  break;   }}for(i = count - 1;i >= label && i >= 0 &&count > 0; --i)    strcpy(reminders[i+1],reminders[i]);strcpy(reminders[label],day_str);strcat(reminders[label],message);count ++;}//for//print(reminders,MAX_MSG);int j = 0;for(;j != MAX_MSG ;j++){printf("line:%d\n",j);printf("%s",reminders[j]);printf("\n");if(strcmp(reminders[j], "") == 0)	break;char arr1[12]= "";char arr2[2] = "";printf("compare %d:",strcmp(arr1,arr2));}return 0;}int  read(char *message,int n){int i = 0;char ch;char *p = message;while( (ch = getchar()) != '\n'){ if(i < n)	  { *p = ch;    p ++;    i ++;  }  else 	  break;}*p = '\0';return i;}void print(char message[][MSG_LEN + 3],int n){int i = 0;for(i = 0; i != MAX_MSG; ++i){printf("%s",message[i]);printf("\n");}}

 

上一篇:c++ set,multiset的区别
下一篇:ubuntu20.04安装mysql教程

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月01日 15时32分51秒