使用者工具

網站工具


c:homework:hw14a

國立屏東大學 資訊工程學系 程式設計(二)

作業14

turnin code c.hw14

due date: May 6, 11:30AM

  1. 參考4/29課堂示範的程式

設計一個C語言程式名為stringBox.c,可進行10個20個字元以內的字串操作。在你的程式中,必須宣告一個指標永遠指向目前字元數最長的字串,如果一樣時,則列出最後加入的字串。程式執行時,可接受使用者命令進行相關操作,包含:

  1. l 列示目前已有的字串
  2. i 新增字串(字串中不會有空白字元)
  3. m 列示字元數最多的字串(若字元數相同時,則列出最後新增的字串)
  4. d 刪除字串(從1開始)
  5. e 清空整個盒子
  6. q 結束程式的執行

程式執行結果參考:

[9:19 user@ws hw14] ./a.out
[command] l
-empty-
[command] m
-null-
[command] q
-bye-
[9:19 user@ws hw14] ./a.out
[command] l
-empty-
[command] i
string=? Happy
[command] l
Happy-->end
[command] i
string=? birthday
[command] i
string=? to
[command] i
string=? you
[command] l
Happy-->birthday-->to-->you-->end
[command] m
The string with maximum length is birthday.
[command] i
string=? abcdefghijk
[command] m
The string with maximum length is abcdefghijk.
[command] i
string=? aaa
[command] i
string=? bbb
[command] i
string=? ccc
[command] i
string=? ddd
[command] i
string=? eee
[command] l
Happy-->birthday-->to-->you-->abcdefghijk-->aaa-->bbb-->ccc-->ddd-->eee-->end
[command] i
out of space!
[command] m
The string with maximum length is abcdefghijk.
[command] d
which one? 5
[command] l
Happy-->birthday-->to-->you-->aaa-->bbb-->ccc-->ddd-->eee-->end
[command] m
The string with maximum length is birthday.
[command] i
string=? fff
[command] l
Happy-->birthday-->to-->you-->aaa-->bbb-->ccc-->ddd-->eee-->fff-->end
[command] d
which one? 8
[command] l
Happy-->birthday-->to-->you-->aaa-->bbb-->ccc-->eee-->fff-->end
[command] d
which one? 8
[command] d
which one? 8
[command] l
Happy-->birthday-->to-->you-->aaa-->bbb-->ccc-->end
[command] d
which one? 8
out of range!
[command] e
[command] l
-empty-
[command] q
-bye-
[9:19 user@ws hw12]

可參考4/29日上課講解

#include <stdio.h>
#include <stdlib.h>
 
int main() 
{
  int quit=0;
  int box[10];
  int temp;
  int index=0;
  char cmd;
  int *max;
 
  while(!quit)
  {
    printf("[command]");
    scanf(" %c", &cmd);
 
    switch(cmd)
    {
      case 'l':
        if(index==0)
        {  
          printf("-empty-\n");
          }
        else
        {
          for(temp=0;temp<index;temp++)
          {
            printf("%d-->",box[temp]);
          }
          printf("end\n");
        }
        break;
      case 'i':
        if(index==10)
        {
          printf("out of space!\n");
        }
        else
        {
          printf("value=? ");
          scanf(" %d", &temp);
          box[index]=temp;
 
          if(index==0)
          {
           max=&box[index];
          }
          else
          {
            if(box[index]> *max)
            {
              max=&box[index];
            }
          }
          index++;
        }
        break;
      case 'm' :
        if(index==0)
          printf("-null-\n");
        else
          printf("The maximum value is %d.\n", *max);
        break;
      case 'q' :
        quit=1;
        printf("-bye-\n");
        break;
    }
  }
}

c/homework/hw14a.txt · 上一次變更: 2019/07/02 15:01 由 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki