c语言编个小程序
for(x=0.2;x<=2;x+=0.2)
浮点数一般不用来做循环控制,可以使用
int j;
for(x=0.2;j<=9;x+=0.2*j)
你写的for循环只执行了x=0.2和x=1.2的值
#include
int main()
{
int i, a[10];
a[0]=2;
for (i=1;i<10;i++) a[i]=a[i-1]*2-1;
for (i=9;i<=0;i--) printf("%d
",a[i]);
return 0;
}
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
#define ROW 100
#define COL 80
void sort();
int main()
{
int n,i;
char s[ROW][COL+1];
printf("Please input the number of the string you want to sort(not more than %d):\n",ROW);
scanf("%d",&n);
getchar(); /*开始没加这句只能输入n-1个字符串,排错花了几十分钟,教训深刻,看来是上一句的回车键在作怪,这说明gets()函数也是从缓冲区而非控制台取出字符的*/
printf("Please input the strings one by one (not more than %d characters each):\n",COL);
for(i=0;i<n;i++)
gets(s[i]);
sort(s,n);
printf("\nNow,the sequence after sort is:\n");
for(i=0;i<n;i++)
printf("%s\n",s[i]);
getch();
return 0;
}
void sort(char str[][COL+1],int n) /* 冒泡排序 */
{
int i,j;
char temp[COL+1];
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(strcmp(str[j],str[j+1])>0)
{
strcpy(temp,str[j]);
strcpy(str[j],str[j+1]);
strcpy(str[j+1],temp);
}
}
本题利用指针数组得出的另一种解法如下:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
#define ROW 100
#define COL 80
void sort();
void print();
int main()
{
int n,i;
char s[ROW][COL+1],*p[ROW];
printf("Please input the number of the string you want to sort(not more than %d):\n",ROW);
scanf("%d",&n);
getchar();
printf("Please input the strings one by one (not more than %d characters each):\n",COL);
for(i=0;i<n;i++)
{
gets(s[i]);
p[i]=s[i];
}
sort(p,n);
printf("\nNow,the sequence after sort is:\n");
print(p,n);
getch();
return 0;
}
void sort(char *str[],int n) /* 选择排序 */
{
char *temp;
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(strcmp(*(str+k),*(str+j))>0)
k=j;
if(k!=i)
{
temp=*(str+i);
*(str+i)=*(str+k);
*(str+k)=temp;
}
}
}
void print(char *str[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%s\n",*(str+i));
}
这个明显是C++写的 用到了C++的输入输出流的写法 还有那个iostream.h的头文件 hoho ~~~~
这个到底是C还是C++?
cout<<"输入若干个字符串"<<endl;
地方官
相关视频
c语言编一段小程序,在液晶屏上实现显示"ABCD1234".用单片机AT 89c51_百度...
_nop_(); \/\/空操作四个机器周期,给硬件反应时间 result=BF; \/\/将忙碌标志电平赋给result EN=0;return result;} \/ 函数功能:将模式设置指令或显示地址写入液晶模块 入口参数:dictate \/ void WriteInstruction (unsigned char dictate){ while(BusyTest()==1); \/\/如果忙就等待 RS=0; ...
用C语言编写一个输出图形的小程序。
include"stdio.h"void main(){char diamond[][7]={{'*','*','*'},{'*','*','*','*','*'},{'*','*','*','*','*','*','*'},{'*','*','*','*','*'},{'*','*','*'}};int i,j;for(i=0;i<5;i++){for(j=0;j<7;j++)printf("%c",diamond[i...
用C语言写一个小程序,麻烦大家了
char s2[] = "bbbbbbbbb"; char str[100]; strnmerge(str, 10, s1, s2); cout << "s1 = " << s1 << endl; cout << "s2 = " << s2 << endl; cout << "str = " << str << endl; return 0;}这个适用于哪儿些c语言程序鸭什么时候要啊 今天就...
c语言编个小程序
本题的一个完整c程序如下,win-tc和Dev-c++下已调试通过。其中通用排序函数为void sort()include "stdio.h"include "stdlib.h"include "conio.h"include "string.h"define ROW 100 define COL 80 void sort();int main(){ int n,i;char s[ROW][COL+1];printf("Please input the number ...