當前位置:首頁 » 編程語言 » c語言常式

c語言常式

發布時間: 2022-01-15 04:33:18

1. c語言編程實例

第一個問題用switch語句
第二個用if else語句的嵌套

呵呵不好意思 我比較懶不想打代碼了

2. C語言編程實例

首先main中對x進行賦值,是12345678這個無符號長整形數,然後傳到part()函數中,結構體那部分不用考慮,其它地方也沒有對num進行更改,不會對內容有影響,所以輸出結果為12345678;
然後p=&n把n的地址賦給指針p,n.w=num把12345678賦給w,此時結構體n的地址其實就是12345678所在的地址,p指向n,同時結構體*p中的數組part的初始地址也在p上,也就是說p指向的數組part於n.w的首地址是相同的;
看你的輸出結果,你的C中int和long應該都是32位吧,所以輸出part[0]就是輸出part地址上的前32位,即num,part[1]上都是空的,什麼也沒有,所以就是8個C;
PS:我想這個程序的意義是把long型整數進行拆分,分別輸出高低位兩部分,但是你的C中的int和long都是32位,所以沒成功吧
呵呵,也不知道說的對不,C很久沒用了

3. c語言 程序

這個是比較早期的作品。。。沒有文件讀取和用戶界面

#include <iostream>
#include <cstring>
struct st
{
int no;
char name[20];
int math;
int eng;
int pro;
int sum;
double ave;
};
struct Node
{
st content;
Node *next;
};
void input(st ,Node *&head);
void output(Node *head);
void del(int a,Node *&head);
void out(Node *p);
using namespace std;
Node *head=NULL;
int main()
{
A:
cout << "請不要搞惡,會崩的!!!!\n";
cout << "請輸入要求操作的代號\n";
cout << "1 添加新數據。\n";
cout << "2 查找數據。\n";
cout << "3 修改數據。\n";
cout << "4 刪除數據。\n";
cout << "5 統計。\n";
cout << "6 說明文檔\n";
cout << "7 退出。\n";
int choice;
cin >> choice;
switch (choice)
{
case 1:
{
cout << "開始輸入,請依次輸入這位同學的學號、姓名、數學、英語、程序設計的成績;輸完請輸-1" << endl;
st cont;
for(;;)
{
cin >> cont.no ;
if(cont.no==-1) break;
cin >> cont.name >> cont.math >> cont.eng >> cont.pro;
cont.sum = cont.math + cont.eng + cont.pro;
cont.ave = cont.sum / 3.0;
input(cont,head);
}
break;
}
case 2:
{
if(head==NULL) {cout << "已經為空!!\n"; break;}
cout << "請輸入要查找的類型" <<endl;
cout << "1 學號\n" ;
cout << "2 姓名\n" ;
cout << "3 數學成績\n" ;
cout << "4 英語成績\n" ;
cout << "5 程序設計成績\n" ;
cout << "6 總成績\n" ;
cout << "7 平均成績\n" ;
int type;
cin >> type;
switch (type)
{
case 1:
{
int target;
cout << "請輸入學號。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.no==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
i++;
}
if(p->content.no==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
cout << "是否刪除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 2:
{
char target[20];
cout << "請輸入姓名。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(strcmp(target , p->content.name)==0)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
i++;
}
if(strcmp(target , p->content.name)==0)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
cout << "是否刪除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 3:
{
int target;
cout << "請輸入數學成績。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.math==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
i++;
}
if(p->content.math==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
cout << "是否刪除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 4:
{
int target;
cout << "請輸入英語成績。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.eng==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
i++;
}
if(p->content.eng==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
cout << "是否刪除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 5:
{
int target;
cout << "請輸入程序設計成績。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.pro==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
i++;
}
if(p->content.pro==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
cout << "是否刪除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 6:
{
int target;
cout << "請輸入總成績。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.sum==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
i++;
}
if(p->content.sum==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
cout << "是否刪除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 7:
{

int target;
cout << "請輸入平均成績。" <<endl;
cin >> target;
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.ave==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
i++;
}
if(p->content.ave==target)
{
cout << "第" << i << "個" << "為" << target << endl;
out(p);
}
cout << "是否刪除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
default:cerr << "跟你說了別惡搞,真的會崩的\n" ;
}//switch (type)
break;
}
case 3:
{
if(head==NULL) {cout << "已經為空!!\n"; break;}
cout << "請輸入要修改的數據的編號"<<endl;
int a;
cin >> a;
cout << "請輸入要更改的類型"<<endl;
cout << "1 學號\n" ;
cout << "2 姓名\n" ;
cout << "3 數學成績\n" ;
cout << "4 英語成績\n" ;
cout << "5 程序設計成績\n" ;
int typ;
cin >> typ;
switch(typ)
{
case 1:
{
cout << "請輸入新值。" <<endl;
int n;
cin >> n;
Node *p=head;
if(a==1)
{
p->content.no = n;
}
else
{
for(int i=1;i<a;i++)
{
p=p->next;
}
p->content.no = n;
}
break;
}
case 2:
{
cout << "請輸入新值。" <<endl;
char n[20];
cin >> n;
Node *p=head;
if(a==1)
{
strcpy(p->content.name , n);
}
else
{
for(int i=1;i<a;i++)
{
p=p->next;
}
strcpy(p->content.name , n);
}
break;
}
case 3:
{
cout << "請輸入新值。" <<endl;
int n;
cin >> n;
Node *p=head;
int temp ;
if(a==1)
{
temp=p->content.math ;
p->content.math = n;
}
else
{
for(int i=1;i<a;i++)
{
p=p->next;
}
temp=p->content.math ;
p->content.math = n;
}
p->content.sum=p->content.sum - temp + n;
p->content.ave=p->content.sum/3.0;
break;
}
case 4:
{
cout << "請輸入新值。" <<endl;
int n;
cin >> n;
Node *p=head;
int temp ;
if(a==1)
{
temp=p->content.eng ;
p->content.eng = n;
}
else
{
for(int i=1;i<a;i++)
{
p=p->next;
}
temp=p->content.eng;
p->content.eng = n;
}
p->content.sum=p->content.sum - temp + n;
p->content.ave=p->content.sum/3.0;
break;
}
case 5:
{
cout << "請輸入新值。" <<endl;
int n;
cin >> n;
Node *p=head;
int temp ;
if(a==1)
{
temp=p->content.pro ;
p->content.pro = n;
}
else
{
for(int i=1;i<a;i++)
{
p=p->next;
}
temp=p->content.pro;
p->content.pro = n;
}
p->content.sum=p->content.sum - temp + n;
p->content.ave=p->content.sum/3.0;
break;
}
default:cerr << "跟你說了別惡搞,真的會崩的\n" ;
}//switch(typ)
break;
}
case 4:
{
if(head==NULL) {cout << "已經為空!!\n"; break;}
cout << "請輸入要刪除的數據的位置" <<endl;
int loc;
cin >> loc;
del(loc,head);
break;
}
case 5:
{
if(head==NULL) {cout << "已經為空!!\n"; break;}

cout << "請選擇統計類型" << endl;
cout << "1 每門課90分以上的" <<endl;
cout << "2 每門課60分以下的" <<endl;
cout << "3 單科分數排名" <<endl;
cout << "4 總分排名" <<endl;
int tp;
cin >> tp;
switch (tp)
{
case 1:
{
cout << "選擇科目\n" << "1 數學\n2 英語\n3 程序設計\n";
int dec;
cin >> dec;
if(dec == 1)
{
Node *q;
cout << "學號\t" << "姓名\t" << "數學\t"<< "英語\t"<< "程設\t"<< "總分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.math>90) out(q);
}
if(q->content.math>90) out(q);
}
else if(dec == 2)
{
Node *q;
cout << "學號\t" << "姓名\t" << "數學\t"<< "英語\t"<< "程設\t"<< "總分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.eng>90) out(q);
}
if(q->content.eng>90) out(q);
}
else if(dec == 3)
{
Node *q;
cout << "學號\t" << "姓名\t" << "數學\t"<< "英語\t"<< "程設\t"<< "總分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.pro>90) out(q);
}
if(q->content.pro>90) out(q);
}
else
cout << "輸入錯誤\n";
break;
}
case 2:
{
cout << "選擇科目\n" << "1 數學\n2 英語\n3 程設\n";
int dec;
cin >> dec;
if(dec == 1)
{
Node *q;
cout << "學號\t" << "姓名\t" << "數學\t"<< "英語\t"<< "程設\t"<< "總分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.math<60) out(q);
}
if(q->content.math<60) out(q);
}
else if(dec == 2)
{
Node *q;
cout << "學號\t" << "姓名\t" << "數學\t"<< "英語\t"<< "程設\t"<< "總分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.eng<60) out(q);
}
if(q->content.eng<60) out(q);
}
else if(dec == 3)
{
Node *q;
cout << "學號\t" << "姓名\t" << "數學\t"<< "英語\t"<< "程設\t"<< "總分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.pro<60) out(q);
}
if(q->content.pro<60) out(q);
}
else
cout << "輸入錯誤\n";
break;
}
case 3:
{
cout << "選擇科目\n" << "1 數學\n2 英語\n3 程序設計\n";
int dec;
cin >> dec;
if(dec == 1)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.math > q_max->content.math)
q_max=q2;
if(q1->content.math !=q_max->content.math )
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}

}
output(head);

}
else if(dec == 2)
{
for(Node *q=head;q->next!=NULL;q=q->next)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.eng > q_max->content.eng )
q_max=q2;
if(q1->content.eng !=q_max->content.eng )
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}

}
}
output(head);
}
else if(dec == 3)
{
for(Node *q=head;q->next!=NULL;q=q->next)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.pro > q_max->content.pro)
q_max=q2;
if(q1->content.pro!=q_max->content.pro)
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}
}

}
output(head);
}
else
cout << "輸入錯誤\n";
break;
}
case 4:
{
for(Node *q=head;q->next!=NULL;q=q->next)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.sum > q_max->content.sum)
q_max=q2;
if(q1->content.sum!=q_max->content.sum)
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}
}

}
output(head);
break;
}
default:cerr << "跟你說了別惡搞,真的會崩的\n" ;
}//switch(tp)
break;
}
case 6:
{
cout <<"################################################################################\n";
cout <<"這是B版,容易崩潰,請按照正常方式輸入,如果您想測試它是否會崩,那肯定崩!!!!\n";
cout <<endl;
cout <<"################################################################################\n";
break;
}
case 7:
{
cout << "謝謝使用,沒崩說明你強\n";
return 0;
break ;
}
default:cerr << "跟你說了別惡搞,真的會崩的\n" ;
} //switch(choice)
goto A;
}

void del (int a,Node *&head)
{
if(head==NULL) {cout << "已經為空!!\n" <<endl;return ;}
Node *p=head;
if(a==1)
{
head=p->next;
delete p;
}
else
{
Node *q=p->next;
for(int i=1;i<a-1;i++)
{
p=p->next;
q=p->next;
}
p->next=q->next;
delete q;
}
}
void out(Node *p)
{

cout << p->content.no <<'\t';
cout << p->content.name <<'\t';
cout << p->content.math <<'\t';
cout << p->content.eng <<'\t';
cout << p->content.pro <<'\t';
cout << p->content.sum <<'\t';
cout << p->content.ave <<'\n';
}

void output(Node *head)
{
cout << "學號\t" << "姓名\t" << "數學\t"<< "英語\t"<< "程設\t"<< "總分\t" << "平均分\n" ;
for (Node *p=head;p!=NULL;p=p->next)
{
cout << p->content.no<<'\t';
cout << p->content.name<<'\t';
cout << p->content.math<<'\t';
cout << p->content.eng <<'\t';
cout << p->content.pro<<'\t';
cout << p->content.sum<<'\t';
cout << p->content.ave<<'\t';
cout << endl;
}
}

void input(st cont,Node *&head)
{
Node *p=new Node;
p->content=cont;
if (head==NULL)
{
head=p;
p->next=NULL;
}
else
{
p->next=head;
head=p;
}
}

4. C語言常式是什麼

常式的作用類似於函數,但含義更為豐富一些。常式是某個系統對外提供的功能介面或服務的集合。比如操作系統的API、服務等就是常式;Delphi或C++Builder提供的標准函數和庫函數等也是常式。我們編寫一個DLL的時候,裡面的輸出函數就是這個DLL的常式。 可以這么簡單地來理解:把一段相對獨立的代碼寫成單獨的一個模塊就是函數的概念。我們可以在自己的程序中編寫很多個函數,從而實現模塊化編程。但這些模塊或者說函數並不一定向外輸出(即提供給別的程序使用),只用於當前這個程序裡面。此時這些函數就僅僅具有獨立函數的意義,但不是常式。

轉網路

5. 求簡單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語言

6. 求最簡單的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));

}

7. C語言實例

#include
<stdio.h>
#define
N
50
//
排隊人數(可任意更改)
#define
CAL
3
//凡報3的人出列(可任意更改)
//下面是排隊編號函數:從h
開始的n個人依次編號1到n
void
stdline(int
*h,int
n)
{
int
i;
for(i=1;i<n+1;i++)
*(h+i-1)=i;
}
/*下面函數表示從指針h處開始的人數為boy個人排隊,從1報數,每報到call的人出列*/
void
outline(int
*h,int
boy,int
call)
{
int
*p,
chu,
callnum;
/*說明:
p
工作指針,表示從頭依次指向每個元素,點名
chu
計數器,記錄出列的人數
callnum
計數器,記錄點名次序
*/
chu=0;
callnum=0;//各計數器清零
p=h;
//開始時,工作指針指向數組首
printf("出列順序是:\n");
while(chu<boy)
{
if(*p!=0)
callnum++;
//每次加報數
if(callnum==call)
//如果某一個人報到出列數call...
{
printf("%5d",*p);
//列印編號,表示出列
chu++;
//出列人數加1
if(chu==boy)//如果全部出列....
{
*h=*p;
//把最後一個出列人的編號記入地址開始處
return;
//結束
}
if(chu%10==0)printf("\n");//每輸出10個換行
callnum=0;
//出列後,重新報數
*p=0;
//出列後,將其編號賦零,以示區別
}
p++;
//工作指針移向下一個人,即下一個數組元素
if(p>h+boy-1)p=h;/*如果移到最後一個元素的後面,則讓指向地址開頭繼續報數*/
}
}
void
main()
{
int
a[N];
//用數組模擬隊列,每個元素代表一個人
stdline(a,N);//編號
outline(a,N,CAL);//計算並列印出列順序
printf("\n最後留下來的是
%d
號\n",*a);/*在函數中,已經把最後一個人的編號寫入了數組首地址處,
這里輸出就可以了*/
}

8. C語言 程序

在手機上用易歷知食軟體里的微C程序設計來編寫個示例,並運行和測試。手機上的代碼如下圖所示:

9. C語言簡單例子

C語言中,一般會用到函數。系統默認從主函數開始運行,即main()函數。一般結構為:
void main(void)
{
....
}
一般函數是帶有參數的,即後面括弧中需要一個或n個變數。中間用逗號隔開。例如
void add(int a, int b)
{
...
}
其中a,b為整形的參數,在大部分系統中int 代表16位的數。
有的函數還有返回值:像上面的一個加法函數,可以寫成:
int add(int a,int b)
{
return (a+b); //retuen為返回值
}
當我們要調用該函數時,只需要這要寫:
void main(void)
{
int sum = 0;
sum = add(x1,x2); //x1,x2為兩個常數
}

10. 經典C語言程序例子

題目01:在一個已知的字元串中查找最長單詞,假定字元串中只含字母和空格,空格用來分隔不同的單詞。

(10)c語言常式擴展閱讀:

C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。

盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。

熱點內容
聚合腳本平台 發布:2024-09-20 17:51:55 瀏覽:180
訪問攔截怎麼解除安卓 發布:2024-09-20 17:28:48 瀏覽:275
蘿卜干存儲 發布:2024-09-20 17:21:37 瀏覽:715
蘋果手機如何遷移軟體到安卓手機 發布:2024-09-20 17:21:34 瀏覽:692
查看伺服器ip限制 發布:2024-09-20 16:56:27 瀏覽:389
p搜系統只緩存1頁為什麼 發布:2024-09-20 16:48:51 瀏覽:839
上網的賬號和密碼是什麼東西 發布:2024-09-20 16:31:31 瀏覽:612
安卓手機王者榮耀如何調超高視距 發布:2024-09-20 16:31:30 瀏覽:428
安卓G是什麼app 發布:2024-09-20 16:23:09 瀏覽:81
iphone怎麼壓縮文件 發布:2024-09-20 16:08:18 瀏覽:356