本文目錄一覽:
- 1、菜鳥問一道簡單的C語言編程題
- 2、初學C語言的一個簡單編程題,求我的問題在哪.
- 3、一道簡單的c語言編程題
- 4、初學者的一道C語言編程題目
- 5、初學者請教 兩道簡單的c語言編程題目
- 6、初學編程,大家幫忙看下這道c語言題怎麼做?萬分感謝
菜鳥問一道簡單的C語言編程題
完整程序如下,我加的就是定義了一個函數AREA
#include stdio.h
#include math.h
//函數的定義,定義area=sqrt((s*(s-a)*(s-b)*(s-c))根號下的內容,返回一個值,在主函數里調用該函數
float AREA(float s,float a,float b,float c){
float X=s*(s-a)*(s-b)*(s-c);
return X;
}
void main()
{float a,b,c,area,s;
printf(“\nInput a,b,c: “);
scanf(“%f%f%f”,a,b,c);
if(a+bc a+cb b+ca) /*判斷是否能構成三角形*/
{ s=(a+b+c)/2;
area=sqrt(AREA(s,a,b,c));
printf(“\narea=%f\n”,area);
}
else printf(“\nData error!\n”);}
初學C語言的一個簡單編程題,求我的問題在哪.
錯誤1:main()函數沒有函數類型 用void類型或者其他,例如:void main(){}
錯誤2:switch語句中的case 後面沒有break 就會一直執行,知道結束或者遇 到break為止,default語句是swtich的必執行語句。我似乎明白你的意思,但是你這裡有明顯的邏輯錯誤。
改正如下:
include”stdio.h “
void main()
{
//定於數據要初始化
long n = 0;
int dn = 0;
int n1 = 0;
int n2 = 0;
int n3 = 0;
int n4 = 0;
int n5 = 0;
printf(“input an integer 0~99999”);
scanf(“%d”,n);
if n99999 dn=-1;
else if n9999 dn=5;
else if n999 dn=4;
else if n99 dn=3;
else if n9 dn=2;
else if n=0 dn=1;
else dn=-1;
switch(dn)
{
case 5 :
n5=n/10000;
case 4 :
n = n – 10000*n5;
n4=n/1000;
case 3 :
n = n – 1000*n4
n3=n/100;
case 2 :
n = n – 100*n3;
n2=n/10;
case 1 :
n1 = n – 10*n2;
case -1 :
n1=n2=n3=n4=n5=-1;
}
printf(“這是一個%d位數”,dn);
printf(“\n第1位數是%d\n第2位數是%d\n第3位數是%d\n第4位數是%d\n第5位數是%d,n1,n2,n3,n4,n5”);
}
手寫的沒調試過,你自己試試吧
一道簡單的c語言編程題
#include stdio.h
#include stdlib.h
int main()
{
int a,b,c,d;
scanf(“%d%d”,a,b);
//藉助其他變數
c=a-b;
b=b+c;
a=a-c;
printf(“%d %d\n”,a,b);
//不藉助於其他變數;
scanf(“%d%d”,c,d);
c=c+d;
d=c-d;
c=c-d;
printf(“%d %d\n”,c,d);
return 0;
}
初學者的一道C語言編程題目
#include “stdio.h”
int main( )
{
int ri,repeat;
int i, digit, m, n, number, sum;
scanf(“%d”,repeat);
for(ri=1;ri=repeat;ri++)
{
scanf(“%d%d”, m, n);
/*———*/
for(i=m;i=n;i++)
{
sum=0;
number=i;
while(number0)
{
digit=number%10;
number/=10;
sum+=digit*digit*digit;
}
if(i==sum)
{
printf(“%d \n”,sum);
}
}
/*———*/
}
初學者請教 兩道簡單的c語言編程題目
[回答]
1. 用C語言自己的時間函數
difftime(time_t time1, time_t time0) / 86400
這樣做得缺點是,這裡的time_t類型,只支持到2037年
2. 自己定義函數
樓主用的方法,在實際編程里不常用,變化比較大。
下面這個方法是直接計算兩個日期到公元元年的天數
然後相減,這個是實際編程時的演算法,經過很多程序員的洗鍊,已經被廣泛使用
#includestdio.h
#define LEAP(y) (y%(y%100?4:400)==0) /*公元y年是否閏年*/
long totaldays(int year,int month,int day)/*公元紀年的總天數*/
{ int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int c=day,m,y=year-1;
days[2]=28+LEAP(year);
for(m=1;mmonth;m++)c+=days[m];
return c+y/4-y/100+y/400+y*365L;
}
void main()
{ int y,m,d; long t1,t2;
printf(“date 1: “);
scanf(“%d%*c%d%*c%d”,y,m,d);
t1=totaldays(y,m,d);
printf(“date 2: “);
scanf(“%d%*c%d%*c%d”,y,m,d);
t2=totaldays(y,m,d);
printf(“total days: %ld\n”,t2-t1);
}
這應該是全部的實現方法了
樓主的方法沒必要簡化了
尤其是刻意追求短代碼就更沒有必要了
要知道,代碼短了,可讀性就差
兩者協調好,才能稱為好程序^^
悉雨辰寂
初學編程,大家幫忙看下這道c語言題怎麼做?萬分感謝
先給你第一題的,百度知道的這個編輯器真的不適合粘貼代碼
#includestdio.h
#includestring.h
#define MAX_ARRAY_SIZE 1024
#define MAX_MAP_SIZE 10
/* 輸入數組,連續輸入,如:aedabcdaeas */
int inputArray(char *buff) {
int len = 0;
/* 使用fgets來防止緩衝區溢出 */
if (NULL == fgets(buff, MAX_ARRAY_SIZE, stdin)) {
return 0;
}
len = strlen(buff);
/* fgets 返回的數據可能是換行符結尾的,也可能不是,對換行符結尾的進行處理 */
if (buff[len – 1] == ‘\n’) {
buff[len – 1] = ‘\0’;
len -= 1;
}
return len;
}
int processArray(int len, char *chars, char *map) {
/* 保存反向映射便於查找 */
int tmap[128];
int maplen = 0;
int i = 0;
char *p = chars;
memset(tmap, -1, sizeof(int) * 128);
for (i = 0; i len; i++) {
if (*p ‘z’ || *p ‘a’) {
return -*p;
}
if (tmap[*p] == -1) {
if (maplen = MAX_MAP_SIZE) {
return -1;
}
tmap[*p] = maplen;
map[maplen] = *p;
maplen += 1;
}
*p = ‘0’ + tmap[*p];
p++;
}
return maplen;
}
int main() {
/* 用於輸入的字元數組 */
char buff[MAX_ARRAY_SIZE];
/* 用於保存轉換規則的數組 */
char map[MAX_MAP_SIZE];
/* 保存字元數組長度 */
int len = 0;
int maplen = 0;
int i = 0;
len = inputArray(buff);
if (len = 0) {
puts(“Cancelled”);
} else if (len 10) {
puts(“Not enough 10 chars”);
} else {
maplen = processArray(len, buff, map);
if (maplen = 0) {
puts(“轉換結果:”);
for (i = 0; i len; i++) {
printf(“%c “, buff[i]);
}
puts(“”);
puts(“映射規則:”);
for (i = 0; i maplen; i++) {
printf(“%c – %d\n”, map[i], i);
}
puts(“”);
} else if (maplen == -1) {
puts(“Different Chars count is OverLimit of 10”);
} else if (maplen = -2) {
printf(“Unexpected char %c\n”, -maplen);
}
}
return 0;
}
執行結果:
原創文章,作者:AJBI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/145507.html