字符串排序,

#include<stdio.h>
#include<string.h>
int main()
{
char s[100][20];
char t[20];
int n=0,i=0,j;
while(gets(s[i])!=NULL){
i++;
n++;
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(s[i],s[j])>0)
{
strcpy(t,s[i]);
strcpy(s[i],s[j]);
strcpy(s[j]=t);
}
for(i=0;i<n;i++)
puts(s[i]);
}
哪里错了,运行都运行不了,字符串排序
额。。。。知道了 strcpy(s[j]=t);逗号写成等号了

#include <iostream> #include <fstream> using namespace std; void main() { ifstream infile("D:\\Input.txt" , ios::in ); ofstream outfile("D:\\Output.txt", ios::out); if (infile) { int No = 0; infile >> No; char *p[100]; for (int i = 0; i < No; i++) { p[i] = new char; infile >> p[i]; } for (int i = 0; i < No; i++) { for (int j = i + 1; j < No; j++) { if (strlen(p[j]) < strlen(p[i])) { //后者长度小于前者就交换 char *p1 = p[i]; p[i] = p[j]; p[j] = p1 ; } else if (strlen(p[j]) == strlen(p[i])) { //后者长度等于前者就比较 if (strcmp(p[i], p[j]) > 0) { char *p2 = p[i]; p[i] = p[j]; p[j] = p2 ; } } } outfile << p[i] << "\t"; } outfile << endl; for (int i = 0; i < No; i++) { for (int j = i + 1; j < No; j++) { if (strcmp(p[i], p[j]) > 0) { char *p3 = p[i]; p[i] = p[j]; p[j] = p3 ; } } outfile << p[i] << "\t"; } outfile << endl; } else { outfile << "测试文件Input.txt不存在!!!" << endl; } infile.close() ; outfile.close(); } 运行结果:
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答