示例c語言
① c語言實例
/*看你掛了一天都沒人幫忙,實在是看不過去了.我來給你寫.*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main()
{
FILE *fp;
char c;
int a[5]={0};
if((fp=fopen("data.txt","r"))==NULL)
{
printf("can not open the date.txt\n");
exit(0);
}
c=fgetc(fp);
while(c!=EOF)
{
if(((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z')))
{
a[0]++;
}
if((c>='0')&&(c<='9'))
{
a[1]++;
}
if(c==' ')
{
a[2]++;
}
if(c==',')
{
a[3]++;
}
if(c=='.')
{
a[4]++;
}
c=fgetc(fp);
}
fclose(fp);
printf("############the result is :###########\n");
printf("\n 英文 數字 空格 逗號 句號\n");
for(int i=0;i<5;i++)
printf("%6d",a[i]);
printf("\n");
}
② 給個C語言的for簡單例子
#include<stdio.h>
voidmain()
{
inti;
for(i=1;i<=100;i++)
printf("i=%d ",i);
}
③ c語言程序設計實例
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
struct addr{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
}add_list[MAX];
void init_list(void),enter(void);
void delete(void),list(void);
void load(void),save(void);
int menu_select(void),find_free(void);
int main(void)
{
char choice;
init_list();/*initialize the structure array*/
for(;;){
choice=menu_select();
switch(choice){
case 1:enter();
break;
case 2:delete();
break;
case 3:list();
break;
case 4:save();
break;
case 5:load();
break;
case 6:exit(0);
}
}
return 0;
}
/*Initialize the list*/
void init_list(void)
{
register int t;
for(t=0;t<MAX;++t)
add_list[t].name[0]='0';
}
/*Get a menu selection*/
int menu_select(void)
{
char s[80];
int c;
printf("1.Enter a name\n");
printf("2.Delete a name\n");
printf("3.List the file\n");
printf("4.Save the file\n");
printf("5.Load the file\n");
printf("6.Quit\n");
do{
printf("\nEnter your choice:");
gets(s);
c=atoi(s);
}while(c<0||>6);
return c;
}
/*Input addresses into the list*/
void enter(void)
{
int slot;
char s[80];
slot=find_free();
if(slot==-1){
printf("\nList Full");
return;
}
printf("Enter name:");
gets(add_list[slot].name);
printf("Enter street:");
gets(add_list[slot].street);
printf("Enter city:");
gets(add_list[slot].city);
printf("Enter state:");
gets(add_list[slot].state);
printf("Enter zip:");
gets(s);
add_list[slot].zip=strtoul(s,'\0',10);
}
/*Find an unused structure*/
int find_free(void)
{
register int t;
for(t=0;add_list[t].name[0]&&t<MAX;++t);
if(t==MAX)
return -1;/*no slots free*/
return t;
}
/*Delete an address*/
void delete(void)
{
register int slot;
char s[80];
printf("enter record # :");
gets(s);
slot=atoi(s);
if(slot>=0&&slot<MAX)
add_list[slot].name[0]='\0';
}
/*Display the list on the screen*/
void list(void)
{
register int t;
for(t=0;t<MAX;++t){
if(add_list[t].name[0]){
printf("%s\n",add_list[t].name);
printf("%s\n",add_list[t].street);
printf("%s\n",add_list[t].city);
printf("%s\n",add_list[t].state);
printf("%s\n",add_list[t].zip);
}
}
printf("\n\n");
}
/*Save the list*/
void save(void)
{
FILE *fp;
register int i;
if((fp=fopen("maillist","wb"))==NULL)
printf("Cannot open file\n");
return;
for(i=0;i<MAX;i++)
if(*add_list[i].name)
if(fwrite(&add_list[i],sizeof(struct addr),1,fp)!=1)
printf("File write error\n");
fclose(fp);
}
/*Load the file*/
void load(void)
{
FILE *fp;
register int i;
if((fp=fopen("maillist","rb"))==NULL){
printf("Cannot open file\n");
return;
}
init_list();
for(i=0;i<MAX;i++)
if(fread(&add_list[i],sizeof(struct addr),1,fp)!=1){
if(feof(fp))break;
printf("File read error\n");
}
fclose(fp);
}
④ c語言實例講解
以下這一段出了問題
for(i=2;i<n;i++);
if(n%i==0)
break;
for()後面跟的是語句,你打的for(i=2;i<n;i++);就壞在括弧外面的 「; 」
這相當於,不執行任何東西……因為 ; 是一個空語句……
你的本意是當for循環條件成立時執行
if(n%i==0)
break;
但是for()後面的;使得這兩個語句沒有發揮應有的作用……
請記住,for(),while(),do while,if()後面一般都不會緊跟著 」 ; 「
修改後程序如下,應該能實現的你要求
#include<stdio.h>
int main(void)
{
int n,i;
printf("please input a number>2:");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
break;
}
if(i<n)
printf("%d not a sushu\n",n);
else
printf("%d is a sushu\n",n);
return 0;
}
⑤ c語言 示例 問題
max不是系統中默認的函數而是程序作者自己定義的一個新的函數,在主函數中使用這個新的函數電腦是不能直接編譯的,後面這一段程序是對max這個函數的一個定義,只有正確的定義了這個函數,才能使用。順便說一句,在這段函數中對max函數的定義出現在主函數之後,系統編譯時還是會出現一定問題的,因該在主函數中scanf(「%d,%d」,&a,&b); /*輸入變數a和b的值*/ 之後加入語句
int max(int x,int y);來聲明一下。
⑥ 經典C語言程序例子
題目01:在一個已知的字元串中查找最長單詞,假定字元串中只含字母和空格版,空格用來分隔權不同的單詞。
(6)示例c語言擴展閱讀:
C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。
盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。
⑦ c語言例子
你看看這段代碼:
#include <stdio.h>
int main ()
{
int i,j,k;
for ( i=1;i<=3;i++) //對A有3中選擇X,內Y,Z
for ( j=1;j<=3;j++) //對B有3中選擇X,Y,Z
for (k=1;k<=3;k++) //對C有3中選擇X,Y,Z
if (i!容=j&&j!=k&&i!=k&&i!=1&&j!=1&&j!=3)
//和A,B,C對手的必須不相同 (i!=1 就是A的對手不是X,j!=1 就是B的對手不是X,j!=3就是B的對手不是Z)
printf ("A--%c\nB--%c\nC--%c\n",i+'X'-1,j+'X'-1,k+'X'-1);
return 1;
}
⑧ C語言編程實例
int countCattles(int cattles, int years){
return years<=0?cattles: countCattles(cattles*2,years-4);
}
===
就可來以源了
printf("20年後有%d只\n ", countCattles(1,20);
⑨ C語言的經典編程例子
//最經典的當然是HelloWorld了。
#include"stdio.h"
intmain(void)
{
printf("HelloWorld! ");
}
⑩ 求生活中的C語言實例(50--200行)
給你個小型工資管理系統吧 /*2、小型工資管理系統 編寫一個小型工資管理系統。假設公司有四類人員: 總經理:固定月薪10000元; 銷售經歷:固定月薪5000元,另按其負責部門當月銷售額的4%提成; 銷售員:按其負責部門當月銷售額的6%提成; 兼職技術員:每小時80元。*/ #include<iostream> using namespace std; float *process(float xse,float t) { float zjl=10000; float xsjl=5000; float xsy=0; float jzy=0; static float a[5]; xsjl=5000+0.04*xse; xsy=0.06*xse; jzy=80*t; a[0]=xsjl; a[1]=xsy; a[2]=jzy; return a; } void show(float a[]) { cout<<"總經理本月薪10000"<<endl; cout<<"——————————————————————"<<endl; cout<<"銷售經理本月薪"<<a[0]<<endl; cout<<"——————————————————————"<<endl; cout<<"銷售員本月薪"<<a[1]<<endl; cout<<"——————————————————————"<<endl; cout<<"兼職技術員本月薪"<<a[2]<<endl; cout<<"——————————————————————"<<endl; } float main() { float x,hour; float *p; char ch; do{cout<<"工資管理"<<endl; cout<<"——————————————————————"<<endl; cout<<"總經理固定月薪10000"<<endl; cout<<"——————————————————————"<<endl; cout<<"銷售經理固定月薪5000元,另按其負責部門當月銷售額的4%提成"<<endl; cout<<"——————————————————————"<<endl; cout<<"銷售員:按其負責部門當月銷售額的6%提成"<<endl; cout<<"——————————————————————"<<endl; cout<<"兼職技術員:每小時80元"<<endl; cout<<"——————————————————————"<<endl; cout<<"1、輸入本月銷售額和兼職員工作小時"<<endl; cout<<"2、退出"<<endl; cin>>ch; switch(ch){ case '1': cout<<"輸入本月銷售額和兼職員工作小時"<<endl; cin>>x>>hour; p=process(x,hour); show(p);break; case '2':exit(0);break; } }while(1); return 0; }