C语言源代码
- 格式:doc
- 大小:43.00 KB
- 文档页数:17
#include 〈stdio.h〉void print_star(void){printf("*****************\n"); }void print_welcome(void){printf(”C language,welcome!\n");}void main(){print_star();print_welcome();print_star();getchar();}演示2#include "stdio。
h"int sum(int i,int j){return(i + j);}void main(){int n1,n2;printf("input 2 numbers:\n”);scanf("%d%d”,&n1,&n2);printf("the sum = %d\n",sum(n1,n2));getchar();}演示3#include "stdio。
h"int maxnum(int,int,int);main(){int a,b,c;printf("Please enter 3 numbers:\n");scanf(”%d,%d,%d”,&a,&b,&c);printf(”Maxnum is %d\n",maxnum(a,b,c));}int maxnum(int x,int y,int z){int max=x;if(y>max)max = y;if(z>max)max = z;return max;}演示4#include 〈stdio。
h〉int s1(int n){int j,s;s=0;for(j=1;j<=n;j++)s=s+j;return s;}int sum(int n){int i,s=0;for(i=1;i<=n;i++)s=s+s1(i);return s;}void main(){int n;printf(”n:”);scanf(”%d",&n);printf("s=%d\n”,sum(n));}演示5#include <stdio.h〉void func(int n){int i;for(i=n-1;i〉=1;i--)n=n+i;printf("n=%d\n",n);}void main(){int n;printf("Input n:");scanf("%d”,&n);func(n);printf(”n=%d\n",n);}演示6#include <stdio。
人工智能c语言源代码人工智能是一个广泛的领域,涉及多种算法和技术。
在C语言中,可以实现一些基本的人工智能功能。
下面是一个简单的示例,展示了如何使用C语言编写一个简单的决策树算法:c.#include <stdio.h>。
// 定义决策树节点结构。
typedef struct DecisionTreeNode {。
char question;struct DecisionTreeNode yesNode;struct DecisionTreeNode noNode;} DecisionTreeNode;// 创建决策树节点。
DecisionTreeNode createNode(char question) {。
DecisionTreeNode node = (DecisionTreeNode)malloc(sizeof(DecisionTreeNode));node->question = question;node->yesNode = NULL;node->noNode = NULL;return node;}。
// 决策树算法。
void decisionTree() {。
DecisionTreeNode root = createNode("Is it a mammal?");root->yesNode = createNode("Does it have fur?");root->yesNode->yesNode = createNode("It's amammal!");root->yesNode->noNode = createNode("It's not a mammal!");root->noNode = createNode("Does it have feathers?");root->noNode->yesNode = createNode("It's a bird!");root->noNode->noNode = createNode("It's not abird!");printf("%s\n", root->question);char answer;scanf(" %c", &answer);DecisionTreeNode currentNode = root;while (currentNode != NULL) {。
转换进制程序C语⾔,C语⾔各进制转换源代码#include#include#includevoid ten_two(int);void ten_eight(int);void ten_sixten(int);void two_ten(int);void two_eight(int);void two_sixten(int);void eight_two(int);void eight_ten(int);void eight_sixten(int);void sixten_two(char r[], int k);void sixten_ten(char r[], int k);void sixten_eight(char r[], int k);int main(void){int i, j, number = 0;int temp;char change[10];printf("=============================================================\n");printf(" 欢 迎 使 ⽤ 进 制 转 换 程 序 \n");printf(" 作 者:李 研 时 间:2009 年7 ⽉5 ⽇ \n");printf("=============================================================\n");do{temp = 0;//fflush(stdin);printf("\n请选择转换前的进制:\n2⼆进制\n8⼋进制\n10⼗进\n16⼗六进制\n0退出\n");printf("转换前的进制是:");scanf("%d", &i);case 2:printf("\n请选择转换后的进制:\n2⼆进制\n8⼋进制\n10⼗进\n16⼗六进制\n0退出\n"); printf("转换后的进制是:");scanf("%d", &j);switch(j){case 2:printf("\n同进制之间不⽤转化!\n");temp = 1;break;case 8:printf("\n请输⼊您想要转化的数:");scanf("%d", &number);two_eight(number);temp = 1;break;case 10:printf("\n请输⼊您想要转化的数:");scanf("%d", &number);two_ten(number);temp = 1;break;case 16:printf("\n请输⼊您想要转化的数:");scanf("%d", &number);two_sixten(number);temp = 1;break;case 0:printf("谢谢使⽤!!");}break;printf("转换后的进制是:");scanf("%d", &j);switch(j){case 8:printf("\n同进制之间不⽤转化!\n");temp = 1;break;case 2:printf("\n请输⼊您想要转化的数:");scanf("%d", &number);eight_two(number);temp = 1;break;case 10:printf("\n请输⼊您想要转化的数:");scanf("%d", &number);eight_ten(number);temp = 1;break;case 16:printf("\n请输⼊您想要转化的数:");scanf("%d", &number);eight_sixten(number);temp = 1;break;case 0:printf("谢谢使⽤!!");}break;case 10:printf("\n请选择转换后的进制:\n2⼆进制\n8⼋进制\n10⼗进\n16⼗六进制\n0退出\n");{case 10:printf("\n同进制之间不⽤转化!\n");temp = 1;break;case 2:printf("\n请输⼊您想要转化的数:");scanf("%d", &number);ten_two(number);temp = 1;break;case 8:printf("\n请输⼊您想要转化的数:");scanf("%d", &number);ten_eight(number);temp = 1;break;case 16:printf("\n请输⼊您想要转化的数:");scanf("%d", &number);ten_sixten(number);temp = 1;break;case 0:printf("谢谢使⽤!!");}break;case 16:printf("\n请选择转换后的进制:\n2⼆进制\n8⼋进制\n10⼗进\n16⼗六进制\n0退出\n"); printf("转换后的进制是:");scanf("%d", &j);printf("\n同进制之间不⽤转化!\n"); temp = 1;break;case 2:printf("\n请输⼊您想要转化的数:"); fflush(stdin);gets(change);for(number = 0;; number++){if(change[number] == '\0') break;}sixten_two(change, number);temp = 1;break;case 8:printf("\n请输⼊您想要转化的数:"); fflush(stdin);gets(change);for(number = 0;; number++){if(change[number] == '\0') break;}sixten_eight(change, number); temp = 1;break;case 10:printf("\n请输⼊您想要转化的数:"); fflush(stdin);gets(change);if(change[number] == '\0')break;}sixten_ten(change, number);temp = 1;break;case 0:printf("谢谢使⽤!!");}break;case 0:printf("\n谢谢使⽤!\n");}}while(temp == 1);return 0;}/*以下为: ⼆进制转换为⼗进制,⼋进制,⼗六进制.*/ void two_ten(int a){int i, s = 0;int result = 0;for(i = 1; a != 0; i *= 2){if(a % 10 > 1){s = 1;break;}else{result += (a % 10) * i;}if(s == 1)printf("您的输⼊有误!请重新输⼊\n"); elseprintf("\n转换后的数为:%d\n", result); }void two_eight(int a){int i, j, k, s = 0;int p[30];int result = 0;for(i = 1; a != 0; i *= 2){if(a % 10 > 1){s = 1;break;}else{result += (a % 10) * i;a = a / 10;}}for(j = 0; result != 0; j++){p[j] = result % 8;result = result / 8;}if(s == 1)printf("It is wrong\n");elsefor(k = j - 1; k >= 0; k--) {printf("%d", p[k]);}printf("\n");}}void two_sixten(int a){int i, j, k, s = 0;char p[30];int result = 0;for(i = 1; a != 0; i *= 2){if(a % 10 > 1){s = 1;break;}else{result += (a % 10) * i;a = a / 10;}}for(j = 0; result != 0; j++) {p[j] = result % 16;result = result / 16;if (p[j] > 10){switch(p[j])p[j] = 'A';break;case 11:p[j] = 'B';break;case 12:p[j] = 'C';break;case 13:p[j] = 'D';break;case 14:p[j] = 'E';break;case 15:p[j] = 'F';break;}}else p[j] += 48;}if(s == 1)printf("It is wrong\n"); else{printf("\n转换后的数为:"); for(k = j - 1; k >= 0; k--) {printf("%c", p[k]);}printf("\n");}void eight_two(int a){int i, j, k, s = 0;int result = 0;int p[30];for(i = 1; a != 0; i *= 8){if(a % 10 > 7){s = 1;break;}else{result += (a % 10) * i;a = a / 10;}}for(j = 0; result != 0; j++) {p[j] = result % 2;result = result / 2;}if(s == 1)printf("It is wrong\n"); else{printf("\n转换后的数为:"); for(k = j - 1; k >= 0; k--) {printf("%d", p[k]);}}void eight_ten(int a){int i, s = 0;int result = 0;for(i = 1; a != 0; i *= 8){if(a % 10 > 7){s = 1;break;}else{result += (a % 10) * i;a = a / 10;}}if(s == 1)printf("It is wrong\n");else{printf("\n转换后的数为:%d\n", result); }}void eight_sixten(int a){int i, j, k, s = 0;char p[30];int result = 0;for(i = 1; a != 0; i *= 8){}else{result += (a % 10) * i;a = a / 10;}}for(j = 0; result != 0; j++) {p[j] = result % 16;result = result / 16;if(p[j]p[j] += 48;else{switch(p[j]){case 10:p[j] = 'A';break;case 11:p[j] = 'B';break;case 12:p[j] = 'C';break;case 13:p[j] = 'D';break;case 14:}}}if(s == 1)printf("It is wrong\n");else{printf("\n转换后的数为:");for(k = j - 1; k >= 0; k--){printf("%c", p[k]);}printf("\n");}}/*以下为: ⼗六进制转换为⼗进制,⼆进制,⼋进制.*/ void sixten_ten(char a[], int k){int i, j, s = 0;int result = 0;int b[50];for(i = 0; i{if(a[i] <= '9' && a[i] >= '1'){b[i] = a[i] - 48;}else{case 'B': b[i] = 11; break; case 'C': b[i] = 12; break; case 'D': b[i] = 13; break; case 'E': b[i] = 14; break; case 'F': b[i] = 15; break; case 'a': b[i] = 10; break; case 'b': b[i] = 11; break; case 'c': b[i] = 12; break; case 'd': b[i] = 13; break; case 'e': b[i] = 14;s = 1;}}}for(i = 1, j = k - 1; j >= 0; j--, i *= 16) {result += b[j] * i;}if(s == 1)printf("It is wrong\n");else{printf("\n转换后的数为:%d", result); }}void sixten_two(char a[], int k){int i, j, s = 0;int result = 0;int b[50];int p[30];for(i = 0; i{if(a[i] <= '9' && a[i] >= '1')b[i] = a[i] - 48;else{switch(a[i]){break; case 'C': b[i] = 12; break; case 'D': b[i] = 13; break; case 'E': b[i] = 14; break; case 'F': b[i] = 15; break; case 'a': b[i] = 10; break; case 'b': b[i] = 11; break; case 'c': b[i] = 12; break; case 'd': b[i] = 13; break; case 'e': b[i] = 14; break; case 'f':}}}for(j = k - 1, i = 1; j >= 0; j--, i *= 16) {result += b[j] * i;}for(j = 0; result != 0; j++){p[j] = result % 2;result = result / 2;}if(s == 1)printf("It is wrong\n");else{printf("\n转换后的数为:");for(k = j - 1; k >= 0; k--){printf("%d", p[k]);}printf("\n");}}void sixten_eight(char a[], int k){int i, j, s = 0;int result = 0;int b[30];int p[30];if(a[i] <= '9' && a[i] >= '1') b[i] = a[i] - 48;else{switch(a[i]){case 'A':b[i] = 10;break;case 'B':b[i] = 11;break;case 'C':b[i] = 12;break;case 'D':b[i] = 13;break;case 'E':b[i] = 14;break;case 'F':b[i] = 15;break;case 'a':b[i] = 10;break;case 'b':b[i] = 11;break;case 'c':b[i] = 12;b[i] = 13;break;case 'e':b[i] = 14;break;case 'f':b[i] = 15;break;default:s = 1;}}}for(j = k - 1, i = 1; j >= 0; j--, i *= 16) {result += b[j] * i;}for(j = 0; result != 0; j++){p[j] = result % 8;result = result / 8;}if(s == 1)printf("It is wrong\n");else{printf("\n转换后的数为:");for(k = j - 1; k >= 0; k--){printf("%d", p[k]);}printf("\n");/*以下为: ⼗进制转换为⼆进制,⼋进制,⼗六进制.*/ void ten_two(int a){int j, k;int p[30];for(j = 0; a != 0; j++){p[j] = a % 2;a = a / 2;}printf("\n转换后的数为:");for(k = j - 1; k >= 0; k--){printf("%d", p[k]);}printf("\n");}void ten_eight(int a){int j, k;int p[30];for(j = 0; a != 0; j++){p[j] = a % 8;a = a / 8;}printf("\n转换后的数为:");for(k = j - 1; k >= 0; k--){printf("%d", p[k]);}printf("\n");{int j, k;int p[30];for(j = 0; a != 0; j++) {p[j] = a % 16;a = a / 16;if(p[j]p[j] += 48;else{switch(p[j]){case 10:p[j] = 'A';break;case 11:p[j] = 'B';break;case 12:p[j] = 'C';break;case 13:p[j] = 'D';break;case 14:p[j] = 'E';break;case 15:p[j] = 'F';break;}}}printf("\n转换后的数为:"); for(k = j - 1; k >= 0; k--) {printf("%c", p[k]);}printf("\n");}。
c语言正弦函数源代码#include <stdio.h>#include <math.h>#define PI 3.14159265int main() {double angle, result;int i;printf("Angle\t\tSin(Angle)\n");printf("------------------------\n");for (i = 0; i <= 360; i += 10) {angle = i * PI / 180;result = sin(angle);printf("%d\t\t%f\n", i, result);}return 0;}这是一个使用C语言编写的正弦函数源代码。
首先,我们包含了stdio.h和math.h头文件,分别用于输入输出和数学计算。
然后,我们定义了一个常量PI,其值为3.14159265,用于将角度转换为弧度。
在main函数中,我们声明了两个变量angle和result,分别用于存储角度和正弦值。
我们还声明了一个整型变量i,用于控制循环。
接下来,我们使用printf函数打印表头,包括角度和正弦值的标题。
然后,我们使用for循环从0度到360度,每次增加10度。
在循环中,我们将角度转换为弧度,并使用sin函数计算正弦值。
最后,我们使用printf函数打印角度和对应的正弦值。
最后,我们返回0,表示程序正常结束。
这段代码可以生成一个角度从0度到360度,每隔10度的正弦函数表。
通过运行这段代码,我们可以得到正弦函数在这些角度下的值,从而更好地理解正弦函数的性质。
------------------------------------------------------------------------摘自宋鲁生程序设计大赛乘法口诀表#include <stdio.h>#include <conio.h>void main(void){int i,j,x,y;clrscr();printf("\n\n * * * 乘法口诀表* * * \n\n");x=9;y=5;for(i=1;i<=9;i++){gotoxy(x,y);printf("%2d ",i);x+=3;}x=7;y=6;for(i=1;i<=9;i++){gotoxy(x,y);printf("%2d ",i);y++;}x=9;y= 6;for(i=1;i<=9;i++){for(j=1;j<=9;j++){gotoxy(x,y);printf("%2d ",i*j);y++;}y-=9;x+=3;}printf("\n\n");}用一维数组统计学生成绩#include <stdio.h>void main(){char SelectKey,CreditMoney,DebitMoney;while(1){do{clrscr();puts("=========================");puts("| Please select key: |");puts("| 1. Quary |");puts("| 2. Credit |");puts("| 3. Debit |");puts("| 4. Return |");puts("=========================");SelectKey = getch();}while( SelectKey!='1' && SelectKey!='2' && SelectKey!='3' &&SelectKey!='4' );switch(SelectKey){case '1':clrscr();puts("================================");puts("| Your balance is $1000. |");puts("| Press any key to return... |");puts("================================");getch();break;case '2':do{clrscr();puts("==================================");puts("| Please select Credit money: |");puts("| 1. $50 |");puts("| 2. $100 |");puts("| 3. Return |");puts("==================================");CreditMoney = getch();}while( CreditMoney!='1' && CreditMoney!='2' && CreditMoney!='3' );switch(CreditMoney){case '1':clrscr();puts("=========================================");puts("| Your Credit money is $50,Thank you! |");puts("| Press any key to return... |");puts("=========================================");getch();break;case '2':clrscr();puts("==========================================");puts("| Your Credit money is $100,Thank you! |");puts("| Press any key to return... |");puts("==========================================");getch();break;case '3':break;}break;case '3':do{clrscr();puts("====================================");puts("| Please select Debit money: |");puts("| 1. $50 |");puts("| 2. $100 |");puts("| 3. $500 |");puts("| 4. $1000 |");puts("| 5. Return |");puts("====================================");DebitMoney = getch();}while( DebitMoney!='1' && DebitMoney!='2' && DebitMoney!='3' &&DebitMoney!='4' && DebitMoney!='5' );switch(DebitMoney){case '1':clrscr();puts("===========================================");puts("| Your Debit money is $50,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '2':clrscr();puts("===========================================");puts("| Your Debit money is $100,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '3':clrscr();puts("===========================================");puts("| Your Debit money is $500,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '4':clrscr();puts("===========================================");puts("| Your Debit money is $1000,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '5':break;}break;case '4':clrscr();puts("================================");puts("| Thank you for your using! |");puts("| Good bye! |");puts("================================");return;}}模拟ATM(自动柜员机)界面#include <stdio.h> void main(){int Password=0,Number=0,price=58,i=0;while( Password != 1234 ){if( i >= 3 )return;i++;puts("Please input Password: ");scanf("%d",&Password);}i=0;while( Number!=price ){do{puts("Please input a number between 1 and 100: ");scanf("%d",&Number);printf("Your input number is %d\n",Number);}while( !(Number>=1 && Number<=100) );if( Number >= 90 ){printf("Too Bigger! Press any key to try again!\n");}else if( Number >= 70 && Number < 90 ){printf("Bigger!\n");}else if( Number >= 1 && Number <= 30 ){printf("Too Small! Press any key to try again!\n");}else if( Number > 30 && Number <= 50 ){printf("Small! Press any key to try again!\n");}else{if( Number == price ){printf("OK! You are right! Bye Bye!\n");}else if( Number < price ){printf("Sorry,Only a little smaller! Press any key to try again!\n");}else if( Number > price ){printf(" Sorry, Only a little bigger! Press any key to try again!\n");}getch();}}用二维数组实现矩阵转置/* 用二维数组实现矩阵的转置*/#include <stdio.h>#define ROW 3#define COL 4main(){int matrixA[ROW][COL],matrixB[COL][ROW];int i,j; clrscr();printf("Enter elements of the matrixA,");printf("%d*%d:\n",ROW,COL);for( i=0; i<ROW; i++ ){for( j=0; j<COL; j++ ){scanf("%d",&matrixA[i][j]);}}for( i=0; i<ROW; i++ ){for( j=0; j<COL; j++ ){matrixB[j][i] = matrixA[i][j];}}printf("MatrixB,");printf("%d*%d:\n",COL,ROW);for( i=0; i<COL; i++ ){for( j=0; j<ROW; j++ ){printf("%8d",matrixB[i][j]);}printf("\n");}printf("\n Press Any Key to Quit... \n");getch();}求解二维数组的最大/最小元素#define MAXN 20int a[MAXN][MAXN];main(){int min, /* 存储最小值*/max; /* 存储最大值*/int row,col,n;clrscr();printf("Please input the order of the matrix:\n");/* 输入方阵的阶次*/ scanf("%d",&n);printf("Please input the elements of the matrix,\n from a[0][0] to a[%d][%d]:\n",n-1,n-1);for(row=0;row<n;row++)for(col=0;col<n;col++)scanf("%d",&a[row][col]);for(min=a[0][0],row=0;row<n;row++){/* 从每行选出最大数*/for(max=a[row][0],col=1;col<n;col++)/*从row行选出最大数*/if(max<a[row][col])max=a[row][col];if(min>max)/* 保存至row行的最小数*/min=max;}printf("The minimum of maximum number is %d\n",min);for(max=a[0][0],row=0;row<n;row++){/* 每行选出最小数*/for(min=a[row][0],col=1;col<n;col++)/* 从row行选出最小数*/ if(min>a[row][col])min=a[row][col];if(max<min)/*保存至row行的最大数*/max=min;}printf("The maximum of minimum numbers is %d\n",max);printf("\nPress any key to quit...\n");getch();}利用数组求前n个质数#define N 50main(){int primes[N];int pc,m,k; clrscr();printf("\n The first %d prime numbers are:\n",N);primes[0]=2;/*2是第一个质数*/pc =1;/*已有第一个质数*/m =3;/*被测试的数从3开始*/while(pc<N){/*调整m使它为下一个质数*/k=0;while(primes[k]*primes[k]<=m)if(m%primes[k]==0){/*m是合数*/m+=2;/*让m取下一个奇数*/k=1;/*不必用primes[0]=2去测试m,所以k从一开始*/}elsek++;/*继续用下一个质数去测试*/primes[pc++]=m;m+=2;/*除2外,其余质数均是奇数*/}/*输出primes[0]至primes[pc-1]*/for(k=0;k<pc;k++)printf("%4d",primes[k]);printf("\n\n Press any key to quit...\n ");getch();}编制万年历#include "stdio.h"long int f(int year,int month){/*f(年,月)=年-1,如月<3;否则,f(年,月)=年*/if(month<3) return year-1;else return year;} long int g(int month){/*g(月)=月+13,如月<3;否则,g(月)=月+1*/if(month<3) return month+13;else return month+1;} long int n(int year,int month,int day){/*N=1461*f(年、月)/4+153*g(月)/5+日*/return 1461L*f(year,month)/4+153L*g(month)/5+day;} int w(int year,int month,int day){/*w=(N-621049)%7(0<=w<7)*/return(int)((n(year,month,day)%7-621049L%7+7)%7);} int date[12][6][7];int day_tbl[ ][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};main(){int sw,leap,i,j,k,wd,day;int year;/*年*/char title[]="SUN MON TUE WED THU FRI SAT";clrscr();printf("Please input the year whose calendar you want to know: ");/*输入年*/scanf("%d%*c",&year);/*输入年份值和掠过值后的回车*/sw=w(year,1,1);leap=year%4==0&&year%100||year%400==0;/*判闰年*/for(i=0;i<12;i++)for(j=0;j<6;j++)for(k=0;k<7;k++)date[i][j][k]=0;/*日期表置0*/for(i=0;i<12;i++)/*一年十二个月*/for(wd=0,day=1;day<=day_tbl[leap][i];day++){/*将第i+1月的日期填入日期表*/date[i][wd][sw]=day;sw=++sw%7;/*每星期七天,以0至6计数*/if(sw==0) wd++;/*日期表每七天一行,星期天开始新的一行*/} printf("\n|==================The Calendar of Year %d=====================|\n|",year);for(i=0;i<6;i++){/*先测算第i+1月和第i+7月的最大星期数*/for(wd=0,k=0;k<7;k++)/*日期表的第六行有日期,则wd!=0*/wd+=date[i][5][k]+date[i+6][5][k];wd=wd?6:5;printf("%2d %s %2d %s |\n|",i+1,title,i+7,title);for(j=0;j<wd;j++){printf(" ");/*输出四个空白符*//*左栏为第i+1月,右栏为第i+7月*/for(k=0;k<7;k++)if(date[i][j][k])printf("%4d",date[i][j][k]);else printf(" ");printf(" ");/*输出十个空白符*/for(k=0;k<7;k++)if(date[i+6][j][k])printf("%4d",date[i+6][j][k]);else printf(" ");printf(" |\n|");}/*scanf("%*c");/*键入回车输出下一个月的日历*/}puts("=================================================================|") ;puts("\n Press any key to quit...");getch();}对数组元素排序rest(int a[], int n){int i,low,high,t; for(i=0,low=0,high=n-1;i<=high;) {if(a[i]>0){/*a[i]与a[high]交换,随之high减1*/t=a[i];a[i]=a[high];a[high]=t;high--;}else if(a[i]==0)i++; /* 掠过该元素*/else{/*a[i]与a[low]交换,随之low增1, i增1*/t=a[i];a[i]=a[low];a[low]=t;low++;i++;}}}int s[]={8,4,0,-1,6,0,-5};main(){int i;clrscr();printf("\n The arry before rest is:\n");for(i=0;i<sizeof(s)/sizeof(s[0]);i++)printf("%4d",s[i]);rest(s,sizeof(s)/sizeof(s[0]));printf("\n The arry after rest is:\n");for(i=0;i<sizeof(s)/sizeof(s[0]);i++)printf("%4d",s[i]);printf("\n Press any key to quit...\n");getch();}任意进制数的转换/* 函数trans将无符号整数n翻译成d(2<=d<=16)进制表示的字符串s */ #define M sizeof(unsigned int)*8int trans(unsigned n, int d, char s[]){static char digits[] ="0123456789ABCDEF"; /* 十六进制数字的字符*/char buf[M+1];int j, i = M;if(d<2||d>16){s[0]='\0'; /* 不合理的进制,置s为空字符串*/return 0; /* 不合理的进制,函数返回0 */}buf[i]='\0';do{buf[--i]=digits[n%d]; /*译出最低位,对应字符存入对应工作数组中*/n/=d;}while(n);/* 将译出在工作数组中的字符串复制到s */for(j=0;(s[j]=buf[i])!='\0';j++,i++);/* 其中控制条件可简写成s[j]=buf[i] */return j;}/* 主函数用于测试函数trans() */main(){unsigned int num = 253;int scale[]={2,3,10,16,1};char str[33];int i;clrscr();for(i=0;i<sizeof(scale)/sizeof(scale[0]);i++){if(trans(num,scale[i],str))printf("%5d = %s(%d)\n",num,str,scale[i]);elseprintf("%5d => (%d) Error! \n",num,scale[i]);}printf("\n Press any key to quit...\n");getch();}判断回文数/* 函数circle用于判断正整数n的d进制数表示形式是否是回文数*/ int circle(int n, int d){int s=0,m=n;while(m){s=s*d+m%d;m/=d;}return s==n;}/* main函数用于测试circle函数*/int num[]={232,27,851};int scale[]={2,10,16};main(){int i,j;clrscr();for(i=0;i<sizeof(num)/sizeof(num[0]);i++)for(j=0;j<sizeof(scale)/sizeof(scale[0]);j++)if(circle(num[i],scale[j]))printf("%d -> (%d) is a Circle Number!\n",num[i],scale[j]);elseprintf("%d -> (%d) is not a Circle Number!\n",num[i],scale[j]);printf("\n Press any key to quit...\n");getch();}求解钢材切割的最佳订单#include <stdio.h>#define N 20#define DELTA 2int bestlen;int bestsele[N];int sele[N];int n;int orderlen[N];int total;main(){int i;clrscr();printf("\n Please enter total length of the steel:\n");/* 输入钢材总长*/scanf("%d",&total);printf("\n Please enter number of order:\n"); /* 输入定单数*/ scanf("%d",&n);printf("\n Please enter the orders:\n"); /* 输入各定单*/for(i=0;i<n;i++)scanf("%d",&orderlen[i]);bestlen=0; /*最佳解用料的初值*/for(i=0;i<n;i++)sele[i]=bestsele[i]=0; /*置当前选择和最佳选择初值*/try(); /* 调用函数求解*/for(i=0;i<n;i++) /* 输出结果*/if(bestsele[i])printf("order %d length = %d\n",i+1,orderlen[i]);printf("\n Press any key to quit...");getch();}try(){int i,len;for(len=i=0;i<n;i++) /* 求当前选中的用料量*/if(sele[i])len+=orderlen[i]+DELTA;if(len-DELTA<=total) /* 注意最后一段可能不需要切割*/{if(bestlen < len){/* 找到一个更好的解*/bestlen = len;for(i=0;i<n;i++)bestsele[i]=sele[i];}for(i=0;i<n;i++) /* 对所有未选定单逐一作选中尝试循环*/if(!sele[i]){sele[i]=1; /* 做选中尝试*/try();sele[i]=0;}}}指向数组的指针main(){int x,y,z; /* 定义三个int型变量*/int *xp = &x, /* 定义指针变量xp,并赋值为x的地址,使xp指向x */ *yp = &y, /* 定义指针变量yp,并赋值为y的地址,使yp指向y */*zp = &z; /* 定义指针变量zp,并赋值为z的地址,使zp指向z */int t;printf("\nPlease input x,y,z:\n");scanf("%d%d%d",xp,yp,zp); /* 通过变量的指针,为变量输入值*/ if(*xp>*yp) /* 通过指向变量的指针引用变量的值*/{t=*xp; /* 通过指向变量的指针引用变量的值*/*xp=*yp;/* 通过指向变量x的指针xp,引用变量x的值*/*yp=t; /* 通过指向变量y的指针yp,引用变量y的值*/}if(*xp>*zp) /* 通过指向变量的指针,引用变量的值*/{t=*xp; /* 通过指向变量x的指针xp,引用变量x的值*/*xp=*zp;/* 通过指向变量x的指针xp,引用变量x的值*/*zp=t; /* 通过指向变量z的指针zp,引用变量z的值*/}if(*yp>*zp) /* 通过指向变量的指针,引用变量的值*/{t=*yp; /* 通过指向变量的指针,引用变量的值*/*yp=*zp;/* 通过指向变量y的指针yp,引用变量y的值*/*zp=t;/* 通过指向变量z的指针zp,引用变量z的值*/}printf("x = %d\ty = %d\tz = %d\n",x,y,z);printf("\nPress any key to quit...\n");getch();}阿拉伯数字转换为罗马数字#include <stdio.h>#define ROWS 4#define COLS 4int nums[ROWS][COLS]={{1000,1000,1000,1000},{900,500,400,100},{90,50,40,10},{9,5,4,1}};char *roms[ROWS][COLS]={{"m","m","m","m"}, {"cm","d","cd","c"},{"xc","l","xl","x"},{"ix","v","iv","i"}}; main(int argc,char *argv[ ]){int low,high;char roman[25]; if(argc<2){ printf("Usage:roman decimal_number\n");/*运行程序需带整数参数*/}high=low=atoi(argv[1]);/*将第一个参数转换成整数*/checknum(low);if(argc>2){/*带两个参数*/high=atoi(argv[2]);checknum(high);if(low>high){low=high;high=atoi(argv[1]);}}elselow=1;for(;low<=high;low++){to_roman(low,roman);printf("%d\t%s\n",low,roman);}} checknum(int val)/*检查参数合理性*/{if(val<1||val>9999){printf("The number must be in range 1..9999.\n");exit(0);}}to_roman(int decimal,char roman[ ])/*将整数转换成罗马数字表示*/ {int power,index;roman[0]='\0';for(power=0;power<ROWS;power++)for(index=0;index<COLS;index++)while(decimal>=nums[power][index]){strcat(roman,roms[power][index]);decimal-=nums[power][index];}}通讯录的输入输出#include <stdio.h>#define ZIPLEN 10#define PHONLEN 15/*struct addr类型定义*/ struct addr{char *name;/*姓名*/char *address;/*地址*/char zip[ZIPLEN];/*邮政编码*/char phone[PHONLEN];/*电话号码*/}; main()/*本主函数示意上述输入输出函数的用法*/{struct addr p[100];int i,j;clrscr();for(i=0;readaddr(p+i);i++);for(j=0;j<i;j++) writeaddr(p+j);puts("\n Press any key to quit...");getch();} /* 函数readaddr用于输入一个通信录函数*/int readaddr(struct addr *dpt){int len;char buf[120];/*输入字符串的缓冲区*/ printf("\nPlease input theName:\n");/*输入姓名*/if(scanf("%s",buf)==1){len=strlen(buf);dpt->name=(char *)malloc(len+1);/*申请存贮姓名的空间*/ strcpy(dpt->name,buf);}else return 0;/*Ctrl+Z结束输入*/printf("Please input the Address:\n");/*输入地址*/if(scanf("%s",buf)==1){len=strlen(buf);dpt->address=(char *)malloc(len+1);/*申请存贮地址的空间*/ strcpy(dpt->address,buf);}else{/*Ctrl+Z结束输入*/free(dpt->name);/*释放存贮姓名的空间*/return 0;}printf("Please input the Zip code:\n");/*输入邮编*/if(scanf("%s",buf)==1)strncpy(dpt->zip,buf,ZIPLEN-1);else{free(dpt->name);/*释放存贮姓名的空间*/free(dpt->address);/*释放存贮地址的空间*/return 0;/*Ctrl+Z结束输入*/}printf("Please input the Phone number:\n");/*输入电话号码*/ if(scanf("%s",buf)==1)strncpy(dpt->phone,buf,PHONLEN-1);else{free(dpt->name);free(dpt->address);return 0;/*Ctrl+Z结束输入*/}return 1;} /* 函数writeaddr用于输出通讯录*/int writeaddr(struct addr*dpt){printf("Name : %s\n", dpt->name);/*输出姓名*/printf("Address : %s\n", dpt->address);/*输出地址*/printf("Zip : %s\n", dpt->zip);/*输出邮编*/printf("Phone : %s\n\n", dpt->phone);/*输出电话号码*/}扑克牌的结构表示enum suits{CLUBS,DIAMONDS,HEARTS,SPADES}; struct card{enum suits suit;char value[3];};struct card deck[52];char cardval[][3]= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};char suitsname[][9]={"CLUBS","DIAMONDS","HEARTS","SPADES"}; main() {int i,j;enum suits s;clrscr();for(i=0;i<=12;i++)for(s=CLUBS;s<=SPADES;s++){j=i*4+s;deck[j].suit=s;strcpy(deck[j].value,cardval[i]);}for(j=0;j<52;j++)printf("(%s%3s)%c",suitsname[deck[j].suit],deck[j].value,j%4==3?'\n':'\t');puts("\nPress any key to quit...");getch();}用“结构”统计学生成绩#include <stdio.h>#define N 200#define SCORES 5#define NUMLEN 10struct std_type{char no[NUMLEN];/*学号*/char *name;/*名字符串指针*/int scores[SCORES];/*五门功课的成绩*/};struct std_type students[N];int order[N];int total[N]; /*[函数]输入一个学生信息函数*/int readastu(struct std_type *spt){int len,j;char buf[120];/*输入字符串的缓冲区*/ printf("\nNumber : ");/*输入学号*/if(scanf("%s",buf)==1)strncpy(spt->no,buf,NUMLEN-1);elsereturn 0;/*Ctrl+Z结束输入*/printf("Name : ");/*输入姓名*/if(scanf("%s",buf)==1){len=strlen(buf);spt->name=(char *)malloc(len+1);/*申请存贮姓名的空间*/ strcpy(spt->name,buf);}else return 0;/*Ctrl+Z结束输入*/printf("Scores : ");/*输入成绩*/for(j=0;j<SCORES;j++)if(scanf("%d",spt->scores+j)!=1)break;if(j==0)/*一个成绩也未输入*/{free(spt->name);/*释放存贮姓名的空间*/return 0;}for(;j<SCORES;j++)/*少数未输入的成绩用0分代之*/ spt->scores[j]=0;return 1;} /*[函数]输出一个学生信息的函数*/int writeastu(struct std_type *spt){int i; printf("Number : %s\n",spt->no);/*输出学号*/printf("Name : %s\n",spt->name);/*输出姓名*/printf("Scores : ");/*输出成绩*/for(i=0;i<SCORES;i++)printf("%4d",spt->scores[i]);printf("\n\n");} main(){int n,i,j,t; clrscr();for(n=0;readastu(students+n);n++);/*采用冒泡法对学生信息数组排序*/for(i=0;i<n;i++){order[i]=i;/*预置第i个输入的学生*/for(t=0,j=0;j<SCORES;j++)/*求第i个学生的总分*/t+=students[i].scores[j];total[i]=t;}/*冒泡排序*/for(i=0;i<n-1;i++)/*共扫视n-1遍*/for(j=0;j<n-1-i;j++)if(total[order[j]]<total[order[j+1]]){/*交换名次*/t=order[j];order[j]=order[j+1];order[j+1]=t;}for(j=0;j<n;j++)/*输出*/writeastu(students+order[j]);printf("\n Press any key to quit...\n");getch();}报数游戏#include <stdio.h>struct ele{int no;struct ele *link;}main(){int n,m,i;struct ele *h,*u,*p;clrscr();printf("Please input n&m:\n");scanf("%d%d",&n,&m);/*输入n和m*/h=u=(struct ele *)malloc(sizeof(struct ele));/*形成首表元*/ h->no=1;for(i=2;i<=n;i++)/*形成其余的n-1个表元*/{u->link=(struct ele *)malloc(sizeof(struct ele));u=u->link;u->no=i;/*第i个表元置编号i*/}u->link=h;/*末表元后继首表元,形成环*/puts("\nThe numbers of who will quit the cycle in turn are:"); while(n){for(i=1;i<m;i++)/*掠过m-1个表元*/u=u->link;p=u->link;/*p指向第m个表元*/u->link=p->link;/*第m个表元从环中脱钩*/printf("%4d",p->no);free(p);/*释放第m个表元占用的空间*/n--;}printf("\n\n Press any key to quit...\n");getch();}学生成绩管理程序/*学生成绩管理程序编制一个统计学生考试分数的管理程序。
经典 C 语言源代码1、(1)某年某月某日是星期几#include<stdio.h>int main(){int year, month, day;while (scanf_s("%d%d%d", &year, &month, &day) != EOF){if (month == 1 || month == 2)// 判断month 是否为1 或2 {year--;month += 12;}int c = year / 100;int y = year - c * 100;int week = (c / 4) - 2 * c + (y + y / 4) + (13 * (month + 1) / 5) day - 1;while (week<0) { week += 7; }week %= 7;switch (week){case 1:printf("Monday\n"); break;case 2:printf("Tuesday\n"); break;case 3:printf("Wednesday\n"); break;case 4:printf("Thursday\n"); break;case 5:printf("Friday\n"); break;case 6:printf("Saturday\n"); break;case 0:printf("Sunday\n"); break;}}return 0;}1、(2)某年某月某日是第几天(一维数组)#include "stdio.h" void main() {int i, flag, year, month, day, dayth;int month_day[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };printf(" 请输入年/月/日:\n");scanf_s("%d/%d/%d", &year, &month, &day);dayth = day;flag = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);if (flag)month_day[2] = 29;for (i = 1; i < month; i++)dayth = dayth + month_day[i];Prin tf("%d∕%d∕%d 是第%d 天\n", year, mon th, day, dayth);}2、30 个数中找最小的数及其位置#include "stdio.h"# define SIZE 30void main() {int i;float data[SIZE];int min;Printf("请输入%d个浮点数:∖n",SIZE);for (i = 0; i < SIZE; i++) {//scan f_s("%f", & data[i]); data[i] = rand() % 30 + 1;Printf("%f、", data[i]);}min = 0;for (i = 1; i < SIZE; i++) { if(data[i] < data[min])min = i;}Printf("最小值是%5.2f位置是%5d∖n", data[min], min); }3、30 个数从小到大排序(1)#include "stdio.h"# define SIZE 30void main() {int i,j;float data[SIZE],temP;int min;Printf("请输入%d个整型数:∖n",SIZE);for (i = 0; i < SIZE; i++) {scanf_s("%f", &data[i]);}for (i = 0; i < SIZE; i++) { min = i;for (j = i + 1; j < SIZE; j++)if (data[j] < data[min])temp = data[min];min = j;data[min] = data[i]; data[i] = temp;}printf("\n 排序后的结果是:\n");for (i = 0; i < SIZE; i++) printf("%5.2f", data[i]);}(2)模块化程序(数组名作为函数参数) #include "stdio.h"# define SIZE 5void accept_array(float a[], int size); void sort(float a[], int size); void show_array(float a[], int size);void main() {float score[SIZE]; accept_array(score, SIZE);printf(" 排序前:"); show_array(score, SIZE);sort(score, SIZE);printf(" 排序后:"); show_array(score, SIZE); }void accept_array(float a[], int size) {int i;printf(" 请输入%d 个分数:", size);for (i = 0; i < size; i++) scanf_s("%f", &a[i]);}void show_array(float a[], int size) {int i;for (i = 0; i < size; i++)printf(" %5.2f", a[i]);printf("\n");}void sort(float a[],int size) {int i, min, j;float temp;for (i = 0; i < SIZE; i++) {min = i;for (j = i + 1; j < SIZE; j++)if (a[j] < a[min])min = j;}temp = a[min]; a[min] = a[i]; a[i] = temp;}}4、( 1)指针加减:#include "stdio.h"#define SIZE 10 void main() {int a[SIZE] = { 1,2,3,4,5,6,7,8,9,10 }; int *pa, i;pa = &a[0];//pa=a; printf("\n");for (i = 0; i < SIZE; i++) { printf(" %d", *pa);//printf(" %d", *(pa+1)); pa++;(2)指针比较:#include "stdio.h"#define SIZE 10void main() {int a[SIZE] = { 1,2,3,4,5,6,7,8,9,10 };int *pa, i;int *qa;pa = qa = &a[0];printf(" 请输入%d 整型数:",SIZE); for (; pa < qa + SIZE;pa++) scanf_s("%d", pa);for (pa--; qa <= pa; pa--)printf(" %d", *pa);}5、两字符串相连:#include "stdio.h"#include "string.h"void str_cat(char str1[], char str2[]); void main() {int i, j;char str1[160];char str2[80];printf(" 请输入第一个字符串:"); gets(str1);printf(" 请输入第二个字符串:"); gets(str2);str_cat(str1, str2);puts(str1);}void str_cat(char str1[], char str2[]) { int i, j;i = 0;while (str1[i] != '\0')i++;j = 0;while (str2[j] != '\0') {str1[i] = str2[j]; i++; j++;}str1[i] = '\0';6、二维数组( a,b 转置) #include "stdio.h" void main() {int i, j, b[2][3];int a[3][2] = { {1,2},{3,4},{5,6} };for (i = 0; i < 2; i++) {}for (j = 0; j < 3; j++)b[i][j] = a[j][i];}printf("\na:\n");for (i = 0; i < 3; i++) { for (j = 0; j < 2; j++) printf("%5d", a[i][j]);printf("\n");}printf("\nb:\n");for(i = 0; i < 2; i++) { for (j = 0; j < 3; j++) printf("%5d", b[i][j]);printf("\n");}7、输入一个二维数组并输出(指针)#include "stdio.h"void main() {int x[2][3];int i, j;for (i = 0; i < 2; i++)for (j = 0; j < 3; j++)scanf_s("%d", *(x + i) + j); putchar('\n');for (i = 0; i < 2; i++){for (j = 0; j < 3; j++)printf("%d ", *(*(x + i) + j));putchar('\n');}}8、冒泡法排序一个数组#include "stdio.h"#define size 10 void maopao(int a[]); void main() { int a[10];int i;printf(" 请输入10 个整数:\n"); for (i = 0; i < 10; i++) scanf_s("%d", &a[i]); maopao(a);}void maopao(int a[]) {int i, j, temp;for (i = 0; i < 9; i++) {// 进行9 轮排序for (j = 0; j < 9 - i; j++)// 每轮进行9-i 次交换{if (a[j] > a[j + 1]){ temp = a[j];a[j] = a[j + 1];// 大的沉底,小的上浮a[j + 1] = temp;}}}printf(" 排序结果:\n"); for (i = 0; i < 10; i++) printf("%4d", a[i]); }9、两数组A,B,要求A<B,如A :4,7,9B :1,3,5,8,9变换后A :1,3,5B :4,7,8,9,9#include <stdio.h>VOid ReArranger(int* A, int* B, int m, int n) //A 和B是各有m 个和n 个整数的非降序数组,本算法将B 数组元素逐个插入到A 中,使A 中各元素均不大于B 中各元素,且两数组仍保持非降序排列。
c语言入门源代码C语言作为一种非常流行和广泛应用的编程语言,在计算机科学领域发挥了重要作用。
掌握C语言编程的基本知识和技能是初学者的首要任务。
本文将为你提供一些C语言入门的源代码示例,帮助你更好地理解和掌握C语言编程。
代码示例一:Hello World#include <stdio.h>int main() {printf("Hello World!\n");return 0;}这是一个经典的C语言程序,用于向屏幕输出"Hello World!"。
在C 语言中,使用printf函数来实现输出操作。
其中,"\n"表示换行符,用于使输出在新的一行显示。
代码示例二:求和#include <stdio.h>int main() {int num1, num2, sum;printf("请输入两个整数:\n");scanf("%d%d", &num1, &num2);sum = num1 + num2;printf("两个数的和为:%d\n", sum);return 0;}这段代码演示了如何实现两个整数的求和。
首先,使用printf函数提示用户输入两个整数。
接下来,使用scanf函数从用户处获取输入的整数值,并将它们分别赋给num1和num2变量。
然后,将num1和num2变量相加得到sum变量的值,并使用printf函数输出结果。
代码示例三:判断奇偶数#include <stdio.h>int main() {int num;printf("请输入一个整数:\n");scanf("%d", &num);if(num % 2 == 0) {printf("%d是偶数。
\n", num);}else {printf("%d是奇数。
c语言windows编译过程
在Windows操作系统上,使用C语言进行编译的过程主要包括以下几个步骤:
1. 编写源代码:使用文本编辑器编写C语言源代码文件,扩展名为.c。
2. 预处理:使用预处理器对源代码进行处理,包括宏展开、头文件包含、条件编译等。
预处理器生成的文件通常具有扩展名为.i。
3. 编译:使用编译器将预处理后的源代码翻译成汇编代码。
编译器生成的文件通常具有扩展名为.asm。
4. 汇编:使用汇编器将汇编代码翻译成机器码。
汇编器生成的文件通常具有扩展名为.obj。
5. 链接:使用链接器将多个目标文件(.obj)以及所需的库文件进行链接,生成可执行文件。
链接器生成的文件通常具有扩展名为.exe。
在Windows上,可以使用多种工具进行C语言的编译,最常用的是Microsoft Visual Studio(包含了Visual C++编译器)和MinGW (包含了GCC编译器)。
在使用Visual Studio进行编译时,可以通过IDE界面进行操作,也可以使用命令行工具(如cl.exe)执行编译命令。
在使用MinGW进行编译时,一般使用命令行工具(如gcc.exe)执行编译命令。
编译命令的具体参数可以根据需要进行设置,如指定源代码文件、输出文件名、链接的库文件等。
需要注意的是,编译过程中可能会出现一些错误或警告信息,需要根据编译器的提示进行修改和调试。
经典C语言源代码1、(1)某年某月某日是星期几#include<stdio.h>int main(){int year, month, day;while (scanf_s(%d%d%d, &year, &month, &day) != EOF) {if (month == 1 || month == 2)//判断month是否为1或2 {year--;month += 12;}int c = year / 100;int y = year - c * 100;int week = (c / 4) - 2 * c + (y + y / 4) + (13 * (month + 1) / 5) + day - 1;while (week<0) { week += 7; }week %= 7;switch (week){case 1:printf(Monday\n); break;case 2:printf(Tuesday\n); break;case 3:printf(Wednesday\n); break;case 4:printf(Thursday\n); break;case 5:printf(Friday\n); break;case 6:printf(Saturday\n); break;case 0:printf(Sunday\n); break;}}return 0;}1、(2)某年某月某日是第几天(一维数组)#include stdio.hvoid main() {int i, flag, year, month, day, dayth;int month_day[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; 牰湩晴尨请输入年/ 月/日:\n);scanf_s(%d/%d/%d, &year, &month, &day);dayth = day;flag = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); if (flag)month_day[2] = 29;for (i = 1; i < month; i++)dayth = dayth + month_day[i];printf(%d/%d/%d是第%d天\n, year, month, day, dayth);}2、30个数中找最小的数及其位置#include stdio.h# define SIZE 30void main() {int i;float data[SIZE];int min;牰湩晴尨请输入%d个浮点数:\n,SIZE);for (i = 0; i < SIZE; i++) {//scanf_s(%f, &data[i]);data[i] = rand() % 30 + 1;, data[i]);、printf(%f}min = 0;for (i = 1; i < SIZE; i++) {if (data[i] < data[min])min = i;}牰湩晴尨最小值是%5.2f,位置是]\n, data[min], min); }3、30个数从小到大排序(1)#include stdio.h# define SIZE 30void main() {int i,j;float data[SIZE],temp;int min;牰湩晴尨请输入%d个整型数:\n,SIZE); for (i = 0; i < SIZE; i++) {scanf_s(%f, &data[i]);}for (i = 0; i < SIZE; i++) {min = i;for (j = i + 1; j < SIZE; j++)if (data[j] < data[min])min = j;temp = data[min];data[min] = data[i];data[i] = temp;}printf(\排序后的结果是:\n);for (i = 0; i < SIZE; i++)printf(%5.2f, data[i]);}(2)模块化程序(数组名作为函数参数)#include stdio.h# define SIZE 5void accept_array(float a[], int size);void sort(float a[], int size);void show_array(float a[], int size);void main() {float score[SIZE];accept_array(score, SIZE);牰湩晴尨排序前:);show_array(score, SIZE);sort(score, SIZE);牰湩晴尨排序后:);show_array(score, SIZE);}void accept_array(float a[], int size) { int i;牰湩晴尨请输入%d个分数:, size); for (i = 0; i < size; i++)scanf_s(%f, &a[i]);}void show_array(float a[], int size) { int i;for (i = 0; i < size; i++)%5.2f, a[i]); printf(printf(\);}void sort(float a[],int size) {int i, min, j;float temp;for (i = 0; i < SIZE; i++) {min = i;for (j = i + 1; j < SIZE; j++)if (a[j] < a[min])min = j;temp = a[min];a[min] = a[i];a[i] = temp;}}4、(1)指针加减:#include stdio.h#define SIZE 10void main() {int a[SIZE] = { 1,2,3,4,5,6,7,8,9,10 };int *pa, i;pa = &a[0];//pa=a;printf(\);for (i = 0; i < SIZE; i++) {printf( %d, *pa);//printf( %d, *(pa+1));pa++;}}(2)指针比较:#include stdio.h#define SIZE 10void main() {int a[SIZE] = { 1,2,3,4,5,6,7,8,9,10 }; int *pa, i;int *qa;pa = qa = &a[0];牰湩晴尨请输入%d整型数:,SIZE); for (; pa < qa + SIZE; pa++)scanf_s(%d, pa);for (pa--; qa <= pa; pa--)%d, *pa);printf(}5、两字符串相连:#include stdio.h #include string.hvoid str_cat(char str1[], char str2[]); void main() {int i, j;char str1[160];char str2[80];牰湩晴尨请输入第一个字符串:); gets(str1);牰湩晴尨请输入第二个字符串:);gets(str2);str_cat(str1, str2);puts(str1);}void str_cat(char str1[], char str2[]) { int i, j;i = 0;while (str1[i] != '\0')i++;j = 0;while (str2[j] != '\0') {str1[i] = str2[j];i++; j++;}str1[i] = '\0';}转置)a,b、二维数组(6#include stdio.h void main() {int i, j, b[2][3];6} };for (i = 0; i < 2; i++) {for (j = 0; j < 3; j++)b[i][j] = a[j][i];}printf(\a:\n);for (i = 0; i < 3; i++) {for (j = 0; j < 2; j++)printf(], a[i][j]);printf(\);}printf(\b:\n);for(i = 0; i < 2; i++) {for (j = 0; j < 3; j++)printf(], b[i][j]);printf(\);}}7、输入一个二维数组并输出(指针)#include stdio.hvoid main() {int x[2][3];int i, j;for (i = 0; i < 2; i++)for (j = 0; j < 3; j++)scanf_s(%d, *(x + i) + j);putchar('\n');for (i = 0; i < 2; i++){for (j = 0; j < 3; j++)printf(%d , *(*(x + i) + j));putchar('\n');}}8、冒泡法排序一个数组#include stdio.h #define size 10void maopao(int a[]);void main() {int a[10];int i;牰湩晴尨请输入10 个整数:\n);for (i = 0; i < 10; i++)scanf_s(%d, &a[i]);maopao(a);}void maopao(int a[]) {int i, j, temp;for (i = 0; i < 9; i++) {//进行9轮排序9-ifor (j = 0; j < 9 - i; j++)//每轮进行次交换{if (a[j] > a[j + 1]){temp = a[j];a[j] = a[j + 1];//大的沉底,小的上浮a[j + 1] = temp;}}}牰湩晴尨排序结果:\n);for (i = 0; i < 10; i++)printf(M, a[i]);}9、两数组A,B,要求A<B,如A:4,7,9B:1,3,5,8,9变换后A:1,3,5B:4,7,8,9,9#include <stdio.h>void ReArranger(int* A, int* B, int m, int n) //A和B是各有m个和n 个整数的非降序数组,本算法将B数组元素逐个插入到A中,使A中各元素均不大于B中各元素,且两数组仍保持非降序排列。
#include<stdio.h>main(){float pi=3.14159265,r;printf("enter radius:\n");scanf("%f",&r);printf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r);system("pause");}#include<stdio.h>main(){int m,n,x,y;printf("inputm,n:\n");scanf("%d%d",&m,&n);if (n%2==1){printf("error!! n bu shi ji shu!\n",n);return;}/*n在这里不能是奇数*/x=(4*m-n)/2;y=(n-2*m)/2;if((x>=0)&&(y>=0))printf("x=%d,y=%d\n",x,y); elseprintf("shu ru cuo wu!\n");getch();}#include<stdio.h>#include<math.h>#include<stdlib.h>main(){float a,b,C;printf("enter 3number(a,b,C):\n"); scanf("%.2f %.2f %.2f",&a,&b,&C); s=0.5*a*b*sinC;printf("s=%.2f\n",s);system("pause");}#include<stdio.h>main(){int ds,nl,yf;char c;printf("shu ru ds;\n");scanf("%d",&ds);if (ds<14||ds>1184){printf("input error!press any end...\n"); scanf("%c",&c);exit(0);}/*输入的数字必须是-14~1184之间*/nl=(ds+115)%100;yf=(ds+115)/100;printf("nl=%d,yf=%d\n",nl,yf);system("pause");}#include<stdio.h>#include<string.h>main(){char s1[100],s2[100],s3[100],s4[100]; printf("input a string:\n");gets(s1);strcpy(s1,s2);strcat(s1,"--------------");strcpy(s3,strcat(s1,"--------------"));strcat(strcpy(s3,strcat(s1,"--------------")),s2);strcpy(s4,strcat(strcpy(s3,strcat(s1,"--------------")),s2)); puts(s4);system("pasue");getch();}#include<stdio.h>#include<math.h>main(){float x1,x2,a,b,c;printf("input 3 number(a,b,c):\n");scanf("%f%f%f",&a,&b,&c);x1=(-b+sqrt(b*b-4*a*c))/(2*a);x2=(-b-sqrt(b*b-4*a*c))/(2*a);printf("x1=%f,X2=%f\n",x1,x2);system("pause");getch();}#include<stdio.h>main(){int a,b,c,t;printf("input 3 number:\n");scanf("%d%d%d",&a,&b,&c);if(a>b){t=a;a=b,b=t;}if(a>c){t=a;a=c;c=t;}if(b>c){t=b;b=c;c=t;}printf("1:%d,2:%d,3:%d\n",a,b,c);system("pause");}#include<stdio.h>#include<stdlib.h>#include<conio.h>main(){float pi=3.14159265,r;textbackground(YELLOW);/* 设置背景色为黄色,注意颜色应该大写,可更改 */ textcolor(RED); /* 设置文件颜色为红色,可更改 */clrscr(); /* 清屏,使设置生效 */printf("enter radius:");scanf("%f",&r);if(r<0)printf("Enter Error!\n");elseprintf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r); system("pause");/* 暂停,按任一键继续 */#include<stdio.h>#include<math.h>#include<conio.h>main(){float a,b,c,delt,x1,x2,p,q;textcolor(YELLOW);clrscr();printf("Input a b c:\n");scanf("%f%f%f",&a,&b,&c);if(a==0){printf("It's not a quadratic equation!\n");system("pause");return;}delt=b*b-4*a*c;if(delt>=0){x1=(-b+sqrt(delt))/(2*a);x2=(-b-sqrt(delt))/(2*a);printf("x1=%.3f x2=%.4f\n",x1,x2);}else{p=-b/(2*a);q=sqrt(-delt)/(2*a);printf("p=%.4fq=%.4f\n",p,q);}system("pause");getch();}}#include<stdio.h>main(){float pi=3.14159265,r;int k=0;while(k<=3){printf("enter radius:\n");scanf("%f",&r);printf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r); printf("press any key to continue\npress esc to exit."); k++;}}#include<stdio.h>#include<stdlib.h>#include<time.h>#include<conio.h>main(){int a,b,c,oper;long limit,i=0;char char1;textcolor(GREEN);/*设置字体颜色为绿色*/clrscr(); /*清屏,是设置生效*/while(i<=3){printf("qing xuan ze jia huo jian(1or2,1:+,2:-\n"); scanf("%d",&oper);printf("Enter max (<10000):\n");scanf("%ld",&limit);srand((unsigned long)time(0));a=rand()*limit/RAND_MAX;b=rand()*limit/RAND_MAX;while((a<b)&&(oper==2)){a=rand()*limit/RAND_MAX;b=rand()*limit/RAND_MAX;}char1=(oper==2?'-':'+');printf("%d%c%d\n",a,char1,b);scanf("%d",&c);if((oper==2&&a-b==c)||(oper!=2&&a+b==c)) printf("OK!You are very clever!\n"); elseprintf("The result is not correct!\n"); i++;}getch();}#include<stdio.h>#include<conio.h>main(){int y,i=0;textcolor(YELLOW);/*天下事无难易之分只有做与不做之别*/ textbackground(GREEN);clrscr();/*清屏,是设置生效*/while(i<=3){printf("Input year:\n");scanf("%d",&y);if(y%4==0){if(y%100==0){if(y%400==0)printf("y shi run nian!\n"); elseprintf("y bu shi run nian !\n"); }elseprintf("y shi run nian!\n");}elseprintf("y bu shi run nian!");i++;}getche();}#include<stdio.h>#include<time.h>#include<string.h>main(){int xz,wday1,hour1;struct tm *timeptr;time_t secsnow;char s1[30],s2[30],s3[30];printf("input whom do you say to?:\n"); scanf("%d",&xz);if(xz==1)strcpy(s1,"mother");else if(xz==2)strcpy(s1,"father");elsestrcpy(s1,"");time(&secsnow);timeptr=localtime(&secsnow);wday1=timeptr->tm_wday;if(wday1==6)strcpy(s2,"Happy saturday!");else if(wday1==0)strcpy(s2,"Happy sunday");elsestrcpy(s2,"");hour1=timeptr->tm_hour;if(hour1>=4&&hour1<=10)strcpy(s3,"Good morning!");else if(hour1>=17&&hour1<=22)strcpy(s3,"Good afternoon!");elsestrcpy(s3,"Good evening!");printf("%s%s%s",s1,s2,s3);getch();}#include<stdio.h>#include<conio.h>main(){int day,year,month,i=0;textbackground(BROWN);clrscr();while(i<=3){printf("Input 2 number:\n");scanf("%d%d",&year,&month);switch(month){case 1:case 3:case 5:case 7:case 8:case 10:case 12: day=31;break;case 4:case 6:case 11:day=30;break;case 2:day=28;if((year%4==0&&year%100!=0)||year%400==0)day=29;break;deflault:printf("Invalid month input!\n");return;}printf("There are%d days in %d.%d\n",day,year,month); i++;getch();}}。
经典C语言源代码1、(1)某年某月某日是星期几#include<>int main(){int year, month, day;while (scanf_s("%d%d%d", &year, &month, &day) != EOF){if (month == 1 || month == 2)d.%d", &start[0], &start[1], &start[2]);printf("请输入结束日期,如:\n");scanf_s("%d.%d.%d", &end[0], &end[1], &end[2]);int sum = 0;for (int mid = start[0]; mid < end[0]; mid++) {if ((mid % 400 == 0) || (mid % 4 == 0 && mid % 100 != 0)) { sum = sum + 366;}elsesum = sum + 365;}sum = sum - indexday(start[0],start[1],start[2]) +indexday(end[0],end[1],end[2]);printf("在%d.%d.%d-%d.%d.%d之间有%d天\n",start[0],start[1],start[2],end[0],end[1],end[2], sum);}int indexday(int year, int month, int day) {int i, flag, dayth;int month_day[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };dayth = day;flag = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);if (flag)month_day[2] = 29;for (i = 1; i < month; i++)dayth = dayth + month_day[i];return dayth;}18、递归求1*1+2*2+3*3+n*n#include ""long Element(int n) {if (n == 1)return 1 * 1;elsereturn Element(n - 1) + n*n; }void main() {int n;printf("请输入n的值:\n");scanf_s("%d", &n);printf("所求值为%d\n", Element(n));}19、最大公约数(辗转相除)#include<>void main() /* 辗转相除法求最大公约数*/{int m, n, a, b, t, c;printf("Input two integer numbers:\n");scanf_s("%d%d", &a, &b);m = a; n = b;while (b != 0) /* 余数不为0,继续相除,直到余数为0 */ {c = a%b; a = b; b = c;}printf("The largest common divisor:%d\n", a);printf("The least common multiple:%d\n", m*n / a); }20、杨辉三角#include<>void main(){int i, j, n, k;printf("Enter n:"); scanf_s("%d", &n);for (i = 1; i <= n; i++){k = 1;for (j = 1; j<i; j++){printf("%3d", k);k = k*(i - j) / j;}//每次要打印的下一个数等于前一个数乘以其所在行数和列数的差再除以其列数printf("%3d", k);printf("\n");}}21、约瑟夫#include <>void main(){int n, m, i, s=0;printf ("Enter n: m: ");scanf("%d%d", &n, &m);for (i=2; i<=n; i++) s=(s+m)%i;printf ("The winner is %d\n", s+1); }22、斐波拉契#include<>void main(){long f, f1, f2; int i, n;printf("Enter n : ");scanf_s("%d", &n);f1 = 1;f2 = 1;printf("%10d%10d", f1, f2);for (i = 1; i <= n; i++){f = f1 + f2;printf("%10d", f);f1 = f2; f2 = f;if (i % 10 == 0)printf("\n");}}23、海滩上有一堆桃子,五只猴子来分。
c语言源代码在计算机科学领域,C语言是一种被广泛应用的程序设计语言,它以其简洁、高效和可移植性而闻名。
本文将提供一些C语言的源代码示例,帮助读者更好地理解和应用这门编程语言。
1. Hello World程序#include <stdio.h>int main() {printf("Hello, World!");return 0;}上述代码是C语言中经典的Hello World程序。
它使用了stdio.h头文件,其中包含了用于输入输出的函数。
main()函数是程序的入口点,它执行printf函数并打印出"Hello, World!"的字符串。
最后,return语句表示程序正常结束。
2. 计算两个数的和#include <stdio.h>int main() {int num1, num2, sum;printf("请输入两个整数:");scanf("%d %d", &num1, &num2);sum = num1 + num2;printf("两个数的和为:%d", sum);return 0;}以上代码展示了如何用C语言编写一个简单的计算两个数的和的程序。
通过使用scanf函数,用户可以在程序运行时输入两个整数。
程序将这两个数相加,并使用printf函数打印出结果。
3. 判断一个数是否为质数#include <stdio.h>int main() {int num, i, isPrime = 1;printf("请输入一个正整数:");scanf("%d", &num);for (i = 2; i <= num / 2; ++i) {if (num % i == 0) {isPrime = 0;break;}}if (isPrime)printf("%d是质数", num);elseprintf("%d不是质数", num);return 0;}上述代码展示了如何用C语言编写一个判断一个数是否为质数的程序。
c语言源程序经过编译程序编译后所产生的文件扩展名在撰写这篇文章之前,让我们首先来深入探讨一下C语言源程序经过编译程序编译后所产生的文件扩展名。
C语言作为一种高级程序设计语言,其源代码需要通过编译器进行编译后才能生成可执行文件。
而在编译过程中,会产生一些特定的文件扩展名,我们将从简到繁地来讨论这个主题。
1. .c源文件我们需要了解的是C语言源程序文件的扩展名为.c。
在编写C语言程序时,我们通常将源代码保存为以.c为后缀的文件,比如hello.c、main.c等。
这些.c文件包含了完整的C语言代码,需要通过编译器将其转换为机器语言才能执行。
2. .obj目标文件在进行编译时,编译器会将.c文件编译生成目标文件,其文件扩展名通常为.obj。
目标文件是编译器输出的中间文件,包含了编译后的机器语言代码和一些符号表等信息,但还不能直接作为可执行文件运行。
3. .exe可执行文件经过连接器的处理,将目标文件连接生成可执行文件,其文件扩展名为.exe。
可执行文件包含了机器语言代码和连接器生成的一些其他信息,可以直接在操作系统中执行,完成程序的运行。
4. .o目标文件和.a库文件在Unix/Linux系统中,编译生成的目标文件通常以.o为扩展名,而库文件通常以.a为扩展名。
这与Windows系统的.obj和.exe略有不同,但本质相同,都是编译和连接生成的文件。
5. 综述C语言源程序经过编译程序编译后所产生的文件扩展名包括.c、.obj (.o)、.exe以及.a等。
这些文件扩展名代表了C语言程序经过编译、连接等阶段生成的不同类型文件,对于我们理解程序编译过程及调试程序都有着重要的意义。
6. 个人观点和理解个人认为,了解C语言源程序编译后所产生的文件扩展名是非常重要的,这有助于我们深入理解程序编译连接的过程,也有助于我们更好地进行程序调试和优化。
对于想要深入学习C语言的初学者来说,掌握这些知识也是基础中的基础。
通过本文的讨论,希望读者能对C语言源程序编译后所产生的文件扩展名有更深入的理解,从而在编程学习和实践中能够更加得心应手。
#include<stdio。
h〉#include<stdlib.h〉#include〈string。
h〉struct tushu{ /*图书结构体*/ char num[10]; /*编号*/char name[20];/*书名*/char writer[20];/*作者*/char press[20];/*出版社*/char kind[20];/*类别*/double time; /*时间*/double price;/*价格*/struct tushu *next;};struct stu /*学生结构体*/ {int snum;/*学号*/char mima[10]; /*密码*/struct stu *next;};FILE *fp; /*图书文件*/FILE *fp1; /*管理员信息文件*/ FILE *fp2; /*学生信息文件*/void menu();/*管理员主菜单(管理员进入对图书及学生信息进行管理操作)*/void xmenu();/*学生主菜单(学生进入可对图书,密码进行操作)*/void gfind(); /*管理员查询(管理员可按一定的方式查询图书)*/void xfind();/*学生查询(学生可按一定的方式查询图书)*/ void secret();/*管理员权限(管理员登陆所用,输入错误次数过多自动退出)*/void sort();/*排序(管理员可按一定的方式对图书进行排序,排序完之后可选择文件进行保存)*/void fprint(struct tushu *head);/*保存(可追加的保存,如添加可用)*/void fprint_(struct tushu *head);/*保存(可覆盖保存如修改,删除,排序后用)*/void hfprint(struct tushu *head); /*还书保存(还书成功后自动保存到文件)*/void jfprint_(struct tushu *head);/*借书保存(借书成功之后自动从图书馆删除)*/struct tushu * Input(); /*图书添加(可进行图书的添加)*/struct tushu *create(); /*从文件创建链表(从文件中读出信息,建立单链表)*/void gBrowse(struct tushu *head);/*管理员浏览(对图书进行遍历)*/void xBrowse(struct tushu *head);/*学生浏览(学生对图书进行遍历)*/void count(struct tushu *head); /*统计数量(管理员可对图书进行统计)*/void Findofname(struct tushu *head); /*按书名查找*/ void Findofwriter(struct tushu *head); /*按作者查找*/void Findofkind(struct tushu *head); /*按类别查找*/void xFindofname(struct tushu *head);/*学生按书名查找*/void xFindofwriter(struct tushu *head);/*学生按作者查找*/ void xFindofkind(struct tushu *head); /*学生按类别查找*/ void Sort_time(struct tushu * head); /*按时间排序(管理员按时间对图书进行排序,排序完之后可选择文件进行保存)*/ void Sort_price(struct tushu *head); /*按价格排序*/void Sort_num(struct tushu * head);/*按编号排序*/ void Delete(struct tushu * head,char m[15]);/*按编号删除(管理员可按编号删除图书)*/void Revise(struct tushu *head);/*修改(管理员可对图书进行修改,并选择是否保存)*/void borrow(struct tushu *head); /*借书*/void huanshu(); /*还书(学生借完书之后进行还书,若没有图书则不能借)*/void gxinxi(); /*管理员信息(有管理员的账号及密码,可进行修改)*/void xmima(struct stu *head1);/*学生密码修改(学生可对自己的密码进行修改)*/struct stu *xcreate();/*从文件创建学生信息(从文件读出学生信息,建立学生链表)*/void xsecret(struct stu *head1);/*学生权限(学生登陆所用)*/void menu() /*管理员主菜单(管理员进入对图书及学生信息进行管理操作)*/{int choice,n=0;struct tushu *head;struct stu *head1,*p;char m[15];there:printf("┏━┓━━━━━━━━━━━━━━━━━━━┏━┓\n"); printf(" ┃┃socat 图书管理系统printf(” ┃┗━━━━━━━━━━━━━━━━━━━┛┃\n"); printf(" ┃●[0]退出系统┃\n");printf(” ┃┃\n”);prin tf(” ┃●[1]帮助┃\n");printf(” ┃┃\n”);printf(” ┃●[2]浏览图书┃\n");printf(” ┃┃\n");printf(” ┃●[3]统计图书数目┃\n”);printf(" ┃┃\n”);printf(” ┃●[4]查询┃\n”);printf(” ┃printf(” ┃●[5]添加┃\n");printf(” ┃┃\n”);printf(” ┃●[6]排序┃\n");printf(” ┃┃\n");printf(" ┃●[7]修改┃\n”);printf(” ┃┃\n");printf(" ┃●[8]删除┃\n”);printf(" ┃┃\n”);printf(" ┃●[9]修改账号及密码┃\n");printf(” ┃┃\n”);printf(” ┃●[10]学生信息printf(”┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");printf(” 请选择:”);fflush(stdin);head=create();scanf("%d”,&choice);if(choice==1){//help();printf(”没有内容!\n”);system("pause”);system(”cls”);menu();}else if(choice==2){system(”cls");if(head==NULL){printf(”没有图书,请先添加图书!\n”);system("pause");system("cls");menu();}gBrowse(head);}else if(choice==3){system(”cls");count(head);}else if(choice==4){system("cls");if(head==NULL){printf(”没有图书,请先添加图书!\n”);system("pause”);system(”cls”);menu();}gfind();}else if(choice==5){Input();}else if(choice==6){system("cls");if(head==NULL){printf(”没有图书,请先添加图书!\n”); system("pause”);system(”cls”);menu();}sort(head);}else if(choice==7){system("cls”);if(head==NULL){printf(”没有图书,请先添加图书!\n”);system("pause");system("cls”);menu();}Revise(head);}else if(choice==8){if(head==NULL){printf("没有图书,请先添加图书!\n");system(”pause");system(”cls");menu();}printf(" 请输入想要删除的图书编号:”);scanf(”%s",m);Delete(head,m);}else if(choice==9){gxinxi();else if(choice==10){system(”cls”);head1=xcreate();if(head1==NULL){printf("没有学生信息,请到xuesheng_list.txt添加!\n”); sys tem(”pause”);system(”cls”);menu();}for(p=head1;p!=NULL;p=p—>next){printf(”学生学号密码\n”);printf("%d %s\n",p->snum,p—>mima);}system(”pause”);system("cls”);menu();}else if(choice==0)system(”cls");printf(”\n\n\n\n”);printf(” ━━━━━━━━感谢使用图书管理系统━━━━━━━━\n\n\n”);exit(0);}else{system("cls");printf(”\n\n\n\n ━━━━输入错误,请重新输入!━━━━\n\n\n ");system("pause”);system(”cls”);n++;if(n==3){printf(” \n\n\n ━━━━━━━━你错误次数太多,自动退出!━━━━━━━━\n\n\n”);printf(" ━━━━━━━━感谢使用图书管理系统━━━━━━━━\n\n\n");system(”pause");exit(0);}goto there;}}void xmenu()/*学生主菜单(学生进入可对图书,密码进行操作)*/{struct tushu *head;struct stu *head1;int choice,n=0;there:printf(”┏━┓━━━━━━━━━━━━━━━━━━━┏━┓\n");printf(” ┃┃socat 图书借阅系统┃┃\n");printf(” ┃┗━━━━━━━━━━━━━━━━━━━┛┃\n"); printf(” ┃●[0]退出系统┃\n”);printf(” ┃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]修改密码printf("┗━━━━━━━━━━━━━━━━━━━━━━━┛\n”); printf(" 请选择:");fflush(stdin);head=create();scanf("%d”,&choice);if(choice==1){//xhelp();printf(”没有内容!\n");system(”pa use");system(”cls”);xmenu();}else if(choice==2){system("cls");if(head==NULL){printf(”没有图书!\n");system("pause”);xmenu();}xBrowse(head);}else if(choice==3){if(head==NULL){printf(”没有图书!\n”);system(”pause”);system(”cls”);xmenu();}xfind();}else if(choice==4){if(head==NULL){printf(”没有图书!\n");system(”pause”);xmenu();}borrow(head);}else if(choice==5){huanshu(head);}else if(choice==6){system("cls”);head1=xcreate();if(head1==NULL){printf(”学生信息被清空!!\n”);system("pause");system("cls”);xmenu();}xmima(head1); ;}else if(choice==0){system("cls");printf(”\n\n\n\n”);printf(” ━━━━━━━━感谢使用图书管理系统━━━━━━━━\n\n\n");exit(0);}else{system("cls”);printf("\n\n\n\n ━━━━输入错误,请重新输入!━━━━\n\n\n ”);system(”pause”);system(”cls”);n++;if(n==3){printf(” \n\n\n ━━━━━━━━你错误次数太多,自动退出!━━━━━━━━\n\n\n”);printf(” ━━━━━━━━感谢使用图书管理系统━━━━━━━━\n\n\n”);system("pause”);exit(0);}goto there;}}void gfind()/*管理员查询(管理员可按一定的方式查询图书)*/{int choice,n=0;struct tushu *head;there:system(”cls”);printf(” ┏━┓━━━━━━━━━━━━━━━━━━━┏━┓\n”);printf(” ┃┃socat 图书借阅系统┃┃\n”);printf(” ┃┗━━━━━━━━━━━━━━━━━━━┛┃\n");printf(" ┃●[0]返回┃\n");printf(" ┃┃\n”);printf(" ┃●[1]按书名查找┃\n”);printf(" ┃┃\n”);printf(" ┃●[2]按作者查找┃\n”);printf(” ┃┃\n”);printf(" ┃●[3]按类别查找┃\n”);printf(" ┃┃\n”);printf(" ┃● ┃\n”);printf(”┗━━━━━━━━━━━━━━━━━━━━━━━┛\n”);printf(" 请选择:”);fflush(stdin);head=create();scanf(”%d”,&choice);if(choice==1){system(”cls");Findofname(head); }else if(choice==2){system(”cls”); Findofwriter(head); }else if(choice==3){system(”cls");Findofkind(head); }else if(choice==0){system(”cls”);menu();}else{system(”cls”);printf(”\n\n\n\n ━━━━输入错误,请重新输入!━━━━\n\n\n ”);system("pause");system(”cls");n++;if(n==3){printf(" \n\n\n ━━━━━━━━你错误次数太多,自动退出! ━━━━━━━━\n\n\n”);printf(" ━━━━━━━━感谢使用图书管理系统━━━━━━━━\n\n\n”);system(”pause”);exit(0);}goto there;}}void xfind()/*学生查询(学生可按一定的方式查询图书)*/ {struct tushu *head;int choice,n=0;there:system("cls”);printf(” ┏━┓━━━━━━━━━━━━━━━━━━━┏━┓\n”);printf(” ┃┃socat 图书借阅系统┃┃\n”);printf(” ┃┗━━━━━━━━━━━━━━━━━━━┛┃\n");printf(” ┃●[0]返回┃\n”);printf(” ┃┃\n");printf(" ┃●[1]按书名查找┃\n”);printf(” ┃┃\n”);printf(" ┃●[2]按作者查找┃\n”);printf(" ┃┃\n");printf(” ┃●[3]按类别查找┃\n");printf(” ┃┃\n”);printf(" ┃● ┃\n");printf(”┗━━━━━━━━━━━━━━━━━━━━━━━┛\n"); printf(” 请选择:");fflush(stdin);head=create();scanf(”%d",&choice);if(choice==1){system(”cls”);xFindofname(head);}else if(choice==2){system(”cls”);xFindofwriter(head);}else if(choice==3){system("cls");xFindofkind(head);}else if(choice==0){system(”cls");xmenu();}else{system("cls");printf("\n\n\n\n ━━━━输入错误,请重新输入!━━━━\n\n\n ”);system(”pause”);system(”cls");n++;if(n==3){printf(” \n\n\n ━━━━━━━━你错误次数太多,自动退出!━━━━━━━━\n\n\n”);printf(” ━━━━━━━━感谢使用图书借阅系统━━━━━━━━\n\n\n”);system("pause”);exit(0);}goto there;}}void sort(){struct tushu *head;int choice,n=0;there:system("cls”);printf(” ┏━┓━━━━━━━━━━━━━━━━━━━┏━┓\n");printf(” ┃┃socat 图书借阅系统┃┃\n");printf(" ┃┗━━━━━━━━━━━━━━━━━━━┛┃\n”);printf(" ┃●[0]返回┃\n”);printf(” ┃┃\n");printf(” ┃●[1]按时间排序┃\n”);printf(” ┃┃\n”);printf(” ┃●[2]按价格排序┃\n");printf(" ┃┃\n”);printf(” ┃●[3]按编号排序┃\n”);printf(" ┃┃\n”);printf(” ┃●┃\n”);printf(”┗━━━━━━━━━━━━━━━━━━━━━━━┛\n”);printf(" 请选择:”);fflush(stdin);head=create();scanf("%d",&choice);if(choice==1){system("cls”); Sort_time(head);}else if(choice==2){sys tem(”cls");Sort_price(head);}else if(choice==3){system(”cls”);Sort_num(head); }else if(choice==0){system("cls”); menu();}else{system(”cls");printf("\n\n\n\n ━━━━输入错误,请重新输入!━━━━\n\n\n ”);system(”pause”);system(”cls”);n++;if(n==3){printf(” \n\n\n ━━━━━━━━你错误次数太多,自动退出! ━━━━━━━━\n\n\n”);printf(” ━━━━━━━━感谢使用图书借阅系统━━━━━━━━\n\n\n”);system("pause”);exit(0);}goto there;}}struct tushu *Input(){struct tushu *p1,*p2,*head,*ptr;char num;int x,i=0;system("cls”);p1=(struct tushu *)malloc(sizeof(struct tushu));head=p1;p1-〉price=—1;while(i!=1){printf(”请输入编号,以’#’结束\n”);scanf("%s",p1—>num);if(strcmp(p1-〉num,”#")==0)i=1;while(i!=1){printf(”请依次输入书名作者出版社类别出版时间价格\n”); scanf(”%s%s%s%s%lf%lf”,p1—>name,p1—>writer,p1—〉press,p1-〉kind,&p1-〉time,&p1—〉price);p2=p1;p1=(struct tushu *)malloc(sizeof(struct tushu));p2—〉next=p1;break;}}if(p1-〉price!=-1)p2—〉next=NULL;elsehead=NULL;system("cls”);printf("\n\n\n\t\t\t图书信息输入结束!\n\n\n”);system(”pause”);system(”cls”);printf(”\n\n\n\t\t\t是否保存图书信息?(1。
c语言火车源代码以下是一个简单的C语言火车源代码示例,用于模拟火车的运行:c复制代码#include<stdio.h>#include<stdlib.h>#include<time.h>#define MAX_TRAIN_LENGTH 100#define TRAIN_SPEED 10typedef struct {int position;int direction; // 0: stop, 1: forward, -1: backward} Train;void move_train(Train *train) {if (train->direction == 1) {train->position += TRAIN_SPEED;} else if (train->direction == -1) {train->position -= TRAIN_SPEED;}}int is_train_at_destination(Train *train) {return train->position >= MAX_TRAIN_LENGTH;}int main() {srand(time(NULL)); // 初始化随机数生成器Train train = {0, 0}; // 初始化火车初始位置和方向for (int i = 0; i < 50; i++) { // 模拟火车运行50次move_train(&train); // 移动火车if (is_train_at_destination(&train)) { // 如果火车到达目的地,重置位置和方向train.position = 0;train.direction = rand() % 2; // 随机选择前进或后退方向}printf("Train position: %d\n", train.position); // 输出火车位置}return0;}这个程序使用了一个结构体来表示火车,其中包含火车的位置和方向。
c语言爱心源代码爱心,是一种无私奉献的情感,它使人们彼此关心、互相帮助。
在计算机科学领域,我们也可以通过编程语言来表达爱心。
C语言作为一种广泛使用的编程语言,也可以用来编写爱心源代码。
爱心源代码是一段具有特殊形状的代码,通过在控制台上输出一颗爱心来表达爱心的意义。
下面,我们将使用C语言来编写一段爱心源代码,并对其进行解析和说明。
```c#include <stdio.h>int main() {int i, j, n, m;int heart[10][10] = {0}; // 存储爱心形状的数组// 设置爱心形状heart[4][4] = 1;for (i = 3; i <= 5; i++) {heart[i][3] = 1;heart[i][5] = 1;}for (i = 2; i <= 6; i++) {heart[i][2] = 1;heart[i][6] = 1;}heart[7][3] = 1;heart[7][4] = 1;heart[7][5] = 1;heart[8][4] = 1;// 输出爱心形状for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { if (heart[i][j] == 1) { printf("*");} else {printf(" ");}}printf("\n");}return 0;}```以上是一段简单的爱心源代码。
在代码中,我们首先定义了一个二维数组`heart`,用于存储爱心的形状。
数组的行和列分别代表控制台上的行数和列数。
我们将爱心的形状抽象为1和0,其中1代表爱心的一部分,0代表空白的位置。
在设置爱心形状的部分,我们通过对数组`heart`进行逐个赋值的方式,将爱心的形状保存在数组中。
然后,在输出爱心形状的部分,我们通过遍历数组`heart`,根据数组中的值决定在控制台上输出`*`或空格,从而呈现出爱心的形状。
源文件#include <stdio.h>#include <malloc.h>#include <string.h>#include <stdlib.h>#define NULL 0#define LEN sizeof(struct student)struct student{ char name[20];long int num;int English[4];int Math;int com[30];int ave;struct student *next;};//定义一个结构题int TOTAL_NUM = 0;//学生总数struct student *head = NULL;void welcome (); //欢迎界面void mainmenu (); //主界面void record (); //记录数据void insert(struct student *stu); //插入数据void display(struct student *stu); //显示一个学生的信息void displayAll (); //显示所有学生的信息void query_by_num (); //按学号查询学生信息void readData (); //读取文件里学生的信息void writeData (); //向文件写入学生信息void freeAll (); //清空链表内容void del (); //删除学生信息void change (); //更改学生信息void devise (struct student *p); //选择更改内容int main (void){ welcome ();//显示主菜单mainmenu ();return 0;}//显示欢迎信息void welcome (){printf ("+----------------------------------------------------+\n");printf ("| |\n");printf ("| 欢迎使用计算机科学与技术2班学生信息管理系统|\n");printf ("| |\n");printf ("+----------------------------------------------------+\n");}//系统主菜单void mainmenu (){ int choice;choice = -1;readData ();printf ("\n温馨提示:为保证您的操作得到保存,请按正常顺序退出系统^_^\n");do{printf ("\n\n\n");printf ("\t\t\t--------------------------------------------\n");printf ("\t\t\t+ 计算机科学与技术2班学生信息管理系统|\n");printf ("\t\t\t--------------------------------------------\n");printf ("\t\t\t+ [1]----录入学生信息|\n"); printf ("\t\t\t+ [2]----浏览学生信息|\n"); printf ("\t\t\t+ [3]----查询学生信息|\n");printf ("\t\t\t+ [4]----删除学生信息|\n"); printf ("\t\t\t+ [5]----修改学生信息|\n"); printf ("\t\t\t+ [0]----退出系统|\n"); printf ("\t\t\t+*·*·*·*·*·*·*·*·*·*·*·*·*·*·*|\n");printf ("\t\t\t--------------------------------------------\n");printf ("请输入您的选择:");scanf ("%d", &choice);switch (choice){case 0:writeData ();freeAll ();exit (0);case 1:record ();break;case 2:displayAll ();break;case 3:query_by_num ();break;case 4:del ();break;case 5:change ();break;default:printf ("\n无效选项!");break;}}while (choice != 0);}//录入学生信息void record (){ struct student *p0;p0 = (struct student *)malloc(LEN);printf ("请输入学生的姓名:");scanf ("%s",p0->name);printf ("请输入学生的学号:");scanf ("%ld",&p0->num);printf ("请输入学生的英语分数:");scanf ("%ld",p0->English);printf ("请输入学生的数学分数:");scanf ("%d",&p0->Math);printf ("请输入学生的计算机分数:");scanf ("%ld",p0->com);printf ("请输入学生的平均分:");scanf ("%ld",&p0->ave);insert (p0);printf ("该学生的信息为:\n");printf("-------------------------------------------------------------------------------\n"); printf ("姓名\t学号\t\t英语分数\t数学分数\t计算机分数\t\t平均分\n");display (p0);}void insert (struct student *stu){struct student *p0, *p1, *p2;p1 = head;p0 = stu;if (head == NULL){head = p0;p0->next = NULL;}else{while ((p0->num > p1->num)&&(p1->next != NULL)) {p2 = p1;p1 = p1->next;}if (p0->num <= p1->num){if (head == p1)head = p0;elsep2->next = p0;p0->next = p1;}else{p1->next = p0;p0->next = NULL;}}TOTAL_NUM++;}void display (struct student *p){printf ("%s\t%ld\t%d\t%ld\t%ld\t\t%ld\n", p->name, p->num, p->English, p->Math, p->com, p->ave);}//浏览学生信息void displayAll(){struct student *p;printf("学生总数:%d\n", TOTAL_NUM);p = head;if (head != NULL){printf("\n姓名\t学号\t\t英语分数\t数学分数\t计算机分数\t\t平均分\n");printf("-------------------------------------------------------------------------------\n");do{display(p);p = p->next;}while(p != NULL);}printf ("\n");}//按学号查询学生信息void query_by_num (){int num;struct student *p1;printf("请输入学生的学号:"); scanf("%ld", &num);if(head==NULL){printf("无学生记录!\n"); return;}p1 = head;while (num!=p1->num && p1->next!=NULL)p1 = p1->next;if (num == p1->num){printf ("姓名\t学号\t\t英语分数\t数学分数\t计算机分数\t\t平均分\n");printf("-------------------------------------------------------------------------------\n"); display (p1);}elseprintf ("没有该学生记录,请核对!");}//写入文件void writeData (){FILE* fp;//文件指针struct student *p;fp = fopen("1.txt", "w");if (!fp){printf("文件打开错误!");return;}fprintf(fp,"%d\n", TOTAL_NUM);for(p = head; p!= NULL; p= p->next){fprintf(fp,"%s\t%ld\t%ld\t%d\t%ld\t%ld\n", p->name, p->num, p->English, p->Math, p->com, p->ave);}fclose (fp);}void freeAll (){struct student *p1, *p2;p1 = p2=head;while(p1){p2=p1->next;free (p1);p1 = p2;}}//读取文件void readData (){FILE* fp;//文件指针struct student *p1, *p2;fp = fopen("1.txt", "r");if (!fp){printf("文件打开错误!");return;}fscanf(fp,"%d\n", &TOTAL_NUM);head = p1 = p2 = (struct student *)malloc(LEN);fscanf(fp,"%s\t%ld\t%ld\t%d\t%ld\t%ld\n", p1->name, &p1->num, p1->English, &p1->Math, p1->com, &p1->ave);while(!feof(fp)){p1 = (struct student *)malloc(LEN);fscanf(fp,"%s\t%ld\t%ld\t%d\t%ld\t%ld\n", p1->name, &p1->num, p1->English, &p1->Math, p1->com, &p1->ave);p2->next = p1;p2 = p1;}p2->next = NULL;fclose(fp);}//删除学生信息void del (){struct student *p1, *p2;long int num;if (head == NULL){printf("无学生记录!\n");return;}printf("请输入您要删除的学生的学号:"); scanf("%ld", &num);p1 = head;while (num != p1->num && p1->next != NULL) {p2 = p1;p1 = p1->next;}if(num == p1->num){if(p1 == head)head = p1->next;else p2->next = p1->next;free(p1);TOTAL_NUM--;}elseprintf("没有该学生记录,请核对!\n");}//修改学生信息void change (){struct student *p1, *p2;long int num;if (head == NULL){printf ("无学生记录!\n");return;}printf ("请输入您要修改的学生的学号:");scanf ("%ld", &num);p1 = head;while (num != p1->num && p1->next != NULL){p2 = p1;p1 = p1->next;}if(num == p1->num)devise (p1);elseprintf("没有该学生记录,请核对!\n");}void devise (struct student *p){int choice;choice = -1;do{printf("请选择您要修改的学生的信息内容:\n");printf("+----------------------+\n");printf("| 姓名请按1 |\n");printf("| 学号请按2 |\n");printf("| 英语分数请按3 |\n"); printf("| 数学分数请按4 |\n");printf("| 计算机分数请按5 |\n"); printf("| 平均分请按6 |\n");printf("| 取消请按0 |\n");printf("+----------------------+\n");printf("请输入您的选择:");scanf("%d", &choice);switch (choice){case 0:return;case 1:printf("请输入新姓名:"); scanf("%s", p->name);break;case 2:printf("请输入新学号:"); scanf("%d", &p->num);break;case 3:printf("请输入新英语分数:");scanf("%s", p->English);break;case 4:printf("请输入新数学分数:");scanf("%s", &p->Math);break;case 5:printf("请输入新计算机分数:");scanf("%s", p->com);break;case 6:printf("请输入新平均分:");scanf("%lf", &p->ave);break;default:printf("\n无效选项!");break;}}while(choice != 0);}。