|
50鱼币
#include <stdio.h>
#include <string.h>
#define LIM 2
#define STLEN 100
#define EXIT 5
#define STARS "********************************************************************"
int usr_input(char * strings[]);
int menu();
void prt_strings(char * strings[], int num);
void a_ord(char * strings[], int num);
void len_ord(char * strings[], int num);
void fist_word_len_ord(char * strings[], int num);
char * s_gets(char * Buffer, int maxCount);
int main(void)
{
int choose, num;
char usr_strings[LIM + 1][STLEN];
char *strings[LIM + 1];
for (int i = 0; i < LIM; i++)
{
strings[i] = usr_strings[i];
}
num = usr_input(strings);
while ((choose = menu()) != EXIT)
{
switch (choose)
{
case 1:
prt_strings((char **) usr_strings, num);
break;
case 2:
a_ord(strings, num);
break;
case 3:
len_ord(strings, num);
break;
case 4:
fist_word_len_ord(strings, num);
default:
;
}
}
return 0;
}
int menu()
{
puts(STARS);
int choose;
puts("1)Print a list of source strings 2)Prints the strings in order in ASCII");
puts("3)Prints strings in increasing length 4)Prints the string in increasing order of the first word length");
puts("5)Exit");
while ((scanf("%d", &choose)) != 1 && choose > 0 && choose < 5)
{
puts("Please enter a number of the potion");
}
return choose;
}
int usr_input(char * strings[])
{
puts(STARS);
int ct = 0;
printf("Please enter some strings,not more than %d.\n", LIM);
while (s_gets(strings[ct], STLEN))
{
ct++;
if (ct >= LIM)
break;
}
puts("Entry is complete.");
return ct;
}
void prt_strings(char *strings[], int num)
{
puts(STARS);
for (int i = 0; i < num; i++)
{
puts(strings[i]);
}
}
void a_ord(char * strings[], int num)
{
puts(STARS);
char * tmp;
for (int i = 0; i < num - 1; i++)
{
for (int j = i+1; j < num; j++)
{
if (strcmp(strings[i], strings[j]) > 0)
{
tmp = strings[i];
strings[i] = strings[j];
strings[j] = tmp;
}
}
}
}
void len_ord(char * strings[], int num)
{
char * tmp;
for (int i = 0; i < num - 1; i++)
{
for (int j = i+1; j < num; j++)
{
if (strlen(strings[i]) > strlen(strings[j]))
{
tmp = strings[i];
strings[i] = strings[j];
strings[j] = tmp;
}
}
}
}
void fist_word_len_ord(char * strings[], int num)
{
char * tmp;
while (*strings)
{
for (int i = 0; strings[i] != NULL; i++)
{
for (int j = i+1; j < num - 1; j++) {
if ((strchr(strings[i], ' ') - strings[i]) > (strchr(strings[j], ' ') - strings[j]))
{
tmp = strings[i];
strings[i] = strings[j];
strings[j] = tmp;
}
}
}
}
}
char * s_gets(char Buffer[], int maxCount){
char * ret_value;
int i = 0;
ret_value = fgets(Buffer, maxCount, stdin);
if (ret_value){
//替换'\n'为'\0'
while (Buffer[i] != '\n' && Buffer[i] != '\0') {
i++;
}
if (Buffer[i] == '\n')
Buffer[i] = '\0';
else{
while (getchar() != '\n');
}
}
return ret_value;
}
本帖最后由 jackz007 于 2022-10-14 17:11 编辑
楼主,你认为这个代码它简单吗?#include <stdio.h>
#include <string.h>
#define LIM 10
#define STLEN 256
int menu(void)
{
int m ;
printf(" *************************************************\n") ;
printf(" ** **\n") ;
printf(" ** User Menu **\n") ;
printf(" ** **\n") ;
printf(" *************************************************\n\n") ;
printf(" 1. Print a list of source strings \n") ;
printf(" 2. Prints the strings in order in ASCII\n") ;
printf(" 3. Prints strings in increasing length\n") ;
printf(" 4. Prints the string in increasing order of the first word length\n") ;
printf(" 5. Exit\n\n") ;
printf("Please enter a number of the potion : ") ;
for(m = 0 ; m < 1 || m > 5 ;) scanf("%d" , & m) ;
printf("\n") ;
return m ;
}
void operate(char strings[][STLEN], int num)
{
int d[num] , e[num] , i , j , k , m ;
for(;;) {
m = menu() ;
if(m > 0 && m < 5) {
for(i = 0 ; i < num ; i ++) d[i] = e[i] = i ;
if (m == 3) {
for(i = 0 ; i < num ; i ++) for(e[i] = j = 0 ; strings[i][j] ; j ++) e[i] ++ ; // e[i] 保存 strings[i] 的长度
} else if(m == 4) {
for(i = 0 ; i < num ; i ++) {
for(j = 0 ; strings[i][j] && strings[i][j] == ' ' ; j ++) ;
for(k = j ; strings[i][k] && strings[i][k] != ' ' ; k ++) ;
e[i] = k - j ; // e[i] 保存 strings[i] 中第一个 word 的长度
}
}
if(m > 1 && m < 5) { // 只有功能 2 ~ 4 才需要调整 d 中的元素
for(i = 0 ; i < num - 1 ; i ++) {
for(j = i + 1 ; j < num ; j ++) {
if(m == 2 && strcmp(strings[d[i]] , strings[d[j]]) > 0 || e[d[i]] > e[d[j]]) { // 排序,m = 2 通过 strcmp() 比较字符串,m = 3、4 比较的都是 e
k = d[i] ;
d[i] = d[j] ;
d[j] = k ;
}
}
}
}
for(i = 0 ; i < num ; i ++) printf("%s\n" , strings[d[i]]) ;
printf("\n\n") ;
} else {
break ;
}
}
}
int main(void)
{
int i ;
char strings[LIM][STLEN] ;
printf("Please enter some strings , not more than %d.\n" , LIM) ;
for(i = 0 ; i < LIM && (gets(strings[i])) ; i ++) ;
operate(strings , i) ;
}
编译、运行实况: D:\[00.Exerciese.2022]\C>g++ -o x x.c
D:\[00.Exerciese.2022]\C>X
Please enter some strings , not more than 10.
aBCDxyz sdkfhpdgj ' 0968023
ADFDFDGFxv ffk;fjljj 2058-5 2-=3r0-009 Vvvldkjl;jfidflkjg
dfkjlgoi 3086058 l;fj
^Z
*************************************************
** **
** User Menu **
** **
*************************************************
1. Print a list of source strings
2. Prints the strings in order in ASCII
3. Prints strings in increasing length
4. Prints the string in increasing order of the first word length
5. Exit
Please enter a number of the potion : 1
aBCDxyz sdkfhpdgj ' 0968023
ADFDFDGFxv ffk;fjljj 2058-5 2-=3r0-009 Vvvldkjl;jfidflkjg
dfkjlgoi 3086058 l;fj
*************************************************
** **
** User Menu **
** **
*************************************************
1. Print a list of source strings
2. Prints the strings in order in ASCII
3. Prints strings in increasing length
4. Prints the string in increasing order of the first word length
5. Exit
Please enter a number of the potion : 2
ADFDFDGFxv ffk;fjljj 2058-5 2-=3r0-009 Vvvldkjl;jfidflkjg
aBCDxyz sdkfhpdgj ' 0968023
dfkjlgoi 3086058 l;fj
*************************************************
** **
** User Menu **
** **
*************************************************
1. Print a list of source strings
2. Prints the strings in order in ASCII
3. Prints strings in increasing length
4. Prints the string in increasing order of the first word length
5. Exit
Please enter a number of the potion : 3
dfkjlgoi 3086058 l;fj
aBCDxyz sdkfhpdgj ' 0968023
ADFDFDGFxv ffk;fjljj 2058-5 2-=3r0-009 Vvvldkjl;jfidflkjg
*************************************************
** **
** User Menu **
** **
*************************************************
1. Print a list of source strings
2. Prints the strings in order in ASCII
3. Prints strings in increasing length
4. Prints the string in increasing order of the first word length
5. Exit
Please enter a number of the potion : 4
aBCDxyz sdkfhpdgj ' 0968023
dfkjlgoi 3086058 l;fj
ADFDFDGFxv ffk;fjljj 2058-5 2-=3r0-009 Vvvldkjl;jfidflkjg
*************************************************
** **
** User Menu **
** **
*************************************************
1. Print a list of source strings
2. Prints the strings in order in ASCII
3. Prints strings in increasing length
4. Prints the string in increasing order of the first word length
5. Exit
Please enter a number of the potion : 5
D:\[00.Exerciese.2022]\C>
|
-
题目
最佳答案
查看完整内容
楼主,你认为这个代码它简单吗?
编译、运行实况:
|