当前位置:首页 » 编程语言 » 求一个c语言程序
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

求一个c语言程序

发布时间: 2022-07-12 17:20:09

❶ 求一个c语言程序,关于求和和函数调用的

#include<stdio.h>

int sum(int n)

{

int i=0,s=0;

while(i<=n)

{

s=s+i;

i++;

}


return s;

}

int main()

{

int n;

printf("请输入要累积的数:");

scanf("%d",&n);

sum(n);

printf("累加的结果为%d",sum(n));

return 0;

}

(1)求一个c语言程序扩展阅读:

使用函数的优势:

C语言程序鼓励和提倡人们把一个大问题划分成一个个子问题,对应于解决一个子问题编制一个函数,因此,C语言程序一般是由大量的小函数而不是由少量大函数构成的,即所谓“小函数构成大程序”。

这样的好处是让各部分相互充分独立,并且任务单一。因而这些充分独立的小模块也可以作为一种固定规格的小“构件”, 用来构成新的大程序。

参考资料来源:网络-C语言

❷ 求一个C语言程序

#include
"stdio.h"
#include
"conio.h"
int
circulating(int
x,int
y,int
z)
{
static
int
count=0;
static
int
w;
if(x+y<z||x>=z||y>=z)
return
-1;
if(x+y==z)
return
0;
else
{
w=x+y-z;
count++;
while(w+y>z)
{circulating(w,y,z);}
return
count;
}
}
void
main()
{
int
a,b,c,n;
printf("请输入a,b,c,\n");
scanf("%d%d%d",&a,&b,&c);
if(circulating(a,b,c)+1)
{
n=circulating(a,b,c);
printf("做差的次数为%d",n/2);
}
else
{
printf("NO");
}
getch();
}
我是个新手,全是自己写的,按你的要求~!呵呵~记得给我分呀
我给的最早~

❸ 求一个完整的数据结构程序(C语言)

#include
"stdio.h"
#include
"malloc.h"
#define
null
0
struct
node
/*定义结构体*/
{int
data;
struct
node
*next;
};
struct
node
*head;
struct
node
*p;
struct
node
*s;
void
creat()
/*创建单链表*/
{int
ch;
head=(struct
node
*)malloc(sizeof(struct
node));
head->next=null;
p=head;
printf("请输入数据:
");
scanf("%d",&ch);
while(ch!=-1)
{s=(struct
node
*)malloc(sizeof(struct
node));
s->data=ch;
s->next=null;
p->next=s;
p=s;
printf("请输入数据:
");
scanf("%d",&ch);
}
}
void
outline()
/*输出单链表*/
{p=head->next;
while(p!=null)
{printf("%d
",p->data);
p=p->next;
}
printf("\n");
}
int
locate(int
x)
/*按值查找*/
{p=head->next;
while((p!=null)&&(p->data!=x))
p=p->next;
if(p==null)
return
0;
else
return
(p->data);
}
main()
/*主函数*/
{int
a=0;
creat();
outline();
printf("请输入你要找的数:\n");
scanf("%d",&a);
printf("你要找的数的下标是:
%d\n",locate(a));
printf("结点个数是:
%d\n",countnode());
}

❹ 求一个c语言程序

#include <stdio.h>
#include <stdlib.h>
#include "string.h"

typedef struct{
char word[20];
int count;
}WORD;

int find(char* source, char target)
{
int i,j;
int s_len=strlen(source);
for(i=0;i<s_len;i++)
{
if(source[i]==target)
{
return i;
}
}
return -1;
}

char* right(char* source,int n)
{
char* buf;
int len=strlen(source);
buf=(char*)malloc(sizeof(char)*(len-n)+1);
int i,j=0;
for(i=n;i<len;i++)
{
buf[j++]=source[i];
}
buf[j]='\0';
return buf;
}

void del_double_space(char* s)
{
int i,j=0,len=strlen(s);
for(i=0;i<len;)
{
s[j++]=s[i++];
if(s[i]==' ' && s[i-1]==' ')
{
i++;
}
}
s[j]='\0';
len=strlen(s);
for(i=0;i<len;i++)
{
if(s[i]>='A' && s[i]<='Z')
{
s[i]=s[i]+32;
}
}
}

char* left(char* source,int n)
{
char* buf;
buf=(char*)malloc(sizeof(char)*n+1);
int len,i,j=0;
len=strlen(source);
for(i=0;i<n;i++)
{
if(source[i]!='\0')
{
buf[j++]=source[i];
}
else
{
break;
}
}
buf[j]='\0';
return buf;
}

void sort(WORD* w)
{
WORD t;
int count=0;
int i,j;
for(i=0;i<100;i++)
{
if(w[i].count>0)
{
count++;
}
}
for(i=0;i<count;i++)
{
for(j=0;j<count-1-i;j++)
{
if(w[j].count<w[j+1].count)
{
t=w[j];
w[j]=w[j+1];
w[j+1]=t;
}
else if(w[j].count==w[j+1].count)
{
if(strcmp(w[j].word,w[j+1].word)>0)
{
t=w[j];
w[j]=w[j+1];
w[j+1]=t;
}
}
}
}
}

int main(int argc, char *argv[])
{
char all[1000]={'\0'};
while(1)
{
strcpy(all,"\0");
printf("\n\n请输入一段英文文字:");
fflush(stdin);
scanf("%[^\n]",all);
WORD word[100];
int i,an=0,len,count=0;
len=strlen(all);
for(i=0;i<100;i++)
{
strcpy(word[i].word,"\0");
word[i].count=0;
}
for(i=0;i<len;i++)
{
if(all[i]==',' || all[i]=='.' || all[i]=='!' || all[i]=='?' || all[i]==';')
{
an++;
if(i==len-1)
{
all[i]='\0';
}
else
{
all[i]=' ';
}
}
else if(i==len-1)
{
an++;
}
}
del_double_space(all);
while(1)
{
char *buf,*newbuf;
int n;
buf=(char*)malloc(sizeof(char)*20);
newbuf=(char*)malloc(sizeof(char)*1000);
n=find(all,' ');
if(n!=-1)
{
buf=left(all,n);
}
else
{
strcpy(buf,all);
}
// printf("\n%s",buf);
int isfind=0;
for(i=0;i<count;i++)
{
if(strcmp(word[i].word,buf)==0)
{
word[i].count=word[i].count+1;
isfind=1;
break;
}
}
if(isfind==0)
{
strcpy(word[count].word,buf);
word[count].count=1;
count++;
}
if(find(all,' ')==-1)
{
free(buf);
free(newbuf);
break;
}
newbuf=right(all,n+1);
strcpy(all,newbuf);
free(buf);
free(newbuf);
// printf("\n%s",all);
// getch();
}
sort(word);
printf("\n共有%d句话",an);
printf("\n共有%d个单词",count);
printf("\n单词 词频");
for(i=0;i<count;i++)
{
printf("\n%-10s %d",word[i].word,word[i].count);
}
printf("\n\n是否继续?[y/n]");
char n;
fflush(stdin);
scanf("%c",&n);
if(n=='y' || n=='Y')
{
continue;
}
else if( n=='n' || n=='N')
{
break;
}
}
return 0;
}

❺ 求简单C语言程序代码!

输入2个正整数m和n,求其最大公约数和最小公倍数

#include

#include

int main()

int m,n,p,q,s,r;

printf("请输入两个正整数;m,n ");

scanf("%d,%d",&m,&n);

#include<stdio.h>

main()

int a,b,t=0;

scanf("%d %d",&a,&b);

if (a<b)

printf("%d %d %d %d %d",(a+b),(a-b),(a/b),(a*b),(a%b));

}

主要特点

C语言是一种结构化语言,它有着清晰的层次,可按照模块的方式对程序进行编写,十分有利于程序的调试,且c语言的处理和表现能力都非常的强大,依靠非常全面的运算符和多样的数据类型,可以轻易完成各种数据结构的构建,通过指针类型更可对内存直接寻址以及对硬件进行直接操作,因此既能够用于开发系统程序,也可用于开发应用软件。

以上内容参考:网络-c语言

❻ 求一个小程序( 用c语言编写的)

第二步描述有问题,如果答案正确是什么意思?应该是你输入2刚判断为正确,输入其他则为错误吧。
#include
<stdio.h>
void
main()
{int
a;char
b;
loop:printf("do
you
know
1+1=?\n");
scanf("input
your
result:\n");
scanf("%d",&a);
if(a==2)
printf("good
you
correct\n");
printf("try
again?
y/n\n");
scanf("%c",b);
{if
(b='y')
goto
loop;
//goto虽然不提倡用,但是在这种情况下用也不影响可读性。
else
if
(b='n')
printf("bye
bye
!!");}
if(a!=2)
printf("oh
my
god
you
error!!!");
printf("try
again?
y/n\n");
scanf("%c",b);
if
(b='y')
goto
loop;
if
(b='n')
printf("bye
bye
!!");
}
用调用函数可能还能简单点,不过这样直接点~

❼ 求最简单的C语言程序

#include<stdio.h>

main()

{

int a,b,t=0;

scanf("%d %d",&a,&b);

if (a<b)

{

t=a;

a=b;

b=t;

}

printf("%d %d %d %d %d",(a+b),(a-b),(a/b),(a*b),(a%b));

}