要是在输入一个后 显示"是""否"接着输入 点"是"接着输入 点"否"退出 用函数做 哪个高手可以指点一二否 |
----------------解决方案--------------------------------------------------------
#include<stdio.h> yesorno() {char ch; printf("enter ch:\n"); scanf("%c",&ch); if (strcmp(ch,'y')==0) return 'y'; else return 'n'; } void main() { char *q[20],a[20],ch; int i,j,m; scanf("%d",&m); q=(char*)malloc(m*20); for(i=0;i<m;i++) {scanf("%s",q+i); ch=yesorno(); if (strcmp(ch,'y')==0) scanf("%s",q+i); else break; }
for(i=1;i<m;i++) { for(j=0;j<m-j;j++) { if(strcmp(q[j],q[j+1])>0) { strcpy(a,q[j]); strcpy(q[j],q[j+1]); strcpy(q[j+1],q); } } }
for(i=0;i<m;i++) { printf("%s",q[i]); printf("\n"); } getch(); }
----------------解决方案--------------------------------------------------------
q=(char*)malloc(m*20);
同样是这一句,运行一楼的程序时提示警告,运行2楼的时候提示错误,倒底问题在哪里呢?
----------------解决方案--------------------------------------------------------
char *q;
q=(char *)malloc(m*sizeof(char *));
去掉后面的20;
----------------解决方案--------------------------------------------------------
不是那里错 我自己改好了
#include<stdio.h>
#include<string.h>
void main()
{
char (*q)[20],a[20];
int i,j,m;
scanf("%d",&m);
q=(char*)malloc(m*20);
for(i=0;i<m;i++)
{
scanf("%s",q+i);
}
for(i=1;i<m;i++)
{
for(j=0;j<m-i;j++)
{
if(strcmp(q[j],q[j+1])>0)
{
strcpy(a,q[j]);
strcpy(q[j],q[j+1]);
strcpy(q[j+1],a);
}
}
}
for(i=0;i<m;i++)
{
printf("%s",q[i]);
printf("\n");
}
getch();
}
----------------解决方案--------------------------------------------------------
还是有个警告啊
----------------解决方案--------------------------------------------------------
q=(char(*)[20])malloc(m*20);
这样改就对了
----------------解决方案--------------------------------------------------------
帮你改了下 //输入N个字符串 比较大小 从小到大输出 //要是在输入一个后 显示"是""否"接着输入 点"是"接着输入 点"否"退出 //用函数做
#include<stdio.h> #include<string.h> #include<stdlib.h>
int Is_quit();
int Is_quit() { char judge; printf("Will you go on to start another sorting ? \nIf you go on press 'y' , or 'n'\n"); fflush(stdin); scanf("%c",&judge); if(judge=='y') return 1; else return 0; } int main() { char a[20],(*q)[20];//(*q)[20] 表示q是指针类型变量,指向一个包含20个元素的一维数组,数组元素类型为字符型 int i,j,m; int k; Label: printf("please enter the string's num:\n"); scanf("%d",&m); q=(char(*)[20])malloc(m*20*sizeof(char)); for(i=0;i<m;i++) { printf("please enter the th%d string:\n",i+1); scanf("%s",*(q+i)); } for(i=0;i<m;i++) { k=i; for(j=i;j<m;j++) { if(strcmp(q[k],q[j])>0) { k=j; } } if(k!=i) { strcpy(a,q[i]); strcpy(q[i],q[k]); strcpy(q[k],a); } } printf("\nthe strings after sorting are as following:\n"); for(i=0;i<m;i++) { printf("%s",q[i]); printf("\n"); } if(Is_quit()) goto Label; return 0; }
----------------解决方案--------------------------------------------------------