本文目錄一覽:
- 1、用C語言編寫二進制數運算模擬程序
- 2、用C語言編寫程序 提示用戶輸入一個數字並顯示該數 使用字符模擬七段顯示器效果
- 3、用c語言編寫一個計算機程序
- 4、用c語言編寫程序,謝謝!!
- 5、用C語言編寫並調試一個模擬的進程調度程序,採用“簡單時間片輪轉法”調度算法對五個進程進行調度。
- 6、如何用C語言編寫一個簡單的程序!
用C語言編寫二進制數運算模擬程序
用數組模擬就行。
思路:
1、將輸入轉化成二進制數,逆序存入數組中。
2、用兩數組模擬四則運算,並將運算的結果存入第三個數組中。
3、逆序輸出第三個數組。
希望對你有幫助!
用C語言編寫程序 提示用戶輸入一個數字並顯示該數 使用字符模擬七段顯示器效果
#include stdio.h
#include string.h
#define MAX_DIGHTS 10
const int segments[10][7] = {
{1, 1, 1, 1, 1, 1, 0}, /* 0 */
{0, 1, 1, 0, 0, 0, 0}, /* 1 */
{1, 1, 0, 1, 1, 0, 1}, /* 2 */
{1, 1, 1, 1, 0, 0, 1}, /* 3 */
{0, 1, 1, 0, 0, 1, 1}, /* 4 */
{1, 0, 1, 1, 0, 1, 1}, /* 5 */
{1, 0, 1, 1, 1, 1, 1}, /* 6 */
{1, 1, 1, 0, 0, 0, 0}, /* 7 */
{1, 1, 1, 1, 1, 1, 1}, /* 8 */
{1, 1, 1, 1, 0, 1, 1} /* 9 */
};
char dights[3][MAX_DIGHTS * 4];
void clear_dight_array(void);
void process_dights_array(int dight, int position);
void print_dights_array(void);
int main(void) {
unsigned char numbers[MAX_DIGHTS];
int ch, i = 0, j;
clear_dight_array();
printf(“Enter a number: “);
while ((ch = getchar()) != EOF ch != ‘\n’)
if (i MAX_DIGHTS ch = ‘0’ ch = ‘9’)
process_dights_array(ch – ‘0’, i++);
print_dights_array();
return 0;
}
void clear_dight_array(void) {
memset(dights, ‘ ‘, sizeof dights);
}
void process_dights_array(int dight, int position) {
int n = position * 4;
if (segments[dight][0])
dights[0][n + 1] = ‘_’;
if (segments[dight][1])
dights[1][n + 2] = ‘|’;
if (segments[dight][2])
dights[2][n + 2] = ‘|’;
if (segments[dight][3])
dights[2][n + 1] = ‘_’;
if (segments[dight][4])
dights[2][n] = ‘|’;
if (segments[dight][5])
dights[1][n] = ‘|’;
if (segments[dight][6])
dights[1][n + 1] = ‘_’;
}
void print_dights_array(void) {
int i, j;
for (i = 0; i 3; i++) {
for (j = 0; j MAX_DIGHTS * 4; j++) {
putchar(dights[i][j]);
}
putchar(‘\n’);
}
}
用c語言編寫一個計算機程序
我對你提出的問題的題意的理解是編一個計算器程序。。。。。。。。。如果要是那樣子的話我給出代碼:
#include stdio.h
int main()
{
char cp;
int a, b;
scanf(“%d %c %d”, a, cp, b);
if (cp == ‘-‘)printf(“%d”, a – b);
else if(cp == ‘+’)printf(“%d”, a + b);
else if (cp == ‘*’)printf(“%d”, a * b);
else if (cp == ‘%’)printf(“%d”, a % b);
return 0;
}
如果要知道這幾個符號在機器中的實現機理的話:
+和-不說了*就相當於多做幾遍加法。而%是用位運算之類的方法進行運算的所以%的效率最低
不知道是不是你的編譯器有問題我的程序運行起來是得15的
你是否正確輸入了????
用c語言編寫程序,謝謝!!
純模擬,看上去有點複雜,可是一點算法都沒有。。。
#includeiostream
using namespace std;
int main()
{
int n,m,;
cinnm;
if (((n%4==0) and (n%100!=0)) or (n%400==0))
{
cout”Leap year,”;
if (m==1) cout”Winter,31″endl;
if (m==2) cout”Winter,29″endl;
if (m==3) cout”Spring,31″endl;
if (m==4) cout”Spring,30″endl;
if (m==5) cout”Spring,31″endl;
if (m==6) cout”Summer,30″endl;
if (m==7) cout”Summer,31″endl;
if (m==8) cout”Summer,31″endl;
if (m==9) cout”Fall,30″endl;
if (m==10) cout”Fall,31″endl;
if (m==11) cout”Fall,30″endl;
if (m==12) cout”Winter,31″endl;
} else
{
cout”Common year,”;
if (m==1) cout”Winter,31″endl;
if (m==2) cout”Winter,28″endl;
if (m==3) cout”Spring,31″endl;
if (m==4) cout”Spring,30″endl;
if (m==5) cout”Spring,31″endl;
if (m==6) cout”Summer,30″endl;
if (m==7) cout”Summer,31″endl;
if (m==8) cout”Summer,31″endl;
if (m==9) cout”Fall,30″endl;
if (m==10) cout”Fall,31″endl;
if (m==11) cout”Fall,30″endl;
if (m==12) cout”Winter,31″endl;
}
return 0;
}
望採納。。。
用C語言編寫並調試一個模擬的進程調度程序,採用“簡單時間片輪轉法”調度算法對五個進程進行調度。
#include “stdio.h”
#include “stdlib.h”
#include “string.h”
struct PCB {
char NAME[10]; /*進程名*/
int ROUND; /*進程輪轉時間片*/
int REACHTIME; /*進程到達時間*/
int CPUTIME; /*進程佔用CPU時間*/
int COUNT; /*計數器*/
int NEEDTIME; /*進程完成還要的CPU時間*/
char STATE; /*進程的狀態*/
struct PCB *NEXT; /*鏈指針*/
};
struct LINK { /*PCB的鏈結構*/
struct PCB *RUN; /*當前運行進程指針*/
struct PCB *READY; /*就緒隊列頭指針*/
struct PCB *TAIL; /*就緒隊列尾指針*/
struct PCB *FINISH; /*完成隊列頭指針*/
};
void INIT(LINK *); /*對PCB的鏈結構初始化*/
void INSERT(LINK *); /*將執行了一個單位時間片數且還未完成的進程的PCB插到就緒隊列的隊尾*/
void FIRSTIN(LINK *); /*將就緒隊列中的第一個進程投入運行*/
void PRINT(LINK *); /*打印每執行一個時間片後的所有進程的狀態*/
void PR(PCB *); /*打印一個進程的狀態*/
int CREATE(LINK *,int); /*創建新的進程*/
void ROUNDSCH(LINK *); /*按時間片輪轉法調度進程*/
void main() {
LINK pcbs;
int i;
INIT(pcbs);
i=0;
printf(“創建5個進程\n\n”);
while(i5) {
if(CREATE(pcbs,i+1)==1) {
printf(“進程已創建\n\n”);
i++;
}
else
printf(“進程創建失敗\n\n”);
}
FIRSTIN(pcbs);
ROUNDSCH(pcbs);
}
void ROUNDSCH(LINK *p) {
PCB *pcb;
while(p-RUN!=NULL) {
pcb=(PCB *)malloc(sizeof(PCB));
strcpy(pcb-NAME,p-RUN-NAME);
pcb-ROUND=p-RUN-ROUND;
pcb-REACHTIME=p-RUN-REACHTIME;
pcb-CPUTIME=p-RUN-CPUTIME;
pcb-COUNT=p-RUN-COUNT;
pcb-NEEDTIME=p-RUN-NEEDTIME;
pcb-STATE=p-RUN-STATE;
pcb-NEXT=p-RUN-NEXT;
pcb-CPUTIME++;
pcb-NEEDTIME–;
pcb-COUNT++;
if(pcb-NEEDTIME==0) {
pcb-NEXT=p-FINISH-NEXT;
p-FINISH-NEXT=pcb;
pcb-STATE=’F’;
p-RUN=NULL;
if(p-READY!=p-TAIL)
FIRSTIN(p);
}
else {
p-RUN=pcb;
if(pcb-COUNT==pcb-ROUND) {
pcb-COUNT=0;
if(p-READY!=p-TAIL) {
pcb-STATE=’W’;
INSERT(p);
FIRSTIN(p);
}
}
}
PRINT(p);
}
}
void INIT(LINK *p) {
p-RUN=NULL;
p-TAIL=p-READY=(PCB *)malloc(sizeof(PCB));
p-READY-NEXT=NULL;
p-FINISH=(PCB *)malloc(sizeof(PCB));
p-FINISH-NEXT=NULL;
}
int CREATE(LINK *p,int n) {
PCB *pcb,*q;
pcb=(PCB *)malloc(sizeof(PCB));
flushall();
printf(“請輸入第%d個進程的名稱:\n”,n);
gets(pcb-NAME);
printf(“請輸入第%d個進程的輪轉時間片數:\n”,n);
scanf(“%d”,(pcb-ROUND));
printf(“請輸入第%d個進程的到達時間:\n”,n);
scanf(“%d”,(pcb-REACHTIME));
pcb-CPUTIME=0;
pcb-COUNT=0;
printf(“請輸入第%d個進程需運行的時間片數:\n”,n);
scanf(“%d”,(pcb-NEEDTIME));
pcb-STATE=’W’;
pcb-NEXT=NULL;
if(strcmp(pcb-NAME,””)==0||pcb-ROUND=0||pcb-NEEDTIME=0) /*輸入錯誤*/
return 0;
q=p-READY;
while(q-NEXT!=NULLq-NEXT-REACHTIME=pcb-REACHTIME)
q=q-NEXT;
pcb-NEXT=q-NEXT;
q-NEXT=pcb;
if(pcb-NEXT==NULL)
p-TAIL=pcb;
return 1;
}
void FIRSTIN(LINK *p) {
PCB *q;
q=p-READY-NEXT;
p-READY-NEXT=q-NEXT;
q-NEXT=NULL;
if(p-READY-NEXT==NULL)
p-TAIL=p-READY;
q-STATE=’R’;
p-RUN=q;
}
void INSERT(LINK *p) {
PCB *pcb;
pcb=(PCB *)malloc(sizeof(PCB));
strcpy(pcb-NAME,p-RUN-NAME);
pcb-ROUND=p-RUN-ROUND;
pcb-REACHTIME=p-RUN-REACHTIME;
pcb-CPUTIME=p-RUN-CPUTIME;
pcb-COUNT=p-RUN-COUNT;
pcb-NEEDTIME=p-RUN-NEEDTIME;
pcb-STATE=p-RUN-STATE;
pcb-NEXT=p-RUN-NEXT;
p-TAIL-NEXT=pcb;
p-TAIL=pcb;
p-RUN=NULL;
pcb-STATE=’W’;
}
void PRINT(LINK *p) {
PCB *pcb;
printf(“執行一個時間片後的所有進程的狀態:\n\n”);
if(p-RUN!=NULL)
PR(p-RUN);
if(p-READY!=p-TAIL) {
pcb=p-READY-NEXT;
while(pcb!=NULL) {
PR(pcb);
pcb=pcb-NEXT;
}
}
pcb=p-FINISH-NEXT;
while(pcb!=NULL) {
PR(pcb);
pcb=pcb-NEXT;
}
}
void PR(PCB *p) {
printf(“進程名:%s\n”,p-NAME);
printf(“進程輪轉時間片:%d\n”,p-ROUND);
printf(“進程到達時間:%d\n”,p-REACHTIME);
printf(“進程佔用CPU時間:%d\n”,p-CPUTIME);
printf(“計數器:%d\n”,p-COUNT);
printf(“進程完成還要的CPU時間:%d\n”,p-NEEDTIME);
printf(“進程的狀態:%c\n\n”,p-STATE);
}
如何用C語言編寫一個簡單的程序!
上了大學有很多同學都在學習C語言,C++就是C語言的一種,那麼怎麼用c語言寫一個簡單的程序hello world呢,下邊來給大家演示一下
工具/材料
電腦,c語言軟件
01
鼠標左鍵雙擊c語言軟件,打開,打開後界面如圖,點擊關閉即可
02
點擊上方程序窗口左上角的文件,選擇新建
03
在打開的窗口中選擇文件,下邊一般是第四個 c++Source file,輸入文件名(hellw.c),一定要以“.c”為後綴結尾
04
進入編輯頁面在,頁面編輯源代碼就可以
#includestdio.h
void main()
{
printf(“hello world!”\n);
}
然後選擇保存,打印,輸出運行。
05
輸出的效果就是這樣了,一個簡單的c語言程序就寫好了
特別提示
所有的輸入都要在英文半角的情況下輸入,不然程序會不能識別,會報錯。
原創文章,作者:CMTJ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/144924.html