帮忙debug(简单)
#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;
char *strings;
for (int i = 0; i < LIM; i++)
{
strings = usr_strings;
}
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, 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);
}
}
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, strings) > 0)
{
tmp = strings;
strings = strings;
strings = 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) > strlen(strings))
{
tmp = strings;
strings = strings;
strings = tmp;
}
}
}
}
void fist_word_len_ord(char * strings[], int num)
{
char * tmp;
while (*strings)
{
for (int i = 0; strings != NULL; i++)
{
for (int j = i+1; j < num - 1; j++) {
if ((strchr(strings, ' ') - strings) > (strchr(strings, ' ') - strings))
{
tmp = strings;
strings = strings;
strings = 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 != '\n' && Buffer != '\0') {
i++;
}
if (Buffer == '\n')
Buffer = '\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[], int num)
{
int d , e , i , j , k , m ;
for(;;) {
m = menu() ;
if(m > 0 && m < 5) {
for(i = 0 ; i < num ; i ++) d = e = i ;
if (m == 3) {
for(i = 0 ; i < num ; i ++) for(e = j = 0 ; strings ; j ++) e ++ ;// e 保存 strings 的长度
} else if(m == 4) {
for(i = 0 ; i < num ; i ++) {
for(j = 0 ; strings && strings == ' ' ; j ++) ;
for(k = j ; strings && strings != ' ' ; k ++) ;
e = k - j ;// e 保存 strings 中第一个 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] , strings]) > 0 || e] > e]) { // 排序,m = 2 通过 strcmp() 比较字符串,m = 3、4 比较的都是 e
k = d ;
d = d ;
d = k ;
}
}
}
}
for(i = 0 ; i < num ; i ++) printf("%s\n" , strings]) ;
printf("\n\n") ;
} else {
break ;
}
}
}
int main(void)
{
int i ;
char strings ;
printf("Please enter some strings , not more than %d.\n" ,LIM) ;
for(i = 0 ; i < LIM && (gets(strings)) ; i ++) ;
operate(strings , i) ;
}
编译、运行实况:
D:\\C>g++ -o x x.c
D:\\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:\\C> 有什么问题?
给一下
输入
正确的输出
现在这个程序不正确的输出
如果这个不正确 不明显,那就指出正确和不正确之间的差别
人造人 发表于 2022-10-14 01:08
给一下
输入
正确的输出
已经改好了,下面代码可以正确输出,但如果我要复用prt_strings()这个函数用于所有选项的输出,该怎么改呢?
#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;
char *strings;
for (int i = 0; i < LIM; i++)
{
strings = usr_strings;
}
num = usr_input(strings);
while ((choose = menu()) != EXIT)
{
switch (choose)
{
case 1:
prt_strings(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, 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);
}
}
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, strings) > 0)
{
tmp = strings;
strings = strings;
strings = tmp;
}
}
}
for (int i = 0; i < num; i++) {
puts(strings);
}
}
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) > strlen(strings))
{
tmp = strings;
strings = strings;
strings = tmp;
}
}
}
for (int i = 0; i < num; i++) {
puts(strings);
}
}
void fist_word_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 ((strchr(strings, ' ') - strings) > (strchr(strings, ' ') - strings))
{
tmp = strings;
strings = strings;
strings = tmp;
}
}
}
for (int i = 0; i < num; i++) {
puts(strings);
}
}
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 != '\n' && Buffer != '\0') {
i++;
}
if (Buffer == '\n')
Buffer = '\0';
else{
while (getchar() != '\n');
}
}
return ret_value;
}
给一下输入/输出
像这样
https://fishc.com.cn/thread-218849-1-3.html
你这程序需要输入什么?
你期望它输出什么?
这个程序现在输出了什么?
和你期望的输出有什么不同? jackz007 发表于 2022-10-14 14:46
楼主,你认为这个代码它简单吗?
编译、运行实况:
这就是强者的世界吗。。
但没注释看得头晕,不好学习,能麻烦给个注释嘛。 jackz007 发表于 2022-10-14 14:46
楼主,你认为这个代码它简单吗?
编译、运行实况:
你的意思是用数组来储存字符串的输出顺序吗? 本帖最后由 jackz007 于 2022-10-14 16:52 编辑
须弥芥子 发表于 2022-10-14 16:46
你的意思是用数组来储存字符串的输出顺序吗?
是的,我用数组 d 来作为 strings 排序时的索引,这样,根据排序结果,只需要调整 d 的元素,就可以在不动 strings 的情况下,达到实现各种依序输出各个字符串的目的。 须弥芥子 发表于 2022-10-14 16:41
这就是强者的世界吗。。
但没注释看得头晕,不好学习,能麻烦给个注释嘛。
注释来了,7 楼的代码已经更新。 jackz007 发表于 2022-10-14 17:13
注释来了,7 楼的代码已经更新。
谢啦~
页:
[1]