當前位置:首頁 » 編程語言 » c語言設計統計代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言設計統計代碼

發布時間: 2022-09-11 01:53:29

『壹』 c語言程序設計: 任意輸入n個整數,分別統計奇數的和、奇數的個數、偶數的和、偶數的個數。

程序為:

#include<stdio.h>

void main()

{

int a[100],i,item,sum1,sum2,item1,item2;//sum1,sum2分別儲存奇數、偶數之和,item1,item2分別表示奇數、偶數的個數

printf("請輸入所要輸入整數的個數:");

scanf("%d",&item);

printf("請輸入%d個整數: ",item);

for(i=0;i<item;i++)

scanf("%d",&a[i]);

sum1=sum2=item1=item2=0;

for(i=0;i<item;i++)

{

if(a[i]==0) item2++;//0是特殊偶數

else if(a[i]%2==0)

{

sum2=sum2+a[i];

item2++;

}

else

{

sum1=sum1+a[i];

item1++;

}

}

printf("奇數之和為:%d,偶數之和為:%d ",sum1,sum2);

}

(1)c語言設計統計代碼擴展閱讀:

for表達式

表達式1:一般為賦值表達式,給控制變數賦初值;

表達式2:關系表達式或邏輯表達式,循環控制條件;

表達式3:一般為賦值表達式,給控制變數增量或減量。

語句:循環體,當有多條語句時,必須使用復合語句。

『貳』 用c語言設計一個程序統計一個班的學生成績

#include<stdio.h>

#include<stdlib.h>

//定義一個學生結構體數組

struct Student{

int id;

double score;

}Student[100];

//記錄實際導入學生個數

int len = 0;

//求最高分

void getmax() {

int maxid = 0;

double maxscore = -1;

for (int i = 0; i < len; i++) {

if (Student[i].score > maxscore) {

maxscore = Student[i].score;

maxid = Student[i].id;

}

}

printf("班級最高分為:%.2lf學號是:%d ", maxscore, maxid);

}

//求最低分

void getmin() {

int minid = 0;

double minscore = 101;

for (int i = 0; i < len; i++) {

if (Student[i].score < minscore) {

minscore = Student[i].score;

minid = Student[i].id;

}

}

printf("班級最低分為:%.2lf學號是:%d ", minscore, minid);

}

//求總分

double getsum() {

double sum = 0;

for (int i = 0; i < len; i++) {

sum += Student[i].score;

}

return sum;

}

//求平均分

double getave() {

double ave = 0;

return getsum() / len;

}

//導入學生信息

void input() {

int id = 0;

double score = 0;

while (1) {

printf("請輸入學生學號:");

scanf_s("%d", &id);

if (id == -1) { break; }

printf("請輸入學生成績:");

scanf_s("%lf", &score);

Student[len].id = id;

Student[len].score = score;

len++;

}

printf("信息導入完畢! ");

}

int main() {

input();

printf("全班總分為:%.2lf ", getsum());

printf("全班平均分為:%.2lf ", getave());

getmax();

getmin();

return 0;

}

註:此代碼運行於VS2017,如果在VC6.0中運行,可將scanf_s改成scanf

並且for(int i=0;i<len;i++){xxx}改成int i=0;for(i=0;i<len;i++){xxx}即可正常運行

程序運行測試圖如下:

『叄』 用C語言怎麼編寫一個統計名字次數的程序急!急!急!!!!

代碼如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ()
{
struct person /*定義一個結構體*/
{
char name[20]; /*默認名字不超過20長度,且沒有空格。*/
int time; /*出現次數*/
};
struct person data[200]; /*默認不超過200個不同名*/

int i;
for (i = 0; i < 200; i ++) /*初始化一下*/
{
data[i].name[0] = 0;
data[i].time = 0;
}

char temp[20];
int total = 0; /*一開始一共有0個人*/
while (scanf ("%s", temp) != EOF) /*循環讀入所有姓名*/
{
int i = 0;
for (i = 0; i < total; i ++) /*和已經存在的姓名對比,看是否一樣*/
{
if (strcmp (temp, data[i].name) == 0) /*如果一樣*/
{
data[i].time ++;
break;
}
}
if (i == total) /*找了一圈沒有找到已經存在的,就證明這個名字從來沒有出現過,存一個新的*/
{
strcpy (data[total].name, temp);
data[total].time = 1;
total ++;
}

}
for (i = 0; i < total; i ++) /*最後循環把結果輸出*/
printf ("%s ---- %d\n", data[i].name, data[i].time);

system ("pause");
return 0;
}

祝學習進步!

『肆』 用c語言設計一個程序統計一個班的學生成績

代碼如下:

註:此代碼運行於VS2017,如果在VC6.0中運行,可將scanf_s改成scanf

並且for(int i=0;i<len;i++){xxx}改成int i=0;for(i=0;i<len;i++){xxx}即可正常運行

#include<stdio.h>

#include<stdlib.h>

//定義一個學生結構體數組

struct Student{

int id;

double score;

}Student[100];

//記錄實際導入學生個數

int len = 0;

//求最高分

void getmax() {

int maxid = 0;

double maxscore = -1;

for (int i = 0; i < len; i++) {

if (Student[i].score > maxscore) {

maxscore = Student[i].score;

maxid = Student[i].id;

}

}

printf("班級最高分為:%.2lf學號是:%d ", maxscore, maxid);

}

//求最低分

void getmin() {

int minid = 0;

double minscore = 101;

for (int i = 0; i < len; i++) {

if (Student[i].score < minscore) {

minscore = Student[i].score;

minid = Student[i].id;

}

}

printf("班級最低分為:%.2lf學號是:%d ", minscore, minid);

}

//求總分

double getsum() {

double sum = 0;

for (int i = 0; i < len; i++) {

sum += Student[i].score;

}

return sum;

}

//求平均分

double getave() {

double ave = 0;

return getsum() / len;

}

//導入學生信息

void input() {

int id = 0;

double score = 0;

while (1) {

printf("請輸入學生學號:");

scanf_s("%d", &id);

if (id == -1) { break; }

printf("請輸入學生成績:");

scanf_s("%lf", &score);

Student[len].id = id;

Student[len].score = score;

len++;

}

printf("信息導入完畢! ");

}

int main() {

input();

printf("全班總分為:%.2lf ", getsum());

printf("全班平均分為:%.2lf ", getave());

getmax();

getmin();

return 0;

}

程序運行測試圖如下:

『伍』 求一篇用C語言編寫的使用數組和指針統計成績的程序代碼

void main()
{
int a[8] = {75, 80, 83, 85, 86, 30, 0, 0};
string name[] = {"數學", "物理", "外語", "政治", "體育"};
string MeanLow[4];
a[6] = (a[0] + a[1] + a[2] + a[3] + a[4]) / 5;
char* strAve = (char*)malloc(10);
MeanLow[0] = itoa(a[5], strAve, 10);
MeanLow[1] = itoa(a[6], strAve, 10);
int b = a[0];
for (int i = 0; i < 4; i++)
{
b = min(b, a[i + 1]);
}
MeanLow[2] = itoa(b, strAve, 10);
for (int i = 0; i < 5; i++)
{
if (a[i] == b)
{
MeanLow[3] = name[i];
}
}
printf("數學:75\n物理:80\n外語:83\n政治:85\n體育:86\n統計結果如下:\n");
printf("人數:30\n平均成績:%s\n最低分數科目的成績:%s\n最低分數的科目:%s\n", MeanLow[1], MeanLow[2], MeanLow[3]);
free(strAve);
}

『陸』 編寫C語言程序統計輸入字元串的個數、

很簡單了,代碼如下:
#include
void
main()
{
char
a[100];
int
i,count=0,flag=1,n=0;
gets(a);//從鍵盤輸入字元串
for(i=0;a[i];i++)
{
if((a[i]>='a'
&&
a[i]<='z')
||
(a[i]>='a'
&&
a[i]<='z'))//判斷是不是字元
{
flag=0;//設置字元標志
continue;//返回下一個循環
}
if(!flag)//前面一位是字元,即當前是某個單詞字元結束
{
count++;//統計單詞個數
flag=1;//設置非字元標志
}
}
i--;
if((a[i]>='a'
&&
a[i]<='z')
||
(a[i]>='a'
&&
a[i]<='z'))
count++;//增加字元串結束的單詞
printf("共有%d單詞\n",count);
}

『柒』 用C語言怎樣寫數據統計代碼

....
for(i =0;i <10;i++)
{
scanf("%d",a);
if(a>0)
cout1++;//正數計數
if(a == 0)
count2++;//0個數計數
if(a<0)
count3++;//負數個數
}
printf("%d\n",count1);
..
..

『捌』 C語言編寫程序統計輸入的行數

循環讀入字元,遇到 累計行數,最終輸出累計值即可。

以EOF作為終止條件的代碼如下:

#include<stdio.h>
intmain()
{
intcnt=1;//最少會輸出一行,每遇到一個換行,表示多輸入了一行。
intc;
while((c=getchar())!=EOF)
{
if(c==' ')cnt++;//統計行數。
}
printf("%d ",cnt);

return0;
}

『玖』 用C語言來編寫:商品銷售統計程序

#include<iostream>
#include<cstring>
#include<fstream>
#include<stdlib.h>//system("cls")//清屏
#include<conio.h>//getche()
using namespace std;
//全局變數
int i=0;//已錄入商品總個數
char ch;//cin>>ch
int n;//case(n)
char code[10];
char name[10];
char unit[10];
int amount;
float unitprice;
float total=0;//總價
ofstream f1("./test.txt");
ofstream f2("./sell.txt");//構建輸出流,沒有文件就建立

class Goods
{
private:
char code[10];//代碼
char name[10];//名稱
char unit[10];//單位
int amount;//總數
float unitprice;//單價
public:
Goods();
Goods(char co[10],char na[10],char un[10],int am,float unpr);//構造函數
void f_write();// 錄入
void f_change();//改變
void f_delete();//刪除
void display();//顯示全部商品信息
void s_buy();//買入
};
//構造函數
Goods::Goods(){}
Goods::Goods(char co[10],char na[10],char un[10],int am,float unpr)
{
strcpy(code,co);
strcpy(name,na);
strcpy(unit,un);
amount=am;
unitprice=unpr;
}
Goods *g[50];
//商品信息錄入
void Goods::f_write()
{
cout<<"請輸入第"<<i+1<<"件商品代碼:"<<endl;
cin>>code;
cout<<"請輸入第"<<i+1<<"件商品名稱:"<<endl;
cin>>name;
cout<<"請輸入第"<<i+1<<"件商品計量單位:"<<endl;
cin>>unit;
cout<<"請輸入第"<<i+1<<"件商品總數:"<<endl;
cin>>amount;
cout<<"請輸入第"<<i+1<<"件商品單價:"<<endl;
cin>>unitprice;
g[i]=new Goods(code,name,unit,amount,unitprice);
i++;
cout<<"信息錄入成功!(繼續錄入按y,返回上一層按n)"<<endl;
cin>>ch;
if(ch=='y')
{
f_write();
}
}
//改變商品信息
void Goods::f_change()
{
cout<<"請輸入要改變的商品代碼:";
cin>>code;
for(int h=0;h<i;h++)
{
if(0 == strcmp(g[h]->code,code))
{
cout<<"商品信息如下:"<<endl;
cout<<"代碼 名稱 單價 總數 單位"<<endl;
cout<<g[h]->code<<"\t"<<g[h]->name<<"\t"<<g[h]->unitprice
<<"\t"<<g[h]->amount<<"\t"<<g[h]->unit<<endl;
char newco,newna,newun;
int newam;
float newunpr;
cout<<"你想要修改:1、代碼;2、名稱;3、單價;4、總數;5、單位。"<<endl;
cin>>n;
switch(n)
{
case 1:
cout<<"請輸入修改後的商品代碼:";
cin>>newco;
g[h]->code[10]=newco;
cout<<"修改成功!"<<endl;
break;
case 2:
cout<<"請輸入修改後的商品名稱:";
cin>>newna;
g[h]->name[10]=newna;
cout<<"修改成功!"<<endl;
break;
case 3:
cout<<"請輸入商品單價:";
cin>>newunpr;
g[h]->unitprice=newunpr;
cout<<"修改成功!"<<endl;
break;
case 4:
cout<<"請輸入修改後的商品總數:";
cin>>newam;
g[h]->amount=newam;
cout<<"修改成功!"<<endl;
break;
case 5:
cout<<"請輸入修改後的商品單位:";
cin>>newun;
g[h]->unit[10]=newun;
cout<<"修改成功!"<<endl;
break;
}
break;
}//if
}//for循環
cout<<"是否繼續修改?(y/n)"<<endl;
cin>>ch;
if(ch=='y')
{
f_change();
}
}
//刪除信息
void Goods::f_delete()
{
cout<<"請輸入要刪除的商品代碼:";
cin>>code;
for(int h=0;h<i;h++)
{
if(0 == strcmp(g[h]->code,code))
{
for(int k=h;k<i;k++)
{
g[k]=g[k+1];
i--;
}
}
}
cout<<"刪除成功!"<<endl;
cout<<"是否繼續刪除?(y/n)"<<endl;
cin>>ch;
if(ch=='y')
{
f_delete();
}
}
//列印信息
void Goods::display()
{
system("cls");
cout<<" "<<endl;
cout<<"-----------全部商品信息如下-------------------"<<endl;
cout<<" "<<endl;
cout<<"代碼 名稱 單價 總數 單位"<<endl;
f1<<" "<<endl;
f1<<"---------------全部商品信息如下--------------"<<endl;
f1<<" "<<endl;
f1<<"代碼 名稱 單價 總數 單位"<<endl;
if(i==0)
{
cout<<"系統未曾錄入任何商品信息,或記錄被刪除!";
}
for(int k=0;k<i;k++)
{
cout<<g[k]->code<<"\t"<<g[k]->name<<"\t"<<g[k]->unitprice
<<"\t"<<g[k]->amount<<"\t"<<g[k]->unit<<endl;
f1<<g[k]->code<<"\t"<<g[k]->name<<"\t"<<g[k]->unitprice
<<"\t"<<g[k]->amount<<"\t"<<g[k]->unit<<endl;
}
cout<<endl;
}
//買入
void Goods::s_buy()
{
float price=0;//單個商品價格
cout<<"請輸入想要買的商品代碼:";
cin>>code;
for(int h=0;h<i;h++)
{
if(0 == strcmp(g[h]->code,code))
{
cout<<"請輸入想要購買的商品數量:";
cin>>amount;//當前要購買的數量
price=amount*g[h]->unitprice;
g[h]->amount=g[h]->amount-amount;
cout<<endl;
cout<<"代碼 名稱 單價 數量 小計"<<endl;
cout<<g[h]->code<<"\t"<<g[h]->name<<"\t"<<g[h]->unitprice
<<"\t"<<g[h]->amount<<g[h]->unit<<"\t"<<price<<endl;
f2<<"代碼 名稱 單價 數量 小計"<<endl;
f2<<g[h]->code<<"\t"<<g[h]->name<<"\t"<<g[h]->unitprice
<<"\t"<<g[h]->amount<<g[h]->unit<<"\t"<<price<<endl;
total=total+price;
break;
}
}
cout<<"按1繼續購買,按2結束。"<<endl;
cin>>n;
if(n==2)
{
cout<<endl;
cout<<"購買結束,總計:"<<total<<"元!"<<endl;
}
else
{
s_buy();
}
}

//類外函數
//第一部分操作顯示
void f_screen()
{
system("cls");
Goods g;
cout<<"按相應鍵操作:"<<endl;
cout<<"0.錄入信息 1.更改信息 2.刪除信息 3.返回上一層"<<endl;
cin>>n;
switch(n)
{
case 0:
g.f_write();
if(ch=='n'||ch=='N')
f_screen();
break;
case 1:
g.f_change();
if(ch=='n'||ch=='N')
f_screen();
break;
case 2:
g.f_delete();
if(ch=='n'||ch=='N')
f_screen();
break;
}
}
//第二部分操作顯示
void s_screen()
{
Goods g;
g.display();
cout<<endl;
g.s_buy();
}
//初始屏幕顯示
void screen()
{
system("cls");
cout<<" "<<endl;
cout<<"-----------------商品銷售統計系統---------------"<<endl;
cout<<" "<<endl;
f2<<" "<<endl;
f2<<"----------------商品銷售統計系統--------------"<<endl;
f2<<" "<<endl;
cout<<"更改商品信息請按1,進行銷售統計請按-1。"<<endl;
cin>>ch;
if(ch=='1')
{
f_screen();
if(n==3)
screen();
}
else if(ch=='-1')
{
s_screen();
}
else
{
cout<<"輸入錯誤,系統重新啟動!";
screen();
}
}
int main()
{
screen();
return 0;
}

『拾』 c語言:編寫程序,從鍵盤輸入80個字元,統計其中數字字元的個數,並輸出統計結果

可以參考下面的代碼:#include <stdio.h>intmain(){inta,b,c,ch;a=b=c=0;//計數器初始化為0.while((ch=getchar())!='\n')//循環讀取字元,到換行結束。{if(ch>='0' && ch<='9')//數字a++;else if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))//字母b++;else//其它c++;}printf("%d%d%d\n",a,b,c);//輸出結果。return0;}(10)c語言設計統計代碼擴展閱讀:printf()函數函數printf()函數是格式化輸出函數, 一般用於向標准輸出設備按規定格式輸出信息。在編寫程序時經常會用到此函數。函數的原型為:int printf(const char *format, ...);函數返回值為整型。若成功則返回輸出的字元數,輸出出錯則返回負值,printf()函數的調用格式為:printf("<格式化字元串>", <參量表>);while語句的一般表達式為:while(表達式){循環體}。參考資料來源:網路-printf()參考資料來源:網路-while (循環語句及英文單詞)