當前位置:首頁 » 編程語言 » 求一個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));

}