c語言雙語課件chapter3,c語言課堂作業一

本文目錄一覽:

cannot open Debug/chapter3.exe for writing

把這個Debug文件夾刪掉,重新編譯源文件, 會自動生成Debug目錄的…

(如果刪不掉, 建議把c++編譯器關掉在刪)

C語言數組任務(3),快速採納

#include stdio.h

#include string.h

void putout(int result[], int n=16);

void moveforward(int result[], int n)

{

if( n==0) return;

int i;

for(i=n; i=16; ++i) result[i-n] = result[i];

for(i=16-n; i16; ++i)result[i] = 0;

}

void getMul(int result[], int param[], int n=8)

{

int i, j;

int *tmp = new int[16];

int *param_tmp = new int[8];

for(i=0; i16; ++i)

{

tmp[i] = 0;

if(i=8) param_tmp[i-8] = result[i];

}

for(i=0; i16; ++i) result[i] = 0;

for(i=0; in; ++i)

{

for(j=0; j16; ++j) tmp[j] = 0;

for(j=0; j8; ++j) tmp[j+8] = param_tmp[j]*param[7-i];

for(j=15; j=0; –j)

{

if(tmp[j]=10)

{

if(j0) tmp[j-1] += tmp[j]/10;

tmp[j] = tmp[j]%10;

}

}

moveforward(tmp, i);

for(j=15; j=0; –j)

{

result[j] += tmp[j];

if(result[j]=10)

{

if(j0) result[j-1] += result[j]/10;

result[j] = result[j]%10;

}

}

}

}

void putout(int result[], int n)

{

int i(0);

while(result[i] == 0) ++i;

//printf(“result:”);

for(; i n; ++i) printf(“%d”,result[i]);

//printf(“\n”);

}

void main()

{

char param1[9], param2[9];

int result[16], param[8];

printf(“Input two number:”);

scanf(“%s”, param1);

scanf(“%s”, param2);

int i, j(0);

for(i=0; i16; ++i)

{

if(i8) param[i] = 0;

result[i] = 0;

}

j=0;

for(i=strlen(param1)-1; i=0; –i, j++) result[15-j] = param1[i]-‘0’;

j=0;

for(i=strlen(param2)-1; i=0; –i, j++) param[7-j] = param2[i]-‘0’;

getMul(result, param, strlen(param2));

printf(“%s*%s=”, param1, param2);

putout(result);

printf(“\n”);

}

c語言求一個 3×3的整型矩陣對角線元素之和

c語言求一個3×3的整型矩陣對角線元素之和步驟如下:

1、打開Dev-c++軟體,點擊「新建源代碼」。

2、具體程序如下:

#include stdio.h

int main()

{

int a[3][5]={{1,2,3},{4,5,6},{7,8,9}};

int i,j,sum,m=0,sum1=0,sum2=0;

printf(“array a:\n”);

for(i=0;i3;i++)

{

for(j=0;j3;j++)

{

printf(“%4d”,a[i][j]);

m=m+1;

}

if(m%3==0)

printf(“\n”);

}

sum1=a[0][0]+a[1][1]+a[2][2];

sum2=a[0][2]+a[1][1]+a[2][0];

sum=sum1+sum2;

printf(“對角線元素之和為%d”,sum);

return 0;

}

3、此次我們的程序最終目的是求出3×3的整型矩陣對角線元素之和,所以我們首先要向計算機輸入我們3×3矩陣中的所有元素。

4、其次,我們要對輸入的矩陣的元素進行放置於相應的位置,以便於我們之後的計算其對角元素之和。

5、再次,我們需要向計算機指出要計算機計算出哪些元素之和和需要計算元素的具體位置在哪兒。

6、這裡需要說明一下,我們需要定義三個累積和sum1,sum2,sum來進行計算後的結果的暫時存儲。

7、其中:if(m%3==0)表示要求最後輸出的結果呈輸出3個數之後進行換行。

8、當我們的程序寫完之後,點擊運行即可求出我們所要求出的結果了。

圖書信息管理系統C語言

自己寫的!

題目和你們一樣!

#includestdio.h

#includemath.h

#includestring.h

#includestdlib.h

struct books_list

{

char author[20]; /*作者名*/

char bookname[20]; /*書名*/

char publisher[20]; /*出版單位*/

char pbtime[15]; /*出版時間*/

char loginnum[10]; /*登陸號*/

float price; /*價格*/

char classfy[10]; /*分類號*/

struct books_list * next; /*鏈表的指針域*/

};

struct books_list * Create_Books_Doc(); /*新建鏈表*/

void InsertDoc(struct books_list * head); /*插入*/

void DeleteDoc(struct books_list * head , int num);/*刪除*/

void Print_Book_Doc(struct books_list * head);/*瀏覽*/

void search_book(struct books_list * head); /*查詢*/

void info_change(struct books_list * head);/*修改*/

void save(struct books_list * head);/*保存數據至文件*/

/*新建鏈表頭節點*/

struct books_list * Create_Books_Doc()

{

struct books_list * head;

head=(struct books_list *)malloc(sizeof(struct books_list)); /*分配頭節點空間*/

head-next=NULL; /*頭節點指針域初始化,定為空*/

return head;

}

/*保存數據至文件*/

void save(struct books_list * head)

{

struct books_list *p;

FILE *fp;

p=head;

fp=fopen(“data.txt”,”w+”); /*以寫方式新建並打開 data.txt文件*/

fprintf(fp,”┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n”); /*向文件輸出表格*/

fprintf(fp,”┃登錄號┃ 書 名 ┃ 作 者┃ 出版單位 ┃ 出版時間 ┃分類號┃ 價格 ┃\n”);

fprintf(fp,”┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\n”);

/*指針從頭節點開始移動,遍歷至尾結點,依次輸出圖書信息*/

while(p-next!= NULL)

{

p=p-next;

fprintf(fp,”┃%-6.6s┃%-10.10s┃%-10.10s┃%-10.10s┃%-12.12s┃%-6.6s┃%.2f ┃\n”,p-loginnum,p-bookname,p-author,p-publisher,p-pbtime,p-classfy,p-price);

}

fprintf(fp,”┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━┻━━━┻━━━━┛\n”);

fclose(fp);

printf(” 已將圖書數據保存到 data.txt 文件\n”);

}

/*插入*/

void InsertDoc(struct books_list *head)

{

/*定義結構體指針變數 s指向開闢的新結點首地址 p為中間變數*/

struct books_list *s, *p;

char flag=’Y’; /*定義flag,方便用戶選擇重複輸入*/

p=head;

/*遍歷到尾結點,p指向尾結點*/

while(p-next!= NULL)

{

p=p-next;

}

/*開闢新空間,存入數據,添加進鏈表*/

while(flag==’Y’||flag==’y’)

{

s=(struct books_list *)malloc(sizeof(struct books_list));

printf(“\n 請輸入圖書登陸號:”);

fflush(stdin);

scanf(“%s”,s-loginnum);

printf(“\n 請輸入圖書書名:”);

fflush(stdin);

scanf(“%s”,s-bookname);

printf(“\n 請輸入圖書作者名:”);

fflush(stdin);

scanf(“%s”,s-author);

printf(“\n 請輸入圖書出版社:”);

fflush(stdin);

scanf(“%s”,s-publisher);

printf(“\n 請輸入圖書出版時間:”);

fflush(stdin);

scanf(“%s”,s-pbtime);

printf(“\n 請輸入圖書分類號:”);

fflush(stdin);

scanf(“%s”,s-classfy);

printf(“\n 請輸入圖書價格:”);

fflush(stdin);

scanf(“%f”,s-price);

printf(“\n”);

p-next=s; /*將新增加的節點添加進鏈表*/

p=s; /*p指向尾節點,向後移*/

s-next=NULL;

printf(” ━━━━ 添加成功!━━━━”);

printf(“\n 繼續添加?(Y/N):”);

fflush(stdin);

scanf(“%c”,flag);

printf(“\n”);

if(flag==’N’||flag==’n’)

{break;}

else if(flag==’Y’||flag==’y’)

{continue;}

}

save(head); /*保存數據至文件*/

return;

}

/*查詢操作*/

void search_book(struct books_list *head)

{

struct books_list * p;

char temp[20];

p=head;

if(head==NULL || head-next==NULL) /*判斷資料庫是否為空*/

{

printf(” ━━━━ 圖書庫為空!━━━━\n”);

}

else

{

printf(“請輸入您要查找的書名: “);

fflush(stdin);

scanf(“%s”,temp);

/*指針從頭節點開始移動,遍歷至尾結點,查找書目信息*/

while(p-next!= NULL)

{

p=p-next;

if(strcmp(p-bookname,temp)==0)

{

printf(“\n圖書已找到!\n”);

printf(“\n”);

printf(“登錄號: %s\t\n”,p-loginnum);

printf(“書名: %s\t\n”,p-bookname);

printf(“作者名: %s\t\n”,p-author);

printf(“出版單位: %s\t\n”,p-publisher);

printf(“出版時間: %s\t\n”,p-pbtime);

printf(“分類號: %s\t\n”,p-classfy);

printf(“價格: %.2f\t\n”,p-price);

}

if(p-next==NULL)

{

printf(“\n查詢完畢!\n”);

}

}

}

return;

}

/*瀏覽操作*/

void Print_Book_Doc(struct books_list * head)

{

struct books_list * p;

if(head==NULL || head-next==NULL) /*判斷資料庫是否為空*/

{

printf(“\n ━━━━ 沒有圖書記錄! ━━━━\n\n”);

return;

}

p=head;

printf(“┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n”);

printf(“┃登錄號┃ 書 名 ┃ 作 者┃ 出版單位 ┃ 出版時間 ┃分類號┃ 價格 ┃\n”);

printf(“┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\n”);

/*指針從頭節點開始移動,遍歷至尾結點,依次輸出圖書信息*/

while(p-next!= NULL)

{

p=p-next;

printf(“┃%-6.6s┃%-10.10s┃%-10.10s┃%-10.10s┃%-12.12s┃%-6.6s┃%.2f ┃\n”,p-loginnum,p-bookname,p-author,p-publisher,p-pbtime,p-classfy,p-price); /*循環輸出表格*/

}

printf(“┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━┻━━━┻━━━━┛\n”);

printf(“\n”);

}

/*修改操作*/

void info_change(struct books_list * head)

{

struct books_list * p;

int panduan=0; /*此變數用於判斷是否找到書目*/

char temp[20];

p=head;

printf(“請輸入要修改的書名:”);

scanf(“%s”,temp);

while(p-next!= NULL)

{

p=p-next;

if(strcmp(p-bookname,temp)==0)

{

printf(“\n 請輸入圖書登陸卡號:”);

fflush(stdin);

scanf(“%s”,p-loginnum);

printf(“\n 請輸入圖書書名:”);

fflush(stdin);

scanf(“%s”,p-bookname);

printf(“\n 請輸入圖書作者名:”);

fflush(stdin);

scanf(“%s”,p-author);

printf(“\n 請輸入圖書出版社:”);

fflush(stdin);

scanf(“%s”,p-publisher);

printf(“\n 請輸入圖書出版時間:”);

fflush(stdin);

scanf(“%s”,p-pbtime);

printf(“\n 請輸入圖書分類號:”);

fflush(stdin);

scanf(“%s”,p-classfy);

printf(“\n 請輸入圖書價格:”);

fflush(stdin);

scanf(“%f”,p-price);

printf(“\n”);

panduan=1;

}

}

if(panduan==0)

{

printf(“\n ━━━━ 沒有圖書記錄! ━━━━\n\n”);

}

return;

}

/*刪除操作*/

void DeleteDoc(struct books_list * head)

{

struct books_list *s,*p; /*s為中間變數,p為遍歷時使用的指針*/

char temp[20];

int panduan; /*此變數用於判斷是否找到了書目*/

panduan=0;

p=s=head;

printf(” [請輸入您要刪除的書名]:”);

scanf(“%s”,temp);

/*遍歷到尾結點*/

while(p!= NULL)

{

if(strcmp(p-bookname,temp)==0)

{

panduan++;

break;

}

p=p-next;

}

if(panduan==1)

{

for(;s-next!=p;) /*找到所需刪除卡號結點的上一個結點*/

{

s=s-next;

}

s-next=p-next; /*將後一節點地址賦值給前一節點的指針域*/

free(p);

printf(“\n ━━━━ 刪除成功! ━━━━\n”);

}

else /*未找到相應書目*/

{

printf(” 您輸入的書目不存在,請確認後輸入!\n”);

}

return;

}

int main(void)

{

struct books_list * head;

char choice;

head=NULL;

for(;;) /*實現反覆輸入選擇*/

{

printf(” ┏━┓━━━━━━━━━━━━━━━━━━━┏━┓\n”);

printf(” ┃ ┃ socat 圖書管理系統 ┃ ┃\n”);

printf(” ┃ ┗━━━━━━━━━━━━━━━━━━━┛ ┃\n”);

printf(” ┃ ●[1]圖書信息錄入 ┃\n”);

printf(” ┃ ┃\n”);

printf(” ┃ ●[2]圖書信息瀏覽 ┃\n”);

printf(” ┃ ┃\n”);

printf(” ┃ ●[3]圖書信息查詢 ┃\n”);

printf(” ┃ ┃\n”);

printf(” ┃ ●[4]圖書信息修改 ┃\n”);

printf(” ┃ ┃\n”);

printf(” ┃ ●[5]圖書信息刪除 ┃\n”);

printf(” ┃ ┃\n”);

printf(” ┃ ●[6]退出系統 ┃\n”);

printf(” ┗━━━━━━━━━━━━━━━━━━━━━━━┛\n”);

printf(” 請選擇:”);

fflush(stdin);

scanf(“%c”,choice);

if(choice==’1′)

{

if(head==NULL)

{

head=Create_Books_Doc();

}

InsertDoc(head);

}

else if(choice==’2′)

{

Print_Book_Doc(head);

}

else if(choice==’3′)

{

search_book(head);

}

else if(choice==’4′)

{

info_change(head);

}

else if(choice==’5′)

{

DeleteDoc(head);

}

else if(choice==’6′)

{

printf(“\n”);

printf(” ━━━━━━━━ 感謝使用圖書管理系統 ━━━━━━━━\n”);

break;

}

else

{

printf(” ━━━━ 輸入錯誤,請重新輸入!━━━━”);

break;

}

}

return 0;

}

原創文章,作者:BWYK,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/141132.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
BWYK的頭像BWYK
上一篇 2024-10-04 00:24
下一篇 2024-10-04 00:24

相關推薦

  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • 小甲魚Python最新版課後作業及答案

    Python編程語言已經成為最受歡迎的計算機編程語言之一。而小甲魚的Python教程已經深受廣大編程學習者的青睞,其中課後作業及答案是學習過程中的必要部分。下面我們從幾個方面具體闡…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28
  • Python作為中心語言,在編程中取代C語言的優勢和挑戰

    Python一直以其簡單易懂的語法和高效的編碼環境而著名。然而,它最近的發展趨勢表明Python的使用範圍已經從腳本語言擴展到了從Web應用到機器學習等廣泛的開發領域。與此同時,C…

    編程 2025-04-28

發表回復

登錄後才能評論