本文目錄一覽:
c語言,編寫模擬翻硬幣得程序。
#includestdio.h
int main(void)
{
int n;//讀入一個數,為結束的次數
int i;
int a[10];//10個硬幣
int biaoji = 0;//當這個標記能被3或7整除,說明恰好數了3次或7次
scanf(“%d”, n);
for (i = 0; i 10; i++) //將硬幣都初始化為正面的狀態,即1
a[i] = 1;
biaoji = 1;//因為a[]的下標不允許超過9,故要重置
a[biaoji – 1] = !a[biaoji – 1];//改變硬幣的狀態
for (i = 0; i 10; i++)
printf(“%d”, a[i]);
return 0;
}
主要特點
C語言是一種結構化語言,它有着清晰的層次,可按照模塊的方式對程序進行編寫,十分有利於程序的調試,且c語言的處理和表現能力都非常的強大,依靠非常全面的運算符和多樣的數據類型,可以輕易完成各種數據結構的構建,通過指針類型更可對內存直接尋址以及對硬件進行直接操作,因此既能夠用於開發系統程序,也可用於開發應用軟件。
C語言模擬題
一.選擇題:
1.B 2.D 3.A 4.B 5.D 6.B 7.B 8.A
二.讀程序:
1. 67,D 2. 3 3. 3 4. 3
用C語言編程模擬處理機調度(實現一種算法)
#include stdlib.h
#include conio.h
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct pcb { /* 定義進程控制塊PCB */
char name[10];
char state;
int super;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
void sort() /* 建立對進程進行優先級排列函數*/
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p-super)(ready-super))) /*優先級最大者,插入隊首*/
{
p-link=ready;
ready=p;
}
else /* 進程比較優先級,插入適當的位置中*/
{
first=ready;
second=first-link;
while(second!=NULL)
{
if((p-super)(second-super)) /*若插入進程比當前進程優先數大,*/
{ /*插入到當前進程前面*/
p-link=second;
first-link=p;
second=NULL;
insert=1;
}
else /* 插入進程優先數最低,則插入到隊尾*/
{
first=first-link;
second=second-link;
}
}
if(insert==0) first-link=p;
}
}
void input() /* 建立進程控制塊函數*/
{
int i,num;
system(“cls”); /*清屏*/
printf(“\n 請輸入進程數: “);
scanf(“%d”,num);
for(i=1;i=num;i++)
{
printf(“\n 進程號No.%d:\n”,i);
p=getpch(PCB);
printf(“\n 輸入進程名:”);
scanf(“%s”,p-name);
printf(“\n 輸入進程優先數:”);
scanf(“%d”,p-super);
printf(“\n 輸入進程運行時間:”);
scanf(“%d”,p-ntime);
printf(“\n”);
p-rtime=0;p-state=’W’;
p-link=NULL;
sort(); /* 調用sort函數*/
}
}
int space()
{
int l=0;
PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr-link;
}
return(l);
}
void disp(PCB * pr) /*建立進程顯示函數,用於顯示當前進程*/
{
printf(“\n 進程名\t 狀態\t 優先數\t 需要運行時間\t 已經運行時間\n”);
printf(“|%s\t”,pr-name);
printf(“|%c\t”,pr-state);
printf(“|%d\t”,pr-super);
printf(“|%d\t\t”,pr-ntime);
printf(“|%d\t”,pr-rtime);
printf(“\n”);
}
void check() /* 建立進程查看函數 */
{
PCB* pr;
printf(“\n **** 當前正在運行的進程是:\n”); /*顯示當前運行進程*/
disp(p);
pr=ready;
printf(“\n **** 當前就緒隊列狀態為:\n”); /*顯示就緒隊列狀態*/
while(pr!=NULL)
{
disp(pr);
pr=pr-link;
}
}
void destroy() /*建立進程撤消函數(進程運行結束,撤消進程)*/
{
printf(“\n 進程 [%s] 已完成.\n”,p-name);
free(p);
}
void running() /* 建立進程就緒函數(進程運行時間到,置就緒狀態*/
{
(p-rtime)++;
if(p-rtime==p-ntime)
destroy(); /* 調用destroy函數*/
else
{
(p-super)–;
p-state=’W’;
sort(); /*調用sort函數*/
}
}
void main() /*主函數*/
{
int len,h=0;
char ch;
input();
len=space();
while((len!=0)(ready!=NULL))
{
ch=getchar();
h++;
printf(“—————————————————–“);
printf(“\n 現在是第%d次運行: \n”,h);
p=ready;
ready=p-link;
p-link=NULL;
p-state=’R’;
check();
running();
printf(“\n 按任意鍵繼續……\n”);
}
printf(“\n\n 進程已經完成.\n”);
}
C語言模擬命令行調用GCC編譯器編譯一個.c文件?
包含stdlib.h頭文件,調用的時候用
system(“gcc
-o
yourfile
yourfile.c”);
就可以了。
參考
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/286703.html