當前位置:首頁 » 編程語言 » c語言簡短的代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言簡短的代碼

發布時間: 2022-05-13 01:01:40

『壹』 寫一個簡短的c語言代碼

最簡單的C語言代就是輸出「helloWord」,通常是作為初學編程語言時的第一個程序代碼。具體代碼如下:

#include <stdio.h>

int main(){

printf("Hello, World! ");

return 0;

}

(1)c語言簡短的代碼擴展閱讀:

1、程序的第一行#include <stdio.h>是預處理器指令,告訴 C 編譯器在實際編譯之前要包含 stdio.h 文件。

2、下一行intmain()是主函數,程序從這里開始執行。

3、下一行printf(...)是C中另一個可用的函數,會在屏幕上顯示消息"Hello,World!"。

4、下一行return0;終止main()函數,並返回值0。

『貳』 求一段簡單的C語言代碼

#include <iostream>
using namespace std;

#define countof(x) sizeof(x)/sizeof(x[0])

int main()
{
char szText[256];
int nBytes = 0;//位元組數
int nSpace = 0;//空格數
int nRow = 0;//行數
int nAbc = 0;//大小寫字母數

cout<<"請輸入要統計的字元串,以#號結束"<<endl;
cin.get( szText, countof(szText), '#' );
for ( int i = 0; i < strlen(szText); i++ )
{
if ( (szText[i] >= 'a' && szText[i] <= 'z')
|| (szText[i] >= 'A' && szText[i] <= 'Z') )
{
nAbc++;
}
else if ( szText[i] == ' ' )
{
nSpace++;
}
else if ( szText[i] == '\n' )
{
nRow++;
}
nBytes++;
}
cout<<"位元組數:"<<nBytes<<endl;
cout<<"空格數:"<<nSpace<<endl;
cout<<"行數:"<<nRow<<endl;
cout<<"大小寫字母數:"<<nAbc<<endl;
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語言

『肆』 跪求100行左右的c語言簡單代碼,大一水平就行,什麼類型都可以。

//學生成績管理系統C代碼
/*頭文件*/
#include<stdio.h>
#include<dos.h>
#include<stdlib.h>/*其它說明*/
#include<string.h>/*字元串函數*/
#include<mem.h>/*內存操作函數*/
#include<ctype.h>/*字元操作函數*/
#include<alloc.h>/*動態地址分配函數*/

#defineLENsizeof(STUDENT)

typedefstructstu/*定義結構體數組用於緩存數據*/
{
charnum[6];
charname[5];
intscore[3];
intsum;
floataverage;
intorder;
structstu*next;
}STUDENT;

/*函數原型*/
STUDENT*init();/*初始化函數*/
intmenu_select();/*菜單函數*/
STUDENT*create();/*創建鏈表*/
voidprint(STUDENT*head);/*顯示全部記錄*/
voidsearch(STUDENT*head);/*查找記錄*/
STUDENT*delete(STUDENT*head);/*刪除記錄*/
STUDENT*sort(STUDENT*head);/*排序*/
STUDENT*insert(STUDENT*head,STUDENT*newnode);/*插入記錄*/
voidsave(STUDENT*head);/*保存文件*/
STUDENT*load();/*讀文件*/

/*主函數界面*/
main()
{
STUDENT*head,newnode;
head=init();/*鏈表初始化,使head的值為NULL*/
for(;;)/*循環無限次*/
{
switch(menu_select())
{
case1:head=create();break;
case2:print(head);break;
case3:search(head);break;
case4:head=delete(head);break;
case5:head=sort(head);break;
case6:head=insert(head,&newnode);break;/*&newnode表示返回地址*/
case7:save(head);break;
case8:head=load();break;
case9:exit(0);/*如菜單返回值為9則程序結束*/
}
}
}

/*初始化函數*/
STUDENT*init()
{
returnNULL;/*返回空指針*/
}

/*菜單選擇函數*/
menu_select()
{
intn;
structdated;/*定義時間結構體*/
getdate(&d);/*讀取系統日期並把它放到結構體d中*/
printf("pressanykeytoenterthemenu......");/*按任一鍵進入主菜單*/
getch();/*從鍵盤讀取一個字元,但不顯示於屏幕*/
clrscr();/*清屏*/
printf("******************************************************************************** ");
printf(" Welcometo ");
printf(" Thestudentscoremanagesystem ");
printf("*************************************MENU*************************************** ");
printf(" 1.Entertherecord ");/*輸入學生成績記錄*/
printf(" 2.Printtherecord ");/*顯示*/
printf(" 3.Searchrecordonname ");/*尋找*/
printf(" 4.Deletearecord ");/*刪除*/
printf(" 5.Sorttomakenewafile ");/*排序*/
printf(" 6.Insertrecordtolist ");/*插入*/
printf(" 7.Savethefile ");/*保存*/
printf(" 8.Loadthefile ");/*讀取*/
printf(" 9.Quit ");/*退出*/
printf(" MadebyHuHaihong. ");
printf("******************************************************************************** ");
printf(" %d\%d\%d ",d.da_year,d.da_mon,d.da_day);/*顯示當前系統日期*/
do{
printf(" Enteryourchoice(1~9):");
scanf("%d",&n);
}while(n<1||n>9);/*如果選擇項不在1~9之間則重輸*/
return(n);/*返回選擇項,主函數根據該數調用相應的函數*/
}

/*輸入函數*/
STUDENT*create()
{
inti,s;
STUDENT*head=NULL,*p;/*定義函數.此函數帶回一個指向鏈表頭的指針*/
clrscr();
for(;;)
{p=(STUDENT*)malloc(LEN);/*開辟一個新的單元*/
if(!p)/*如果指針p為空*/
{printf(" Outofmemory.");/*輸出內存溢出*/
return(head);/*返回頭指針,下同*/
}
printf("Enterthenum(0:listend):");
scanf("%s",p->num);
if(p->num[0]=='0')break;/*如果學號首字元為0則結束輸入*/
printf("Enterthename:");
scanf("%s",p->name);
printf("Pleaseenterthe%dscores ",3);/*提示開始輸入成績*/
s=0;/*計算每個學生的總分,初值為0*/
for(i=0;i<3;i++)/*3門課程循環3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0||p->score[i]>100)/*確保成績在0~100之間*/
printf("Dataerror,pleaseenteragain. ");
}while(p->score[i]<0||p->score[i]>100);
s=s+p->score[i];/*累加各門成績*/
}
p->sum=s;/*將總分保存*/
p->average=(float)s/3;/*先用強制類型轉換將s轉換成float型,再求平均值*/
p->order=0;/*未排序前此值為0*/
p->next=head;/*將頭結點做為新輸入結點的後繼結點*/
head=p;/*新輸入結點為新的頭結點*/
}
return(head);
}

/*顯示全部記錄函數*/
voidprint(STUDENT*head)
{
inti=0;/*統計記錄條數*/
STUDENT*p;/*移動指針*/
clrscr();
p=head;/*初值為頭指針*/
printf(" ************************************STUDENT************************************ ");
printf("------------------------------------------------------------------------------- ");
printf("|Rec|Num|Name|Sc1|Sc2|Sc3|Sum|Ave|Order| ");
printf("------------------------------------------------------------------------------- ");
while(p!=NULL)
{
i++;
printf("|%3d|%4s|%-4s|%3d|%3d|%3d|%3d|%4.2f|%-5d| ",
i,p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("------------------------------------------------------------------------------- ");
printf("**************************************END************************************** ");
}

/*查找記錄函數*/
voidsearch(STUDENT*head)
{
STUDENT*p;/*移動指針*/
chars[5];/*存放姓名用的字元數組*/
clrscr();
printf("Pleaseenternameforsearching. ");
scanf("%s",s);
p=head;/*將頭指針賦給p*/
while(strcmp(p->name,s)&&p!=NULL)/*當記錄的姓名不是要找的,或指針不為空時*/
p=p->next;/*移動指針,指向下一結點*/
if(p!=NULL)/*如果指針不為空*/
{printf(" *************************************FOUND************************************ ");
printf("------------------------------------------------------------------------------- ");
printf("|Num|Name|sc1|sc2|sc3|Sum|Ave|Order| ");
printf("------------------------------------------------------------------------------- ");
printf("|%4s|%4s|%3d|%3d|%3d|%3d|%4.2f|%-5d| ",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("------------------------------------------------------------------------------- ");
printf("***************************************END************************************** ");
}
else
printf(" Thereisnonum%sstudentonthelist. ",s);/*顯示沒有該學生*/
}

/*刪除記錄函數*/
STUDENT*delete(STUDENT*head)
{intn;
STUDENT*p1,*p2;/*p1為查找到要刪除的結點指針,p2為其前驅指針*/
charc,s[6];/*s[6]用來存放學號,c用來輸入字母*/
clrscr();
printf("Pleaseenterthedeletednum:");
scanf("%s",s);
p1=p2=head;/*給p1和p2賦初值頭指針*/
while(strcmp(p1->num,s)&&p1!=NULL)/*當記錄的學號不是要找的,或指針不為空時*/
{p2=p1;/*將p1指針值賦給p2作為p1的前驅指針*/
p1=p1->next;/*將p1指針指向下一條記錄*/
}
if(strcmp(p1->num,s)==0)/*學號找到了*/
{printf("**************************************FOUND************************************ ");
printf("------------------------------------------------------------------------------- ");
printf("|Num|Name|sc1|sc2|sc3|Sum|Ave|Order| ");
printf("------------------------------------------------------------------------------- ");
printf("|%4s|%4s|%3d|%3d|%3d|%3d|%4.2f|%-5d| ",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("------------------------------------------------------------------------------- ");
printf("***************************************END************************************** ");
printf("AreyousuretodeletethestudentY/N?");/*提示是否要刪除,輸入Y刪除,N則退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N')break;/*如果不刪除,則跳出本循環*/
if(c=='y'||c=='Y')
{
if(p1==head)/*若p1==head,說明被刪結點是首結點*/
head=p1->next;/*把第二個結點地址賦予head*/
else
p2->next=p1->next;/*否則將一下結點地址賦給前一結點地址*/
n=n-1;
printf(" Num%sstudenthavebeendeleted. ",s);
printf("Don'tforgettosave. ");break;/*刪除後就跳出循環*/
}
}
}
else
printf(" Thereisnonum%sstudentonthelist. ",s);/*找不到該結點*/
return(head);
}

/*排序函數*/
STUDENT*sort(STUDENT*head)
{inti=0;/*保存名次*/
STUDENT*p1,*p2,*t,*temp;/*定義臨時指針*/
temp=head->next;/*將原表的頭指針所指的下一個結點作頭指針*/
head->next=NULL;/*第一個結點為新表的頭結點*/
while(temp!=NULL)/*當原表不為空時,進行排序*/
{
t=temp;/*取原表的頭結點*/
temp=temp->next;/*原表頭結點指針後移*/
p1=head;/*設定移動指針p1,從頭指針開始*/
p2=head;/*設定移動指針p2做為p1的前驅,初值為頭指針*/
while(t->average<p1->average&&p1!=NULL)/*作成績平均分比較*/
{
p2=p1;/*待排序點值小,則新表指針後移*/
p1=p1->next;
}
if(p1==p2)/*p1==p2,說明待排序點值大,應排在首位*/
{
t->next=p1;/*待排序點的後繼為p*/
head=t;/*新頭結點為待排序點*/
}
else/*待排序點應插入在中間某個位置p2和p1之間,如p為空則是尾部*/
{
t->next=p1;/*t的後繼是p1*/
p2->next=t;/*p2的後繼是t*/
}
}
p1=head;/*已排好序的頭指針賦給p1,准備填寫名次*/
while(p1!=NULL)/*當p1不為空時,進行下列操作*/
{
i++;/*結點序號*/
p1->order=i;/*將結點序號賦值給名次*/
p1=p1->next;/*指針後移*/
}
printf("Sortingissucessful. ");/*排序成功*/
return(head);
}

/*插入記錄函數*/
STUDENT*insert(STUDENT*head,STUDENT*newnode)
{STUDENT*p0,*p1,*p2;
intn,sum1,i;
p1=head;/*使p1指向第一個結點*/
p0=newnode;/*p0指向要插入的結點*/
printf(" Pleaseenteranewnoderecord. ");/*提示輸入記錄信息*/
printf("Enterthenum:");
scanf("%s",newnode->num);
printf("Enterthename:");
scanf("%s",newnode->name);
printf("Pleaseenterthe%dscores. ",3);
sum1=0;/*保存新記錄的總分,初值為0*/
for(i=0;i<3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&newnode->score[i]);
if(newnode->score[i]>100||newnode->score[i]<0)
printf("Dataerror,pleaseenteragain. ");
}while(newnode->score[i]>100||newnode->score[i]<0);
sum1=sum1+newnode->score[i];/*累加各門成績*/
}
newnode->sum=sum1;/*將總分存入新記錄中*/
newnode->average=(float)sum1/3;
newnode->order=0;
if(head==NULL)/*原來的鏈表是空表*/
{head=p0;p0->next=NULL;}/*使p0指向的結點作為頭結點*/
else
{while((p0->average<p1->average)&&(p1->next!=NULL))
{p2=p1;/*使p2指向剛才p1指向的結點*/
p1=p1->next;/*p1後移一個結點*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0;/*插到原來第一個結點之前*/
elsep2->next=p0;/*插到p2指向的結點之後*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;}/*插到最後的結點之後*/
}
n=n+1;/*結點數加1*/
head=sort(head);/*調用排序的函數,將學生成績重新排序*/
printf(" Student%shavebeeninserted. ",newnode->name);
printf("Don'tforgettosavethenewnodefile. ");
return(head);
}

/*保存數據到文件函數*/
voidsave(STUDENT*head)
{FILE*fp;/*定義指向文件的指針*/
STUDENT*p;/*定義移動指針*/
charoutfile[10];
printf("Enteroutfilename,forexamplec:\score ");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)/*為輸出打開一個二進制文件,為只寫方式*/
{
printf("Cannotopenthefile ");
return;/*若打不開則返回菜單*/
}
printf(" Savingthefile...... ");
p=head;/*移動指針從頭指針開始*/
while(p!=NULL)/*如p不為空*/
{
fwrite(p,LEN,1,fp);/*寫入一條記錄*/
p=p->next;/*指針後移*/
}
fclose(fp);/*關閉文件*/
printf("Savethefilesuccessfully! ");
}

/*從文件讀數據函數*/
STUDENT*load()
{STUDENT*p1,*p2,*head=NULL;/*定義記錄指針變數*/
FILE*fp;/*定義指向文件的指針*/
charinfile[10];
printf("Enterinfilename,forexamplec:\score ");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL)/*打開一個二進制文件,為只讀方式*/
{
printf("Cannotopenthefile. ");
return(head);
}
printf(" Loadingthefile! ");
p1=(STUDENT*)malloc(LEN);/*開辟一個新單元*/
if(!p1)
{
printf("Outofmemory! ");
return(head);
}
head=p1;/*申請到空間,將其作為頭指針*/
while(!feof(fp))/*循環讀數據直到文件尾結束*/
{
if(fread(p1,LEN,1,fp)!=1)break;/*如果沒讀到數據,跳出循環*/
p1->next=(STUDENT*)malloc(LEN);/*為下一個結點開辟空間*/
if(!p1->next)
{
printf("Outofmemory! ");
return(head);
}
p2=p1;/*使p2指向剛才p1指向的結點*/
p1=p1->next;/*指針後移,新讀入數據鏈到當前表尾*/
}
p2->next=NULL;/*最後一個結點的後繼指針為空*/
fclose(fp);
printf("! ");
return(head);
}

『伍』 c語言。輸出圖形,簡單代碼怎麼寫

#include<stdio.h>

#include<stdlib.h>

char a[25][25],b[2];

int n;

void fill()

{ int i,j,k;

char *p=a[0];

for(k=0; k<(n+1)/2; k++)

{ for(i=0; i<n-2*k; i++)

{p=*(a+k)+k+i*25;

for(j=0; j<n-2*k; j++)

*p++=b[k%2];

}

}

}

int main()

{ int i,j;

scanf("%d %c %c",&n,&b[0],&b[1]);

fill();

for(i=0; i<n; i++)

{ for(j=0; j<n; j++)

printf("%c",a[i][j]);

printf(" ");

}

return 0;

}

『陸』 求寫一個簡單的c語言代碼,要最簡單的那種。謝謝

我幫你寫個簡單點的吧,你這個是5位還好,要是位數更多呢,呵呵,
#include
void
main()
{
int
x,n=0;
printf("input
x:");
scanf("%d",&x);
if(x<=0)
printf("error!");
else
while(x)
{x/=10;
n++;}
printf("這是一個%d位數。\n",n);
}

『柒』 c語言100行簡單一點的代碼

登錄幼兒園200個小朋友的數據:姓名、性別、年齡、身高、體重、出生日期,分別按年齡排序後輸出。
#include<stdio.h>
#define N 200
struct child
{
char name[10];
char sex[3];
int age;
int height;
float weight;
struct {
int year;
int month;
int day;
}bdate;
}ch[N];
void input()
{
int i;
for(i=0;i<N;i++)
{
printf("\n請輸入第%d名小朋友信息:\n",i+1);
printf("姓名:");
scanf("%s",ch[i].name);
printf("性別:");
scanf("%s",ch[i].sex);
printf("年齡:");
scanf("%d",&ch[i].age);
printf("身高:");
scanf("%d",&ch[i].height);
printf("體重:");
scanf("%f",&ch[i].weight);
printf("出生日期[YYYY-MM-DD]:");
scanf("%d-%d-%d",&ch[i].bdate.year,&ch[i].bdate.month,&ch[i].bdate.day);
}
}
void sort()
{
struct child ct;
int i,j;
for(i=0;i<N-1;i++)
for(j=0;j<N-i-1;j++)
if(ch[j].height<ch[j+1].height)
{
ct=ch[j];
ch[j]=ch[j+1];
ch[j+1]=ct;
}
}
void output()
{
int i;
printf("\n\t幼兒園小朋友一覽(依身高排序)\n");
printf("===================================================\n");
printf(" 姓名 性別 年齡 身高 體重 出生日期 \n");
printf("===================================================\n");
for(i=0;i<N;i++)
printf(" %-8s %-2s %2d %d %3.1f %d.%d.%d\n",ch[i].name,ch[i].sex,ch[i].age,ch[i].height,ch[i].weight,ch[i].bdate.year,ch[i].bdate.month,ch[i].bdate.day);
}
void main()
{
input();
sort();
output();
}

『捌』 C語言簡單代碼

如果你想知道為什麼會出現7253的話...

從代碼上一行行看下來,當你看到c=a+b; 時,你還沒輸入哦,這時a和b是多少呢?
不知道,計算機也不知道,於是,給了一個隨機數(當然,說是隨機數應該不準確,只是內存里分別分配給a和b的各兩個位元組空間里原有的值.是別的程序運行後留下的,這個值可不確定啊,很可怕的一件事...)

把c=a+b;放到scanf("%d,%d",&a,&b);後面,那麼,當你輸入之後才去運行加法,這時a和b的值就是你輸入的值了...這樣才能輸出正確的加法結果...

『玖』 c語言基礎代碼,越詳細,解釋越簡單,越好

你並沒有把詳細的 C 語言基礎代碼寫出來,別人怎麼幫助你添加註釋語句啊?因為所說的 C 語言基礎代碼實際上並沒有一定之規。怎麼樣才算是基礎代碼、有幾行代碼就可以、足夠了?這些都是靈活的、並不是一成不變的。例如,最、最簡單的 C 語言基礎代碼就是:很多 C 語言教材上的第一個程序,輸出:"Hello, World !"。該詳細的 C 語言代碼如下:
#include <stdio.h> /* 基本輸入輸出頭文件,包括:printf、scanf等的庫函數原型說明 */
void main( ) /* 任何一個 C 語言源代碼都必須包含主函數 main( ),void 表示該函數不返回任何值 */
{ /* 在 C 語言代碼中,任何一個函數都是以 { 開始,並且以 } 結束 */
printf( "Hello, World !\n" ) ; /* 在電腦屏幕上輸出字元串:Hello, World */

}

『拾』 請描述一下一個簡單的C語言源程序代碼都包括哪些

您好,很高興回答您的問題。
在C語言中,無論是什麼樣程度的代碼程序,都包含有以下幾個方面:
宏定義(或者包含的頭文件,視情況而定)
函數返回值類型 主函數(類型 變數)
{變數定義或者初始化;
輸入語句;
計算語句;
輸出語句;
}
函數返回值類型 函數名(類型 變數,類型 變數,。。。)(自定義函數視情況而定)
{變數定義或者初始化;
輸入語句;
計算語句;
輸出語句;
}