Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ พิมพ์ค่าประเภทข้อมูลต่างๆ ตามอินพุต


สมมติว่าเราได้รับค่าประเภท integer, long, character, float และ double เป็นอินพุต เราต้องพิมพ์ค่าเหล่านั้นรักษาความแม่นยำ

เช่น integer = 15, long = 59523256297252, char = 'y', float = 367.124, double = 6464292.312621 ผลลัพธ์:

15 59523256297252 y 367.124 6464292.31262

ขั้นตอนการแก้ปัญหา

  • พิมพ์ค่าที่รับเข้ามาในบรรทัดแยกกัน
  • ใช้ printf("%.5f") สำหรับ double เพื่อควบคุมความแม่นยำ

ตัวอย่างโค้ด

#include <iostream> #include <cstdio> using namespace std; void solve(int a, long b, char c, float d, double e) { cout << a << endl << b << endl << c << endl << d << endl; printf("%.5f", e); } int main() { solve(15, 59523256297252, 'y', 367.124, 6464292.312621); return 0; }

อินพุต

15, 59523256297252, 'y', 367.124, 6464292.312621

เอาต์พุต

15 59523256297252 y 367.124 6464292.31262