69,374
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a,const void *b)
{
const int *p=(const int *)a;
const int *q=(const int *)b;
if(*p<*q)
{
return -1;
}
else if(*p==*q)
{
return 0;
}
else
{
return 1;
}
}
const char *str="33 4 21 -9";
int convert(int *out,const char *in)
{
int count = 0;
while(sscanf(in,"%d",out)!=EOF)
{
++count;
while(*in!=0 && *in!=' ') //mark,上边这里越界访问了..
{
++in;
}
while(*in==' ')
{
++in;
}
++out;
}
return count;
}
/*
_CRTIMP void __cdecl qsort(void*, size_t, size_t,
int (*)(const void*, const void*));
*/
int main()
{
int output[100];
int count=convert(output,str);
qsort(output,sizeof(int),count,cmp);
for(int i=0;i<count;++i)
{
printf("%d ",output[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a,const void *b)
{
const int *p=(const int *)a;
const int *q=(const int *)b;
if(*p<*q)
{
return -1;
}
else if(*p==*q)
{
return 0;
}
else
{
return 1;
}
}
const char *str="33 4 21 -9";
int convert(int *out,const char *in)
{
int count = 0;
while(sscanf(in,"%d",out)!=EOF)
{
++count;
while(*in!=' ')
{
++in;
}
while(*in==' ')
{
++in;
}
++out;
}
return count;
}
/*
_CRTIMP void __cdecl qsort(void*, size_t, size_t,
int (*)(const void*, const void*));
*/
int main()
{
int output[100];
int count=convert(output,str);
qsort(output,sizeof(int),count,cmp);
for(int i=0;i<count;++i)
{
printf("%d ",output[i]);
}
return 0;
}
size_t toSize_t(char *arr,int num)
{
size_t ret1,ret = 0;
char* arr_temp =new char[num];
for(int i=0;i<num;i++)
{
arr_temp[i] = arr[i];
}
for(int i=num;i != 0;i--)
{
ret1 = 0;
arr_temp[i-1] >>= 4;
arr[i-1] &= 0x0f;
ret1 = arr_temp[i-1];
ret1 <<= 4;
ret1 |= arr[i-1];
if(i != num)
ret <<= 8;
ret |= ret1;
}
cout<<endl;
delete[]arr_temp;
return ret;
}
e]#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void sortNumStr(char st[]);
int main ()
{
char str[]="33 4 21 -9 ";
sortNumStr(str);
return 0;
}
void sortNumStr(char st[])
{
char str1[100]={0};
char s[10];
char *p;
int a[10];
int i=0, j, k, temp;
p=strtok(st," ");
while (p!=NULL)//分解
{
a[i++] = atoi(p);
p=strtok(NULL," ");
}
for (j=0; j<i-1;++j)//排序
for (k=0; k<i-1-j; ++k)
{
if (a[k]>a[k+1])
{
temp= a[k];
a[k]=a[k+1];
a[k+1]=temp;
}
}
for (j=0; j<i; ++j)//变回来
{
itoa(a[j],s,10);
strcat(str1,s);
strcat(str1," ");
}
printf("%s\n", str1);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void sortNumStr(char st[]);
int main ()
{
char str[]="33 4 21 -9 ";
sortNumStr(str);
return 0;
}
void sortNumStr(char st[])
{
char str1[100]={0};
char s[10];
char *p;
int a[10];
int i=0, j, k, temp;
p=strtok(st," ");
while (p!=NULL)//分解
{
a[i++] = atoi(p);
p=strtok(NULL," ");
}
for (j=0; j<i-1;++j)//排序
for (k=0; k<i-1-j; ++k)
{
if (a[k]>a[k+1])
{
temp= a[k];
a[k]=a[k+1];
a[k+1]=temp;
}
}
for (j=0; j<i; ++j)//变回来
{
itoa(a[j],s,10);
strcat(str1,s);
strcat(str1," ");
}
printf("%s\n", str1);
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int b[1000];
int FindAndSum(char *str) // 求和函数
{
int len,i,j,k,a[10000],temp,t,p;
len=strlen(str);
p=0;
for(i=0,j=0;i<len;i++)
{
if(str[i]>='0' && str[i]<='9')
{
if(str[i-1]=='-')
a[j++]=-1*(str[i]-'0');
else
a[j++]=str[i]-'0';
if(i==len-1)
{
b[p]=0;
for(k=0;k<j;k++)
{
temp=1;
for(t=1;t<=j-1-k;t++) // 求权相加
temp*=10;
b[p]+=a[k]*temp;
}
p++,j=0;
}
}
else
{
if(i-1>=0 && str[i-1]>='0' && str[i-1]<='9')
{
b[p]=0;
for(k=0;k<j;k++)
{
temp=1;
for(t=1;t<=j-1-k;t++) // 求权相加
temp*=10;
b[p]+=a[k]*temp;
}
p++,j=0;
}
}
}
return p;
}
int main(void)
{
int num,i,j,temp;
char *str="33 4 21 -9";
num=FindAndSum(str);
for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
if(b[j]<b[i])
{
temp=b[j];
b[j]=b[i];
b[i]=temp;
}
}
}
for(i=0;i<num;i++)
printf("%d ",b[i]);
printf("\n");
system("pause");
return 0;
}