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

一段簡單的c語言代碼

發布時間: 2022-08-01 03:37:04

① 求簡單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語言:求一段簡單的代碼。

#包括
使用命名空間std;

定義countof(x)的大小(X)/大小(X [0])

:( )
{
字元szText [256];
詮釋nBytes個= 0 ;/ /位元組數
詮釋nSpace = 0 ;/ /數量的空格
詮釋NROW = 0 ;/ /行數
詮釋NABC = 0 ;/ /大小寫的字母數字

法院<<「請輸入一個字元串,統計,以#號結束的。」 << endl;
cin.get(szText,countof(szText),'#');
為(int i = 0;我的strlen(szText),我+ +)
> {
((szText [I]> ='A'&& szText [I] <='Z')
| |(szText [I]> ='A'&& szText [我] <='Z'))
{
NABC + +;
}
其他(szText [I] =='')
{
nSpace + +;
}
其他(szText [I] =='\ n')
{
NROW + +;
}
nBytes個+ +;

}
cout <<「請位元組數:」<< nBytes個<< endl;
法院<<「號的空格:」「; nSpace < <endl;
法院<<「行數:」<< endl << NROW;
cout <<「請一些大寫和小寫字母:」NABC << endl;
> 0;
}

③ 求幾個比較有趣,簡單的C語言源代碼 小白自己敲著練一下手感

最簡單的模擬計時器:

#include<stdio.h>

#include<conio.h>

#include<windows.h>

int m=0,s=0,ms=0; //m是分 s是秒 ms是毫秒

//以下是5個自編函數

void csh( ); //初始化界面

void yinc(int x,int y); //隱藏游標的函數(y值設為0就會隱藏)

void jishi( ); //計時器運行(每100毫秒變化一次)

void Color (short x, short y); //設定顏色的函數(y設為0就是黑底)

void gtxy (int x, int y); //控制游標位置的函數

int main( ) //主函數

{ csh( );

getch( );

while(1)

{ jishi( );

Sleep(100); //間隔100毫秒

if( kbhit( ) )break; //有鍵按下就退出循環

}

return 0;

}

void csh( ) //初始化界面

{Color(14,0); //設定淡黃字配黑底

printf(「 計時器」);

Color(10,0); //設定淡綠字配黑底

printf(" ┌───────────┐");

printf(" │ │");

printf(" └───────────┘");

gtxy(10,4); //游標到屏幕第10列4行處輸出

Color(7,0); //恢復白字黑底

printf(" 00:00:00 ");

yinc(1,0 ); //隱藏游標(yinc代表隱藏)

return;

}

void jishi( ) //計時器運行

{ms+=1;

if(ms==10){s+=1;ms=0;}

if(s==60){m+=1;s=0;}

gtxy(10,4);

Color(9,0); //設定淡藍字配黑底

if(m>9) printf(" %d:",m);

else printf(" 0%d:",m);

Color(14,0); //設定淡黃字配黑底

if(s>9) printf("%d:",s);

else printf("0%d:",s);

Color(12,0); //設定淡紅字配黑底

printf("0%d",ms);

}

void gtxy (int x, int y) //控制游標位置的函數

{ COORD pos;

pos.X = x;

pos.Y = y;

SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );

}

void Color (short ForeColor= 7, short BackGroundColor= 0) //設定顏色的函數

{ HANDLE handle = GetStdHandle ( STD_OUTPUT_HANDLE );

SetConsoleTextAttribute ( handle, ForeColor + BackGroundColor * 0x10 );

}

void yinc(int x,int y) //隱藏游標的設置(gb代表游標)

{ CONSOLE_CURSOR_INFO gb={x,y}; //x為1-100,y為0就隱藏游標

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &gb);

}

④ 求 一個超簡單的C語言程序代碼

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void
main()
{
int
a,b,d,f;
char
c[4],e[5]="EXIT";
while(true)
{printf("BEGIN(開始)EXIT(退出):");
scanf("%s",c);
while(strcmp(c,e)!=0)
{printf("請輸入a和b:");
scanf("%d
%d",&a,&b);
d=a+b;
printf("%d\n",d);
printf("是否繼續[1.是;0.否]:");
scanf("%d",&f);
if(f==0)
break;
else
continue;
}
if(strcmp(c,e)==0)
printf("結束\n");
printf("按回車返回開頭");
fflush(stdin);
getchar();
system("cls");
}
}
你題意不是太明確,我隨意編了一個不知道是不是你想要的。

⑤ 求一段簡單的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;
}

⑥ 求50行簡單C語言程序代碼,基礎的就好

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

#define NUM 10
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//冒泡排序演算法
//基本思想:比較相鄰的兩個數,如果前者比後者大,則進行交換。每一輪排序結束,選出一個未排序中最大的數放到數組後面。
void bubbleSort(int *arr, int n) {
int i,j;
for (i = 0; i<n - 1; i++)
for (j = 0; j < n - i - 1; j++) {
//如果前面的數比後面大,進行交換
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
//最差時間復雜度為O(n^2),平均時間復雜度為O(n^2)。穩定性:穩定。輔助空間O(1)。
//升級版冒泡排序法:通過從低到高選出最大的數放到後面,再從高到低選出最小的數放到前面,
//如此反復,直到左邊界和右邊界重合。當數組中有已排序好的數時,這種排序比傳統冒泡排序性能稍好。
//升級版冒泡排序演算法
void bubbleSort_1(int *arr, int n) {
//設置數組左右邊界
int left = 0, right = n - 1;
//當左右邊界未重合時,進行排序
while (left<=right) {
int i,j;
//從左到右遍歷選出最大的數放到數組右邊
for (i =left; i < right; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
right--;
//從右到左遍歷選出最小的數放到數組左邊
for (j = right; j> left; j--) {
if (arr[j + 1] < arr[j]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
left++;
}
}
int main(int argc, char *argv[]) {
int arr[NUM],i,j,temp;
printf("請輸入10個數:\n");
for(i=0; i<NUM; i++) {
printf("請輸入第(%d)個數:",i+1);
scanf("%d",&arr[i]);
}
printf("\n輸入如下排列:\n");
for(i=0; i<NUM; i++) {
printf("%4d",arr[i]);
}/*
for(i=0; i<NUM; i++) {
for(j=i+1; j<NUM; j++) {
if(arr[i]>arr[j]) {
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}*/
bubbleSort_1(arr,NUM);
/*printf("\n從小到大如下排列:\n");
for(i=0; i<NUM; i++) {
printf("%4d",arr[i]);
}*/
printf("\n從大到小如下排列:\n");
for(i=NUM-1; i>=0; i--) {
printf("%4d",arr[i]);
}
return 0;
}

⑦ %很簡單的一段C語言代碼%

#include
<stdio.h>
#include
<string.h>
void
main()
{
char
a[10],b[10];
printf("請輸入您要倒置的字元串:");
gets(a);
int
k=strlen(a);
int
i,j;
for(i=0,j=k-1;
j>=0;
i++,j--)//這改了下,
{
b[i]=a[j];
}
b[i]='\0';////這要加一句,不然下面的輸出要出亂碼,
printf("%s\n",b);
///加了個回車好看,
}

⑧ 幫忙寫一段簡單的c語言代碼

#include <stdio.h>


int main()

{

FILE *fp;

int a[3][9],i,j;

if((fp=fopen("C:\cDemo\data.txt","r"))!=NULL)

{

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

{

for(j=0;j<9;++j)

{

fscanf(fp,"%d",&a[i][j]);

}

}

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

{

for(j=0;j<9;++j)

{

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

}

printf(" ");

}

}

else

{

puts("Can't open C:\cDemo\data.txt");

}

return 0;

}

⑨ 誰幫我編一個簡單的C語言程序代碼急急急

#include
<stdio.h>
main(int
argc)
{
print();
if(0
!=
argc)
{
printf("女:\n鵝鵝鵝\n白毛浮綠水\n紅掌撥清波\n");
}
}
print()
{
printf("男:\n鵝鵝鵝\n白毛浮綠水\n紅掌撥清波\n");
printf("<<<<<<請輸入任意鍵>>>>>>\n");
getch();
}
輸出是這樣的:
男:
鵝鵝鵝
白毛浮綠水
紅掌撥清波
<<<<<<請輸入任意鍵>>>>>>
女:
//輸入任意鍵後顯示的
鵝鵝鵝
白毛浮綠水
紅掌撥清波
不知道能不能幫上你~

⑩ 最簡單的C語言代碼

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

#include <stdio.h>

int main(){

printf("Hello, World! ");

return 0;

}

(10)一段簡單的c語言代碼擴展閱讀:

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

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

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

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